11package main
22
33import (
4+ "errors"
5+ "os/exec"
46 "testing"
57
8+ executorMock "github.com/shellhub-io/shellhub/gateway/mocks"
69 "github.com/spf13/afero"
710 "github.com/stretchr/testify/assert"
11+ "github.com/stretchr/testify/mock"
812)
913
1014func TestCertBot_generateProviderCredentialsFile (t * testing.T ) {
@@ -24,3 +28,116 @@ func TestCertBot_generateProviderCredentialsFile(t *testing.T) {
2428
2529 assert .Equal (t , "dns_digitalocean_token = test" , string (buffer ))
2630}
31+
32+ func TestCertBot_generateCertificateFromDNS (t * testing.T ) {
33+ tests := []struct {
34+ name string
35+ config Config
36+ expected error
37+ expectCalls func (* executorMock.Executor )
38+ }{
39+ {
40+ name : "failed to run the command" ,
41+ config : Config {
42+ Tunnels : & Tunnels {
43+ Domain : "localhost" ,
44+ Provider : "digitalocean" ,
45+ Token : "test" ,
46+ },
47+ },
48+ expectCalls : func (executorMock * executorMock.Executor ) {
49+ executorMock .On ("Command" , "certbot" ,
50+ "certonly" ,
51+ "--non-interactive" ,
52+ "--agree-tos" ,
53+ "--register-unsafely-without-email" ,
54+ "--cert-name" ,
55+ "*.localhost" ,
56+ "--dns-digitalocean" ,
57+ "--dns-digitalocean-credentials" ,
58+ "/etc/shellhub-gateway/digitalocean.ini" ,
59+ "-d" ,
60+ "*.localhost" ,
61+ ).Return (exec .Command ("" )).Once ()
62+
63+ executorMock .On ("Run" , mock .AnythingOfType ("*exec.Cmd" )).Return (errors .New ("failed to run the command" )).Once ()
64+ },
65+ expected : errors .New ("failed to run the command" ),
66+ },
67+ {
68+ name : "successful certificate generation" ,
69+ config : Config {
70+ Tunnels : & Tunnels {
71+ Domain : "localhost" ,
72+ Provider : "digitalocean" ,
73+ Token : "test" ,
74+ },
75+ },
76+ expectCalls : func (executorMock * executorMock.Executor ) {
77+ executorMock .On ("Command" , "certbot" ,
78+ "certonly" ,
79+ "--non-interactive" ,
80+ "--agree-tos" ,
81+ "--register-unsafely-without-email" ,
82+ "--cert-name" ,
83+ "*.localhost" ,
84+ "--dns-digitalocean" ,
85+ "--dns-digitalocean-credentials" ,
86+ "/etc/shellhub-gateway/digitalocean.ini" ,
87+ "-d" ,
88+ "*.localhost" ,
89+ ).Return (exec .Command ("" )).Once ()
90+
91+ executorMock .On ("Run" , mock .AnythingOfType ("*exec.Cmd" )).Return (nil ).Once ()
92+ },
93+ expected : nil ,
94+ },
95+ {
96+ name : "successful certificate generation in staging" ,
97+ config : Config {
98+ Tunnels : & Tunnels {
99+ Domain : "localhost" ,
100+ Provider : "digitalocean" ,
101+ Token : "test" ,
102+ },
103+ Staging : true ,
104+ },
105+ expectCalls : func (executorMock * executorMock.Executor ) {
106+ executorMock .On ("Command" , "certbot" ,
107+ "certonly" ,
108+ "--non-interactive" ,
109+ "--agree-tos" ,
110+ "--register-unsafely-without-email" ,
111+ "--cert-name" ,
112+ "*.localhost" ,
113+ "--dns-digitalocean" ,
114+ "--dns-digitalocean-credentials" ,
115+ "/etc/shellhub-gateway/digitalocean.ini" ,
116+ "-d" ,
117+ "*.localhost" ,
118+ "--staging" ,
119+ ).Return (exec .Command ("" )).Once ()
120+
121+ executorMock .On ("Run" , mock .AnythingOfType ("*exec.Cmd" )).Return (nil ).Once ()
122+ },
123+ expected : nil ,
124+ },
125+ }
126+
127+ for _ , tc := range tests {
128+ t .Run (tc .name , func (tt * testing.T ) {
129+ executorMock := new (executorMock.Executor )
130+
131+ certbot := newCertBot (& tc .config )
132+ certbot .fs = afero .NewMemMapFs ()
133+ certbot .ex = executorMock
134+
135+ tc .expectCalls (executorMock )
136+
137+ err := certbot .generateCertificateFromDNS ()
138+ assert .Equal (tt , tc .expected , err )
139+
140+ executorMock .AssertExpectations (t )
141+ })
142+ }
143+ }
0 commit comments