Skip to content

Commit

Permalink
Merge pull request #12 from aelsabbahy/enhancement/7
Browse files Browse the repository at this point in the history
Enhancement/7
  • Loading branch information
aelsabbahy committed Oct 30, 2015
2 parents 3413e9f + 18676a8 commit 2ccb02a
Show file tree
Hide file tree
Showing 21 changed files with 362 additions and 108 deletions.
6 changes: 6 additions & 0 deletions cmd/goss/goss.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ func main() {
Name: "add",
Aliases: []string{"a"},
Usage: "add a resource to the test suite",
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "ignore-attr, i",
Usage: "Ignore the following attributes when adding a new resource",
},
},
Subcommands: []cli.Command{
{
Name: "package",
Expand Down
2 changes: 1 addition & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import:
- package: github.com/aelsabbahy/GOnetstat
ref: 2726176f5ee4ec606af71d937f9126c8afc58bc4
- package: github.com/codegangsta/cli
ref: a65b733b303f0055f8d324d805f393cd3e7a7904
ref: 70e3fa51ebed95df8c0fbe1519c1c1f9bc16bb13
- package: github.com/godbus/dbus
ref: 9bb9dbf0e53309fa81d26e7230cbb6cff9373cad
- package: github.com/fatih/color
Expand Down
93 changes: 93 additions & 0 deletions integration-tests/goss/centos6/goss-expected-q.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"file": {
"/etc/passwd": {
"exists": true,
"contains": []
},
"/tmp/goss/foobar": {
"exists": false,
"contains": []
}
},
"package": {
"foobar": {
"installed": false
},
"httpd": {
"installed": true
},
"vim-tiny": {
"installed": false
}
},
"addr": {
"tcp://google.com:22": {
"reachable": false,
"timeout": 500
},
"tcp://google.com:443": {
"reachable": true,
"timeout": 500
}
},
"port": {
"tcp6:80": {
"listening": true
},
"tcp:9999": {
"listening": false
}
},
"service": {
"foobar": {
"enabled": false,
"running": false
},
"httpd": {
"enabled": true,
"running": true
}
},
"user": {
"apache": {
"exists": true
},
"foobar": {
"exists": false
}
},
"group": {
"apache": {
"exists": true
},
"foobar": {
"exists": false
}
},
"command": {
"foobar": {
"exit-status": "127",
"stdout": [],
"stderr": []
},
"httpd -v": {
"exit-status": "0",
"stdout": [],
"stderr": []
}
},
"dns": {
"localhost": {
"resolveable": true,
"timeout": 500
}
},
"process": {
"foobar": {
"running": false
},
"httpd": {
"running": true
}
}
}
21 changes: 11 additions & 10 deletions integration-tests/goss/generate_goss.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
SCRIPT_DIR=$(readlink -f $(dirname $0))

OS=$1
[[ $2 == "-q" ]] && args=("-i" "*")

goss() {
$SCRIPT_DIR/goss -g $SCRIPT_DIR/${OS}/goss-generated.json "$@"
Expand All @@ -13,45 +14,45 @@ goss() {
rm -f $SCRIPT_DIR/goss*generated*.json

for x in /etc/passwd /tmp/goss/foobar;do
goss a file $x
goss a "${args[@]}" file $x
done

[[ $OS == "centos6" ]] && package="httpd" || package="apache2"
[[ $OS == "centos6" ]] && user="apache" || user="www-data"
for x in $package foobar vim-tiny;do
goss a package $x
goss a "${args[@]}" package $x
done

for x in google.com:443 google.com:22;do
goss a addr $x
goss a "${args[@]}" addr $x
done

for x in tcp6:80 9999;do
goss a port $x
goss a "${args[@]}" port $x
done

for x in $package foobar;do
goss a service $x
goss a "${args[@]}" service $x
done

for x in $user foobar;do
goss a user $x
goss a "${args[@]}" user $x
done

for x in $user foobar;do
goss a group $x
goss a "${args[@]}" group $x
done

for x in "$package -v" foobar;do
goss a command "$x"
goss a "${args[@]}" command "$x"
done

for x in localhost;do
goss a dns $x
goss a "${args[@]}" dns $x
done

for x in $package foobar;do
goss a process $x
goss a "${args[@]}" process $x
done


Expand Down
93 changes: 93 additions & 0 deletions integration-tests/goss/wheezy/goss-expected-q.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"file": {
"/etc/passwd": {
"exists": true,
"contains": []
},
"/tmp/goss/foobar": {
"exists": false,
"contains": []
}
},
"package": {
"apache2": {
"installed": true
},
"foobar": {
"installed": false
},
"vim-tiny": {
"installed": false
}
},
"addr": {
"tcp://google.com:22": {
"reachable": false,
"timeout": 500
},
"tcp://google.com:443": {
"reachable": true,
"timeout": 500
}
},
"port": {
"tcp6:80": {
"listening": true
},
"tcp:9999": {
"listening": false
}
},
"service": {
"apache2": {
"enabled": true,
"running": true
},
"foobar": {
"enabled": false,
"running": false
}
},
"user": {
"foobar": {
"exists": false
},
"www-data": {
"exists": true
}
},
"group": {
"foobar": {
"exists": false
},
"www-data": {
"exists": true
}
},
"command": {
"apache2 -v": {
"exit-status": "0",
"stdout": [],
"stderr": []
},
"foobar": {
"exit-status": "127",
"stdout": [],
"stderr": []
}
},
"dns": {
"localhost": {
"resolveable": true,
"timeout": 500
}
},
"process": {
"apache2": {
"running": true
},
"foobar": {
"running": false
}
}
}
4 changes: 4 additions & 0 deletions integration-tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@ for os in centos6 wheezy;do

docker exec goss_int_test_$os bash -c "diff -wu /tmp/goss/${os}/goss-aa-expected.json /tmp/goss/${os}/goss-aa-generated.json"

docker exec goss_int_test_$os bash -c "time /tmp/goss/generate_goss.sh $os -q > /dev/null"

docker exec goss_int_test_$os bash -c "diff -wu /tmp/goss/${os}/goss-expected-q.json /tmp/goss/${os}/goss-generated.json"

#docker rm -vf goss_int_test_$os
done
24 changes: 13 additions & 11 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ func WriteJSON(filePath string, configJSON ConfigJSON) error {
}

func AppendResource(fileName, resourceName, key string, c *cli.Context) error {
ignoreList := c.GlobalStringSlice("ignore-attr")

var configJSON ConfigJSON
if _, err := os.Stat(fileName); err == nil {
configJSON = ReadJSON(fileName)
Expand All @@ -206,37 +208,37 @@ func AppendResource(fileName, resourceName, key string, c *cli.Context) error {
// Need to figure out a good way to refactor this
switch resourceName {
case "Addr":
res, _ := configJSON.Addrs.AppendSysResource(key, sys)
res, _ := configJSON.Addrs.AppendSysResource(key, sys, ignoreList)
resourcePrint(fileName, res)
case "Command":
res, _ := configJSON.Commands.AppendSysResource(key, sys)
res, _ := configJSON.Commands.AppendSysResource(key, sys, ignoreList)
resourcePrint(fileName, res)
case "DNS":
res, _ := configJSON.DNS.AppendSysResource(key, sys)
res, _ := configJSON.DNS.AppendSysResource(key, sys, ignoreList)
resourcePrint(fileName, res)
case "File":
res, _ := configJSON.Files.AppendSysResource(key, sys)
res, _ := configJSON.Files.AppendSysResource(key, sys, ignoreList)
resourcePrint(fileName, res)
case "Group":
res, _ := configJSON.Groups.AppendSysResource(key, sys)
res, _ := configJSON.Groups.AppendSysResource(key, sys, ignoreList)
resourcePrint(fileName, res)
case "Package":
res, _ := configJSON.Packages.AppendSysResource(key, sys)
res, _ := configJSON.Packages.AppendSysResource(key, sys, ignoreList)
resourcePrint(fileName, res)
case "Port":
res, _ := configJSON.Ports.AppendSysResource(key, sys)
res, _ := configJSON.Ports.AppendSysResource(key, sys, ignoreList)
resourcePrint(fileName, res)
case "Process":
res, _ := configJSON.Processes.AppendSysResource(key, sys)
res, _ := configJSON.Processes.AppendSysResource(key, sys, ignoreList)
resourcePrint(fileName, res)
case "Service":
res, _ := configJSON.Services.AppendSysResource(key, sys)
res, _ := configJSON.Services.AppendSysResource(key, sys, ignoreList)
resourcePrint(fileName, res)
case "User":
res, _ := configJSON.Users.AppendSysResource(key, sys)
res, _ := configJSON.Users.AppendSysResource(key, sys, ignoreList)
resourcePrint(fileName, res)
case "Gossfile":
res, _ := configJSON.Gossfiles.AppendSysResource(key, sys)
res, _ := configJSON.Gossfiles.AppendSysResource(key, sys, ignoreList)
resourcePrint(fileName, res)
}

Expand Down
2 changes: 1 addition & 1 deletion resource/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (h *Addr) Validate(sys *system.System) []TestResult {
return results
}

func NewAddr(sysAddr system.Addr) *Addr {
func NewAddr(sysAddr system.Addr, ignoreList []string) *Addr {
address := sysAddr.Address()
reachable, _ := sysAddr.Reachable()
return &Addr{
Expand Down
21 changes: 15 additions & 6 deletions resource/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,26 @@ func (c *Command) Validate(sys *system.System) []TestResult {
return results
}

func NewCommand(sysCommand system.Command) *Command {
func NewCommand(sysCommand system.Command, ignoreList []string) *Command {
command := sysCommand.Command()
exitStatus, _ := sysCommand.ExitStatus()
stdout, _ := sysCommand.Stdout()
stderr, _ := sysCommand.Stderr()
return &Command{
c := &Command{
Command: command,
ExitStatus: exitStatus.(string),
Stdout: readerToSlice(stdout),
Stderr: readerToSlice(stderr),
Stdout: []string{},
Stderr: []string{},
}

if !contains(ignoreList, "stdout") {
stdout, _ := sysCommand.Stdout()
c.Stdout = readerToSlice(stdout)
}
if !contains(ignoreList, "stderr") {
stderr, _ := sysCommand.Stderr()
c.Stderr = readerToSlice(stderr)
}

return c
}

func escapePattern(s string) string {
Expand Down
Loading

0 comments on commit 2ccb02a

Please sign in to comment.