-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from SimonBaeumer/add-tests
Add output success tests and refactoring
- Loading branch information
Showing
12 changed files
with
468 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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't match, expect: [expected] found: []</system-err> | ||
<failure>resource type: my resource id: a property: doesn'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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.