Skip to content

Commit 16af41b

Browse files
test: k8s integration tests (#4423)
Signed-off-by: chenk <[email protected]> Co-authored-by: knqyf263 <[email protected]>
1 parent cab8569 commit 16af41b

File tree

5 files changed

+135
-1
lines changed

5 files changed

+135
-1
lines changed

.github/workflows/test.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ jobs:
8484
- name: Run integration tests
8585
run: mage test:integration
8686

87+
k8s-integration:
88+
name: K8s Integration Test
89+
runs-on: ubuntu-latest
90+
steps:
91+
- name: Check out code into the Go module directory
92+
uses: actions/checkout@v3
93+
94+
- name: Set up Go
95+
uses: actions/setup-go@v4
96+
with:
97+
go-version-file: go.mod
98+
99+
- name: Install tools
100+
uses: aquaproj/[email protected]
101+
with:
102+
aqua_version: v1.25.0
103+
104+
- name: Run k8s integration tests
105+
run: mage test:k8s
106+
87107
module-test:
88108
name: Module Integration Test
89109
runs-on: ubuntu-latest

integration/integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build integration || vm_integration || module_integration
1+
//go:build integration || vm_integration || module_integration || k8s_integration
22

33
package integration
44

integration/k8s_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//go:build k8s_integration
2+
3+
package integration
4+
5+
import (
6+
"encoding/json"
7+
"os"
8+
"path/filepath"
9+
"testing"
10+
11+
"github.com/samber/lo"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
14+
15+
"github.com/aquasecurity/trivy/pkg/k8s/report"
16+
"github.com/aquasecurity/trivy/pkg/types"
17+
)
18+
19+
// Note: the test required k8s (kind) cluster installed.
20+
// "mage test:k8s" will run this test.
21+
22+
func TestK8s(t *testing.T) {
23+
// Set up the output file
24+
outputFile := filepath.Join(t.TempDir(), "output.json")
25+
26+
osArgs := []string{
27+
"k8s",
28+
"cluster",
29+
"--report",
30+
"summary",
31+
"-q",
32+
"--timeout",
33+
"5m0s",
34+
"--format",
35+
"json",
36+
"--components",
37+
"workload",
38+
"--context",
39+
"kind-kind-test",
40+
"--output",
41+
outputFile,
42+
}
43+
44+
// Run Trivy
45+
err := execute(osArgs)
46+
require.NoError(t, err)
47+
48+
var got report.ConsolidatedReport
49+
f, err := os.Open(outputFile)
50+
require.NoError(t, err)
51+
defer f.Close()
52+
53+
err = json.NewDecoder(f).Decode(&got)
54+
require.NoError(t, err)
55+
56+
// Flatten findings
57+
results := lo.FlatMap(got.Findings, func(resource report.Resource, _ int) []types.Result {
58+
return resource.Results
59+
})
60+
61+
// Has vulnerabilities
62+
assert.True(t, lo.SomeBy(results, func(r types.Result) bool {
63+
return len(r.Vulnerabilities) > 0
64+
}))
65+
66+
// Has misconfigurations
67+
assert.True(t, lo.SomeBy(results, func(r types.Result) bool {
68+
return len(r.Misconfigurations) > 0
69+
}))
70+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: nginx-deployment
5+
labels:
6+
app: nginx
7+
spec:
8+
replicas: 1
9+
selector:
10+
matchLabels:
11+
app: nginx
12+
template:
13+
metadata:
14+
labels:
15+
app: nginx
16+
spec:
17+
containers:
18+
- name: nginx
19+
image: nginx:1.14.2
20+
ports:
21+
- containerPort: 80

magefiles/magefile.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ func (Tool) EasyJSON() error {
8484
return sh.Run("go", "install", "github.com/mailru/easyjson/[email protected]")
8585
}
8686

87+
// Kind installs kind cluster
88+
func (Tool) Kind() error {
89+
return sh.RunWithV(ENV, "go", "install", "sigs.k8s.io/[email protected]")
90+
}
91+
8792
// Goyacc installs goyacc
8893
func (Tool) Goyacc() error {
8994
if exists(filepath.Join(GOBIN, "goyacc")) {
@@ -237,6 +242,24 @@ func (t Test) Integration() error {
237242
return sh.RunWithV(ENV, "go", "test", "-v", "-tags=integration", "./integration/...", "./pkg/fanal/test/integration/...")
238243
}
239244

245+
// K8s runs k8s integration tests
246+
func (t Test) K8s() error {
247+
mg.Deps(Tool{}.Kind)
248+
249+
err := sh.RunWithV(ENV, "kind", "create", "cluster", "--name", "kind-test")
250+
if err != nil {
251+
return err
252+
}
253+
defer func() {
254+
_ = sh.RunWithV(ENV, "kind", "delete", "cluster", "--name", "kind-test")
255+
}()
256+
err = sh.RunWithV(ENV, "kubectl", "apply", "-f", "./integration/testdata/fixtures/k8s/test_nginx.yaml")
257+
if err != nil {
258+
return err
259+
}
260+
return sh.RunWithV(ENV, "go", "test", "-v", "-tags=k8s_integration", "./integration/...")
261+
}
262+
240263
// Module runs Wasm integration tests
241264
func (t Test) Module() error {
242265
mg.Deps(t.FixtureContainerImages, t.GenerateExampleModules)

0 commit comments

Comments
 (0)