@@ -3,6 +3,7 @@ package runtime_test
3
3
import (
4
4
"context"
5
5
"errors"
6
+ "fmt"
6
7
"io/fs"
7
8
"testing"
8
9
"time"
@@ -63,6 +64,52 @@ var _ = Describe("NGINX Runtime Manager", func() {
63
64
Expect (metrics .IncReloadErrorsCallCount ()).To (Equal (0 ))
64
65
})
65
66
67
+ It ("Fails to find the main process" , func () {
68
+ process .FindMainProcessReturns (0 , fmt .Errorf ("failed to find process" ))
69
+
70
+ err := manager .Reload (context .Background (), 1 )
71
+
72
+ Expect (err ).To (MatchError ("failed to find NGINX main process: failed to find process" ))
73
+ Expect (process .ReadFileCallCount ()).To (Equal (0 ))
74
+ Expect (process .KillCallCount ()).To (Equal (0 ))
75
+ Expect (verifyClient .WaitForCorrectVersionCallCount ()).To (Equal (0 ))
76
+ })
77
+
78
+ It ("Fails to read file" , func () {
79
+ process .FindMainProcessReturns (1234 , nil )
80
+ process .ReadFileReturns (nil , fmt .Errorf ("failed to read file" ))
81
+
82
+ err := manager .Reload (context .Background (), 1 )
83
+
84
+ Expect (err ).To (MatchError ("failed to read file" ))
85
+ Expect (process .KillCallCount ()).To (Equal (0 ))
86
+ Expect (verifyClient .WaitForCorrectVersionCallCount ()).To (Equal (0 ))
87
+ })
88
+
89
+ It ("Fails to send kill signal" , func () {
90
+ process .FindMainProcessReturns (1234 , nil )
91
+ process .ReadFileReturns ([]byte ("child1\n child2" ), nil )
92
+ process .KillReturns (fmt .Errorf ("failed to send kill signal" ))
93
+
94
+ err := manager .Reload (context .Background (), 1 )
95
+
96
+ Expect (err ).To (MatchError ("failed to send the HUP signal to NGINX main: failed to send kill signal" ))
97
+ Expect (metrics .IncReloadErrorsCallCount ()).To (Equal (1 ))
98
+ Expect (verifyClient .WaitForCorrectVersionCallCount ()).To (Equal (0 ))
99
+ })
100
+
101
+ It ("times out waiting for correct version" , func () {
102
+ process .FindMainProcessReturns (1234 , nil )
103
+ process .ReadFileReturns ([]byte ("child1\n child2" ), nil )
104
+ process .KillReturns (nil )
105
+ verifyClient .WaitForCorrectVersionReturns (fmt .Errorf ("timeout waiting for correct version" ))
106
+
107
+ err := manager .Reload (context .Background (), 1 )
108
+
109
+ Expect (err ).To (MatchError ("timeout waiting for correct version" ))
110
+ Expect (metrics .IncReloadErrorsCallCount ()).To (Equal (1 ))
111
+ })
112
+
66
113
When ("MetricsCollector is nil" , func () {
67
114
It ("panics" , func () {
68
115
metrics = nil
@@ -110,6 +157,36 @@ var _ = Describe("NGINX Runtime Manager", func() {
110
157
Expect (upstreams ).To (BeEmpty ())
111
158
})
112
159
160
+ It ("successfully returns server upstreams" , func () {
161
+ upstreams := ngxclient.Upstreams {
162
+ "upstream1" : {
163
+ Zone : "zone1" ,
164
+ Peers : []ngxclient.Peer {
165
+ {ID : 1 , Name : "peer1-name" },
166
+ },
167
+ Queue : ngxclient.Queue {Size : 10 },
168
+ Keepalives : 5 ,
169
+ Zombies : 2 ,
170
+ },
171
+ "upstream2" : {
172
+ Zone : "zone2" ,
173
+ Peers : []ngxclient.Peer {
174
+ {ID : 2 , Name : "peer2-name" },
175
+ },
176
+ Queue : ngxclient.Queue {Size : 20 },
177
+ Keepalives : 3 ,
178
+ Zombies : 1 ,
179
+ },
180
+ }
181
+
182
+ ngxPlusClient .GetUpstreamsReturns (& upstreams , nil )
183
+
184
+ upstreams , err := manager .GetUpstreams ()
185
+
186
+ Expect (err ).NotTo (HaveOccurred ())
187
+ Expect (upstreams ).To (Equal (upstreams ))
188
+ })
189
+
113
190
It ("returns an error when GetUpstreams fails" , func () {
114
191
ngxPlusClient .GetUpstreamsReturns (nil , errors .New ("failed to get upstreams" ))
115
192
0 commit comments