@@ -14,6 +14,54 @@ import (
1414 "github.com/stretchr/testify/assert"
1515)
1616
17+ var plusAPIs = []* model.APIDetails {
18+ {
19+ URL : "http://127.0.0.1:8081/api" ,
20+ Listen : "127.0.0.1:8081" ,
21+ Location : "/api" ,
22+ WriteEnabled : false ,
23+ Ca : "" ,
24+ },
25+ {
26+ URL : "unix:/var/run/nginx/api.sock" ,
27+ Listen : "unix:/var/run/nginx/api.sock" ,
28+ Location : "/api" ,
29+ WriteEnabled : true , // Crucial for selection logic
30+ Ca : "/etc/certs/my_ca.pem" ,
31+ },
32+ }
33+
34+ var nginxPlusConfigContextForUpdate = & model.NginxConfigContext {
35+ AccessLogs : []* model.AccessLog {
36+ {
37+ Name : "/usr/local/var/log/nginx/access.log" ,
38+ },
39+ },
40+ ErrorLogs : []* model.ErrorLog {
41+ {
42+ Name : "/usr/local/var/log/nginx/error.log" ,
43+ },
44+ },
45+ PlusAPI : plusAPIs [1 ],
46+ StubStatus : & model.APIDetails {
47+ URL : "http://127.0.0.1:8081/status" ,
48+ Listen : "127.0.0.1:8081" ,
49+ },
50+ PlusAPIs : plusAPIs ,
51+ }
52+
53+ func convertAPIDetailsSliceForTest (modelAPIs []* model.APIDetails ) []* mpi.APIDetails {
54+ if modelAPIs == nil {
55+ return nil
56+ }
57+ mpiAPIs := make ([]* mpi.APIDetails , 0 , len (modelAPIs ))
58+ for _ , api := range modelAPIs {
59+ mpiAPIs = append (mpiAPIs , convertToMpiAPIDetails (api ))
60+ }
61+
62+ return mpiAPIs
63+ }
64+
1765func TestInstanceWatcherService_updateNginxInstanceRuntime (t * testing.T ) {
1866 nginxOSSConfigContext := & model.NginxConfigContext {
1967 AccessLogs : []* model.AccessLog {
@@ -68,12 +116,32 @@ func TestInstanceWatcherService_updateNginxInstanceRuntime(t *testing.T) {
68116 nginxConfigContext : nginxPlusConfigContext ,
69117 instance : protos .NginxPlusInstance ([]string {}),
70118 },
119+ {
120+ name : "Test 3: Plus Instance - PlusAPIs Update" ,
121+ nginxConfigContext : nginxPlusConfigContextForUpdate ,
122+ instance : protos .NginxPlusInstance ([]string {}),
123+ },
71124 }
72125
73126 for _ , test := range tests {
74- t .Run (test .name , func (tt * testing.T ) {
127+ t .Run (test .name , func (t * testing.T ) {
128+ updatesRequired := UpdateNginxInstanceRuntime (test .instance , test .nginxConfigContext )
75129 UpdateNginxInstanceRuntime (test .instance , test .nginxConfigContext )
76- if test .name == "Test 2: Plus Instance" {
130+ switch test .name {
131+ case "Test 3: Plus Instance - PlusAPIs Update" :
132+ assert .True (t , updatesRequired ,
133+ "UpdateNginxInstanceRuntime should return true when PlusAPIs are updated" )
134+ expectedAPIs := convertAPIDetailsSliceForTest (test .nginxConfigContext .PlusAPIs )
135+ assert .ElementsMatch (t , expectedAPIs ,
136+ test .instance .GetInstanceRuntime ().GetNginxPlusRuntimeInfo ().GetPlusApis ())
137+ assert .Equal (t , test .nginxConfigContext .PlusAPI .WriteEnabled , test .instance .GetInstanceRuntime ().
138+ GetNginxPlusRuntimeInfo ().GetPlusApi ().GetWriteEnabled ())
139+ assert .Equal (t , test .nginxConfigContext .PlusAPI .Ca , test .instance .GetInstanceRuntime ().
140+ GetNginxPlusRuntimeInfo ().GetPlusApi ().GetCa ())
141+ assert .Equal (t , test .nginxConfigContext .PlusAPI .Listen , test .instance .GetInstanceRuntime ().
142+ GetNginxPlusRuntimeInfo ().GetPlusApi ().GetListen ())
143+
144+ case "Test 2: Plus Instance" :
77145 assert .Equal (t , test .nginxConfigContext .AccessLogs [0 ].Name , test .instance .GetInstanceRuntime ().
78146 GetNginxPlusRuntimeInfo ().GetAccessLogs ()[0 ])
79147 assert .Equal (t , test .nginxConfigContext .ErrorLogs [0 ].Name , test .instance .GetInstanceRuntime ().
@@ -86,7 +154,12 @@ func TestInstanceWatcherService_updateNginxInstanceRuntime(t *testing.T) {
86154 GetNginxPlusRuntimeInfo ().GetStubStatus ().GetListen ())
87155 assert .Equal (t , test .nginxConfigContext .PlusAPI .Listen , test .instance .GetInstanceRuntime ().
88156 GetNginxPlusRuntimeInfo ().GetPlusApi ().GetListen ())
89- } else {
157+ assert .Equal (t , test .nginxConfigContext .PlusAPI .WriteEnabled , test .instance .GetInstanceRuntime ().
158+ GetNginxPlusRuntimeInfo ().GetPlusApi ().GetWriteEnabled ())
159+ assert .Equal (t , test .nginxConfigContext .PlusAPI .Ca , test .instance .GetInstanceRuntime ().
160+ GetNginxPlusRuntimeInfo ().GetPlusApi ().GetCa ())
161+
162+ default :
90163 assert .Equal (t , test .nginxConfigContext .AccessLogs [0 ].Name , test .instance .GetInstanceRuntime ().
91164 GetNginxRuntimeInfo ().GetAccessLogs ()[0 ])
92165 assert .Equal (t , test .nginxConfigContext .ErrorLogs [0 ].Name , test .instance .GetInstanceRuntime ().
0 commit comments