Skip to content

Commit

Permalink
feat: adding configuration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
deimosfr committed Aug 30, 2020
1 parent d274e3c commit e23e4cd
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
79 changes: 71 additions & 8 deletions cfg/validate.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package cfg

import (
"errors"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
)

func ValidateConfig(configFile string) (Config, error) {
cfg := Config{}
allReposName := make(map[string]bool)
allReposUrl := make(map[string]bool)
allDestinationsName := make(map[string]bool)
allDestinationsPath := make(map[string]bool)

data, err := ioutil.ReadFile(configFile)
if err != nil {
Expand All @@ -21,13 +26,71 @@ func ValidateConfig(configFile string) (Config, error) {
return Config{}, err
}

// Todo: Ensure content is coherent
//for _, chart := range cfg.Charts {
// fmt.Println(chart["name"])
//}
// Ensure content is coherent
for _, chart := range cfg.Charts {
if _, ok := chart["name"]; !ok {
return cfg, fmt.Errorf("name is missing in charts config for this element: %v\n", chart)
}
if _, ok := chart["version"]; !ok {
return cfg, fmt.Errorf("version is missing in %s charts config\n", chart["name"])
}
}

return cfg, nil
}
for _, repo := range cfg.Repos {
if _, ok := repo["name"]; !ok {
return cfg, fmt.Errorf("name is missing in repos config for this element: %v\n", repo)
}
if _, ok := repo["url"]; !ok {
return cfg, fmt.Errorf("url is missing in %s repos config\n", repo["name"])
}
}

for _, dest := range cfg.Destinations {
if _, ok := dest["name"]; !ok {
return cfg, fmt.Errorf("name is missing in destinations config for this element: %v\n", dest)
}
if _, ok := dest["path"]; !ok {
return cfg, fmt.Errorf("path is missing in %s destinations config\n", dest["name"])
}
}

// Find duplicates
for _, repo := range cfg.Repos {
if _, ok := allReposName[repo["name"]]; ok {
return cfg, fmt.Errorf("Duplicate repo name found: %s\n", repo["name"])
} else {
allReposName[repo["name"]] = true
}

if _, ok := allReposUrl[repo["url"]]; ok {
return cfg, fmt.Errorf("Duplicate url found: %s\n", repo["url"])
} else {
allReposUrl[repo["url"]] = true
}
}

// Todo: find duplicates charts, repos and destinations
// Todo: check default destination is present
for _, dest := range cfg.Destinations {
if _, ok := allDestinationsName[dest["name"]]; ok {
return cfg, fmt.Errorf("Duplicate destination name found: %s\n", dest["name"])
} else {
allDestinationsName[dest["name"]] = true
}

if _, ok := allDestinationsPath[dest["path"]]; ok {
return cfg, fmt.Errorf("Duplicate destination path found: %s\n", dest["path"])
} else {
allDestinationsPath[dest["path"]] = true
}
}

// check if defaults elements are present
if _, ok := allDestinationsName["default"]; !ok {
return cfg, errors.New("missing default destination")
}

if _, ok := allReposName["stable"]; !ok {
return cfg, errors.New("missing stable repository")
}

return cfg, nil
}
1 change: 1 addition & 0 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Running a git diff then will help to see any differences`,
configFile, _ := cmd.Flags().GetString("config-file")
config, err := cfg.ValidateConfig(configFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/validate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"github.com/Qovery/helm-freeze/cfg"
"os"

Expand All @@ -15,8 +16,10 @@ var validateCmd = &cobra.Command{
configFile, _ := cmd.Flags().GetString("config-file")
_, err := cfg.ValidateConfig(configFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Configuration is valid")
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var versionCmd = &cobra.Command{
}

func GetCurrentVersion() string {
return "0.1.1" // ci-version-check
return "0.2" // ci-version-check
}

func init() {
Expand Down

0 comments on commit e23e4cd

Please sign in to comment.