1
- // +build consul
2
-
3
1
package storageconsul
4
2
5
3
import (
6
- "github.com/caddyserver/certmagic"
7
- consul "github.com/hashicorp/consul/api"
8
- "github.com/stretchr/testify/assert"
4
+ "context"
5
+ "io/fs"
9
6
"os"
10
7
"path"
11
8
"testing"
12
9
"time"
10
+
11
+ "github.com/caddyserver/caddy/v2"
12
+ consul "github.com/hashicorp/consul/api"
13
+ "github.com/stretchr/testify/assert"
14
+
15
+ consulContainer "github.com/testcontainers/testcontainers-go/modules/consul"
13
16
)
14
17
15
- var consulClient * consul. Client
18
+ var consulCont * consulContainer. ConsulContainer
16
19
17
20
const TestPrefix = "consultlstest"
18
21
19
- // these tests needs a running Consul server
20
- func setupConsulEnv (t * testing.T ) * ConsulStorage {
22
+ func TestMain (m * testing.M ) {
23
+ cc , err := consulContainer .Run (context .Background (), "hashicorp/consul:1.15" )
24
+ if err != nil {
25
+ panic (err )
26
+ }
21
27
22
- os . Setenv ( EnvNamePrefix , TestPrefix )
23
- os . Setenv ( consul . HTTPTokenEnvName , "2f9e03f8-714b-5e4d-65ea-c983d6b172c4" )
28
+ consulCont = cc
29
+ defer consulCont . Terminate ( context . Background () )
24
30
25
- cs , err := New ()
31
+ m .Run ()
32
+ }
33
+
34
+ // these tests need a running Consul server
35
+ func setupConsulEnv (t * testing.T ) * ConsulStorage {
36
+ os .Setenv (EnvNamePrefix , TestPrefix )
37
+ consulEndpoint , err := consulCont .ApiEndpoint (context .Background ())
26
38
assert .NoError (t , err )
27
39
40
+ //os.Setenv(consul.HTTPTokenEnvName, "2f9e03f8-714b-5e4d-65ea-c983d6b172c4")
41
+ os .Setenv (consul .HTTPAddrEnvName , consulEndpoint )
42
+
43
+ cs := New ()
44
+ ctx , _ := caddy .NewContext (caddy.Context {Context : context .Background ()})
45
+ cs .Provision (ctx )
46
+
28
47
_ , err = cs .ConsulClient .KV ().DeleteTree (TestPrefix , nil )
29
48
assert .NoError (t , err )
49
+
30
50
return cs
31
51
}
32
52
33
53
func TestConsulStorage_Store (t * testing.T ) {
34
54
cs := setupConsulEnv (t )
35
55
36
- err := cs .Store (path .Join ("acme" , "example.com" , "sites" , "example.com" , "example.com.crt" ), []byte ("crt data" ))
56
+ err := cs .Store (context . Background (), path .Join ("acme" , "example.com" , "sites" , "example.com" , "example.com.crt" ), []byte ("crt data" ))
37
57
assert .NoError (t , err )
38
58
}
39
59
@@ -42,10 +62,10 @@ func TestConsulStorage_Exists(t *testing.T) {
42
62
43
63
key := path .Join ("acme" , "example.com" , "sites" , "example.com" , "example.com.crt" )
44
64
45
- err := cs .Store (key , []byte ("crt data" ))
65
+ err := cs .Store (context . Background (), key , []byte ("crt data" ))
46
66
assert .NoError (t , err )
47
67
48
- exists := cs .Exists (key )
68
+ exists := cs .Exists (context . Background (), key )
49
69
assert .True (t , exists )
50
70
}
51
71
@@ -55,10 +75,10 @@ func TestConsulStorage_Load(t *testing.T) {
55
75
key := path .Join ("acme" , "example.com" , "sites" , "example.com" , "example.com.crt" )
56
76
content := []byte ("crt data" )
57
77
58
- err := cs .Store (key , content )
78
+ err := cs .Store (context . Background (), key , content )
59
79
assert .NoError (t , err )
60
80
61
- contentLoded , err := cs .Load (key )
81
+ contentLoded , err := cs .Load (context . Background (), key )
62
82
assert .NoError (t , err )
63
83
64
84
assert .Equal (t , content , contentLoded )
@@ -70,20 +90,19 @@ func TestConsulStorage_Delete(t *testing.T) {
70
90
key := path .Join ("acme" , "example.com" , "sites" , "example.com" , "example.com.crt" )
71
91
content := []byte ("crt data" )
72
92
73
- err := cs .Store (key , content )
93
+ err := cs .Store (context . Background (), key , content )
74
94
assert .NoError (t , err )
75
95
76
- err = cs .Delete (key )
96
+ err = cs .Delete (context . Background (), key )
77
97
assert .NoError (t , err )
78
98
79
- exists := cs .Exists (key )
99
+ exists := cs .Exists (context . Background (), key )
80
100
assert .False (t , exists )
81
101
82
- contentLoaded , err := cs .Load (key )
102
+ contentLoaded , err := cs .Load (context . Background (), key )
83
103
assert .Nil (t , contentLoaded )
84
-
85
- _ , ok := err .(certmagic.ErrNotExist )
86
- assert .True (t , ok )
104
+ assert .Error (t , err )
105
+ assert .ErrorIs (t , err , fs .ErrNotExist )
87
106
}
88
107
89
108
func TestConsulStorage_Stat (t * testing.T ) {
@@ -92,10 +111,10 @@ func TestConsulStorage_Stat(t *testing.T) {
92
111
key := path .Join ("acme" , "example.com" , "sites" , "example.com" , "example.com.crt" )
93
112
content := []byte ("crt data" )
94
113
95
- err := cs .Store (key , content )
114
+ err := cs .Store (context . Background (), key , content )
96
115
assert .NoError (t , err )
97
116
98
- info , err := cs .Stat (key )
117
+ info , err := cs .Stat (context . Background (), key )
99
118
assert .NoError (t , err )
100
119
101
120
assert .Equal (t , key , info .Key )
@@ -104,14 +123,14 @@ func TestConsulStorage_Stat(t *testing.T) {
104
123
func TestConsulStorage_List (t * testing.T ) {
105
124
cs := setupConsulEnv (t )
106
125
107
- err := cs .Store (path .Join ("acme" , "example.com" , "sites" , "example.com" , "example.com.crt" ), []byte ("crt" ))
126
+ err := cs .Store (context . Background (), path .Join ("acme" , "example.com" , "sites" , "example.com" , "example.com.crt" ), []byte ("crt" ))
108
127
assert .NoError (t , err )
109
- err = cs .Store (path .Join ("acme" , "example.com" , "sites" , "example.com" , "example.com.key" ), []byte ("key" ))
128
+ err = cs .Store (context . Background (), path .Join ("acme" , "example.com" , "sites" , "example.com" , "example.com.key" ), []byte ("key" ))
110
129
assert .NoError (t , err )
111
- err = cs .Store (path .Join ("acme" , "example.com" , "sites" , "example.com" , "example.com.json" ), []byte ("meta" ))
130
+ err = cs .Store (context . Background (), path .Join ("acme" , "example.com" , "sites" , "example.com" , "example.com.json" ), []byte ("meta" ))
112
131
assert .NoError (t , err )
113
132
114
- keys , err := cs .List (path .Join ("acme" , "example.com" , "sites" , "example.com" ), true )
133
+ keys , err := cs .List (context . Background (), path .Join ("acme" , "example.com" , "sites" , "example.com" ), true )
115
134
assert .NoError (t , err )
116
135
assert .Len (t , keys , 3 )
117
136
assert .Contains (t , keys , path .Join ("acme" , "example.com" , "sites" , "example.com" , "example.com.crt" ))
@@ -120,14 +139,14 @@ func TestConsulStorage_List(t *testing.T) {
120
139
func TestConsulStorage_ListNonRecursive (t * testing.T ) {
121
140
cs := setupConsulEnv (t )
122
141
123
- err := cs .Store (path .Join ("acme" , "example.com" , "sites" , "example.com" , "example.com.crt" ), []byte ("crt" ))
142
+ err := cs .Store (context . Background (), path .Join ("acme" , "example.com" , "sites" , "example.com" , "example.com.crt" ), []byte ("crt" ))
124
143
assert .NoError (t , err )
125
- err = cs .Store (path .Join ("acme" , "example.com" , "sites" , "example.com" , "example.com.key" ), []byte ("key" ))
144
+ err = cs .Store (context . Background (), path .Join ("acme" , "example.com" , "sites" , "example.com" , "example.com.key" ), []byte ("key" ))
126
145
assert .NoError (t , err )
127
- err = cs .Store (path .Join ("acme" , "example.com" , "sites" , "example.com" , "example.com.json" ), []byte ("meta" ))
146
+ err = cs .Store (context . Background (), path .Join ("acme" , "example.com" , "sites" , "example.com" , "example.com.json" ), []byte ("meta" ))
128
147
assert .NoError (t , err )
129
148
130
- keys , err := cs .List (path .Join ("acme" , "example.com" , "sites" ), false )
149
+ keys , err := cs .List (context . Background (), path .Join ("acme" , "example.com" , "sites" ), false )
131
150
assert .NoError (t , err )
132
151
133
152
assert .Len (t , keys , 1 )
@@ -138,10 +157,10 @@ func TestConsulStorage_LockUnlock(t *testing.T) {
138
157
cs := setupConsulEnv (t )
139
158
lockKey := path .Join ("acme" , "example.com" , "sites" , "example.com" , "lock" )
140
159
141
- err := cs .Lock (lockKey )
160
+ err := cs .Lock (context . Background (), lockKey )
142
161
assert .NoError (t , err )
143
162
144
- err = cs .Unlock (lockKey )
163
+ err = cs .Unlock (context . Background (), lockKey )
145
164
assert .NoError (t , err )
146
165
}
147
166
@@ -150,17 +169,17 @@ func TestConsulStorage_TwoLocks(t *testing.T) {
150
169
cs2 := setupConsulEnv (t )
151
170
lockKey := path .Join ("acme" , "example.com" , "sites" , "example.com" , "lock" )
152
171
153
- err := cs .Lock (lockKey )
172
+ err := cs .Lock (context . Background (), lockKey )
154
173
assert .NoError (t , err )
155
174
156
175
go time .AfterFunc (5 * time .Second , func () {
157
- err = cs .Unlock (lockKey )
176
+ err = cs .Unlock (context . Background (), lockKey )
158
177
assert .NoError (t , err )
159
178
})
160
179
161
- err = cs2 .Lock (lockKey )
180
+ err = cs2 .Lock (context . Background (), lockKey )
162
181
assert .NoError (t , err )
163
182
164
- err = cs2 .Unlock (lockKey )
183
+ err = cs2 .Unlock (context . Background (), lockKey )
165
184
assert .NoError (t , err )
166
185
}
0 commit comments