-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathwarden_test.go
126 lines (102 loc) · 4.14 KB
/
warden_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//go:build integration
package tests
import (
"fmt"
"testing"
"github.com/kyma-project/warden/pkg"
th "github.com/kyma-project/warden/tests/helpers"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
)
const (
UntrustedImageName = "nginx:latest"
TrustedImageName = "europe-docker.pkg.dev/kyma-project/prod/function-controller:v20230428-1ea34f8e"
)
//TODO: as unit tests:
//pending
//different image names
//mock image validator?
//skip some images from list
//TODO: as integration test?
//notary svc is not available while validating untrusted image, so the controller mark it later
//TODO: update unscanned namespace to scanned
func Test_SimplePodWithImage_ShouldBeCreated(t *testing.T) {
tc := th.NewTestContext(t, "warden-simple").Initialize()
defer tc.Destroy()
container := corev1.Container{Name: "test-container", Image: "nginx"}
pod := tc.Pod().WithContainer(container).Build()
err := tc.Create(pod)
require.NoError(t, err)
defer tc.Delete(pod)
}
func Test_PodInsideVerifiedNamespaceWithUntrustedImage_ShouldBeRejected(t *testing.T) {
tc := th.NewTestContext(t, "warden-verified-namespace-untrusted-image").
ValidationEnabled(true).
Initialize()
defer tc.Destroy()
container := corev1.Container{Name: "test-container", Image: UntrustedImageName}
pod := tc.Pod().WithContainer(container).Build()
err := tc.Create(pod)
require.Error(t, err)
require.ErrorContains(t, err, fmt.Sprintf("Pod images %s validation failed", UntrustedImageName))
}
func Test_PodInsideVerifiedNamespaceWithUntrustedImages_ShouldBeRejected(t *testing.T) {
tc := th.NewTestContext(t, "warden-verified-namespace-untrusted-images").
ValidationEnabled(true).
Initialize()
defer tc.Destroy()
container1 := corev1.Container{Name: "test-container1", Image: UntrustedImageName}
container2 := corev1.Container{Name: "test-container2", Image: "nginx:1.24.0-perl"}
pod := tc.Pod().WithContainer(container1).WithContainer(container2).Build()
err := tc.Create(pod)
require.Error(t, err)
require.ErrorContains(t, err, UntrustedImageName)
require.ErrorContains(t, err, "nginx:1.24.0-perl")
}
func Test_PodInsideVerifiedNamespaceWithTrustedImage_ShouldBeCreatedWithValidationLabel(t *testing.T) {
tc := th.NewTestContext(t, "warden-verified-namespace-trusted-image").
ValidationEnabled(true).
Initialize()
defer tc.Destroy()
container := corev1.Container{Name: "test-container", Image: TrustedImageName}
pod := tc.Pod().WithContainer(container).Build()
err := tc.Create(pod)
require.NoError(t, err)
defer tc.Delete(pod)
var existingPod corev1.Pod
tc.GetPodWhenReady(pod, &existingPod)
require.Contains(t, existingPod.ObjectMeta.Labels, pkg.PodValidationLabel)
require.Equal(t, pkg.ValidationStatusSuccess, existingPod.ObjectMeta.Labels[pkg.PodValidationLabel])
}
func Test_PodInsideNotVerifiedNamespaceWithTrustedImage_ShouldBeCreatedWithoutValidationLabel(t *testing.T) {
tc := th.NewTestContext(t, "warden-not-verified-namespace").
ValidationEnabled(false).
Initialize()
defer tc.Destroy()
container := corev1.Container{Name: "test-container", Image: TrustedImageName}
pod := tc.Pod().WithContainer(container).Build()
err := tc.Create(pod)
require.NoError(t, err)
defer tc.Delete(pod)
var existingPod corev1.Pod
tc.GetPodWhenReady(pod, &existingPod)
require.NotContains(t, existingPod.ObjectMeta.Labels, pkg.PodValidationLabel)
}
func Test_UpdateVerifiedPodWithUntrustedImage_ShouldBeRejected(t *testing.T) {
tc := th.NewTestContext(t, "warden").ValidationEnabled(true).Initialize()
defer tc.Destroy()
container := corev1.Container{Name: "test-container", Image: TrustedImageName}
pod := tc.Pod().WithContainer(container).Build()
err := tc.Create(pod)
require.NoError(t, err)
defer tc.Delete(pod)
var existingPod corev1.Pod
tc.GetPodWhenReady(pod, &existingPod)
require.Contains(t, existingPod.ObjectMeta.Labels, pkg.PodValidationLabel)
require.Equal(t, pkg.ValidationStatusSuccess, existingPod.ObjectMeta.Labels[pkg.PodValidationLabel])
pod = &existingPod
pod.Spec.Containers[0].Image = UntrustedImageName
err = tc.Update(pod)
require.Error(t, err)
require.ErrorContains(t, err, fmt.Sprintf("Pod images %s validation failed", UntrustedImageName))
}