@@ -16,11 +16,12 @@ import (
1616 "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
1717)
1818
19- func setupAdmission (t * testing.T ) (* deploymentAdmission , * mocks.Mocks ) {
19+ func setupAdmission (t * testing.T , deploymentName , namespace string ) (* deploymentAdmission , * mocks.Mocks ) {
2020 pm , mm := mocks .BuildMocks (t )
2121
2222 pc := & kubernetes.PluginConfig {}
23- pc .Deployment = "test-deployment"
23+ pc .Deployment = deploymentName
24+ pc .Namespace = namespace
2425
2526 pcd , _ := json .Marshal (pc )
2627
@@ -57,6 +58,7 @@ func createAdmissionRequest(withVersionLabels bool) admission.Request {
5758 ar .AdmissionRequest .Name = "test-deployment"
5859
5960 dep := & appsv1.Deployment {}
61+ dep .Namespace = "default"
6062 dep .Name = "test-deployment"
6163 dep .Labels = map [string ]string {"app" : "test" }
6264
@@ -74,7 +76,16 @@ func createAdmissionRequest(withVersionLabels bool) admission.Request {
7476
7577func TestIgnoresDeploymentModifiedByControllerWhenActive (t * testing.T ) {
7678 ar := createAdmissionRequest (true )
77- d , mm := setupAdmission (t )
79+ d , mm := setupAdmission (t , "test-deployment" , "default" )
80+
81+ resp := d .Handle (context .TODO (), ar )
82+ require .True (t , resp .Allowed )
83+ mm .StateMachineMock .AssertNotCalled (t , "Deploy" )
84+ }
85+
86+ func TestDoesNothingForNewDeploymentWithNamespaceMismatch (t * testing.T ) {
87+ ar := createAdmissionRequest (false )
88+ d , mm := setupAdmission (t , "test-deployment" , "mine" )
7889
7990 resp := d .Handle (context .TODO (), ar )
8091 require .True (t , resp .Allowed )
@@ -83,7 +94,28 @@ func TestIgnoresDeploymentModifiedByControllerWhenActive(t *testing.T) {
8394
8495func TestCallsDeployForNewDeploymentWhenIdle (t * testing.T ) {
8596 ar := createAdmissionRequest (false )
86- d , mm := setupAdmission (t )
97+ d , mm := setupAdmission (t , "test-deployment" , "default" )
98+
99+ resp := d .Handle (context .TODO (), ar )
100+ require .True (t , resp .Allowed )
101+ mm .StateMachineMock .AssertCalled (t , "Deploy" )
102+ }
103+
104+ func TestAddsRegExpWordBoundaryAndFailsMatchWhenNotPresent (t * testing.T ) {
105+ ar := createAdmissionRequest (false )
106+
107+ // a regexp without a word boundary would match, check we add
108+ // the word boundary when not present
109+ d , mm := setupAdmission (t , "test-" , "default" )
110+
111+ resp := d .Handle (context .TODO (), ar )
112+ require .True (t , resp .Allowed )
113+ mm .StateMachineMock .AssertNotCalled (t , "Deploy" )
114+ }
115+
116+ func TestCallsDeployForNewDeploymentWhenIdleAndUsingRegularExpressions (t * testing.T ) {
117+ ar := createAdmissionRequest (false )
118+ d , mm := setupAdmission (t , "test-(.*)" , "default" )
87119
88120 resp := d .Handle (context .TODO (), ar )
89121 require .True (t , resp .Allowed )
@@ -92,7 +124,7 @@ func TestCallsDeployForNewDeploymentWhenIdle(t *testing.T) {
92124
93125func TestCallsDeployForNewDeploymentWhenFailed (t * testing.T ) {
94126 ar := createAdmissionRequest (false )
95- d , mm := setupAdmission (t )
127+ d , mm := setupAdmission (t , "test-deployment" , "default" )
96128
97129 testutils .ClearMockCall (& mm .StateMachineMock .Mock , "CurrentState" )
98130 mm .StateMachineMock .On ("CurrentState" ).Return (interfaces .StateFail )
@@ -104,7 +136,7 @@ func TestCallsDeployForNewDeploymentWhenFailed(t *testing.T) {
104136
105137func TestReturnsAllowedWhenReleaseNotFound (t * testing.T ) {
106138 ar := createAdmissionRequest (false )
107- d , mm := setupAdmission (t )
139+ d , mm := setupAdmission (t , "test-deployment" , "default" )
108140
109141 testutils .ClearMockCall (& mm .StoreMock .Mock , "ListReleases" )
110142 mm .StoreMock .On ("ListReleases" , & interfaces.ListOptions {"kubernetes" }).Return (
@@ -119,7 +151,7 @@ func TestReturnsAllowedWhenReleaseNotFound(t *testing.T) {
119151
120152func TestReturnsDeniedWhenReleaseActive (t * testing.T ) {
121153 ar := createAdmissionRequest (false )
122- d , mm := setupAdmission (t )
154+ d , mm := setupAdmission (t , "test-deployment" , "default" )
123155
124156 testutils .ClearMockCall (& mm .StateMachineMock .Mock , "CurrentState" )
125157 mm .StateMachineMock .On ("CurrentState" ).Return (interfaces .StateMonitor )
0 commit comments