@@ -2,6 +2,7 @@ package controller
22
33import (
44 "context"
5+ "strings"
56 "testing"
67
78 "github.com/Azure/operation-cache-controller/api/v1alpha1"
@@ -94,21 +95,18 @@ func TestGetProvisionJobName(t *testing.T) {
9495 }{
9596 {
9697 name : "Valid app name and operation ID" ,
97- appName : "my-app" ,
98- opId : "op123" ,
99- expected : "provision-my-app-op123" ,
98+ appName : "op123-my-app" ,
99+ expected : "provision-op123-my-app" ,
100100 },
101101 {
102102 name : "App name exceeds max length" ,
103- appName : "a-very-long-application-name-exceeding-limit" ,
104- opId : "op123" ,
105- expected : "provision-a-very-long-application-name-exceedi-op123" ,
103+ appName : "op1234567890-a-very-long-application-name-exceeding-limit" ,
104+ expected : "provision-op1234567890-a-very-long-application-name-exceeding-l" ,
106105 },
107106 {
108107 name : "Operation ID exceeds max length" ,
109- appName : "my-app" ,
110- opId : "operation-id-exceeding-length" ,
111- expected : "provision-my-app-operation-id-excee" ,
108+ appName : "operationid1234567890123456789012345678901234567890-my-application" ,
109+ expected : "provision-operationid1234567890123456789012345678901234567890-m" ,
112110 },
113111 }
114112
@@ -118,9 +116,6 @@ func TestGetProvisionJobName(t *testing.T) {
118116 ObjectMeta : metav1.ObjectMeta {
119117 Name : tt .appName ,
120118 },
121- Spec : v1alpha1.AppDeploymentSpec {
122- OpId : tt .opId ,
123- },
124119 }
125120 res := GetProvisionJobName (appDeployment )
126121 assert .Equal (t , tt .expected , res )
@@ -138,20 +133,17 @@ func TestGetTeardownJobName(t *testing.T) {
138133 {
139134 name : "Valid app name and operation ID" ,
140135 appName : "my-app" ,
141- opId : "op123" ,
142- expected : "teardown-my-app-op123" ,
136+ expected : "teardown-my-app" ,
143137 },
144138 {
145139 name : "App name exceeds max length" ,
146- appName : "a-very-long-application-name-exceeding-limit" ,
147- opId : "op123" ,
148- expected : "teardown-a-very-long-application-name-exceedi-op123" ,
140+ appName : "op1234567890-a-very-long-application-name-exceeding-limit" ,
141+ expected : "teardown-op1234567890-a-very-long-application-name-exceeding-li" ,
149142 },
150143 {
151144 name : "Operation ID exceeds max length" ,
152- appName : "my-app" ,
153- opId : "operation-id-exceeding-length" ,
154- expected : "teardown-my-app-operation-id-excee" ,
145+ appName : "operationid1234567890123456789012345678901234567890-my-application" ,
146+ expected : "teardown-operationid1234567890123456789012345678901234567890-my" ,
155147 },
156148 }
157149
@@ -161,9 +153,6 @@ func TestGetTeardownJobName(t *testing.T) {
161153 ObjectMeta : metav1.ObjectMeta {
162154 Name : tt .appName ,
163155 },
164- Spec : v1alpha1.AppDeploymentSpec {
165- OpId : tt .opId ,
166- },
167156 }
168157 res := GetTeardownJobName (appDeployment )
169158 assert .Equal (t , tt .expected , res )
@@ -172,59 +161,91 @@ func TestGetTeardownJobName(t *testing.T) {
172161}
173162func TestOperationScopedAppDeployment (t * testing.T ) {
174163 tests := []struct {
175- name string
176- appName string
177- opId string
178- expected string
164+ name string
165+ appName string
166+ opId string
167+ want string
179168 }{
180169 {
181- name : "Both appName and opId within limits " ,
182- appName : "my-app" ,
183- opId : "op123 " ,
184- expected : "op123 -my-app" ,
170+ name : "normal case - combined length within limit " ,
171+ appName : "my-app" ,
172+ opId : "op-12345 " ,
173+ want : "op-12345 -my-app" ,
185174 },
186175 {
187- name : "appName exceeds max length " ,
188- appName : "a-very-long-application-name-exceeding-limit" ,
189- opId : "op123 " ,
190- expected : "op123-a-very-long-application-name-exceedi" ,
176+ name : "app name truncated when too long " ,
177+ appName : strings . Repeat ( "a" , 60 ), // 60 chars, exceeds MaxAppNameLength
178+ opId : "op-12345 " ,
179+ want : "op-12345-" + strings . Repeat ( "a" , MaxAppNameLength ) ,
191180 },
192181 {
193- name : "opId exceeds max length" ,
194- appName : "my-app" ,
195- opId : "operation-id-exceeding-length" ,
196- expected : "operation-id-excee-my-app" ,
182+ name : "operation ID truncated when combined length exceeds limit " ,
183+ appName : strings . Repeat ( "a" , MaxAppNameLength ) ,
184+ opId : strings . Repeat ( "b" , 30 ), // This will exceed when combined
185+ want : strings . Repeat ( "b" , MaxResourceNameLength - MaxAppNameLength - 1 ) + "-" + strings . Repeat ( "a" , MaxAppNameLength ) ,
197186 },
198187 {
199- name : "Both appName and opId exceed max length " ,
200- appName : "another-very-long-application-name-exceeding-limit" ,
201- opId : "another-operation-id-exceeding-length" ,
202- expected : "another-operation--another-very-long-application-name-e" ,
188+ name : "both truncated for very long inputs " ,
189+ appName : strings . Repeat ( "a" , 100 ) ,
190+ opId : strings . Repeat ( "b" , 100 ) ,
191+ want : strings . Repeat ( "b" , MaxResourceNameLength - MaxAppNameLength - 1 ) + "-" + strings . Repeat ( "a" , MaxAppNameLength ) ,
203192 },
204193 {
205- name : "Empty appName and opId " ,
206- appName : "" ,
207- opId : " " ,
208- expected : " " ,
194+ name : "empty app name " ,
195+ appName : "" ,
196+ opId : "op-12345 " ,
197+ want : "op-12345- " ,
209198 },
210199 {
211- name : "Empty appName " ,
212- appName : " " ,
213- opId : "op123 " ,
214- expected : "op123- " ,
200+ name : "empty operation ID " ,
201+ appName : "my-app " ,
202+ opId : " " ,
203+ want : "-my-app " ,
215204 },
216205 {
217- name : "Empty opId" ,
218- appName : "my-app" ,
219- opId : "" ,
220- expected : "my-app" ,
206+ name : "both empty" ,
207+ appName : "" ,
208+ opId : "" ,
209+ want : "-" ,
210+ },
211+ {
212+ name : "exact max length" ,
213+ appName : "app" ,
214+ opId : strings .Repeat ("x" , MaxResourceNameLength - 4 ), // -4 for "-app"
215+ want : strings .Repeat ("x" , MaxResourceNameLength - 4 ) + "-app" ,
221216 },
222217 }
223218
224219 for _ , tt := range tests {
225220 t .Run (tt .name , func (t * testing.T ) {
226- res := OperationScopedAppDeployment (tt .appName , tt .opId )
227- assert .Equal (t , tt .expected , res )
221+ got := OperationScopedAppDeployment (tt .appName , tt .opId )
222+ assert .Equal (t , tt .want , got , "OperationScopedAppDeployment() mismatch for %s" , tt . name )
228223 })
229224 }
230225}
226+
227+ func TestOperationScopedAppDeployment_LengthConstraints (t * testing.T ) {
228+ // Test that the function always respects the maximum length constraint
229+ testCases := []struct {
230+ appNameLen int
231+ opIdLen int
232+ }{
233+ {10 , 10 },
234+ {MaxAppNameLength , 10 },
235+ {50 , 50 },
236+ {100 , 100 },
237+ {MaxResourceNameLength , MaxResourceNameLength },
238+ }
239+
240+ for _ , tc := range testCases {
241+ appName := strings .Repeat ("a" , tc .appNameLen )
242+ opId := strings .Repeat ("b" , tc .opIdLen )
243+
244+ result := OperationScopedAppDeployment (appName , opId )
245+
246+ if len (result ) > MaxResourceNameLength {
247+ t .Errorf ("Result length %d exceeds MaxResourceNameLength %d for appName length %d and opId length %d" ,
248+ len (result ), MaxResourceNameLength , tc .appNameLen , tc .opIdLen )
249+ }
250+ }
251+ }
0 commit comments