3
3
package e2e
4
4
5
5
import (
6
+ "context"
6
7
"fmt"
7
8
"net"
8
9
"strconv"
@@ -12,21 +13,25 @@ import (
12
13
"github.com/stretchr/testify/assert"
13
14
"github.com/stretchr/testify/require"
14
15
"github.com/swaggest/assertjson"
16
+
17
+ "github.com/hetznercloud/hcloud-go/v2/hcloud"
15
18
)
16
19
17
20
func TestFloatingIP (t * testing.T ) {
18
21
t .Parallel ()
19
22
20
- _ , err := createFloatingIP (t , "" )
23
+ floatingIPName := withSuffix ("test-floating-ip" )
24
+
25
+ _ , err := createFloatingIP (t , floatingIPName , "" )
21
26
require .EqualError (t , err , "type is required" )
22
27
23
- _ , err = createFloatingIP (t , "ipv4" )
28
+ _ , err = createFloatingIP (t , floatingIPName , "ipv4" )
24
29
require .EqualError (t , err , "one of --home-location or --server is required" )
25
30
26
- _ , err = createFloatingIP (t , "ipv4" , "--server" , "non-existing-server" )
31
+ _ , err = createFloatingIP (t , floatingIPName , "ipv4" , "--server" , "non-existing-server" )
27
32
require .EqualError (t , err , "server not found: non-existing-server" )
28
33
29
- floatingIPId , err := createFloatingIP (t , "ipv4" , "--home-location" , TestLocationName )
34
+ floatingIPId , err := createFloatingIP (t , floatingIPName , "ipv4" , "--home-location" , TestLocationName )
30
35
if err != nil {
31
36
t .Fatal (err )
32
37
}
@@ -45,7 +50,9 @@ func TestFloatingIP(t *testing.T) {
45
50
})
46
51
})
47
52
48
- out , err := runCommand (t , "floating-ip" , "update" , strconv .Itoa (floatingIPId ), "--name" , "new-test-floating-ip" , "--description" , "Some description" )
53
+ floatingIPName = withSuffix ("new-test-floating-ip" )
54
+
55
+ out , err := runCommand (t , "floating-ip" , "update" , strconv .Itoa (floatingIPId ), "--name" , floatingIPName , "--description" , "Some description" )
49
56
require .NoError (t , err )
50
57
assert .Equal (t , fmt .Sprintf ("Floating IP %d updated\n " , floatingIPId ), out )
51
58
@@ -94,7 +101,7 @@ func TestFloatingIP(t *testing.T) {
94
101
require .NoError (t , err )
95
102
assert .Regexp (t , `ID:\s+[0-9]+
96
103
Type:\s+ipv4
97
- Name:\s+new-test-floating-ip
104
+ Name:\s+new-test-floating-ip-[0-9a-f]{8}
98
105
Description:\s+Some description
99
106
Created:.*?
100
107
IP:\s+(?:[0-9]{1,3}\.){3}[0-9]{1,3}
@@ -113,7 +120,7 @@ Labels:
113
120
out , err = runCommand (t , "floating-ip" , "list" , "--output" , "columns=id,name,type,ip,dns,server,home,blocked,protection,labels,created,age" )
114
121
require .NoError (t , err )
115
122
assert .Regexp (t , `^ID +NAME +TYPE +IP +DNS +SERVER +HOME +BLOCKED +PROTECTION +LABELS +CREATED +AGE
116
- [0-9]+ +new-test-floating-ip +ipv4 +(?:[0-9]{1,3}\.){3}[0-9]{1,3} +s1\.example\.com +- +[a-z]{3}[0-9]* +no +delete +foo=bar.*?
123
+ [0-9]+ +new-test-floating-ip-[0-9a-f]{8} +ipv4 +(?:[0-9]{1,3}\.){3}[0-9]{1,3} +s1\.example\.com +- +[a-z]{3}[0-9]* +no +delete +foo=bar.*?
117
124
$` , out )
118
125
119
126
out , err = runCommand (t , "floating-ip" , "list" , "-o=json" )
@@ -150,10 +157,10 @@ $`, out)
150
157
"labels": {
151
158
"foo": "bar"
152
159
},
153
- "name": "new-test-floating-ip "
160
+ "name": "%s "
154
161
}
155
162
]
156
- ` , floatingIPId , ipStr , ipStr , TestLocationName )), []byte (out ))
163
+ ` , floatingIPId , ipStr , ipStr , TestLocationName , floatingIPName )), []byte (out ))
157
164
158
165
out , err = runCommand (t , "floating-ip" , "delete" , strconv .Itoa (floatingIPId ))
159
166
assert .Regexp (t , `^Floating IP deletion is protected \(protected, [0-9a-f]+\)$` , err .Error ())
@@ -183,7 +190,8 @@ $`, out)
183
190
require .NoError (t , err )
184
191
assert .Equal (t , fmt .Sprintf ("Floating IP %d deleted\n " , floatingIPId ), out )
185
192
186
- floatingIPId , err = createFloatingIP (t , "ipv6" , "--home-location" , TestLocationName )
193
+ floatingIPName = withSuffix ("test-floating-ipv6" )
194
+ floatingIPId , err = createFloatingIP (t , floatingIPName , "ipv6" , "--home-location" , TestLocationName )
187
195
if err != nil {
188
196
t .Fatal (err )
189
197
}
@@ -192,7 +200,7 @@ $`, out)
192
200
require .NoError (t , err )
193
201
assert .Regexp (t , `ID:\s+[0-9]+
194
202
Type:\s+ipv6
195
- Name:\s+test-floating-ip
203
+ Name:\s+test-floating-ipv6-[0-9a-f]{8}
196
204
Description:\s+-
197
205
Created:.*?
198
206
IP:\s+[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+::\/64
@@ -235,8 +243,13 @@ Labels:
235
243
assert .Equal (t , fmt .Sprintf ("Floating IP %d deleted\n " , floatingIPId ), out )
236
244
}
237
245
238
- func createFloatingIP (t * testing.T , ipType string , args ... string ) (int , error ) {
239
- out , err := runCommand (t , append ([]string {"floating-ip" , "create" , "--name" , "test-floating-ip" , "--type" , ipType }, args ... )... )
246
+ func createFloatingIP (t * testing.T , name , ipType string , args ... string ) (int , error ) {
247
+ t .Helper ()
248
+ t .Cleanup (func () {
249
+ _ , _ = client .FloatingIP .Delete (context .Background (), & hcloud.FloatingIP {Name : name })
250
+ })
251
+
252
+ out , err := runCommand (t , append ([]string {"floating-ip" , "create" , "--name" , name , "--type" , ipType }, args ... )... )
240
253
if err != nil {
241
254
return 0 , err
242
255
}
@@ -250,5 +263,9 @@ func createFloatingIP(t *testing.T, ipType string, args ...string) (int, error)
250
263
if err != nil {
251
264
return 0 , err
252
265
}
266
+
267
+ t .Cleanup (func () {
268
+ _ , _ = client .FloatingIP .Delete (context .Background (), & hcloud.FloatingIP {ID : int64 (id )})
269
+ })
253
270
return id , nil
254
271
}
0 commit comments