Skip to content

Commit bea949f

Browse files
committed
migrate tests into ginkgo framework
1 parent 920e568 commit bea949f

File tree

4 files changed

+124
-114
lines changed

4 files changed

+124
-114
lines changed

pkg/connectivity/junit.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package connectivity
2+
3+
import (
4+
"encoding/xml"
5+
junit "github.com/jstemmer/go-junit-report/formatter"
6+
"github.com/sirupsen/logrus"
7+
"io"
8+
"os"
9+
"strconv"
10+
"strings"
11+
)
12+
13+
func PrintJUnitResults(filename string, summary *Summary) {
14+
if filename == "" {
15+
return
16+
}
17+
18+
f, err := os.Create(filename)
19+
if err != nil {
20+
logrus.Errorf("Unable to create file %q for junit output: %v\n", filename, err)
21+
return
22+
}
23+
24+
defer f.Close()
25+
if err := printJunit(f, summary); err != nil {
26+
logrus.Errorf("Unable to write junit output: %v\n", err)
27+
}
28+
}
29+
30+
func printJunit(w io.Writer, summary *Summary) error {
31+
s := summaryToJunit(summary)
32+
enc := xml.NewEncoder(w)
33+
return enc.Encode(s)
34+
}
35+
36+
func summaryToJunit(summary *Summary) junit.JUnitTestSuite {
37+
s := junit.JUnitTestSuite{
38+
Name: "cyclonus",
39+
Failures: summary.Failed,
40+
TestCases: []junit.JUnitTestCase{},
41+
}
42+
43+
for _, testStrings := range summary.Tests {
44+
_, testName, passed := parseTestStrings(testStrings)
45+
// Only cases where the testname are non-empty are new tests, otherwise it
46+
// is multi-line output of the test.
47+
if testName == "" {
48+
continue
49+
}
50+
tc := junit.JUnitTestCase{
51+
Name: testName,
52+
}
53+
if !passed {
54+
tc.Failure = &junit.JUnitFailure{}
55+
}
56+
s.TestCases = append(s.TestCases, tc)
57+
}
58+
return s
59+
}
60+
61+
func parseTestStrings(input []string) (testNumber int, testName string, passed bool) {
62+
split := strings.SplitN(input[0], ": ", 2)
63+
if len(split) < 2 {
64+
return 0, "", false
65+
}
66+
67+
testNumber, err := strconv.Atoi(split[0])
68+
if err != nil {
69+
logrus.Errorf("error parsing test number from string %q for junit: %v", split[0], err)
70+
}
71+
72+
if len(input) > 1 && input[1] == "passed" {
73+
passed = true
74+
}
75+
76+
return testNumber, split[1], passed
77+
}

pkg/connectivity/printer.go

+1-66
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
package connectivity
22

