@@ -27,56 +27,76 @@ import (
27
27
)
28
28
29
29
var _ = Describe ("Pod Webhook" , func () {
30
+ var (
31
+ pod * corev1.Pod
32
+ podScope rules.Scope
33
+ agentName rules.AgentName = "default"
34
+ )
30
35
31
- Context ("When label is set" , func () {
32
- It ("Should inject a service account" , func () {
33
- By ("By creating a pod" )
34
-
35
- expectedScope := rules.Scope {
36
- Project : "my-project" ,
37
- Environment : "my-environment" ,
38
- Tenant : "my-tenant" ,
39
- Step : "my-step" ,
40
- }
41
- expectedAgent := rules .AgentName ("default" )
42
- mockEngine .On ("GetServiceAccountForScope" , expectedScope , expectedAgent ).Return (rules .ServiceAccountName ("overridden" ), nil )
43
-
44
- pod := & corev1.Pod {
45
- TypeMeta : metav1.TypeMeta {
46
- Kind : "Pod" ,
47
- APIVersion : "v1" ,
36
+ BeforeEach (func () {
37
+ pod = & corev1.Pod {
38
+ TypeMeta : metav1.TypeMeta {
39
+ Kind : "Pod" ,
40
+ APIVersion : "v1" ,
41
+ },
42
+ ObjectMeta : metav1.ObjectMeta {
43
+ Name : "test-pod" ,
44
+ Namespace : "default" ,
45
+ Labels : map [string ]string {
46
+ EnabledLabelKey : "true" ,
48
47
},
49
- ObjectMeta : metav1.ObjectMeta {
50
- Name : "test-pod" ,
51
- Namespace : "default" ,
52
- Labels : map [string ]string {
53
- EnabledLabelKey : "true" ,
54
- },
55
- Annotations : map [string ]string {
56
- ProjectAnnotationKey : "my-project" ,
57
- EnvironmentAnnotationKey : "my-environment" ,
58
- TenantAnnotationKey : "my-tenant" ,
59
- StepAnnotationKey : "my-step" ,
60
- },
48
+ Annotations : map [string ]string {
49
+ ProjectAnnotationKey : "my-project" ,
50
+ EnvironmentAnnotationKey : "my-environment" ,
51
+ TenantAnnotationKey : "my-tenant" ,
52
+ StepAnnotationKey : "my-step" ,
61
53
},
62
- Spec : corev1. PodSpec {
63
- Containers : [] corev1.Container {
64
- {
65
- Name : "nginx" ,
66
- Image : "nginx:1.7.9 " ,
67
- } ,
54
+ },
55
+ Spec : corev1.PodSpec {
56
+ Containers : []corev1. Container {
57
+ {
58
+ Name : "nginx" ,
59
+ Image : "nginx:1.7.9" ,
68
60
},
69
- ServiceAccountName : "want-to-override" ,
70
61
},
71
- Status : corev1.PodStatus {},
72
- }
62
+ ServiceAccountName : "not-overridden" ,
63
+ },
64
+ Status : corev1.PodStatus {},
65
+ }
66
+ podScope = rules.Scope {
67
+ Project : "my-project" ,
68
+ Environment : "my-environment" ,
69
+ Tenant : "my-tenant" ,
70
+ Step : "my-step" ,
71
+ }
72
+ })
73
+
74
+ AfterEach (func () {
75
+ Expect (k8sClient .Delete (ctx , pod )).To (Succeed ())
76
+ })
77
+
78
+ Context ("When label is set" , func () {
79
+ It ("Should inject a service account" , func () {
80
+ By ("By creating a pod" )
81
+
82
+ mockCall := mockEngine .On ("GetServiceAccountForScope" , podScope , agentName ).Return (rules .ServiceAccountName ("overridden" ), nil )
73
83
Expect (k8sClient .Create (ctx , pod )).To (Succeed ())
74
84
75
85
var actualPod corev1.Pod
76
86
Expect (k8sClient .Get (ctx , client .ObjectKeyFromObject (pod ), & actualPod )).To (Succeed ())
77
87
Expect (actualPod .Spec .ServiceAccountName ).To (Equal ("overridden" ))
78
-
79
88
mockEngine .AssertExpectations (GinkgoT ())
89
+ mockCall .Unset ()
90
+ })
91
+ It ("Should not inject a service account" , func () {
92
+ By ("When no matching scope exists" )
93
+ mockCall := mockEngine .On ("GetServiceAccountForScope" , podScope , agentName ).Return (rules .ServiceAccountName ("" ), nil )
94
+ Expect (k8sClient .Create (ctx , pod )).To (Succeed ())
95
+ var actualPod corev1.Pod
96
+ Expect (k8sClient .Get (ctx , client .ObjectKeyFromObject (pod ), & actualPod )).To (Succeed ())
97
+ Expect (actualPod .Spec .ServiceAccountName ).To (Equal ("not-overridden" ))
98
+ mockEngine .AssertExpectations (GinkgoT ())
99
+ mockCall .Unset ()
80
100
})
81
101
})
82
102
})
0 commit comments