Skip to content

Commit

Permalink
Merge pull request #64 from SimonBaeumer/add-tests
Browse files Browse the repository at this point in the history
Add output success tests and refactoring
  • Loading branch information
SimonBaeumer committed Jul 25, 2019
2 parents aa03806 + 95952eb commit 75f3003
Show file tree
Hide file tree
Showing 12 changed files with 468 additions and 85 deletions.
79 changes: 52 additions & 27 deletions outputs/documentation_test.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,70 @@
package outputs

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

func TestDocumentation_Name(t *testing.T) {
j := Documentation{}
assert.Equal(t, "documentation", j.Name())
d := Documentation{}
assert.Equal(t, "documentation", d.Name())
}

func TestDocumentation_Output(t *testing.T) {
var wg sync.WaitGroup
b := &bytes.Buffer{}
d, _ := time.ParseDuration("2s")
j := Documentation{FakeDuration: d}
out := make(chan []resource.TestResult)
r := 1

go func() {
defer wg.Done()
wg.Add(1)
r = j.Output(b, out, time.Now(), util.OutputConfig{})
}()

out <- GetExampleTestResult()

close(out)
wg.Wait()
expectedJson := `Title: my title
func TestDocumentation_Output_Success(t *testing.T) {
duration, _ := time.ParseDuration("2s")
d := Documentation{FakeDuration: duration}
result, exitCode := runOutput(d, getSuccessTestResult())

expected := `Title: my title
resource type: my resource id: a property: matches expectation: [expected]
Total Duration: 2.000s
Count: 1, Failed: 0, Skipped: 0
`
assert.Equal(t, expectedJson, b.String())
assert.Equal(t, 0, r)
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)
}
67 changes: 45 additions & 22 deletions outputs/json_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package outputs

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

func TestJson_Name(t *testing.T) {
Expand All @@ -16,22 +11,11 @@ func TestJson_Name(t *testing.T) {
}

func TestJson_Output(t *testing.T) {
var wg sync.WaitGroup
b := &bytes.Buffer{}
j := Json{FakeDuration: 1000}
out := make(chan []resource.TestResult)
r := 1
result, exitCode := runOutput(
Json{FakeDuration: 1000},
getSuccessTestResult(),
)

go func() {
defer wg.Done()
wg.Add(1)
r = j.Output(b, out, time.Now(), util.OutputConfig{})
}()

out <- GetExampleTestResult()

close(out)
wg.Wait()
expectedJson := `{
"results": [
{
Expand Down Expand Up @@ -61,6 +45,45 @@ func TestJson_Output(t *testing.T) {
}
}
`
assert.Equal(t, expectedJson, b.String())
assert.Equal(t, 0, r)
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)
}
38 changes: 38 additions & 0 deletions outputs/nagios_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package outputs

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

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

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)
}
Loading

0 comments on commit 75f3003

Please sign in to comment.