Skip to content

Commit

Permalink
add metadata support to validate
Browse files Browse the repository at this point in the history
  • Loading branch information
dwelch-spike committed Nov 14, 2023
1 parent aecc82a commit 277e80e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
16 changes: 13 additions & 3 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
30 changes: 24 additions & 6 deletions cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
20 changes: 13 additions & 7 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 277e80e

Please sign in to comment.