Skip to content

Commit

Permalink
Ability to merge results of various runs by combining their JSON output.
Browse files Browse the repository at this point in the history
Signed-off-by: Marios Andreopoulos <opensource@andmarios.com>
  • Loading branch information
andmarios committed May 19, 2018
1 parent 91c333d commit 780237a
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 63 deletions.
177 changes: 116 additions & 61 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

// Please remember that when an error occurs in the code (err != nil),
// Coyote should exit with code 255. Exit codes 1-254 are reserved for the tests.

package main

import (
Expand All @@ -38,14 +41,17 @@ import (
//go:generate go run template-generate/include_templates.go
//go:generate go run version-generate/main.go

const DEFAULT_TITLE = "Coyote Tests"

var (
configFilesArray configFilesArrayFlag // This is set via flag.Var, so look into the init() function
defaultTimeout = flag.Duration("timeout", 5*time.Minute, "default timeout for commands (e.g 2h45m, 60s, 300ms)")
title = flag.String("title", "Coyote Tests", "title to use for report")
title = flag.String("title", DEFAULT_TITLE, "title to use for report")
outputFile = flag.String("out", "coyote.html", "filename to save the results under, if exists it will be overwritten")
outputJsonFile = flag.String("json-out", "", "filename to save the results JSON array under, if exitst it will be overwritten, if empty, will not be written")
version = flag.Bool("version", false, "print coyote version")
customTemplate = flag.String("template", "", "override internal golang template with this")
mergeResults = flag.Bool("merge-results", false, "merge all trailing json results into one")
)

var (
Expand Down Expand Up @@ -102,6 +108,18 @@ func main() {
os.Exit(0)
}

if *mergeResults == true {
if len(flag.Args()) == 0 {
logger.Printf("Requested to merge results, but no results were passed.")
os.Exit(255)
}
err := doMergeResults()
if err != nil {
log.Println(err)
os.Exit(255)
}
os.Exit(0)
}
logger.Printf("Starting coyote-tester\n")
// Open yml configuration
var configData []byte
Expand Down Expand Up @@ -325,63 +343,19 @@ func main() {
if err != nil {
logger.Println(err)
} else {
f, err := os.Create(*outputFile)
defer f.Close()
data := ExportData{
resultsGroups,
errors,
passed,
errors + passed,
totalTime,
time.Now().UTC().Format("2006 Jan 02, Mon, 15:04 MST"),
*title,
}
err := writeResults(data)
if err != nil {
logger.Println(err)

} else {
h := &bytes.Buffer{}
data := struct {
Results []ResultGroup
Errors int
Successful int
TotalTests int
TotalTime float64
Date string
Title string
}{
resultsGroups,
errors,
passed,
errors + passed,
totalTime,
time.Now().UTC().Format("2006 Jan 02, Mon, 15:04 MST"),
*title,
}

jsonData, err := json.Marshal(data)
if err != nil {
logger.Println("Coyote error when creating json.")
os.Exit(255)
}

// Write json file if asked
if *outputJsonFile != "" {
fj, err := os.Create(*outputJsonFile)
defer fj.Close()
if err != nil {
logger.Println(err)
} else {
fj.Write(jsonData)
}
}

templateVars := struct {
Data template.JS
Version template.HTML
}{
template.JS(jsonData),
template.HTML("<!-- Generated by Coyote " + vgVersion + ". -->"),
}

// err = t.ExecuteTemplate(h, "template.html", data)
err = t.Execute(h, templateVars)
if err != nil {
logger.Println(err)
}
f.Write(h.Bytes())

log.Println(err)
os.Exit(255)
}

}
Expand All @@ -390,11 +364,11 @@ func main() {
logger.Println("no errors")
} else {
logger.Printf("errors were made: %d\n", errors)
if errors > 255 {
errors = 255
if errors > 254 {
errors = 254
}
// If we had 254 or less errors, the error code indicates the number of errors.
// If we had 255 or more errors, the error code is 255.
// If we had 253 or less errors, the error code indicates the number of errors.
// If we had 254 or more errors, the error code is 254.
os.Exit(errors)
}
}
Expand Down Expand Up @@ -540,3 +514,84 @@ func replaceVars(text string, localVars map[string]string, globalVars map[string
}
return text
}

// doMergeResults is called when we are asked to take old results
// in json format and merge them and it does exactly that
func doMergeResults() error {
inputFiles := flag.Args()
var inputData []ExportData
for _, v := range inputFiles {
fh, err := os.Open(v)
if err != nil {
return err
}
defer fh.Close()
b, _ := ioutil.ReadAll(fh)
var ed ExportData
json.Unmarshal(b, &ed)
inputData = append(inputData, ed)
}
var outData ExportData
for k, v := range inputData {
outData.Results = append(outData.Results, v.Results...)
outData.Errors += v.Errors
outData.Successful += v.Successful
outData.TotalTests += v.TotalTests
outData.TotalTime += v.TotalTime
if k == 0 {
if *title == DEFAULT_TITLE {
outData.Title = v.Title
} else {
outData.Title = *title
}
outData.Date = v.Date
}
}

err := writeResults(outData)
return err
}

// writeResults create the htlm report file and optionally (if asked)
// a json output file
func writeResults(data ExportData) error {
f, err := os.Create(*outputFile)
if err != nil {
return err
}
defer f.Close()

h := &bytes.Buffer{}

jsonData, err := json.Marshal(data)
if err != nil {
return errors.New("Coyote error when creating json.")
}

// Write json file if asked. We don't return error if this fails.
if *outputJsonFile != "" {
fj, err := os.Create(*outputJsonFile)
defer fj.Close()
if err != nil {
logger.Println(err)
} else {
fj.Write(jsonData)
}
}

templateVars := struct {
Data template.JS
Version template.HTML
}{
template.JS(jsonData),
template.HTML("<!-- Generated by Coyote " + vgVersion + ". -->"),
}

// err = t.ExecuteTemplate(h, "template.html", data)
err = t.Execute(h, templateVars)
if err != nil {
return err
}
f.Write(h.Bytes())
return nil
}
10 changes: 10 additions & 0 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,13 @@ type ResultGroup struct {
Total int
TotalTime float64
}

type ExportData struct {
Results []ResultGroup
Errors int
Successful int
TotalTests int
TotalTime float64
Date string
Title string
}
4 changes: 2 additions & 2 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main
// auto generated by github.com/andmarios/go-versiongen

const (
vgVersion = "v1.3+"
vgHash = "4f1a1ea76788abd1232d0fac204a85ef32cce204"
vgVersion = "v1.4+"
vgHash = "91c333dcd687d6b141220a0f6cf17c6886f68389"
vgClean = false
)

0 comments on commit 780237a

Please sign in to comment.