Skip to content

Commit 99997c4

Browse files
committed
add tests for runtime manager
1 parent fde7eda commit 99997c4

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

internal/mode/static/nginx/runtime/manager_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,42 @@ var _ = Describe("NGINX Runtime Manager", func() {
2323
})
2424
})
2525

26+
func TestEnsureNginxRunning(t *testing.T) {
27+
ctx := context.Background()
28+
cancellingCtx, cancel := context.WithCancel(ctx)
29+
time.AfterFunc(1*time.Millisecond, cancel)
30+
tests := []struct {
31+
ctx context.Context
32+
name string
33+
expectError bool
34+
}{
35+
{
36+
ctx: ctx,
37+
name: "context exceeded",
38+
expectError: true,
39+
},
40+
{
41+
ctx: cancellingCtx,
42+
name: "context cancelled",
43+
expectError: true,
44+
},
45+
}
46+
47+
for _, test := range tests {
48+
t.Run(test.name, func(t *testing.T) {
49+
g := NewWithT(t)
50+
51+
err := EnsureNginxRunning(test.ctx)
52+
53+
if test.expectError {
54+
g.Expect(err).To(HaveOccurred())
55+
} else {
56+
g.Expect(err).ToNot(HaveOccurred())
57+
}
58+
})
59+
}
60+
}
61+
2662
func TestFindMainProcess(t *testing.T) {
2763
readFileFuncGen := func(content []byte) readFileFunc {
2864
return func(name string) ([]byte, error) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package runtime_test
2+
3+
import (
4+
"net/http"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
9+
"github.com/go-logr/logr"
10+
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/runtime"
11+
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/runtime/runtimefakes"
12+
"github.com/nginxinc/nginx-plus-go-client/client"
13+
ngxclient "github.com/nginxinc/nginx-plus-go-client/client"
14+
)
15+
16+
var _ = Describe("ManagerImpl UpdateHTTPServers", func() {
17+
18+
var (
19+
MaxConnsMock = 15
20+
MaxFailsMock = 2
21+
WeightMock = 1
22+
BackupMock = false
23+
DownMock = false
24+
)
25+
26+
BeforeEach(func() {
27+
serversMock := ngxclient.UpstreamServer{
28+
ID: 1,
29+
Server: "10.0.0.1:8080",
30+
MaxConns: &MaxConnsMock,
31+
MaxFails: &MaxFailsMock,
32+
FailTimeout: "test",
33+
SlowStart: "test",
34+
Route: "test",
35+
Backup: &BackupMock,
36+
Down: &DownMock,
37+
Drain: false,
38+
Weight: &WeightMock,
39+
Service: "server",
40+
}
41+
42+
serverMocks := []ngxclient.UpstreamServer{
43+
serversMock,
44+
}
45+
46+
nginxStatusSock := "http://10.0.0.1:8080"
47+
httpClient := &http.Client{}
48+
clientMock, _ := ngxclient.NewNginxClient(nginxStatusSock, client.WithHTTPClient(httpClient))
49+
logrMock := logr.New(GinkgoLogr.GetSink())
50+
51+
mockedManager := runtime.NewManagerImpl(clientMock, nil, logrMock)
52+
err := mockedManager.UpdateHTTPServers("10.0.0.1:8080", serverMocks)
53+
Expect(err).To(BeNil())
54+
55+
})
56+
})
57+
58+
var _ = Describe("ManagerImpl GetUpstreams", func() {
59+
var (
60+
mockNginxClient *ngxclient.NginxClient
61+
manager *runtimefakes.FakeManager
62+
)
63+
64+
var (
65+
MaxConnsMock = 15
66+
MaxFailsMock = 2
67+
WeightMock = 1
68+
BackupMock = false
69+
DownMock = false
70+
)
71+
72+
BeforeEach(func() {
73+
nginxStatusSock := "http://10.0.0.1:8080"
74+
httpClient := &http.Client{}
75+
mockNginxClient, _ = ngxclient.NewNginxClient(nginxStatusSock, client.WithHTTPClient(httpClient))
76+
mockNginxClient.AddHTTPServer("10.0.0.1:8080", ngxclient.UpstreamServer{
77+
ID: 1,
78+
Server: "10.0.0.1:8080",
79+
MaxConns: &MaxConnsMock,
80+
MaxFails: &MaxFailsMock,
81+
FailTimeout: "test",
82+
SlowStart: "test",
83+
Route: "test",
84+
Backup: &BackupMock,
85+
Down: &DownMock,
86+
Drain: false,
87+
Weight: &WeightMock,
88+
Service: "server",
89+
})
90+
manager = &runtimefakes.FakeManager{}
91+
})
92+
93+
Describe("GetUpstreams", func() {
94+
Context("when NGINX Plus is not enabled", func() {
95+
BeforeEach(func() {
96+
manager.IsPlusReturns(false)
97+
})
98+
99+
It("returns no upstreams from NGINX Plus API", func() {
100+
101+
_, err := manager.GetUpstreams()
102+
Expect(err).ToNot(HaveOccurred())
103+
})
104+
105+
})
106+
})
107+
})

0 commit comments

Comments
 (0)