Skip to content

Commit a7b7f87

Browse files
authored
Merge pull request #104 from knabben/clean-category
Adding reporter command line
2 parents 8554e5c + 926dbb3 commit a7b7f87

File tree

17 files changed

+452
-201
lines changed

17 files changed

+452
-201
lines changed
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ jobs:
1818
with:
1919
go-version: 1.21
2020
- uses: actions/checkout@v3
21-
- name: Run unit-tests
21+
- name: Run unit tests
2222
run: |
2323
go test -v ./...
24+
- name: Run bdd tests
25+
run: |
26+
go install github.com/onsi/ginkgo/v2/ginkgo
27+
go get github.com/onsi/gomega/...
28+
make test
2429
- name: Build the binary
2530
run: |
2631
make build

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ build: ## Build the binary using local golang
6161
./hack/build_k8s_test_binary.sh ${KUBERNETES_HASH}
6262
go build -o ./op-readiness .
6363

64+
.PHONY: test
65+
test: ## Run the Sonobuoy plugin
66+
pushd tests; ginkgo run -v; popd
67+
68+
6469
## --------------------------------------
6570
## Container
6671
## --------------------------------------

cmd/reporter.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Copyright 2023 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+
package cmd
18+
19+
import (
20+
"fmt"
21+
"log"
22+
"strconv"
23+
"strings"
24+
25+
"github.com/spf13/cobra"
26+
"sigs.k8s.io/windows-operational-readiness/pkg/report"
27+
)
28+
29+
var (
30+
directory string
31+
failed int
32+
passed int
33+
)
34+
35+
var reporterCmd = &cobra.Command{
36+
Use: "reporter",
37+
Short: "Parse XML Junit results and print a columnar output",
38+
Long: `Parse XML Junit results and print a columnar output`,
39+
Run: func(cmd *cobra.Command, args []string) {
40+
suites, err := report.ParseXMLFiles(directory)
41+
if err != nil {
42+
log.Fatal(err)
43+
}
44+
for _, n := range suites {
45+
presentTestSuite(n)
46+
}
47+
fmt.Printf("Passed tests: %d\n", passed)
48+
fmt.Printf("Failed tests: %d\n", failed)
49+
},
50+
}
51+
52+
func init() {
53+
reporterCmd.Flags().StringVar(&directory, "dir", "", "directory to search in for xml files")
54+
}
55+
56+
func presentTestSuite(r *report.TestSuites) {
57+
for _, t := range r.Suites {
58+
if len(t.TestCases) == 0 {
59+
ftime, err := strconv.ParseFloat(t.Time, 32)
60+
if err != nil {
61+
continue
62+
}
63+
fmt.Printf("[FAILED] | %s - %s | (%2.2fs) | %s\n", t.Index, r.Category, ftime, r.Name)
64+
failed++
65+
}
66+
67+
for _, c := range t.TestCases {
68+
ftime, err := strconv.ParseFloat(c.Time, 32)
69+
if err != nil {
70+
continue
71+
}
72+
fmt.Printf("[%s] | %s - %s | (%2.2fs) | %s | %s\n", strings.ToUpper(string(c.Status)), t.Index, r.Category, ftime, r.Name, c.Name)
73+
if c.Status == report.StatusPassed {
74+
passed++
75+
}
76+
if c.Status == report.StatusFailed {
77+
failed++
78+
}
79+
}
80+
}
81+
}

cmd/root.go

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,30 @@ package cmd
1919
import (
2020
"fmt"
2121
"os"
22+
"path"
2223

2324
"github.com/spf13/cobra"
2425
"go.uber.org/zap"
2526
"go.uber.org/zap/zapcore"
2627
"k8s.io/client-go/tools/clientcmd"
2728
"sigs.k8s.io/windows-operational-readiness/pkg/flags"
29+
"sigs.k8s.io/windows-operational-readiness/pkg/report"
2830
"sigs.k8s.io/windows-operational-readiness/pkg/testcases"
2931
)
3032

