@@ -293,6 +293,54 @@ server {
293293 allow 127.0.0.1;
294294 deny all;
295295 }
296+ }`
297+ testConf24 = `server {
298+ listen 127.0.0.1:9090;
299+ location /writeapi {
300+ api write=off;
301+ allow 127.0.0.1;
302+ deny all;
303+ }
304+ }
305+ server {
306+ listen 127.0.0.1:9091;
307+ location /writeapi {
308+ api write=off;
309+ allow 127.0.0.1;
310+ deny all;
311+ }
312+ }`
313+ testConf25 = `server {
314+ listen 127.0.0.1:9090;
315+ location /writeapi {
316+ api write=off;
317+ allow 127.0.0.1;
318+ deny all;
319+ }
320+ }
321+ server {
322+ listen 127.0.0.1:9091;
323+ location /writeapi {
324+ api write=off;
325+ allow 127.0.0.1;
326+ deny all;
327+ }
328+ }
329+ server {
330+ listen 127.0.0.1:9092;
331+ location /writeapi {
332+ api write=on;
333+ allow 127.0.0.1;
334+ deny all;
335+ }
336+ }
337+ server {
338+ listen 127.0.0.1:9093;
339+ location /writeapi {
340+ api write=off;
341+ allow 127.0.0.1;
342+ deny all;
343+ }
296344}`
297345)
298346
@@ -813,13 +861,15 @@ func TestNginxConfigParser_checkLog(t *testing.T) {
813861 }
814862}
815863
864+ //nolint:maintidx // Does not make sense to add a new test case to do the exact same checks.
816865func TestNginxConfigParser_urlsForLocationDirective (t * testing.T ) {
817866 tmpDir := t .TempDir ()
818867 tests := []struct {
819- oss * model.APIDetails
820- plus * model.APIDetails
821- name string
822- conf string
868+ oss * model.APIDetails
869+ plus * model.APIDetails
870+ name string
871+ conf string
872+ expectedLog string
823873 }{
824874 {
825875 plus : & model.APIDetails {
@@ -1048,11 +1098,34 @@ func TestNginxConfigParser_urlsForLocationDirective(t *testing.T) {
10481098 name : "Test 23: Explicitly Write-Enabled Plus API" ,
10491099 conf : testConf23 ,
10501100 },
1101+ {
1102+ plus : & model.APIDetails {
1103+ URL : "http://localhost:9090/writeapi" ,
1104+ Listen : "localhost:9090" ,
1105+ Location : "/writeapi" ,
1106+ WriteEnabled : false ,
1107+ },
1108+ name : "Test 24: Multiple Plus APIs, all with Write=off, keep the order" ,
1109+ expectedLog : "No write-enabled NGINX Plus API found. Defaulting to read-only API" ,
1110+ conf : testConf24 ,
1111+ },
1112+ {
1113+ plus : & model.APIDetails {
1114+ URL : "http://localhost:9092/writeapi" ,
1115+ Listen : "localhost:9092" ,
1116+ Location : "/writeapi" ,
1117+ WriteEnabled : true ,
1118+ },
1119+ name : "Test 25: Multiple Plus APIs, Prioritize Write-Enabled (9092)" ,
1120+ conf : testConf25 ,
1121+ },
10511122 }
10521123
10531124 for _ , test := range tests {
10541125 t .Run (test .name , func (t * testing.T ) {
10551126 ctx := context .Background ()
1127+ logBuf := & bytes.Buffer {}
1128+ stub .StubLoggerWith (logBuf )
10561129 f , err := os .CreateTemp (tmpDir , "conf" )
10571130 require .NoError (t , err )
10581131 parseOptions := & crossplane.ParseOptions {
@@ -1074,6 +1147,9 @@ func TestNginxConfigParser_urlsForLocationDirective(t *testing.T) {
10741147 if test .plus != nil {
10751148 assert .Equal (t , test .plus , plus [0 ])
10761149 }
1150+ helpers .ValidateLog (t , test .expectedLog , logBuf )
1151+
1152+ logBuf .Reset ()
10771153 })
10781154 }
10791155}
@@ -1085,6 +1161,8 @@ func traverseConfigForAPIs(
10851161
10861162 ncp := NewNginxConfigParser (types .AgentConfig ())
10871163
1164+ allPlusAPIs := []* model.APIDetails {}
1165+
10881166 assert .Len (t , payload .Config , 1 )
10891167 for _ , xpConf := range payload .Config {
10901168 assert .Len (t , xpConf .Parsed , 1 )
@@ -1096,14 +1174,19 @@ func traverseConfigForAPIs(
10961174 }
10971175 _plus := ncp .apiDetailsFromLocationDirective (ctx , parent , directive , plusAPIDirective )
10981176 if _plus != nil {
1099- plus = _plus
1177+ allPlusAPIs = append ( allPlusAPIs , _plus ... )
11001178 }
11011179
11021180 return nil
11031181 })
11041182 require .NoError (t , err )
11051183 }
11061184
1185+ if len (allPlusAPIs ) > 0 {
1186+ sortedAPIs := ncp .sortPlusAPIs (ctx , allPlusAPIs )
1187+ plus = []* model.APIDetails {sortedAPIs [0 ]}
1188+ }
1189+
11071190 return oss , plus
11081191}
11091192
0 commit comments