Skip to content

Commit

Permalink
Merge pull request #46 from aelsabbahy/44_add_meta
Browse files Browse the repository at this point in the history
Add `meta`
  • Loading branch information
aelsabbahy committed Mar 4, 2016
2 parents e153656 + a1af091 commit 804995a
Show file tree
Hide file tree
Showing 18 changed files with 205 additions and 39 deletions.
20 changes: 10 additions & 10 deletions outputs/outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,42 @@ var red = color.New(color.FgRed).SprintfFunc()

func humanizeResult(r resource.TestResult) string {
if r.Err != nil {
return red("%s: %s:\nError: %s", r.Title, r.Property, r.Err)
return red("%s: %s:\nError: %s", r.ResourceId, r.Property, r.Err)
}

if r.Successful {
return green("%s: %s: %s: matches expectation: %s", r.ResourceType, r.Title, r.Property, r.Expected)
return green("%s: %s: %s: matches expectation: %s", r.ResourceType, r.ResourceId, r.Property, r.Expected)
} else {
if r.Human != "" {
return red("%s: %s: %s:\n%s\n", r.ResourceType, r.Title, r.Property, r.Human)
return red("%s: %s: %s:\n%s\n", r.ResourceType, r.ResourceId, r.Property, r.Human)
}
return humanizeResult2(r)
}
}

