-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tools 2715 add the validate command (#23)
* feat: tools2715 add the validate command * chore: reformat validation errors * ci: validation integration tests
- Loading branch information
1 parent
17ed370
commit f7e7e88
Showing
10 changed files
with
346 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/aerospike/asconfig/asconf" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
validateArgMax = 1 | ||
) | ||
|
||
var ( | ||
errValidateTooManyArguments = fmt.Errorf("expected a maximum of %d arguments", convertArgMax) | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(validateCmd) | ||
} | ||
|
||
var validateCmd = newValidateCmd() | ||
|
||
func newValidateCmd() *cobra.Command { | ||
res := &cobra.Command{ | ||
Use: "validate [flags] <path/to/config_file>", | ||
Short: "Validate an Aerospike configuration file.", | ||
Long: `Validate an Aerospike configuration file in any supported format | ||
against a versioned Aerospike configuration schema. | ||
If a file passes validation nothing is output, otherwise errors | ||
indicating problems with the configuration file are shown. | ||
If a file path is not provided, validate reads from stdin. | ||
Ex: asconfig validate --aerospike-version 7.0.0 aerospike.conf`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
logger.Debug("Running validate command") | ||
|
||
if len(args) > validateArgMax { | ||
return errValidateTooManyArguments | ||
} | ||
|
||
// read stdin by default | ||
var srcPath string | ||
if len(args) == 0 { | ||
srcPath = os.Stdin.Name() | ||
} else { | ||
srcPath = args[0] | ||
} | ||
|
||
version, err := cmd.Flags().GetString("aerospike-version") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
logger.Debugf("Processing flag aerospike-version value=%s", version) | ||
|
||
srcFormat, err := getConfFileFormat(srcPath, cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
logger.Debugf("Processing flag format value=%v", srcFormat) | ||
|
||
fdata, err := os.ReadFile(srcPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
conf, err := asconf.NewAsconf( | ||
fdata, | ||
srcFormat, | ||
// we aren't converting to anything so set | ||
// output format to Invalid as a place holder | ||
asconf.Invalid, | ||
version, | ||
logger, | ||
managementLibLogger, | ||
) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
verrs, err := conf.Validate() | ||
if verrs != nil { | ||
// force validation errors to be written to stdout | ||
// so they can more easily be grepd etc. | ||
cmd.Print(verrs.Error()) | ||
return errors.Join(asconf.ErrConfigValidation, SilentError) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return err | ||
}, | ||
} | ||
|
||
// flags and configuration settings | ||
commonFlags := getCommonFlags() | ||
res.Flags().AddFlagSet(commonFlags) | ||
res.MarkFlagRequired("aerospike-version") | ||
|
||
res.Version = VERSION | ||
|
||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
//go:build unit | ||
// +build unit | ||
|
||
package cmd | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
type runTestValidate struct { | ||
flags []string | ||
arguments []string | ||
expectError bool | ||
} | ||
|
||
var testValidateArgs = []runTestValidate{ | ||
{ | ||
flags: []string{}, | ||
arguments: []string{"too", "many", "args"}, | ||
expectError: true, | ||
}, | ||
{ | ||
// missing arg to -a-aerospike-version | ||
flags: []string{"--aerospike-version"}, | ||
arguments: []string{"../testdata/sources/all_flash_cluster_cr.yaml"}, | ||
expectError: true, | ||
}, | ||
{ | ||
flags: []string{"--aerospike-version"}, | ||
arguments: []string{"./bad_extension.ymml"}, | ||
expectError: true, | ||
}, | ||
{ | ||
flags: []string{"--aerospike-version", "bad_version"}, | ||
arguments: []string{"../testdata/sources/all_flash_cluster_cr.yaml"}, | ||
expectError: true, | ||
}, | ||
{ | ||
flags: []string{"--aerospike-version", "6.4.0"}, | ||
arguments: []string{"./fake_file.yaml"}, | ||
expectError: true, | ||
}, | ||
{ | ||
flags: []string{"--aerospike-version", "6.4.0"}, | ||
arguments: []string{"../testdata/cases/server64/server64.yaml"}, | ||
expectError: false, | ||
}, | ||
{ | ||
flags: []string{"--log-level", "debug", "--aerospike-version", "7.0.0"}, | ||
arguments: []string{"../testdata/cases/server70/server70.conf"}, | ||
expectError: false, | ||
}, | ||
} | ||
|
||
func TestRunEValidate(t *testing.T) { | ||
cmd := validateCmd | ||
|
||
for i, test := range testValidateArgs { | ||
cmd.Parent().ParseFlags(test.flags) | ||
cmd.ParseFlags(test.flags) | ||
err := cmd.RunE(cmd, test.arguments) | ||
if test.expectError == (err == nil) { | ||
t.Fatalf("case: %d, expectError: %v does not match err: %v", i, test.expectError, err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.