33
import (
4-
"encoding/xml"
54
"fmt"
6-
"io"
75
"math"
8-
"os"
96
"sort"
10-
"strconv"
117
"strings"
128

13-
junit "github.com/jstemmer/go-junit-report/formatter"
149
"github.com/mattfenwick/cyclonus/pkg/generator"
1510
"github.com/mattfenwick/cyclonus/pkg/utils"
1611
"github.com/olekukonko/tablewriter"
1712
"github.com/pkg/errors"
18-
"github.com/sirupsen/logrus"
1913
v1 "k8s.io/api/core/v1"
2014
networkingv1 "k8s.io/api/networking/v1"
2115
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -39,66 +33,7 @@ func (t *Printer) PrintSummary() {
3933

4034
fmt.Printf("Feature results:\n%s\n\n", t.printMarkdownFeatureTable(summary.FeaturePrimaryCounts, summary.FeatureCounts))
4135
fmt.Printf("Tag results:\n%s\n", t.printMarkdownFeatureTable(summary.TagPrimaryCounts, summary.TagCounts))
42-
if t.JunitResultsFile != "" {
43-
f, err := os.Create(t.JunitResultsFile)
44-
if err != nil {
45-
logrus.Errorf("Unable to create file %q for junit output: %v\n", t.JunitResultsFile, err)
46-
} else {
47-
defer f.Close()
48-
if err := printJunit(f, summary); err != nil {
49-
logrus.Errorf("Unable to write junit output: %v\n", err)
50-
}
51-
}
52-
}
53-
}
54-
55-
func printJunit(w io.Writer, summary *Summary) error {
56-
s := summaryToJunit(summary)
57-
enc := xml.NewEncoder(w)
58-
return enc.Encode(s)
59-
}
60-
61-
func summaryToJunit(summary *Summary) junit.JUnitTestSuite {
62-
s := junit.JUnitTestSuite{
63-
Name: "cyclonus",
64-
Failures: summary.Failed,
65-
TestCases: []junit.JUnitTestCase{},
66-
}
67-
68-
for _, testStrings := range summary.Tests {
69-
_, testName, passed := parseTestStrings(testStrings)
70-
// Only cases where the testname are non-empty are new tests, otherwise it
71-
// is multi-line output of the test.
72-
if testName == "" {
73-
continue
74-
}
75-
tc := junit.JUnitTestCase{
76-
Name: testName,
77-
}
78-
if !passed {
79-
tc.Failure = &junit.JUnitFailure{}
80-
}
81-
s.TestCases = append(s.TestCases, tc)
82-
}
83-
return s
84-
}
85-
86-
func parseTestStrings(input []string) (testNumber int, testName string, passed bool) {
87-
split := strings.SplitN(input[0], ": ", 2)
88-
if len(split) < 2 {
89-
return 0, "", false
90-
}
91-
92-
testNumber, err := strconv.Atoi(split[0])
93-
if err != nil {
94-
logrus.Errorf("error parsing test number from string %q for junit: %v", split[0], err)
95-
}
96-
97-
if len(input) > 1 && input[1] == "passed" {
98-
passed = true
99-
}
100-
101-
return testNumber, split[1], passed
36+
PrintJUnitResults(t.JunitResultsFile, summary)
10237
}
10338

10439
const (

pkg/connectivity/printer_test.go

+45-48
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,63 @@ package connectivity
33
import (
44
"bytes"
55
"flag"
6+
. "github.com/onsi/ginkgo"
7+
. "github.com/onsi/gomega"
8+
"github.com/sirupsen/logrus"
69
"io/ioutil"
7-
"testing"
810
)
911

1012
var update = flag.Bool("update", false, "update .golden files")
1113

12-
func TestPrintJunit(t *testing.T) {
13-
testCases := []struct {
14-
desc string
15-
summary *Summary
16-
expectFile string
17-
expectErr string
18-
}{
19-
{
20-
desc: "Empty summary",
21-
summary: &Summary{},
22-
expectFile: "testdata/empty-summary.golden.xml",
23-
}, {
24-
desc: "2 pass 2 fail",
25-
summary: &Summary{
26-
Tests: [][]string{
27-
{"1: test1", "passed", ""},
28-
{"2: test2 with spaces", "failed", ""},
29-
{"3: test3 with + special %chars/", "passed", ""},
30-
{"4: test4 with\nnewlines", "foo-is-failed", ""},
14+
func RunPrinterTests() {
15+
Describe("Test junit file print", func() {
16+
testCases := []struct {
17+
desc string
18+
summary *Summary
19+
expectFile string
20+
expectErr string
21+
}{
22+
{
23+
desc: "Empty summary",
24+
summary: &Summary{},
25+
expectFile: "testdata/empty-summary.golden.xml",
26+
}, {
27+
desc: "2 pass 2 fail",
28+
summary: &Summary{
29+
Tests: [][]string{
30+
{"1: test1", "passed", ""},
31+
{"2: test2 with spaces", "failed", ""},
32+
{"3: test3 with + special %chars/", "passed", ""},
33+
{"4: test4 with\nnewlines", "foo-is-failed", ""},
34+
},
3135
},
36+
expectFile: "testdata/2-pass-2-fail.golden.xml",
37+
}, {
38+
desc: "Uses failure count from summary",
39+
summary: &Summary{
40+
Failed: 10,
41+
},
42+
expectFile: "testdata/use-summary-failure-count.golden.xml",
3243
},
33-
expectFile: "testdata/2-pass-2-fail.golden.xml",
34-
}, {
35-
desc: "Uses failure count from summary",
36-
summary: &Summary{
37-
Failed: 10,
38-
},
39-
expectFile: "testdata/use-summary-failure-count.golden.xml",
40-
},
41-
}
42-
for _, tC := range testCases {
43-
t.Run(tC.desc, func(t *testing.T) {
44+
}
45+
for _, testCase := range testCases {
4446
var output bytes.Buffer
45-
err := printJunit(&output, tC.summary)
47+
err := printJunit(&output, testCase.summary)
4648
if err != nil {
47-
if len(tC.expectErr) == 0 {
48-
t.Fatalf("Expected nil error but got %v", err)
49-
}
50-
if err.Error() != tC.expectErr {
51-
t.Fatalf("Expected error %q but got %q", err, tC.expectErr)
52-
}
49+
Expect(len(testCase.expectErr)).ToNot(Equal(0))
50+
Expect(err.Error()).To(Equal(testCase.expectErr))
5351
}
5452

5553
if *update {
56-
ioutil.WriteFile(tC.expectFile, output.Bytes(), 0666)
54+
err := ioutil.WriteFile(testCase.expectFile, output.Bytes(), 0666)
55+
Expect(err).To(BeNil())
5756
} else {
58-
fileData, err := ioutil.ReadFile(tC.expectFile)
59-
if err != nil {
60-
t.Fatalf("Failed to read golden file %v: %v", tC.expectFile, err)
61-
}
62-
if !bytes.Equal(fileData, output.Bytes()) {
63-
t.Errorf("Expected junit to equal goldenfile: %v but instead got:\n\n%v", tC.expectFile, output.String())
64-
}
57+
fileData, err := ioutil.ReadFile(testCase.expectFile)
58+
Expect(err).To(BeNil())
59+
logrus.Infof("expected: \n%s\n", fileData)
60+
logrus.Infof("actual: \n%s\n", output.Bytes())
61+
Expect(bytes.Equal(fileData, output.Bytes())).To(BeTrue())
6562
}
66-
})
67-
}
63+
}
64+
})
6865
}

pkg/connectivity/suite_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ import (
1010
func TestConnectivity(t *testing.T) {
1111
RegisterFailHandler(Fail)
1212
RunTestCaseStateTests()
13+
RunPrinterTests()
1314
RunSpecs(t, "connectivity suite")
1415
}

0 commit comments

Comments
 (0)