func humanizeResult2(r resource.TestResult) string {
if r.Err != nil {
return red("%s: %s: Error: %s", r.Title, r.Property, r.Err)
return red("%s: %s: Error: %s", r.ResourceId, r.Property, r.Err)
}

switch r.TestType {
case resource.Value:
if r.Successful {
return green("%s: %s: %s: matches expectation: %s", r.ResourceType, r.Title, r.Property, r.Expected)
return green("%s: %s: %s: matches expectation: %s", r.ResourceType, r.ResourceId, r.Property, r.Expected)
} else {
return red("%s: %s: %s: doesn't match, expect: %s found: %s", r.ResourceType, r.Title, r.Property, r.Expected, r.Found)
return red("%s: %s: %s: doesn't match, expect: %s found: %s", r.ResourceType, r.ResourceId, r.Property, r.Expected, r.Found)
}
case resource.Values:
if r.Successful {
return green("%s: %s: %s: all expectations found: [%s]", r.ResourceType, r.Title, r.Property, strings.Join(r.Expected, ", "))
return green("%s: %s: %s: all expectations found: [%s]", r.ResourceType, r.ResourceId, r.Property, strings.Join(r.Expected, ", "))
} else {
return red("%s: %s: %s: expectations not found [%s]", r.ResourceType, r.Title, r.Property, strings.Join(subtractSlice(r.Expected, r.Found), ", "))
return red("%s: %s: %s: expectations not found [%s]", r.ResourceType, r.ResourceId, r.Property, strings.Join(subtractSlice(r.Expected, r.Found), ", "))
}
case resource.Contains:
if r.Successful {
return green("%s: %s: %s: all patterns found: [%s]", r.ResourceType, r.Title, r.Property, strings.Join(r.Expected, ", "))
return green("%s: %s: %s: all patterns found: [%s]", r.ResourceType, r.ResourceId, r.Property, strings.Join(r.Expected, ", "))
} else {
return red("%s: %s: %s: patterns not found: [%s]", r.ResourceType, r.Title, r.Property, strings.Join(subtractSlice(r.Expected, r.Found), ", "))
return red("%s: %s: %s: patterns not found: [%s]", r.ResourceType, r.ResourceId, r.Property, strings.Join(subtractSlice(r.Expected, r.Found), ", "))
}
default:
return red("Unexpected type %d", r.TestType)
Expand Down
7 changes: 6 additions & 1 deletion resource/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
)

type Addr struct {
Desc string `json:"desc,omitempty" yaml:"desc,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
Address string `json:"-" yaml:"-"`
Reachable bool `json:"reachable" yaml:"reachable"`
Timeout int `json:"timeout" yaml:"timeout"`
Expand All @@ -15,6 +16,10 @@ type Addr struct {
func (a *Addr) ID() string { return a.Address }
func (a *Addr) SetID(id string) { a.Address = id }

// FIXME: Can this be refactored?
func (r *Addr) GetTitle() string { return r.Title }
func (r *Addr) GetMeta() meta { return r.Meta }

func (a *Addr) Validate(sys *system.System) []TestResult {
if a.Timeout == 0 {
a.Timeout = 500
Expand Down
6 changes: 5 additions & 1 deletion resource/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
)

type Command struct {
Desc string `json:"desc,omitempty" yaml:"desc,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
Command string `json:"-" yaml:"-"`
ExitStatus matcher `json:"exit-status" yaml:"exit-status"`
Stdout []string `json:"stdout" yaml:"stdout"`
Expand All @@ -22,6 +23,9 @@ type Command struct {
func (c *Command) ID() string { return c.Command }
func (c *Command) SetID(id string) { c.Command = id }

func (c *Command) GetTitle() string { return c.Title }
func (c *Command) GetMeta() meta { return c.Meta }

func (c *Command) Validate(sys *system.System) []TestResult {
if c.Timeout == 0 {
c.Timeout = 10000
Expand Down
6 changes: 5 additions & 1 deletion resource/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
)

type DNS struct {
Desc string `json:"desc,omitempty" yaml:"desc,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
Host string `json:"-" yaml:"-"`
Resolveable bool `json:"resolveable" yaml:"resolveable"`
Addrs matcher `json:"addrs,omitempty" yaml:"addrs,omitempty"`
Expand All @@ -16,6 +17,9 @@ type DNS struct {
func (d *DNS) ID() string { return d.Host }
func (d *DNS) SetID(id string) { d.Host = id }

func (d *DNS) GetTitle() string { return d.Title }
func (d *DNS) GetMeta() meta { return d.Meta }

func (d *DNS) Validate(sys *system.System) []TestResult {
if d.Timeout == 0 {
d.Timeout = 500
Expand Down
6 changes: 5 additions & 1 deletion resource/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
)

type File struct {
Desc string `json:"desc,omitempty" yaml:"desc,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
Path string `json:"-" yaml:"-"`
Exists bool `json:"exists" yaml:"exists"`
Mode matcher `json:"mode,omitempty" yaml:"mode,omitempty"`
Expand All @@ -20,6 +21,9 @@ type File struct {
func (f *File) ID() string { return f.Path }
func (f *File) SetID(id string) { f.Path = id }

func (f *File) GetTitle() string { return f.Title }
func (f *File) GetMeta() meta { return f.Meta }

func (f *File) Validate(sys *system.System) []TestResult {
sysFile := sys.NewFile(f.Path, sys, util.Config{})

Expand Down
8 changes: 6 additions & 2 deletions resource/gossfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import (
)

type Gossfile struct {
Desc string `json:"desc,omitempty" yaml:"desc,omitempty"`
Path string `json:"-" yaml:"-"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
Path string `json:"-" yaml:"-"`
}

func (g *Gossfile) ID() string { return g.Path }
func (g *Gossfile) SetID(id string) { g.Path = id }

func (g *Gossfile) GetTitle() string { return g.Title }
func (g *Gossfile) GetMeta() meta { return g.Meta }

func NewGossfile(sysGossfile system.Gossfile, config util.Config) (*Gossfile, error) {
path := sysGossfile.Path()
return &Gossfile{
Expand Down
6 changes: 5 additions & 1 deletion resource/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
)

type Group struct {
Desc string `json:"desc,omitempty" yaml:"desc,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
Groupname string `json:"-" yaml:"-"`
Exists bool `json:"exists" yaml:"exists"`
GID matcher `json:"gid,omitempty" yaml:"gid,omitempty"`
Expand All @@ -17,6 +18,9 @@ type Group struct {
func (g *Group) ID() string { return g.Groupname }
func (g *Group) SetID(id string) { g.Groupname = id }

func (g *Group) GetTitle() string { return g.Title }
func (g *Group) GetMeta() meta { return g.Meta }

func (g *Group) Validate(sys *system.System) []TestResult {
sysgroup := sys.NewGroup(g.Groupname, sys, util.Config{})

Expand Down
6 changes: 5 additions & 1 deletion resource/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
)

type Package struct {
Desc string `json:"desc,omitempty" yaml:"desc,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
Name string `json:"-" yaml:"-"`
Installed bool `json:"installed" yaml:"installed"`
Versions matcher `json:"versions,omitempty" yaml:"versions,omitempty"`
Expand All @@ -15,6 +16,9 @@ type Package struct {
func (p *Package) ID() string { return p.Name }
func (p *Package) SetID(id string) { p.Name = id }

func (p *Package) GetTitle() string { return p.Title }
func (p *Package) GetMeta() meta { return p.Meta }

func (p *Package) Validate(sys *system.System) []TestResult {
sysPkg := sys.NewPackage(p.Name, sys, util.Config{})

Expand Down
6 changes: 5 additions & 1 deletion resource/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
)

type Port struct {
Desc string `json:"desc,omitempty" yaml:"desc,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
Port string `json:"-" yaml:"-"`
Listening bool `json:"listening" yaml:"listening"`
IP matcher `json:"ip,omitempty" yaml:"ip,omitempty"`
Expand All @@ -15,6 +16,9 @@ type Port struct {
func (p *Port) ID() string { return p.Port }
func (p *Port) SetID(id string) { p.Port = id }

func (p *Port) GetTitle() string { return p.Title }
func (p *Port) GetMeta() meta { return p.Meta }

func (p *Port) Validate(sys *system.System) []TestResult {
sysPort := sys.NewPort(p.Port, sys, util.Config{})

Expand Down
6 changes: 5 additions & 1 deletion resource/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import (
)

type Process struct {
Desc string `json:"desc,omitempty" yaml:"desc,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
Executable string `json:"-" yaml:"-"`
Running bool `json:"running" yaml:"running"`
}

func (p *Process) ID() string { return p.Executable }
func (p *Process) SetID(id string) { p.Executable = id }

func (p *Process) GetTitle() string { return p.Title }
func (p *Process) GetMeta() meta { return p.Meta }

func (p *Process) Validate(sys *system.System) []TestResult {
sysProcess := sys.NewProcess(p.Executable, sys, util.Config{})

Expand Down
7 changes: 6 additions & 1 deletion resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ type Resource interface {
SetID(string)
}

type IDer interface {
type ResourceRead interface {
ID() string
GetTitle() string
GetMeta() meta
}

type matcher interface{}
type meta map[string]interface{}

func contains(a []string, s string) bool {
for _, e := range a {
if m, _ := filepath.Match(e, s); m {
Expand Down
Loading

0 comments on commit 804995a

Please sign in to comment.