From 277e80efdd1f1d053fd94f62519fe92aef2d7398 Mon Sep 17 00:00:00 2001 From: dylan Date: Tue, 14 Nov 2023 15:16:29 -0800 Subject: [PATCH] add metadata support to validate --- cmd/utils.go | 16 +++++++++++++--- cmd/validate.go | 30 ++++++++++++++++++++++++------ integration_test.go | 20 +++++++++++++------- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/cmd/utils.go b/cmd/utils.go index cb7521c..22286e7 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -54,15 +54,25 @@ func genMetaDataText(args metaDataArgs) ([]byte, error) { return mtext, nil } -func getMetaDataItem(src []byte, key string) (string, error) { +func getMetaDataItemOptional(src []byte, key string) (string, error) { mdata := map[string]string{} err := metadata.Unmarshal(src, mdata) if err != nil { return "", err } - val, ok := mdata[key] - if !ok { + val := mdata[key] + + return val, nil +} + +func getMetaDataItem(src []byte, key string) (string, error) { + val, err := getMetaDataItemOptional(src, key) + if err != nil { + return "", err + } + + if val == "" { return "", fmt.Errorf("metadata does not contain %s", key) } diff --git a/cmd/validate.go b/cmd/validate.go index b193d47..c284f9b 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -48,25 +48,42 @@ func newValidateCmd() *cobra.Command { srcPath = args[0] } - version, err := cmd.Flags().GetString("aerospike-version") + srcFormat, err := getConfFileFormat(srcPath, cmd) if err != nil { return err } - logger.Debugf("Processing flag aerospike-version value=%s", version) + logger.Debugf("Processing flag format value=%v", srcFormat) - srcFormat, err := getConfFileFormat(srcPath, cmd) + fdata, err := os.ReadFile(srcPath) if err != nil { return err } - logger.Debugf("Processing flag format value=%v", srcFormat) + version, err := getMetaDataItemOptional(fdata, metaKeyAerospikeVersion) + if err != nil { + return errors.Join(errMissingAerospikeVersion, err) + } - fdata, err := os.ReadFile(srcPath) + // if the Aerospike server version was not in the file + // metadata, require that it is passed as an argument + if version == "" { + cmd.MarkFlagRequired("aerospike-version") + } + + versionArg, err := cmd.Flags().GetString("aerospike-version") if err != nil { return err } + // the command line --aerospike-version option overrides + // the metadata server version + if versionArg != "" { + version = versionArg + } + + logger.Debugf("Processing flag aerospike-version value=%s", version) + conf, err := asconf.NewAsconf( fdata, srcFormat, @@ -98,9 +115,10 @@ func newValidateCmd() *cobra.Command { } // flags and configuration settings + // --aerospike-version is required unless the server version + // is in the input config file's metadata commonFlags := getCommonFlags() res.Flags().AddFlagSet(commonFlags) - res.MarkFlagRequired("aerospike-version") res.Version = VERSION diff --git a/integration_test.go b/integration_test.go index c141da3..bf3133b 100644 --- a/integration_test.go +++ b/integration_test.go @@ -70,11 +70,11 @@ func TestMain(m *testing.M) { panic(err) } - featKeyDir = os.Getenv("FEATKEY") - fmt.Println(featKeyDir) - if featKeyDir == "" { - panic("FEATKEY environement variable must be full path to a directory containing valid aerospike feature key files featuresv1.conf and featuresv2.conf of feature key format 1 and 2 respectively.") - } + // featKeyDir = os.Getenv("FEATKEY") + // fmt.Println(featKeyDir) + // if featKeyDir == "" { + // panic("FEATKEY environement variable must be full path to a directory containing valid aerospike feature key files featuresv1.conf and featuresv2.conf of feature key format 1 and 2 respectively.") + // } code := m.Run() @@ -997,9 +997,15 @@ var validateTests = []validateTest{ source: filepath.Join(sourcePath, "pmem_cluster_cr.yaml"), }, { - arguments: []string{"validate", "-a", "7.0.0", filepath.Join(extraTestPath, "server64/server64.yaml")}, + arguments: []string{"validate", filepath.Join(extraTestPath, "metadata", "metadata.conf")}, + expectError: false, + expectedResult: "", + source: filepath.Join(extraTestPath, "metadata", "metadata.conf"), + }, + { + arguments: []string{"validate", "-a", "7.0.0", filepath.Join(extraTestPath, "server64", "server64.yaml")}, expectError: true, - source: filepath.Join(extraTestPath, "server64/server64.yaml"), + source: filepath.Join(extraTestPath, "server64", "server64.yaml"), expectedResult: `context: (root).namespaces.0 - description: Additional property memory-size is not allowed, error-type: additional_property_not_allowed context: (root).namespaces.0.storage-engine