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