Skip to content

Commit

Permalink
Add Validator.Validate test
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBaeumer committed May 13, 2019
1 parent 68fa130 commit 8ae054b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cmd/goss/goss.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func main() {
color.NoColor = false
}

v.Validate(startTime)
os.Exit(v.Validate(startTime))
return nil
},
},
Expand Down
1 change: 0 additions & 1 deletion serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func TestHealthHandler_Serve(t *testing.T) {
t.Fatal(err)
}

//var cmdResource resource.Resource
cmdResource := &resource.Command{
Command: "echo hello",
Title: "echo hello",
Expand Down
33 changes: 19 additions & 14 deletions validate.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package goss

import (
"fmt"
"fmt"
"io"
"os"
"runtime"
"sync"
"time"

"github.com/SimonBaeumer/goss/outputs"
"github.com/SimonBaeumer/goss/resource"
"github.com/SimonBaeumer/goss/system"
"github.com/SimonBaeumer/goss/util"
"github.com/fatih/color"
"runtime"
"sync"
"time"

"github.com/SimonBaeumer/goss/outputs"
"github.com/SimonBaeumer/goss/resource"
"github.com/SimonBaeumer/goss/system"
"github.com/SimonBaeumer/goss/util"
"github.com/fatih/color"
)

type Validator struct {
Expand All @@ -22,10 +23,14 @@ type Validator struct {
Outputer outputs.Outputer
Package string //Should be in the package resource config
MaxConcurrent int //Separating concurrency and validation, irritating atm...
OutputWriter io.Writer
}

// Validate validation runtime
func (v *Validator) Validate(startTime time.Time) {
func (v *Validator) Validate(startTime time.Time) int {
if v.OutputWriter == nil {
v.OutputWriter = os.Stdout
}

outputConfig := util.OutputConfig{
FormatOptions: v.FormatOptions,
Expand All @@ -38,15 +43,15 @@ func (v *Validator) Validate(startTime time.Time) {
iStartTime := time.Now()

out := validate(sys, v.GossConfig, v.MaxConcurrent)
exitCode := v.Outputer.Output(os.Stdout, out, iStartTime, outputConfig)
exitCode := v.Outputer.Output(v.OutputWriter, out, iStartTime, outputConfig)
if v.RetryTimeout == 0 || exitCode == 0 {
os.Exit(exitCode)
return exitCode
}

elapsed := time.Since(startTime)
if elapsed + v.Sleep > v.RetryTimeout {
color.Red("\nERROR: Timeout of %s reached before tests entered a passing state", v.RetryTimeout)
os.Exit(3)
return exitCode
}
color.Red("Retrying in %s (elapsed/timeout time: %.3fs/%s)\n\n\n", v.Sleep, elapsed.Seconds(), v.RetryTimeout)

Expand Down
31 changes: 31 additions & 0 deletions validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package goss

import (
"bytes"
"github.com/SimonBaeumer/goss/outputs"
"github.com/SimonBaeumer/goss/resource"
"github.com/stretchr/testify/assert"
"testing"
"time"
)

func TestValidator_Validate(t *testing.T) {
cmdResource := &resource.Command{Title: "echo hello", Command: "echo hello", ExitStatus: 0}

w := &bytes.Buffer{}
v := Validator{
GossConfig: GossConfig{
Commands: resource.CommandMap{"echo hello": cmdResource},
},
MaxConcurrent: 1,
Outputer: outputs.GetOutputer("documentation"),
OutputWriter: w,
}

r := v.Validate(time.Now())

assert.Equal(t, 0, r)
assert.Contains(t, w.String(), "Title: echo hello")
assert.Contains(t, w.String(), "Command: echo hello: exit-status: matches expectation: [0]")
assert.Contains(t, w.String(), "Count: 1, Failed: 0, Skipped: 0")
}

0 comments on commit 8ae054b

Please sign in to comment.