| 
 | 1 | +/*  | 
 | 2 | +Copyright 2019 The Kubernetes Authors.  | 
 | 3 | +
  | 
 | 4 | +Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 5 | +you may not use this file except in compliance with the License.  | 
 | 6 | +You may obtain a copy of the License at  | 
 | 7 | +
  | 
 | 8 | +    http://www.apache.org/licenses/LICENSE-2.0  | 
 | 9 | +
  | 
 | 10 | +Unless required by applicable law or agreed to in writing, software  | 
 | 11 | +distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 13 | +See the License for the specific language governing permissions and  | 
 | 14 | +limitations under the License.  | 
 | 15 | +*/  | 
 | 16 | + | 
 | 17 | +/*  | 
 | 18 | + * This command filters a JUnit file such that only tests with a name  | 
 | 19 | + * matching a regular expression are passed through. By concatenating  | 
 | 20 | + * multiple input files it is possible to merge them into a single file.  | 
 | 21 | + */  | 
 | 22 | +package main  | 
 | 23 | + | 
 | 24 | +import (  | 
 | 25 | +	"encoding/xml"  | 
 | 26 | +	"flag"  | 
 | 27 | +	"io/ioutil"  | 
 | 28 | +	"os"  | 
 | 29 | +	"regexp"  | 
 | 30 | +)  | 
 | 31 | + | 
 | 32 | +var (  | 
 | 33 | +	output = flag.String("o", "-", "junit file to write, - for stdout")  | 
 | 34 | +	tests  = flag.String("t", "", "regular expression matching the test names that are to be included in the output")  | 
 | 35 | +)  | 
 | 36 | + | 
 | 37 | +/*  | 
 | 38 | + * TestSuite represents a JUnit file. Due to how encoding/xml works, we have  | 
 | 39 | + * represent all fields that we want to be passed through. It's therefore  | 
 | 40 | + * not a complete solution, but good enough for Ginkgo + Spyglass.  | 
 | 41 | + */  | 
 | 42 | +type TestSuite struct {  | 
 | 43 | +	XMLName   string     `xml:"testsuite"`  | 
 | 44 | +	TestCases []TestCase `xml:"testcase"`  | 
 | 45 | +}  | 
 | 46 | + | 
 | 47 | +type TestCase struct {  | 
 | 48 | +	Name      string     `xml:"name,attr"`  | 
 | 49 | +	Time      string     `xml:"time,attr"`  | 
 | 50 | +	SystemOut string     `xml:"system-out,omitempty"`  | 
 | 51 | +	Failure   string     `xml:"failure,omitempty"`  | 
 | 52 | +	Skipped   SkipReason `xml:"skipped,omitempty"`  | 
 | 53 | +}  | 
 | 54 | + | 
 | 55 | +// SkipReason deals with the special <skipped></skipped>:  | 
 | 56 | +// if present, we must re-encode it, even if empty.  | 
 | 57 | +type SkipReason string  | 
 | 58 | + | 
 | 59 | +func (s *SkipReason) UnmarshalText(text []byte) error {  | 
 | 60 | +	*s = SkipReason(text)  | 
 | 61 | +	if *s == "" {  | 
 | 62 | +		*s = " "  | 
 | 63 | +	}  | 
 | 64 | +	return nil  | 
 | 65 | +}  | 
 | 66 | + | 
 | 67 | +func (s SkipReason) MarshalText() ([]byte, error) {  | 
 | 68 | +	if s == " " {  | 
 | 69 | +		return []byte{}, nil  | 
 | 70 | +	}  | 
 | 71 | +	return []byte(s), nil  | 
 | 72 | +}  | 
 | 73 | + | 
 | 74 | +func main() {  | 
 | 75 | +	var junit TestSuite  | 
 | 76 | +	var data []byte  | 
 | 77 | + | 
 | 78 | +	flag.Parse()  | 
 | 79 | + | 
 | 80 | +	re := regexp.MustCompile(*tests)  | 
 | 81 | + | 
 | 82 | +	// Read all input files.  | 
 | 83 | +	for _, input := range flag.Args() {  | 
 | 84 | +		if input == "-" {  | 
 | 85 | +			if _, err := os.Stdin.Read(data); err != nil {  | 
 | 86 | +				panic(err)  | 
 | 87 | +			}  | 
 | 88 | +		} else {  | 
 | 89 | +			var err error  | 
 | 90 | +			data, err = ioutil.ReadFile(input)  | 
 | 91 | +			if err != nil {  | 
 | 92 | +				panic(err)  | 
 | 93 | +			}  | 
 | 94 | +		}  | 
 | 95 | +		if err := xml.Unmarshal(data, &junit); err != nil {  | 
 | 96 | +			panic(err)  | 
 | 97 | +		}  | 
 | 98 | +	}  | 
 | 99 | + | 
 | 100 | +	// Keep only matching testcases. Testcases skipped in all test runs are only stored once.  | 
 | 101 | +	filtered := map[string]TestCase{}  | 
 | 102 | +	for _, testcase := range junit.TestCases {  | 
 | 103 | +		if !re.MatchString(testcase.Name) {  | 
 | 104 | +			continue  | 
 | 105 | +		}  | 
 | 106 | +		entry, ok := filtered[testcase.Name]  | 
 | 107 | +		if !ok || // not present yet  | 
 | 108 | +			entry.Skipped != "" && testcase.Skipped == "" { // replaced skipped test with real test run  | 
 | 109 | +			filtered[testcase.Name] = testcase  | 
 | 110 | +		}  | 
 | 111 | +	}  | 
 | 112 | +	junit.TestCases = nil  | 
 | 113 | +	for _, testcase := range filtered {  | 
 | 114 | +		junit.TestCases = append(junit.TestCases, testcase)  | 
 | 115 | +	}  | 
 | 116 | + | 
 | 117 | +	// Re-encode.  | 
 | 118 | +	data, err := xml.MarshalIndent(junit, "", "  ")  | 
 | 119 | +	if err != nil {  | 
 | 120 | +		panic(err)  | 
 | 121 | +	}  | 
 | 122 | + | 
 | 123 | +	// Write to output.  | 
 | 124 | +	if *output == "-" {  | 
 | 125 | +		if _, err := os.Stdout.Write(data); err != nil {  | 
 | 126 | +			panic(err)  | 
 | 127 | +		}  | 
 | 128 | +	} else {  | 
 | 129 | +		if err := ioutil.WriteFile(*output, data, 0644); err != nil {  | 
 | 130 | +			panic(err)  | 
 | 131 | +		}  | 
 | 132 | +	}  | 
 | 133 | +}  | 
0 commit comments