Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Commit

Permalink
test-cases, travis: Add support for TAP reports
Browse files Browse the repository at this point in the history
This commit adds test-cases support for the generation of TAP reports. It also takes
the oppurtunity to rationalize the options for specifying the output format.  There
is now one format option, --format, which accepts a number of different values;
html, tap, colour-text and text.  The default is colour-text.

This change requires a modification to the travis script that executes test-cases,
as the old formatting option used by travis.yml no longer exists.

Signed-off-by: Mark Ryan <mark.d.ryan@intel.com>
  • Loading branch information
Mark Ryan committed Oct 19, 2016
1 parent 74c1e8d commit afad467
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 25 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ script:
- go list ./... | grep -v github.com/01org/ciao/vendor | xargs go list -f '{{.Dir}}' | xargs gofmt -s -l | wc -l | xargs -I % bash -c "test % -eq 0"
- sudo mkdir -p /var/lib/ciao/instances
- sudo chmod 0777 /var/lib/ciao/instances
- test-cases -v -timeout 9 -text -coverprofile /tmp/cover.out -short github.com/01org/ciao/ciao-controller/...
- test-cases -v -timeout 9 -text -coverprofile /tmp/cover.out -append-profile -short github.com/01org/ciao/ciao-launcher github.com/01org/ciao/ciao-scheduler github.com/01org/ciao/payloads github.com/01org/ciao/configuration github.com/01org/ciao/testutil github.com/01org/ciao/ssntp/uuid github.com/01org/ciao/qemu github.com/01org/ciao/openstack/...
- export GOROOT=`go env GOROOT` && sudo -E PATH=$PATH:$GOROOT/bin $GOPATH/bin/test-cases -v -timeout 9 -text -coverprofile /tmp/cover.out -append-profile github.com/01org/ciao/ssntp
- export GOROOT=`go env GOROOT` && export SNNET_ENV=198.51.100.0/24 && sudo -E PATH=$PATH:$GOROOT/bin $GOPATH/bin/test-cases -v -timeout 9 -text -short -tags travis -coverprofile /tmp/cover.out -append-profile github.com/01org/ciao/networking/libsnnet
- test-cases -v -timeout 9 -coverprofile /tmp/cover.out -short github.com/01org/ciao/ciao-controller/...
- test-cases -v -timeout 9 -coverprofile /tmp/cover.out -append-profile -short github.com/01org/ciao/ciao-launcher github.com/01org/ciao/ciao-scheduler github.com/01org/ciao/payloads github.com/01org/ciao/configuration github.com/01org/ciao/testutil github.com/01org/ciao/ssntp/uuid github.com/01org/ciao/qemu github.com/01org/ciao/openstack/...
- export GOROOT=`go env GOROOT` && sudo -E PATH=$PATH:$GOROOT/bin $GOPATH/bin/test-cases -v -timeout 9 -coverprofile /tmp/cover.out -append-profile github.com/01org/ciao/ssntp
- export GOROOT=`go env GOROOT` && export SNNET_ENV=198.51.100.0/24 && sudo -E PATH=$PATH:$GOROOT/bin $GOPATH/bin/test-cases -v -timeout 9 -short -tags travis -coverprofile /tmp/cover.out -append-profile github.com/01org/ciao/networking/libsnnet
# API Documentation is automated by swagger, every PR will verify the documentation can be generated
# verify ciao-controller api documentation can be generated with swagger
- go get github.com/yvasiyarov/swagger
Expand Down
90 changes: 69 additions & 21 deletions test-cases/test-cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,32 +135,55 @@ const htmlTemplate = `
</html>
`

var newLineRegexp = regexp.MustCompile(`(\r\n)|[\n\r]`)

type formatType string

const (
formatText formatType = "text"
formatColourText = "colour-text"
formatHTML = "html"
formatTAP = "tap"
)

func (f *formatType) String() string {
return string(*f)
}
func (f *formatType) Set(val string) error {
v := formatType(val)
if v != formatText && v != formatColourText && v != formatHTML && v != formatTAP {
return fmt.Errorf("invalid format; %s, %s, %s, %s expected",
formatText, formatColourText, formatHTML, formatTAP)
}
*f = formatType(val)
return nil
}

var resultRegexp *regexp.Regexp
var coverageRegexp *regexp.Regexp

var cssPath string
var textOutput bool
var short bool
var race bool
var tags string
var colour bool
var coverProfile string
var appendProfile bool
var format formatType = formatColourText

var timeout int
var verbose bool

func init() {
flag.StringVar(&cssPath, "css", "", "Full path to CSS file")
flag.BoolVar(&textOutput, "text", false, "Output text instead of HTML")
flag.BoolVar(&short, "short", false, "If true -short is passed to go test")
flag.BoolVar(&race, "race", false, "If true -race is passed to go test")
flag.StringVar(&tags, "tags", "", "Build tags to pass to go test")
flag.StringVar(&coverProfile, "coverprofile", "", "Path of coverage profile to be generated")
flag.BoolVar(&appendProfile, "append-profile", false, "Append generated coverage profiles an existing file")
flag.BoolVar(&colour, "colour", true, "If true failed tests are coloured red in text mode")
flag.BoolVar(&verbose, "v", false, "Output package names under test if true")
flag.IntVar(&timeout, "timeout", 0, "Time in minutes after which a package's unit tests should time out. 0 = no timeout")
flag.Var(&format, "format", fmt.Sprintf("Specify output format. Can be '%s', '%s', '%s', or '%s'",
formatText, formatColourText, formatHTML, formatTAP))

flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s [flags] [packages]\n", os.Args[0])
Expand Down Expand Up @@ -672,6 +695,33 @@ func runTests(tests []*PackageTests) (int, *bytes.Buffer, error) {
return exitCode, &errorOutput, nil
}

func generateTAPOutput(tests []*PackageTests) {
i := 0
prefix := findCommonPrefix(tests)
for _, p := range tests {
pkgName := p.Name[len(prefix):]
fmt.Printf("# Tests for %s\n", pkgName)
for _, t := range p.Tests {
if t.Result == "PASS" {
fmt.Printf("ok ")
} else {
fmt.Printf("not ok ")
}
testName := strings.TrimSpace(t.Summary)
if testName == "" {
testName = t.Name
} else {
testName = newLineRegexp.ReplaceAllString(testName, " ")
}
fmt.Printf("%d - %s\n", i+1, testName)
i++
}
}
if i > 0 {
fmt.Printf("1..%d\n", i)
}
}

func main() {

flag.Parse()
Expand All @@ -687,26 +737,24 @@ func main() {
log.Fatal(err)
}

if textOutput {
if colour {
generateColourTextReport(tests, exitCode)
} else {
generateTextReport(tests, exitCode)
switch format {
case formatText:
generateTextReport(tests, exitCode)
if exitCode != 0 {
dumpErrorOutput(errorOutput)
}
} else {
err = generateHTMLReport(tests)
}

if exitCode != 0 {
if colour {
case formatColourText:
generateColourTextReport(tests, exitCode)
if exitCode != 0 {
dumpColourErrorOutput(errorOutput)
} else {
dumpErrorOutput(errorOutput)
}
}

if err != nil {
log.Fatalf("Unable to generate report: %s\n", err)
case formatTAP:
generateTAPOutput(tests)
case formatHTML:
err := generateHTMLReport(tests)
if err != nil {
log.Fatalf("Unable to generate report: %s\n", err)
}
}

os.Exit(exitCode)
Expand Down

0 comments on commit afad467

Please sign in to comment.