Skip to content

Commit 986c3fe

Browse files
henrybarretogustavosbarreto
authored andcommitted
tests(gateway): add tests to renew certificates flow
1 parent 1d58a86 commit 986c3fe

File tree

3 files changed

+260
-15
lines changed

3 files changed

+260
-15
lines changed

gateway/certbot_test.go

Lines changed: 197 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package main
22

33
import (
4+
"context"
45
"errors"
56
"os/exec"
67
"testing"
8+
"time"
79

8-
executorMock "github.com/shellhub-io/shellhub/gateway/mocks"
10+
gatewayMocks "github.com/shellhub-io/shellhub/gateway/mocks"
911
"github.com/spf13/afero"
1012
"github.com/stretchr/testify/assert"
1113
"github.com/stretchr/testify/mock"
@@ -34,7 +36,7 @@ func TestCertBot_generateCertificateFromDNS(t *testing.T) {
3436
name string
3537
config Config
3638
expected error
37-
expectCalls func(*executorMock.Executor)
39+
expectCalls func(*gatewayMocks.Executor)
3840
}{
3941
{
4042
name: "failed to run the command",
@@ -45,7 +47,7 @@ func TestCertBot_generateCertificateFromDNS(t *testing.T) {
4547
Token: "test",
4648
},
4749
},
48-
expectCalls: func(executorMock *executorMock.Executor) {
50+
expectCalls: func(executorMock *gatewayMocks.Executor) {
4951
executorMock.On("Command", "certbot",
5052
"certonly",
5153
"--non-interactive",
@@ -73,7 +75,7 @@ func TestCertBot_generateCertificateFromDNS(t *testing.T) {
7375
Token: "test",
7476
},
7577
},
76-
expectCalls: func(executorMock *executorMock.Executor) {
78+
expectCalls: func(executorMock *gatewayMocks.Executor) {
7779
executorMock.On("Command", "certbot",
7880
"certonly",
7981
"--non-interactive",
@@ -102,7 +104,7 @@ func TestCertBot_generateCertificateFromDNS(t *testing.T) {
102104
},
103105
Staging: true,
104106
},
105-
expectCalls: func(executorMock *executorMock.Executor) {
107+
expectCalls: func(executorMock *gatewayMocks.Executor) {
106108
executorMock.On("Command", "certbot",
107109
"certonly",
108110
"--non-interactive",
@@ -126,7 +128,7 @@ func TestCertBot_generateCertificateFromDNS(t *testing.T) {
126128

127129
for _, tc := range tests {
128130
t.Run(tc.name, func(tt *testing.T) {
129-
executorMock := new(executorMock.Executor)
131+
executorMock := new(gatewayMocks.Executor)
130132

131133
certbot := newCertBot(&tc.config)
132134
certbot.fs = afero.NewMemMapFs()
@@ -147,14 +149,14 @@ func TestCertBot_executeRenewCertificates(t *testing.T) {
147149
name string
148150
config Config
149151
expected error
150-
expectCalls func(*executorMock.Executor)
152+
expectCalls func(*gatewayMocks.Executor)
151153
}{
152154
{
153155
name: "failed to run the renew command",
154156
config: Config{
155157
Staging: false,
156158
},
157-
expectCalls: func(executorMock *executorMock.Executor) {
159+
expectCalls: func(executorMock *gatewayMocks.Executor) {
158160
executorMock.On("Command", "certbot", "renew").Return(exec.Command("")).Once()
159161
executorMock.On("Run", mock.AnythingOfType("*exec.Cmd")).Return(errors.New("failed to run the renew command")).Once()
160162
},
@@ -165,7 +167,7 @@ func TestCertBot_executeRenewCertificates(t *testing.T) {
165167
config: Config{
166168
Staging: false,
167169
},
168-
expectCalls: func(executorMock *executorMock.Executor) {
170+
expectCalls: func(executorMock *gatewayMocks.Executor) {
169171
executorMock.On("Command", "certbot", "renew").Return(exec.Command("")).Once()
170172
executorMock.On("Run", mock.AnythingOfType("*exec.Cmd")).Return(nil).Once()
171173
},
@@ -176,7 +178,7 @@ func TestCertBot_executeRenewCertificates(t *testing.T) {
176178
config: Config{
177179
Staging: true,
178180
},
179-
expectCalls: func(executorMock *executorMock.Executor) {
181+
expectCalls: func(executorMock *gatewayMocks.Executor) {
180182
executorMock.On("Command", "certbot", "renew", "--staging").Return(exec.Command("")).Once()
181183
executorMock.On("Run", mock.AnythingOfType("*exec.Cmd")).Return(nil).Once()
182184
},
@@ -186,7 +188,7 @@ func TestCertBot_executeRenewCertificates(t *testing.T) {
186188

187189
for _, tc := range tests {
188190
t.Run(tc.name, func(tt *testing.T) {
189-
executorMock := new(executorMock.Executor)
191+
executorMock := new(gatewayMocks.Executor)
190192

191193
certbot := newCertBot(&tc.config)
192194
certbot.ex = executorMock
@@ -200,3 +202,187 @@ func TestCertBot_executeRenewCertificates(t *testing.T) {
200202
})
201203
}
202204
}
205+
206+
func TestCertBot_renewCertificates(t *testing.T) {
207+
duration := 100 * time.Millisecond
208+
209+
tests := []struct {
210+
name string
211+
config Config
212+
expectCalls func(*gatewayMocks.Executor, *gatewayMocks.Ticker)
213+
shouldRenewCalled bool
214+
}{
215+
{
216+
name: "failed renewal",
217+
config: Config{
218+
Staging: false,
219+
},
220+
expectCalls: func(executorMock *gatewayMocks.Executor, tickerMock *gatewayMocks.Ticker) {
221+
tickerMock.On("Init", mock.Anything, mock.Anything).Once()
222+
tickerMock.On("Stop").Once()
223+
224+
ch := make(chan time.Time, 1)
225+
ch <- time.Now()
226+
tickerMock.On("Tick").Return(ch).Once()
227+
228+
executorMock.On("Command", "certbot",
229+
"renew",
230+
).Return(exec.Command("")).Once()
231+
232+
executorMock.On("Run", mock.AnythingOfType("*exec.Cmd")).Return(errors.New("failed to renew")).Once()
233+
},
234+
shouldRenewCalled: false,
235+
},
236+
{
237+
name: "failed renewal more than run time",
238+
config: Config{
239+
Staging: false,
240+
},
241+
expectCalls: func(executorMock *gatewayMocks.Executor, tickerMock *gatewayMocks.Ticker) {
242+
tickerMock.On("Init", mock.Anything, mock.Anything).Once()
243+
tickerMock.On("Stop").Once()
244+
245+
ch := make(chan time.Time, 2)
246+
ch <- time.Now()
247+
ch <- time.Now()
248+
tickerMock.On("Tick").Return(ch).Once()
249+
250+
executorMock.On("Command", "certbot",
251+
"renew",
252+
).Return(exec.Command("")).Twice()
253+
254+
executorMock.On("Run", mock.AnythingOfType("*exec.Cmd")).Return(errors.New("failed to renew")).Twice()
255+
},
256+
shouldRenewCalled: false,
257+
},
258+
{
259+
name: "success to renew after failure",
260+
config: Config{
261+
Staging: false,
262+
},
263+
expectCalls: func(executorMock *gatewayMocks.Executor, tickerMock *gatewayMocks.Ticker) {
264+
tickerMock.On("Init", mock.Anything, mock.Anything).Once()
265+
tickerMock.On("Stop").Once()
266+
267+
ch := make(chan time.Time, 2)
268+
ch <- time.Now()
269+
tickerMock.On("Tick").Return(ch).Once()
270+
271+
executorMock.On("Command", "certbot",
272+
"renew",
273+
).Return(exec.Command("")).Once()
274+
275+
executorMock.On("Run", mock.AnythingOfType("*exec.Cmd")).Return(errors.New("failed to renew")).Once()
276+
277+
ch <- time.Now()
278+
executorMock.On("Command", "certbot",
279+
"renew",
280+
).Return(exec.Command("")).Once()
281+
282+
executorMock.On("Run", mock.AnythingOfType("*exec.Cmd")).Return(nil).Once()
283+
},
284+
shouldRenewCalled: true,
285+
},
286+
{
287+
name: "success to renew",
288+
config: Config{
289+
Staging: false,
290+
},
291+
expectCalls: func(executorMock *gatewayMocks.Executor, tickerMock *gatewayMocks.Ticker) {
292+
tickerMock.On("Init", mock.Anything, mock.Anything).Once()
293+
tickerMock.On("Stop").Once()
294+
295+
ch := make(chan time.Time, 1)
296+
ch <- time.Now()
297+
tickerMock.On("Tick").Return(ch).Once()
298+
299+
executorMock.On("Command", "certbot",
300+
"renew",
301+
).Return(exec.Command("")).Once()
302+
303+
executorMock.On("Run", mock.AnythingOfType("*exec.Cmd")).Return(nil).Once()
304+
},
305+
shouldRenewCalled: true,
306+
},
307+
{
308+
name: "success to renew more than one time",
309+
config: Config{
310+
Staging: false,
311+
},
312+
expectCalls: func(executorMock *gatewayMocks.Executor, tickerMock *gatewayMocks.Ticker) {
313+
tickerMock.On("Init", mock.Anything, mock.Anything).Once()
314+
tickerMock.On("Stop").Once()
315+
316+
ch := make(chan time.Time, 2)
317+
ch <- time.Now()
318+
ch <- time.Now()
319+
tickerMock.On("Tick").Return(ch).Once()
320+
321+
executorMock.On("Command", "certbot",
322+
"renew",
323+
).Return(exec.Command("")).Twice()
324+
325+
executorMock.On("Run", mock.AnythingOfType("*exec.Cmd")).Return(nil).Twice()
326+
},
327+
shouldRenewCalled: true,
328+
},
329+
{
330+
name: "success to renew on staging",
331+
config: Config{
332+
Staging: true,
333+
},
334+
expectCalls: func(executorMock *gatewayMocks.Executor, tickerMock *gatewayMocks.Ticker) {
335+
tickerMock.On("Init", mock.Anything, mock.Anything).Once()
336+
tickerMock.On("Stop").Once()
337+
338+
ch := make(chan time.Time, 1)
339+
ch <- time.Now()
340+
tickerMock.On("Tick").Return(ch).Once()
341+
342+
executorMock.On("Command", "certbot",
343+
"renew",
344+
"--staging",
345+
).Return(exec.Command("")).Once()
346+
347+
executorMock.On("Run", mock.AnythingOfType("*exec.Cmd")).Return(nil).Once()
348+
},
349+
shouldRenewCalled: true,
350+
},
351+
}
352+
353+
for _, tc := range tests {
354+
t.Run(tc.name, func(tt *testing.T) {
355+
ctx, cancel := context.WithTimeout(context.Background(), duration)
356+
defer cancel()
357+
358+
tickerMock := new(gatewayMocks.Ticker)
359+
executorMock := new(gatewayMocks.Executor)
360+
361+
config := &tc.config
362+
363+
renewWasCalled := false
364+
config.RenewedCallback = func() {
365+
renewWasCalled = true
366+
}
367+
368+
certbot := newCertBot(config)
369+
certbot.tk = tickerMock
370+
certbot.ex = executorMock
371+
372+
tc.expectCalls(executorMock, tickerMock)
373+
374+
done := make(chan struct{})
375+
go func() {
376+
certbot.renewCertificates(ctx, duration)
377+
close(done)
378+
}()
379+
380+
<-done
381+
382+
assert.Equal(tt, tc.shouldRenewCalled, renewWasCalled)
383+
384+
tickerMock.AssertExpectations(tt)
385+
executorMock.AssertExpectations(tt)
386+
})
387+
}
388+
}

gateway/mocks/executor.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gateway/mocks/ticker.go

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)