Skip to content

Commit

Permalink
Merge pull request #116 from mattfenwick/ginkgo
Browse files Browse the repository at this point in the history
migrate tests into ginkgo framework
  • Loading branch information
mattfenwick authored May 2, 2021
2 parents 920e568 + a2b7cb0 commit 6e98257
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 248 deletions.
60 changes: 60 additions & 0 deletions pkg/connectivity/junit.go
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,
}
}
71 changes: 5 additions & 66 deletions pkg/connectivity/printer.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package connectivity

import (
"encoding/xml"
"fmt"
"io"
log "github.com/sirupsen/logrus"
"math"
"os"
"sort"
"strconv"
"strings"

junit "github.com/jstemmer/go-junit-report/formatter"
"github.com/mattfenwick/cyclonus/pkg/generator"
"github.com/mattfenwick/cyclonus/pkg/utils"
"github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -30,7 +25,7 @@ type Printer struct {
}

func (t *Printer) PrintSummary() {
summary := (&CombinedResults{Results: t.Results}).Summary(t.IgnoreLoopback)
summary := NewSummaryTableFromResults(t.IgnoreLoopback, t.Results)
t.printTestSummary(summary.Tests)
for primary, counts := range summary.TagCounts {
fmt.Println(passFailTable(primary, counts, nil, nil))
Expand All @@ -39,66 +34,10 @@ func (t *Printer) PrintSummary() {

fmt.Printf("Feature results:\n%s\n\n", t.printMarkdownFeatureTable(summary.FeaturePrimaryCounts, summary.FeatureCounts))
fmt.Printf("Tag results:\n%s\n", t.printMarkdownFeatureTable(summary.TagPrimaryCounts, summary.TagCounts))
if t.JunitResultsFile != "" {
f, err := os.Create(t.JunitResultsFile)
if err != nil {
logrus.Errorf("Unable to create file %q for junit output: %v\n", t.JunitResultsFile, err)
} else {
defer f.Close()
if err := printJunit(f, summary); err != nil {
logrus.Errorf("Unable to write junit output: %v\n", err)
}
}
}
}

func printJunit(w io.Writer, summary *Summary) error {
s := summaryToJunit(summary)
enc := xml.NewEncoder(w)
return enc.Encode(s)
}

func summaryToJunit(summary *Summary) junit.JUnitTestSuite {
s := junit.JUnitTestSuite{
Name: "cyclonus",
Failures: summary.Failed,
TestCases: []junit.JUnitTestCase{},
}

for _, testStrings := range summary.Tests {
_, testName, passed := parseTestStrings(testStrings)
// Only cases where the testname are non-empty are new tests, otherwise it
// is multi-line output of the test.
if testName == "" {
continue
}
tc := junit.JUnitTestCase{
Name: testName,
}
if !passed {
tc.Failure = &junit.JUnitFailure{}
}
s.TestCases = append(s.TestCases, tc)
}
return s
}

func parseTestStrings(input []string) (testNumber int, testName string, passed bool) {
split := strings.SplitN(input[0], ": ", 2)
if len(split) < 2 {
return 0, "", false
if err := PrintJUnitResults(t.JunitResultsFile, t.Results, t.IgnoreLoopback); err != nil {
log.Errorf("unable to dump JUnit test results: %+v", err)
}

testNumber, err := strconv.Atoi(split[0])
if err != nil {
logrus.Errorf("error parsing test number from string %q for junit: %v", split[0], err)
}

if len(input) > 1 && input[1] == "passed" {
passed = true
}

return testNumber, split[1], passed
}

const (
Expand Down Expand Up @@ -168,7 +107,7 @@ func (t *Printer) printMarkdownFeatureTable(primaryCounts map[string]map[bool]in

func (t *Printer) printTestSummary(rows [][]string) {
tableString := &strings.Builder{}
tableString.WriteString("Summary:\n")
tableString.WriteString("SummaryTable:\n")
table := tablewriter.NewWriter(tableString)
table.SetRowLine(true)

Expand Down
99 changes: 41 additions & 58 deletions pkg/connectivity/printer_test.go
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))
}
})
}
})
}
6 changes: 3 additions & 3 deletions pkg/connectivity/probe/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func NewTableFromJobResults(resources *Resources, jobResults []*JobResult) *Tabl
return table
}

func (t *Table) Set(from string, to string, value *Item) {
t.Wrapped.Set(from, to, value)
}
//func (t *Table) Set(from string, to string, value *Item) {
// t.Wrapped.Set(from, to, value)
//}

func (t *Table) Get(from string, to string) *Item {
return t.Wrapped.Get(from, to).(*Item)
Expand Down
Loading

0 comments on commit 6e98257

Please sign in to comment.