33+
func init() {
34+
rootCmd.PersistentFlags().StringVar(&testDirectory, "test-directory", "", "Path to YAML root directory containing the tests.")
35+
rootCmd.PersistentFlags().StringVar(&E2EBinary, "e2e-binary", "./e2e.test", "The E2E Ginkgo default binary used to run the tests.")
36+
rootCmd.PersistentFlags().StringVar(&provider, "provider", "local", "The name of the Kubernetes provider (gce, gke, aws, local, skeleton, etc.)")
37+
rootCmd.PersistentFlags().StringVar(&kubeConfig, clientcmd.RecommendedConfigPathFlag, os.Getenv(clientcmd.RecommendedConfigPathEnvVar), "Path to kubeconfig containing embedded authinfo.")
38+
rootCmd.PersistentFlags().StringVar(&reportDir, "report-dir", getEnvOrDefault("ARTIFACTS", ""), "Report dump directory, uses artifact for CI integration when set.")
39+
rootCmd.PersistentFlags().BoolVar(&dryRun, "dry-run", false, "Do not run actual tests, used for sanity check.")
40+
rootCmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "Enable Ginkgo verbosity.")
41+
rootCmd.PersistentFlags().Var(&categories, "category", "Append category of tests you want to run, default empty will run all tests.")
42+
43+
rootCmd.AddCommand(reporterCmd)
44+
}
45+
3146
// NewLoggerConfig return the configuration object for the logger.
3247
func NewLoggerConfig(options ...zap.Option) *zap.Logger {
3348
core := zapcore.NewCore(zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
@@ -64,7 +79,6 @@ var (
6479
os.Exit(1)
6580
}
6681
testCtx := testcases.NewTestContext(E2EBinary, kubeConfig, provider, specs, dryRun, verbose, reportDir, categories)
67-
6882
targetedSpecs := make([]testcases.Specification, 0)
6983
for _, s := range specs {
7084
if !testCtx.CategoryEnabled(s.Category) {
@@ -74,18 +88,31 @@ var (
7488
}
7589
}
7690

77-
for sIdx, s := range targetedSpecs {
78-
zap.L().Info(fmt.Sprintf("[OpReadinessTests] %d / %d Specifications - Running %d Test(s) for Ops Readiness specification: %v", sIdx+1, len(targetedSpecs), len(s.TestCases), s.Category))
91+
for specIdx, s := range targetedSpecs {
92+
zap.L().Info(fmt.Sprintf("[OpReadinessTests] %d / %d Specifications - Running %d Test(s) for Ops Readiness specification: %v", specIdx+1, len(targetedSpecs), len(s.TestCases), s.Category))
93+
7994
if len(s.TestCases) == 0 {
8095
zap.L().Info(fmt.Sprintf("[%s] No Operational Readiness tests to run", string(s.Category)))
81-
} else {
82-
for tIdx, t := range s.TestCases {
83-
logPrefix := fmt.Sprintf("[%s] %v / %v Tests - ", s.Category, tIdx+1, len(s.TestCases))
84-
zap.L().Info(fmt.Sprintf("%sRunning Operational Readiness Test: %v", logPrefix, t.Description))
85-
if err = t.RunTest(testCtx, tIdx+1); err != nil {
86-
zap.L().Error(fmt.Sprintf("%sFailed Operational Readiness Test: %v, error is %v", logPrefix, t.Description, zap.Error(err)))
87-
} else {
88-
zap.L().Info(fmt.Sprintf("%sPassed Operational Readiness Test: %v", logPrefix, t.Description))
96+
continue
97+
}
98+
99+
for testIdx, t := range s.TestCases {
100+
prefix := fmt.Sprintf("%d%d", specIdx+1, testIdx+1)
101+
logPrefix := fmt.Sprintf("[%s] %v / %v Tests - ", s.Category, testIdx+1, len(s.TestCases))
102+
zap.L().Info(fmt.Sprintf("%sRunning Operational Readiness Test: %v", logPrefix, t.Description))
103+
if err = t.RunTest(testCtx, prefix); err != nil {
104+
zap.L().Error(fmt.Sprintf("%sFailed Operational Readiness Test: %v, error is %v", logPrefix, t.Description, zap.Error(err)))
105+
} else {
106+
zap.L().Info(fmt.Sprintf("%sPassed Operational Readiness Test: %v", logPrefix, t.Description))
107+
}
108+
109+
// Specs already ran, cleaning up the XML Junit report
110+
if testCtx.ReportDir != "" && !testCtx.DryRun {
111+
fileName := "junit_" + prefix + "01.xml"
112+
junitReport := path.Join(testCtx.ReportDir, fileName)
113+
zap.L().Info("Cleaning XML files", zap.String("file", fileName), zap.String("path", junitReport))
114+
if err := report.CleanupJUnitXML(junitReport, string(s.Category), t.Description, testIdx+1); err != nil {
115+
zap.L().Error(err.Error())
89116
}
90117
}
91118
}
@@ -105,14 +132,3 @@ func getEnvOrDefault(key, defaultString string) string {
105132
}
106133
return defaultString
107134
}
108-
109-
func init() {
110-
rootCmd.PersistentFlags().StringVar(&testDirectory, "test-directory", "", "Path to YAML root directory containing the tests.")
111-
rootCmd.PersistentFlags().StringVar(&E2EBinary, "e2e-binary", "./e2e.test", "The E2E Ginkgo default binary used to run the tests.")
112-
rootCmd.PersistentFlags().StringVar(&provider, "provider", "local", "The name of the Kubernetes provider (gce, gke, aws, local, skeleton, etc.)")
113-
rootCmd.PersistentFlags().StringVar(&kubeConfig, clientcmd.RecommendedConfigPathFlag, os.Getenv(clientcmd.RecommendedConfigPathEnvVar), "Path to kubeconfig containing embedded authinfo.")
114-
rootCmd.PersistentFlags().StringVar(&reportDir, "report-dir", getEnvOrDefault("ARTIFACTS", ""), "Report dump directory, uses artifact for CI integration when set.")
115-
rootCmd.PersistentFlags().BoolVar(&dryRun, "dry-run", false, "Do not run actual tests, used for sanity check.")
116-
rootCmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "Enable Ginkgo verbosity.")
117-
rootCmd.PersistentFlags().Var(&categories, "category", "Append category of tests you want to run, default empty will run all tests.")
118-
}

go.mod

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@ go 1.21
44

55
require (
66
github.com/go-playground/validator/v10 v10.10.0
7+
github.com/onsi/ginkgo/v2 v2.13.2
8+
github.com/onsi/gomega v1.29.0
79
github.com/spf13/cobra v1.4.0
810
go.uber.org/zap v1.20.0
9-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
11+
gopkg.in/yaml.v3 v3.0.1
1012
k8s.io/client-go v0.23.3
1113
)
1214

1315
require (
1416
github.com/davecgh/go-spew v1.1.1 // indirect
15-
github.com/go-logr/logr v1.2.0 // indirect
17+
github.com/go-logr/logr v1.3.0 // indirect
1618
github.com/go-playground/locales v0.14.0 // indirect
1719
github.com/go-playground/universal-translator v0.18.0 // indirect
20+
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
1821
github.com/gogo/protobuf v1.3.2 // indirect
19-
github.com/golang/protobuf v1.5.2 // indirect
20-
github.com/google/go-cmp v0.5.6 // indirect
22+
github.com/golang/protobuf v1.5.3 // indirect
23+
github.com/google/go-cmp v0.6.0 // indirect
2124
github.com/google/gofuzz v1.1.0 // indirect
25+
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
2226
github.com/imdario/mergo v0.3.5 // indirect
2327
github.com/inconshreveable/mousetrap v1.0.0 // indirect
2428
github.com/json-iterator/go v1.1.12 // indirect
@@ -28,15 +32,16 @@ require (
2832
github.com/spf13/pflag v1.0.5 // indirect
2933
go.uber.org/atomic v1.7.0 // indirect
3034
go.uber.org/multierr v1.6.0 // indirect
31-
golang.org/x/crypto v0.1.0 // indirect
32-
golang.org/x/net v0.5.0 // indirect
35+
golang.org/x/crypto v0.14.0 // indirect
36+
golang.org/x/net v0.17.0 // indirect
3337
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
34-
golang.org/x/sys v0.4.0 // indirect
35-
golang.org/x/term v0.4.0 // indirect
36-
golang.org/x/text v0.6.0 // indirect
38+
golang.org/x/sys v0.14.0 // indirect
39+
golang.org/x/term v0.13.0 // indirect
40+
golang.org/x/text v0.13.0 // indirect
3741
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
42+
golang.org/x/tools v0.14.0 // indirect
3843
google.golang.org/appengine v1.6.7 // indirect
39-
google.golang.org/protobuf v1.27.1 // indirect
44+
google.golang.org/protobuf v1.28.0 // indirect
4045
gopkg.in/inf.v0 v0.9.1 // indirect
4146
gopkg.in/yaml.v2 v2.4.0 // indirect
4247
k8s.io/apimachinery v0.23.3 // indirect

0 commit comments

Comments
 (0)