Skip to content

Commit 7d8341d

Browse files
authored
Merge pull request #29 from j-fuentes/test
Package test command
2 parents 5592b8d + 435e111 commit 7d8341d

File tree

7 files changed

+232
-66
lines changed

7 files changed

+232
-66
lines changed

cmd/test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"strings"
8+
"time"
9+
10+
"github.com/jetstack/preflight/pkg/packagesources/local"
11+
"github.com/jetstack/preflight/pkg/packaging"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
var testParams = struct {
16+
verbose bool
17+
timeout time.Duration
18+
}{}
19+
20+
var testCmd = &cobra.Command{
21+
Use: "test",
22+
Short: "Test REGO inside a Preflight package",
23+
Long: `This uses OPA's engine to run all the test suites
24+
inside the package.
25+
26+
It only works with local packages.
27+
`,
28+
Run: func(cmd *cobra.Command, args []string) {
29+
if len(args) == 0 {
30+
// Fail if given no input
31+
log.Fatal("No packages provided for linting")
32+
} else {
33+
loadedPackages := make([]packaging.Package, 0)
34+
35+
for _, packagePath := range args {
36+
pkgs, err := local.LoadLocalPackages(packagePath)
37+
if err != nil {
38+
log.Fatalf("Error loading packages from %s: %v", packagePath, err)
39+
}
40+
for _, p := range pkgs {
41+
loadedPackages = append(loadedPackages, p)
42+
}
43+
}
44+
45+
packagesWithErrors := make(map[string][]int)
46+
47+
ctx, cancel := context.WithCancel(context.Background())
48+
defer cancel()
49+
50+
for _, pkg := range loadedPackages {
51+
log.Printf("Testing package %s", pkg.PolicyManifest().GlobalID())
52+
numFailures, numTotal, err := packaging.TestPackage(ctx, pkg, testParams.verbose, testParams.timeout)
53+
54+
if err != nil {
55+
log.Fatalf("Error testing package %s: %v", pkg.PolicyManifest().GlobalID(), err)
56+
}
57+
58+
if numFailures != 0 {
59+
packagesWithErrors[pkg.PolicyManifest().GlobalID()] = []int{numFailures, numTotal}
60+
}
61+
}
62+
63+
if len(packagesWithErrors) > 0 {
64+
log.Fatalf("Encountered failed tests in these packages: %s", func() (s string) {
65+
pkgs := make([]string, 0)
66+
for k, v := range packagesWithErrors {
67+
pkgs = append(pkgs, fmt.Sprintf("%s (%d failures in %d tests)", k, v[0], v[1]))
68+
}
69+
return strings.Join(pkgs, ", ")
70+
}())
71+
} else {
72+
log.Printf("All packages tests passed :)")
73+
}
74+
}
75+
},
76+
}
77+
78+
func init() {
79+
packageCmd.AddCommand(testCmd)
80+
81+
testCmd.Flags().BoolVarP(&testParams.verbose, "verbose", "v", false, "set verbose reporting mode")
82+
testCmd.Flags().DurationVarP(&testParams.timeout, "timeout", "t", time.Second*10, "set test timeout")
83+
}

go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,23 @@ require (
88
github.com/gomarkdown/markdown v0.0.0-20191104174740-4d42851d4d5a
99
github.com/gookit/color v1.2.0
1010
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6 // indirect
11+
github.com/juju/errors v0.0.0-20190930114154-d42613fe1ab9
12+
github.com/juju/loggo v0.0.0-20190526231331-6e530bcce5d8 // indirect
13+
github.com/juju/testing v0.0.0-20191001232224-ce9dec17d28b // indirect
1114
github.com/mattn/go-colorable v0.1.4 // indirect
12-
github.com/open-policy-agent/opa v0.15.0
15+
github.com/open-policy-agent/opa v0.16.0
1316
github.com/pkg/errors v0.8.1
1417
github.com/sergi/go-diff v1.0.0 // indirect
1518
github.com/spf13/cobra v0.0.5
19+
github.com/spf13/pflag v1.0.3
1620
github.com/spf13/viper v1.5.0
1721
github.com/yudai/gojsondiff v1.0.0
1822
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
1923
github.com/yudai/pp v2.0.1+incompatible // indirect
2024
golang.org/x/arch v0.0.0-20191126211547-368ea8f32fff // indirect
2125
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
2226
google.golang.org/api v0.13.0
27+
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
2328
gopkg.in/yaml.v2 v2.2.5
2429
k8s.io/api v0.0.0-20190620084959-7cf5895f2711
2530
k8s.io/apimachinery v0.0.0-20190612205821-1799e75a0719

0 commit comments

Comments
 (0)