-
Notifications
You must be signed in to change notification settings - Fork 24
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 #116 from mattfenwick/ginkgo
migrate tests into ginkgo framework
- Loading branch information
Showing
10 changed files
with
232 additions
and
248 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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package connectivity | ||
|
||
import ( | ||
"encoding/xml" | ||
junit "github.com/jstemmer/go-junit-report/formatter" | ||
"github.com/sirupsen/logrus" | ||
"os" | ||
) | ||
|
||
type JUnitTestResult struct { | ||
Passed bool | ||
Name string | ||
} | ||
|
||
func PrintJUnitResults(filename string, results []*Result, ignoreLoopback bool) error { | ||
if filename == "" { | ||
return nil | ||
} | ||
|
||
var junitResults []*JUnitTestResult | ||
for _, result := range results { | ||
junitResults = append(junitResults, &JUnitTestResult{ | ||
Passed: result.Passed(ignoreLoopback), | ||
Name: result.TestCase.Description, | ||
}) | ||
} | ||
|
||
f, err := os.Create(filename) | ||
if err != nil { | ||
logrus.Errorf("Unable to create file %q for junit output: %v\n", filename, err) | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
junitTestSuite := ResultsToJUnit(junitResults) | ||
enc := xml.NewEncoder(f) | ||
enc.Indent("", " ") | ||
return enc.Encode(junitTestSuite) | ||
} | ||
|
||
func ResultsToJUnit(results []*JUnitTestResult) junit.JUnitTestSuite { | ||
var testCases []junit.JUnitTestCase | ||
failed := 0 | ||
|
||
for _, result := range results { | ||
testCase := junit.JUnitTestCase{ | ||
Name: result.Name, | ||
} | ||
if !result.Passed { | ||
testCase.Failure = &junit.JUnitFailure{} | ||
failed++ | ||
} | ||
testCases = append(testCases, testCase) | ||
} | ||
return junit.JUnitTestSuite{ | ||
Name: "cyclonus", | ||
Failures: failed, | ||
TestCases: testCases, | ||
} | ||
} |
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 |
---|---|---|
@@ -1,68 +1,51 @@ | ||
package connectivity | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"io/ioutil" | ||
"testing" | ||
junit "github.com/jstemmer/go-junit-report/formatter" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var update = flag.Bool("update", false, "update .golden files") | ||
|
||
func TestPrintJunit(t *testing.T) { | ||
testCases := []struct { | ||
desc string | ||
summary *Summary | ||
expectFile string | ||
expectErr string | ||
}{ | ||
{ | ||
desc: "Empty summary", | ||
summary: &Summary{}, | ||
expectFile: "testdata/empty-summary.golden.xml", | ||
}, { | ||
desc: "2 pass 2 fail", | ||
summary: &Summary{ | ||
Tests: [][]string{ | ||
{"1: test1", "passed", ""}, | ||
{"2: test2 with spaces", "failed", ""}, | ||
{"3: test3 with + special %chars/", "passed", ""}, | ||
{"4: test4 with\nnewlines", "foo-is-failed", ""}, | ||
func RunPrinterTests() { | ||
Describe("JUnit cyclonus output", func() { | ||
It("should convert results to junit", func() { | ||
testCases := []struct { | ||
desc string | ||
results []*JUnitTestResult | ||
junit junit.JUnitTestSuite | ||
}{ | ||
{ | ||
desc: "Empty summary", | ||
results: nil, | ||
junit: junit.JUnitTestSuite{ | ||
Failures: 0, | ||
Name: "cyclonus", | ||
TestCases: nil, | ||
}, | ||
}, { | ||
desc: "2 pass 2 fail", | ||
results: []*JUnitTestResult{ | ||
{Name: "test1", Passed: true}, | ||
{Name: "test2 with spaces", Passed: false}, | ||
{Name: "test3 with + special %chars/", Passed: true}, | ||
{Name: "test4 with\nnewlines", Passed: false}, | ||
}, | ||
junit: junit.JUnitTestSuite{ | ||
Failures: 2, | ||
Name: "cyclonus", | ||
TestCases: []junit.JUnitTestCase{ | ||
{Name: "test1", Failure: nil}, | ||
{Name: "test2 with spaces", Failure: &junit.JUnitFailure{}}, | ||
{Name: "test3 with + special %chars/", Failure: nil}, | ||
{Name: "test4 with\nnewlines", Failure: &junit.JUnitFailure{}}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectFile: "testdata/2-pass-2-fail.golden.xml", | ||
}, { | ||
desc: "Uses failure count from summary", | ||
summary: &Summary{ | ||
Failed: 10, | ||
}, | ||
expectFile: "testdata/use-summary-failure-count.golden.xml", | ||
}, | ||
} | ||
for _, tC := range testCases { | ||
t.Run(tC.desc, func(t *testing.T) { | ||
var output bytes.Buffer | ||
err := printJunit(&output, tC.summary) | ||
if err != nil { | ||
if len(tC.expectErr) == 0 { | ||
t.Fatalf("Expected nil error but got %v", err) | ||
} | ||
if err.Error() != tC.expectErr { | ||
t.Fatalf("Expected error %q but got %q", err, tC.expectErr) | ||
} | ||
} | ||
|
||
if *update { | ||
ioutil.WriteFile(tC.expectFile, output.Bytes(), 0666) | ||
} else { | ||
fileData, err := ioutil.ReadFile(tC.expectFile) | ||
if err != nil { | ||
t.Fatalf("Failed to read golden file %v: %v", tC.expectFile, err) | ||
} | ||
if !bytes.Equal(fileData, output.Bytes()) { | ||
t.Errorf("Expected junit to equal goldenfile: %v but instead got:\n\n%v", tC.expectFile, output.String()) | ||
} | ||
for _, testCase := range testCases { | ||
actual := ResultsToJUnit(testCase.results) | ||
Expect(actual).To(Equal(testCase.junit)) | ||
} | ||
}) | ||
} | ||
}) | ||
} |
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
Oops, something went wrong.