Skip to content

Commit

Permalink
Add outputer skip and failure tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBaeumer committed Jul 25, 2019
1 parent ae0d05a commit 95952eb
Show file tree
Hide file tree
Showing 12 changed files with 360 additions and 20 deletions.
46 changes: 44 additions & 2 deletions outputs/documentation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ func TestDocumentation_Name(t *testing.T) {
assert.Equal(t, "documentation", d.Name())
}

func TestDocumentation_Output(t *testing.T) {
func TestDocumentation_Output_Success(t *testing.T) {
duration, _ := time.ParseDuration("2s")
d := Documentation{FakeDuration: duration}
result, exitCode := runOutput(d, GetExampleTestResult())
result, exitCode := runOutput(d, getSuccessTestResult())

expected := `Title: my title
resource type: my resource id: a property: matches expectation: [expected]
Expand All @@ -26,3 +26,45 @@ Count: 1, Failed: 0, Skipped: 0
assert.Equal(t, expected, result)
assert.Equal(t, 0, exitCode)
}

func TestDocumentation_Output_Fail(t *testing.T) {
duration, _ := time.ParseDuration("2s")
d := Documentation{FakeDuration: duration}
result, exitCode := runOutput(d, getFailTestResult())

expected := `Title: failure
resource type: my resource id: a property: doesn't match, expect: [expected] found: []
Failures/Skipped:
Title: failure
resource type: my resource id: a property: doesn't match, expect: [expected] found: []
Total Duration: 2.000s
Count: 1, Failed: 1, Skipped: 0
`
assert.Equal(t, expected, result)
assert.Equal(t, 1, exitCode)
}

func TestDocumentation_Output_Skip(t *testing.T) {
duration, _ := time.ParseDuration("2s")
d := Documentation{FakeDuration: duration}
result, exitCode := runOutput(d, getSkipTestResult())

expected := `Title: failure
resource type: my resource id: a property: skipped
Failures/Skipped:
Title: failure
resource type: my resource id: a property: skipped
Total Duration: 2.000s
Count: 1, Failed: 0, Skipped: 1
`
assert.Equal(t, expected, result)
assert.Equal(t, 0, exitCode)
}
8 changes: 7 additions & 1 deletion outputs/json_oneline.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
)

// JsonOneline represents the JsonOneline output type
type JsonOneline struct{}
type JsonOneline struct {
duration time.Duration
}

// Name returns the name
func (r JsonOneline) Name() string { return "json_oneline" }
Expand Down Expand Up @@ -40,6 +42,10 @@ func (r JsonOneline) Output(w io.Writer, results <-chan []resource.TestResult,

summary := make(map[string]interface{})
duration := time.Since(startTime)
//testing purposes
if r.duration != 0 {
duration = r.duration
}
summary["test-count"] = testCount
summary["failed-count"] = failed
summary["total-duration"] = duration
Expand Down
42 changes: 42 additions & 0 deletions outputs/json_oneline_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package outputs

import (
"github.com/stretchr/testify/assert"
"testing"
"time"
)

func TestJsonOneline_Name(t *testing.T) {
j := JsonOneline{}
assert.Equal(t, "json_oneline", j.Name())
}

func TestJsonOneline_Output_FAIL(t *testing.T) {
duration, _ := time.ParseDuration("2s")
result, exitCode := runOutput(JsonOneline{duration: duration}, getFailTestResult())

expected := `{"results":[{"duration":500,"err":null,"expected":["expected"],"found":null,"human":"","meta":null,"property":"a property","resource-id":"my resource id","resource-type":"resource type","result":1,"successful":false,"summary-line":"resource type: my resource id: a property: doesn't match, expect: [expected] found: []","test-type":0,"title":"failure"}],"summary":{"failed-count":1,"summary-line":"Count: 1, Failed: 1, Duration: 2.000s","test-count":1,"total-duration":2000000000}}
`
assert.Equal(t, expected, result)
assert.Equal(t, 1, exitCode)
}

func TestJsonOneline_Output_Success(t *testing.T) {
duration, _ := time.ParseDuration("2s")
result, exitCode := runOutput(JsonOneline{duration: duration}, getSuccessTestResult())

expected := `{"results":[{"duration":500,"err":null,"expected":["expected"],"found":null,"human":"","meta":null,"property":"a property","resource-id":"my resource id","resource-type":"resource type","result":0,"successful":true,"summary-line":"resource type: my resource id: a property: matches expectation: [expected]","test-type":0,"title":"my title"}],"summary":{"failed-count":0,"summary-line":"Count: 1, Failed: 0, Duration: 2.000s","test-count":1,"total-duration":2000000000}}
`
assert.Equal(t, expected, result)
assert.Equal(t, 0, exitCode)
}

func TestJsonOneline_Output_Skip(t *testing.T) {
duration, _ := time.ParseDuration("2s")
result, exitCode := runOutput(JsonOneline{duration: duration}, getSkipTestResult())

expected := `{"results":[{"duration":500,"err":null,"expected":["expected"],"found":null,"human":"","meta":null,"property":"a property","resource-id":"my resource id","resource-type":"resource type","result":2,"successful":true,"summary-line":"resource type: my resource id: a property: skipped","test-type":0,"title":"failure"}],"summary":{"failed-count":0,"summary-line":"Count: 1, Failed: 0, Duration: 2.000s","test-count":1,"total-duration":2000000000}}
`
assert.Equal(t, expected, result)
assert.Equal(t, 0, exitCode)
}
41 changes: 40 additions & 1 deletion outputs/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestJson_Name(t *testing.T) {
func TestJson_Output(t *testing.T) {
result, exitCode := runOutput(
Json{FakeDuration: 1000},
GetExampleTestResult(),
getSuccessTestResult(),
)

expectedJson := `{
Expand Down Expand Up @@ -48,3 +48,42 @@ func TestJson_Output(t *testing.T) {
assert.Equal(t, expectedJson, result)
assert.Equal(t, 0, exitCode)
}

func TestJson_Output_FAIL(t *testing.T) {
result, exitCode := runOutput(
Json{FakeDuration: 1000},
getFailTestResult(),
)

expected := `{
"results": [
{
"duration": 500,
"err": null,
"expected": [
"expected"
],
"found": null,
"human": "",
"meta": null,
"property": "a property",
"resource-id": "my resource id",
"resource-type": "resource type",
"result": 1,
"successful": false,
"summary-line": "resource type: my resource id: a property: doesn't match, expect: [expected] found: []",
"test-type": 0,
"title": "failure"
}
],
"summary": {
"failed-count": 1,
"summary-line": "Count: 1, Failed: 1, Duration: 0.000s",
"test-count": 1,
"total-duration": 1000
}
}
`
assert.Equal(t, expected, result)
assert.Equal(t, 1, exitCode)
}
10 changes: 8 additions & 2 deletions outputs/junit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
)

// JUnit represents the junit output type
type JUnit struct{}
type JUnit struct {
testingTimestamp string
}

// Name returns the name
func (r JUnit) Name() string { return "junit" }
Expand All @@ -28,6 +30,10 @@ func (r JUnit) Output(w io.Writer, results <-chan []resource.TestResult,

// ISO8601 timeformat
timestamp := time.Now().Format(time.RFC3339)
// Testing purposes to set the timestamp directly
if r.testingTimestamp != "" {
timestamp = r.testingTimestamp
}

var summary map[int]string
summary = make(map[int]string)
Expand Down Expand Up @@ -66,7 +72,7 @@ func (r JUnit) Output(w io.Writer, results <-chan []resource.TestResult,
duration := time.Since(startTime)
fmt.Fprintln(w, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
fmt.Fprintf(w, "<testsuite name=\"goss\" errors=\"0\" tests=\"%d\" "+
"failures=\"%d\" skipped=\"%d\" time=\"%.3f\" timestamp=\"%s\">\n",
"failures=\"%d\" skipped=\"%d\" time=\"%.3f\" testingTimestamp=\"%s\">\n",
testCount, failed, skipped, duration.Seconds(), timestamp)

for i := 0; i < testCount; i++ {
Expand Down
60 changes: 60 additions & 0 deletions outputs/junit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package outputs

import (
"github.com/stretchr/testify/assert"
"testing"
"time"
)

func TestJunit_Name(t *testing.T) {
j := JUnit{}
assert.Equal(t, "junit", j.Name())
}

func getDate() string {
return time.Date(
2019, 01, 01, 10, 30, 30, 3000, time.UTC).Format(time.RFC3339)
}

func TestJunit_Output_Fail(t *testing.T) {
result, exitCode := runOutput(JUnit{testingTimestamp: getDate()}, getFailTestResult())

expected := `<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="goss" errors="0" tests="1" failures="1" skipped="0" time="0.000" testingTimestamp="2019-01-01T10:30:30Z">
<testcase name="resource type my resource id a property" time="0.000">
<system-err>resource type: my resource id: a property: doesn&#39;t match, expect: [expected] found: []</system-err>
<failure>resource type: my resource id: a property: doesn&#39;t match, expect: [expected] found: []</failure>
</testcase>
</testsuite>
`
assert.Equal(t, expected, result)
assert.Equal(t, 1, exitCode)
}

func TestJunit_Output_Success(t *testing.T) {
result, exitCode := runOutput(JUnit{testingTimestamp: getDate()}, getSuccessTestResult())

expected := `<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="goss" errors="0" tests="1" failures="0" skipped="0" time="0.000" testingTimestamp="2019-01-01T10:30:30Z">
<testcase name="resource type my resource id a property" time="0.000">
<system-out>resource type: my resource id: a property: matches expectation: [expected]</system-out>
</testcase>
</testsuite>
`
assert.Equal(t, expected, result)
assert.Equal(t, 0, exitCode)
}

func TestJunit_Output_Skip(t *testing.T) {
result, exitCode := runOutput(JUnit{testingTimestamp: getDate()}, getSkipTestResult())

expected := `<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="goss" errors="0" tests="1" failures="0" skipped="1" time="0.000" testingTimestamp="2019-01-01T10:30:30Z">
<testcase name="resource type my resource id a property" time="0.000">
<skipped/><system-out>resource type: my resource id: a property: skipped</system-out>
</testcase>
</testsuite>
`
assert.Equal(t, expected, result)
assert.Equal(t, 0, exitCode)
}
24 changes: 21 additions & 3 deletions outputs/nagios_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,34 @@ import (
"testing"
)

func TestNagiosDocumentation_Name(t *testing.T) {
func TestNagios_Name(t *testing.T) {
j := Nagios{}
assert.Equal(t, "nagios", j.Name())
}

func TestNagiosDocumentation_Output(t *testing.T) {
result, exitCode := runOutput(Nagios{}, GetExampleTestResult())
func TestNagiosOuput_Success(t *testing.T) {
result, exitCode := runOutput(Nagios{}, getSuccessTestResult())

expected := `GOSS OK - Count: 1, Failed: 0, Skipped: 0, Duration: 0.000s
`
assert.Equal(t, expected, result)
assert.Equal(t, 0, exitCode)
}

func TestNagiosOuput_Fail(t *testing.T) {
result, exitCode := runOutput(Nagios{}, getFailTestResult())

expected := `GOSS CRITICAL - Count: 1, Failed: 1, Skipped: 0, Duration: 0.000s
`
assert.Equal(t, expected, result)
assert.Equal(t, 2, exitCode)
}

func TestNagiosOuput_Skip(t *testing.T) {
result, exitCode := runOutput(Nagios{}, getSkipTestResult())

expected := `GOSS OK - Count: 1, Failed: 0, Skipped: 1, Duration: 0.000s
`
assert.Equal(t, expected, result)
assert.Equal(t, 0, exitCode)
}
23 changes: 23 additions & 0 deletions outputs/outputs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package outputs

import (
"github.com/magiconair/properties/assert"
"testing"
)

func Test_Outputers(t *testing.T) {
outputer := GetOutputer("json")
assert.Equal(t, "json", outputer.Name())

registeredOutputers := []string{
"documentation",
"json",
"json_oneline",
"junit",
"nagios",
"rspecish",
"silent",
"tap",
}
assert.Equal(t, registeredOutputers, Outputers())
}
46 changes: 42 additions & 4 deletions outputs/rspecish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,54 @@ func TestRspecish_Name(t *testing.T) {
assert.Equal(t, "rspecish", j.Name())
}

func TestRspecish_Output(t *testing.T) {
func TestRspecish_Output_Success(t *testing.T) {
d, _ := time.ParseDuration("2s")

result, exitCode := runOutput(Rspecish{FakeDuration: d}, GetExampleTestResult())
result, exitCode := runOutput(Rspecish{FakeDuration: d}, getSuccessTestResult())

expectedJson := `.
expected := `.
Total Duration: 2.000s
Count: 1, Failed: 0, Skipped: 0
`
assert.Equal(t, expectedJson, result)
assert.Equal(t, expected, result)
assert.Equal(t, 0, exitCode)
}

func TestRspecish_Output_Fail(t *testing.T) {
d, _ := time.ParseDuration("2s")

result, exitCode := runOutput(Rspecish{FakeDuration: d}, getFailTestResult())

expected := `F
Failures/Skipped:
Title: failure
resource type: my resource id: a property: doesn't match, expect: [expected] found: []
Total Duration: 2.000s
Count: 1, Failed: 1, Skipped: 0
`
assert.Equal(t, expected, result)
assert.Equal(t, 1, exitCode)
}

func TestRspecish_Output_Skip(t *testing.T) {
d, _ := time.ParseDuration("2s")

result, exitCode := runOutput(Rspecish{FakeDuration: d}, getSkipTestResult())

expected := `S
Failures/Skipped:
Title: failure
resource type: my resource id: a property: skipped
Total Duration: 2.000s
Count: 1, Failed: 0, Skipped: 1
`
assert.Equal(t, expected, result)
assert.Equal(t, 0, exitCode)
}
Loading

0 comments on commit 95952eb

Please sign in to comment.