Skip to content
Permalink
Browse files

[#439] Add --vars-inline that is capable of overwriting --vars (#534)

* Add --vars-inline that is capable of overwriting --vars

* Reinforce the --vars-inline overwrite of --vars

* Clean up the TemplateFilter based on Code Climate feedback

* Fix the Travis testing by ensuring all the checks are ran

* Apply suggestions from code review for incorrect environment variable syntax

Co-Authored-By: Ahmed Elsabbahy <aelsabbahy@users.noreply.github.com>

Co-authored-by: Ahmed Elsabbahy <aelsabbahy@users.noreply.github.com>
  • Loading branch information
pedroMMM and aelsabbahy committed Feb 12, 2020
1 parent 6abda7e commit 6cb041d2eb027a45e9a47f2cfbd1211b9e4aaf8e
Showing with 358 additions and 65 deletions.
  1. +1 −1 .travis.yml
  2. +2 −2 add.go
  3. +5 −1 cmd/goss/goss.go
  4. +12 −13 docs/manual.md
  5. +8 −0 integration-tests/goss/goss-shared.yaml
  6. +2 −0 integration-tests/goss/vars.yaml
  7. +4 −3 integration-tests/test.sh
  8. +50 −13 store.go
  9. +238 −0 store_test.go
  10. +1 −1 system/dns_test.go
  11. +30 −28 template.go
  12. +5 −3 validate.go
@@ -24,7 +24,7 @@ before_script:
- ./cc-test-reporter before-build

script:
- make test
- make all

after_script:
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT -d
4 add.go
@@ -14,7 +14,7 @@ import (

// Simple wrapper to add multiple resources
func AddResources(fileName, resourceName string, keys []string, c *cli.Context) error {
OutStoreFormat = getStoreFormatFromFileName(fileName)
outStoreFormat = getStoreFormatFromFileName(fileName)
config := util.Config{
IgnoreList: c.GlobalStringSlice("exclude-attr"),
Timeout: int(c.Duration("timeout") / time.Millisecond),
@@ -161,7 +161,7 @@ func AddResource(fileName string, gossConfig GossConfig, resourceName, key strin

// Simple wrapper to add multiple resources
func AutoAddResources(fileName string, keys []string, c *cli.Context) error {
OutStoreFormat = getStoreFormatFromFileName(fileName)
outStoreFormat = getStoreFormatFromFileName(fileName)
config := util.Config{
IgnoreList: c.GlobalStringSlice("exclude-attr"),
Timeout: int(c.Duration("timeout") / time.Millisecond),
@@ -9,7 +9,6 @@ import (
"github.com/aelsabbahy/goss"
"github.com/aelsabbahy/goss/outputs"
"github.com/urfave/cli"
//"time"
)

var version string
@@ -33,6 +32,11 @@ func main() {
Usage: "json/yaml file containing variables for template",
EnvVar: "GOSS_VARS",
},
cli.StringFlag{
Name: "vars-inline",
Usage: "json/yaml string containing variables for template (overwrites vars)",
EnvVar: "GOSS_VARS_INLINE",
},
cli.StringFlag{
Name: "package",
Usage: "Package type to use [rpm, deb, apk, pacman]",
@@ -47,21 +47,20 @@ VERSION:
0.0.0
COMMANDS:
validate, v Validate system
serve, s Serve a health endpoint
render, r render gossfile after imports
autoadd, aa automatically add all matching resource to the test suite
add, a add a resource to the test suite
help, h Shows a list of commands or help for one command
validate, v Validate system
serve, s Serve a health endpoint
render, r render gossfile after imports
autoadd, aa automatically add all matching resource to the test suite
add, a add a resource to the test suite
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--gossfile, -g "./goss.yaml" Goss file to read from / write to [$GOSS_FILE]
--vars value json/yaml file containing variables for template [$GOSS_VARS]
--package Package type to use [rpm, deb, apk, pacman]
--help, -h show help
--generate-bash-completion
--version, -v print the version
--gossfile value, -g value Goss file to read from / write to (default: "./goss.yaml") [$GOSS_FILE]
--vars value json/yaml file containing variables for template [$GOSS_VARS]
--vars-inline value json/yaml string containing variables for template (overwrites vars) [$GOSS_VARS_INLINE]
--package value Package type to use [rpm, deb, apk, pacman]
--help, -h show help
--version, -v print the version
```
**Note:** *Most flags can be set by using environment variables, see `--help` for more info.*

@@ -229,3 +229,11 @@ matching:
matches:
not:
semver-constraint: ">1.0.0 <2.0.0 !=1.5.0"
vars_inline_simple:
content: {{ .Vars.inline }}
matches:
match-regexp: bar
vars_inline_overwrite:
content: {{ .Vars.overwrite }}
matches:
match-regexp: bar
@@ -13,3 +13,5 @@ precise:
wheezy:
packages:
apache2: "2.2.22-13+deb7u13"

overwrite: foo
@@ -4,6 +4,7 @@ set -xeu

os=$1
arch=$2
vars_inline="{inline: bar, overwrite: bar}"

seccomp_opts() {
local docker_ver minor_ver
@@ -40,13 +41,13 @@ trap "rv=\$?; docker rm -vf $id; exit \$rv" INT TERM EXIT
[[ $os != "arch" ]] && docker_exec "/goss/$os/goss-linux-$arch" -g "/goss/goss-wait.yaml" validate -r 10s -s 100ms && sleep 1

#out=$(docker exec "$container_name" bash -c "time /goss/$os/goss-linux-$arch -g /goss/$os/goss.yaml validate")
out=$(docker_exec "/goss/$os/goss-linux-$arch" --vars "/goss/vars.yaml" -g "/goss/$os/goss.yaml" validate)
out=$(docker_exec "/goss/$os/goss-linux-$arch" --vars "/goss/vars.yaml" --vars-inline "$vars_inline" -g "/goss/$os/goss.yaml" validate)
echo "$out"

if [[ $os == "arch" ]]; then
egrep -q 'Count: 84, Failed: 0, Skipped: 3' <<<"$out"
egrep -q 'Count: 86, Failed: 0, Skipped: 3' <<<"$out"
else
egrep -q 'Count: 101, Failed: 0, Skipped: 5' <<<"$out"
egrep -q 'Count: 103, Failed: 0, Skipped: 5' <<<"$out"
fi

if [[ ! $os == "arch" ]]; then
@@ -23,8 +23,8 @@ const (
YAML
)

var OutStoreFormat = UNSET
var TemplateFilter func(data []byte) []byte
var outStoreFormat = UNSET
var currentTemplateFilter TemplateFilter
var debug = false

func getStoreFormatFromFileName(f string) int {
@@ -52,7 +52,7 @@ func getStoreFormatFromData(data []byte) int {
return 0
}

// Reads json file returning GossConfig
// ReadJSON Reads json file returning GossConfig
func ReadJSON(filePath string) GossConfig {
file, err := ioutil.ReadFile(filePath)
if err != nil {
@@ -76,8 +76,26 @@ func (t *TmplVars) Env() map[string]string {
return env
}

func loadVars(varsFile string, varsInline string) (map[string]interface{}, error) {
vars, err := varsFromFile(varsFile)
if err != nil {
return nil, fmt.Errorf("Error: loading vars file '%s'\n%w", varsFile, err)
}

varsExtra, err := varsFromString(varsInline)
if err != nil {
return nil, fmt.Errorf("Error: loading inline vars\n%w", err)
}

for k, v := range varsExtra {
vars[k] = v
}

return vars, nil
}

func varsFromFile(varsFile string) (map[string]interface{}, error) {
var vars map[string]interface{}
vars := make(map[string]interface{})
if varsFile == "" {
return vars, nil
}
@@ -92,16 +110,34 @@ func varsFromFile(varsFile string) (map[string]interface{}, error) {
return vars, nil
}

// Reads json byte array returning GossConfig
func varsFromString(varsString string) (map[string]interface{}, error) {
vars := make(map[string]interface{})
if varsString == "" {
return vars, nil
}
data := []byte(varsString)
format := getStoreFormatFromData(data)
if err := unmarshal(data, &vars, format); err != nil {
return vars, err
}
return vars, nil
}

// ReadJSONData Reads json byte array returning GossConfig
func ReadJSONData(data []byte, detectFormat bool) GossConfig {
if TemplateFilter != nil {
data = TemplateFilter(data)
if currentTemplateFilter != nil {
var err error
data, err = currentTemplateFilter(data)
if err != nil {
log.Fatalln(err)
}

if debug {
fmt.Println("DEBUG: file after text/template render")
fmt.Println(string(data))
}
}
format := OutStoreFormat
format := outStoreFormat
if detectFormat == true {
format = getStoreFormatFromData(data)
}
@@ -115,14 +151,15 @@ func ReadJSONData(data []byte, detectFormat bool) GossConfig {
return *gossConfig
}

// Reads json file recursively returning string
// RenderJSON Reads json file recursively returning string
func RenderJSON(c *cli.Context) string {
filePath := c.GlobalString("gossfile")
varsFile := c.GlobalString("vars")
varsInline := c.GlobalString("vars-inline")
debug = c.Bool("debug")
TemplateFilter = NewTemplateFilter(varsFile)
currentTemplateFilter = NewTemplateFilter(varsFile, varsInline)
path := filepath.Dir(filePath)
OutStoreFormat = getStoreFormatFromFileName(filePath)
outStoreFormat = getStoreFormatFromFileName(filePath)
gossConfig := mergeJSONData(ReadJSON(filePath), 0, path)

b, err := marshal(gossConfig)
@@ -144,7 +181,7 @@ func mergeJSONData(gossConfig GossConfig, depth int, path string) GossConfig {

// Sort the gossfiles to ensure consistent ordering
var keys []string
for k, _ := range gossConfig.Gossfiles {
for k := range gossConfig.Gossfiles {
keys = append(keys, k)
}
sort.Strings(keys)
@@ -212,7 +249,7 @@ func resourcePrint(fileName string, res resource.ResourceRead) {
}

func marshal(gossConfig interface{}) ([]byte, error) {
switch OutStoreFormat {
switch outStoreFormat {
case JSON:
return marshalJSON(gossConfig)
case YAML:

0 comments on commit 6cb041d

Please sign in to comment.
You can’t perform that action at this time.