forked from hpi-schul-cloud/infra-otc-cert-manager-webhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
528 lines (450 loc) · 15.7 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// The tests in this file test the ability to utilize the otc dns api.
// - Creating a otc dns client.
// - Reading zone information
// - Reading dns records
// - Creating and deleting a TXT record.
//
// VSCode users: Add { ... "go.testFlags": ["-v"] ... } to your settings.json to view log output for non failing tests.
//
// Be aware that some DNS operations may have a delay.
//
package otcdns
import (
"fmt"
"log"
"os"
"testing"
"time"
// otctools "github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
otc "github.com/opentelekomcloud/gophertelekomcloud"
otctools "github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/dns/v2/recordsets"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/dns/v2/zones"
"github.com/stretchr/testify/assert"
)
const (
testSubdomain = "cert-manager-dns01-tests"
sleepTime = 2 * time.Second
)
//
// Allows to overwrite the default value with a custom value.
//
func getTestZone() string {
var testZone string = "hpi-schul-cloud.dev."
if os.Getenv("TEST_ZONE_NAME") == "" {
return testZone
} else {
return os.Getenv("TEST_ZONE_NAME")
}
}
// ===========================================================================
// Client and Zones
// ===========================================================================
//
// Tests, if we can create a otcdns client and if we can retrieve at least
// one zone record.
//
func TestClientCreateWithUser(t *testing.T) {
t.Log("TestClientCreateWithUser start")
// We load the cloud config here, to circumvent that we store duplicated configuration data only.
// The testcase will be independent from the environment and clouds.yaml configuration.
cloudsConfig, err := getCloudProfile(OtcProfileNameUser)
if err != nil {
log.Fatalf("Test preconditions not met. A local clouds.yaml must exist: %s", err)
}
// Prepare our test parameters from cloud config.
authOpts := otc.AuthOptions{
IdentityEndpoint: cloudsConfig.AuthInfo.AuthURL,
Username: cloudsConfig.AuthInfo.Username,
Password: cloudsConfig.AuthInfo.Password,
DomainName: cloudsConfig.AuthInfo.DomainName,
TenantID: cloudsConfig.AuthInfo.ProjectID,
}
endpointOpts := otc.EndpointOpts{
Region: cloudsConfig.RegionName,
}
// Now we can start the test
// Create a client
client, err := NewDNSV2ClientWithAuth(authOpts, endpointOpts)
if err != nil {
t.Fatalf("Unable to create a DNS client: %v", err)
}
runZoneTest(t, client)
t.Log("TestClientCreateWithUser end")
}
//
// Tests, if we can create a otcdns client and if we can retrieve at least
// one zone record.
//
func TestClientCreateWithAkSk(t *testing.T) {
t.Log("TestClientCreateWithTokenAuth start")
// We load the cloud config here, to circumvent that we store duplicated configuration data only.
// The testcase will be independent from the environment and clouds.yaml configuration.
cloudsConfig, err := getCloudProfile(OtcProfileNameAkSk)
if err != nil {
log.Fatalf("Test preconditions not met. A local clouds.yaml must exist: %s", err)
}
// Prepare our test parameters from cloud config.
authOpts := otc.AKSKAuthOptions{
IdentityEndpoint: cloudsConfig.AuthInfo.AuthURL,
AccessKey: cloudsConfig.AuthInfo.AccessKey,
SecretKey: cloudsConfig.AuthInfo.SecretKey,
}
endpointOpts := otc.EndpointOpts{
Region: cloudsConfig.RegionName,
}
// Now we can start the test
// Create a client
client, err := NewDNSV2ClientWithAuth(authOpts, endpointOpts)
if err != nil {
t.Fatalf("Unable to create a DNS client: %v", err)
}
runZoneTest(t, client)
t.Log("TestClientCreateWithTokenAuth end")
}
//
// Tests, if we can create a otcdns client and if we can retrieve at least
// one zone record.
//
// In this case we utilize the configuration that is given by the environment / clouds.yaml.
// We can directly start the test.
//
func TestClientCreateWithCloudConfig(t *testing.T) {
t.Log("TestClientCreateWithZoneList start")
// Create a client
client, err := NewDNSV2Client()
if err != nil {
t.Fatalf("Unable to create a DNS client: %v", err)
}
runZoneTest(t, client)
t.Log("TestClientCreateWithZoneList end")
}
func runZoneTest(t *testing.T, client *OtcDnsClient) {
client.Subdomain = testSubdomain
var allZones []zones.Zone
allPages, err := zones.List(client.Sc, nil).AllPages()
if err != nil {
t.Fatalf("Unable to retrieve zones: %v", err)
}
allZones, err = zones.ExtractZones(allPages)
if err != nil {
t.Fatalf("Unable to extract zones: %v", err)
}
// Details for debugging
//for _, zone := range allZones {
// otctools.PrintResource(t, &zone)
//}
t.Log(fmt.Sprintf("Number of zones in %s: %d", getTestZone(), len(allZones)))
// Test, if we have received more than 0 zone records.
assert.Greater(t, len(allZones), 0, "There should be more than one zone entry for the testdomain.")
}
//
//
//
func TestGetDevZone(t *testing.T) {
t.Log("TestGetDevZone start")
client, err := NewDNSV2Client()
if err != nil {
t.Fatalf("Unable to create a DNS client: %v", err)
}
client.Subdomain = testSubdomain
var pZone *zones.Zone
pZone, err = client.GetHostedZone(getTestZone())
if err != nil {
t.Fatalf("Unable to extract zones: %v", err)
}
// Details for debugging
// otctools.PrintResource(t, pZone)
assert.Equal(t, getTestZone(), pZone.Name, "The queried zone name must be the name of the test zone.")
t.Log("TestGetDevZone end")
}
// ===========================================================================
// RecordSets 1/2
// ===========================================================================
//
// Deletes the whole recordset.
//
// If needed a recordset to delete is created by the test.
//
func TestDeleteTxtRecordSetOnly(t *testing.T) {
t.Log("TestDeleteTxtRecordSetOnly start")
client, err := NewDNSV2Client()
if err != nil {
t.Fatalf("Unable to create a DNS client: %v", err)
}
client.Subdomain = testSubdomain
pZone, err := client.GetHostedZone(getTestZone())
if err != nil {
t.Fatalf("Unable to get zone entry: %s", err)
}
testRecordset, err := client.GetTxtRecordSet(pZone)
if err != nil {
t.Fatalf("Unable to get TXT entry: %s", err)
}
if testRecordset != nil {
// otctools.PrintResource(t, testRecordset)
err = client.DeleteRecordSet(pZone, testRecordset)
if err != nil {
t.Fatalf("Unable to delete recordset: %s", err)
}
}
t.Log("TestDeleteTxtRecordSetOnly end")
}
//
// Before we start our testsuite. Check, that the test record does not exist.
//
func TestHasTxtRecordSetMustNotExist(t *testing.T) {
t.Log("TestHasTxtRecordSetMustNotExist start")
t.Log("It is expected that this is the first test to run and that the test record is not created yet.")
t.Log("If this fails, try running TestDeleteTxtRecordSet.")
client, err := NewDNSV2Client()
if err != nil {
t.Fatalf("Unable to create a DNS client: %v", err)
}
client.Subdomain = testSubdomain
pZone, err := client.GetHostedZone(getTestZone())
if err != nil {
t.Fatalf("Unable to get zone entry: %s", err)
}
recordSetExists, err := client.HasTxtRecordSet(pZone)
if err != nil {
t.Fatalf("Unable to get TXT entry: %s", err)
}
// otctools.PrintResource(t, recordSetExists)
assert.Equal(t, false, recordSetExists, "The record must not exist.")
t.Log("TestHasTxtRecordSetMustNotExist end")
}
//
// With this test we create our test record, which will be queried and manipulated in the following tests.
//
func TestNewTxtRecordSet(t *testing.T) {
t.Log("TestNewTxtRecordSet start")
client, err := NewDNSV2Client()
if err != nil {
t.Fatalf("Unable to create a DNS client: %v", err)
}
client.Subdomain = testSubdomain
pZone, err := client.GetHostedZone(getTestZone())
if err != nil {
t.Fatalf("Unable to extract zones: %v", err)
}
txtValue := fmt.Sprintf("\"challenge test value %d\"", time.Now().UnixNano())
pCreatedRecordset, err := client.NewTxtRecordSet(pZone, txtValue)
if err != nil {
t.Fatalf("Unable to create TXT entry: %s", err)
}
// Details for debugging
// otctools.PrintResource(t, pCreatedRecordset)
assert.Equal(t, txtValue, pCreatedRecordset.Records[0], "The queried record entry must match the desired value.")
t.Log("TestNewTxtRecordSet end")
}
//
// Query the test record, created in the TestNewTxtRecordSet test.
//
func TestGetTxtRecordSet(t *testing.T) {
t.Log("TestGetTxtRecordSet start")
client, err := NewDNSV2Client()
if err != nil {
t.Fatalf("Unable to create a DNS client: %v", err)
}
client.Subdomain = testSubdomain
pZone, err := client.GetHostedZone(getTestZone())
if err != nil {
t.Fatalf("Unable to get zone entry: %s", err)
}
var pRecordset *recordsets.RecordSet
pRecordset, err = client.GetTxtRecordSet(pZone)
if err != nil {
t.Fatalf("Unable to get TXT entry: %s", err)
}
time.Sleep(sleepTime)
// Details for debugging
//otctools.PrintResource(t, pRecordset)
assert.Equal(t, getTestZone(), pRecordset.ZoneName, "The queried zone name must match the test zone.")
assert.Equal(t, "TXT", pRecordset.Type, "The queried record type must match TXT.")
assert.Equal(t, "ACTIVE", pRecordset.Status, "The queried record status must match ACTIVE not PENDING or PENDING_CREATE.")
t.Log("TestGetTxtRecordSet end")
}
//
// Exists test for the test record, created in the TestNewTxtRecordSet test.
//
func TestHasTxtRecordSet(t *testing.T) {
t.Log("TestHasTxtRecordSet start")
client, err := NewDNSV2Client()
if err != nil {
t.Fatalf("Unable to create a DNS client: %v", err)
}
client.Subdomain = testSubdomain
pZone, err := client.GetHostedZone(getTestZone())
if err != nil {
t.Fatalf("Unable to get zone entry: %s", err)
}
recordSetExists, err := client.HasTxtRecordSet(pZone)
if err != nil {
t.Fatalf("Unable to get TXT entry: %s", err)
}
// otctools.PrintResource(t, recordSetExists)
assert.Equal(t, true, recordSetExists, "The record must exist.")
t.Log("TestHasTxtRecordSet end")
}
// ===========================================================================
// Records in the Recordsets
// ===========================================================================
//
// Appends a value record to the already existing one.
// After this we have a TXT entry with two value records.
//
func TestUpdateTxtRecordSetAddValue(t *testing.T) {
t.Log("TestUpdateTxtRecordSetAddValue start")
client, err := NewDNSV2Client()
if err != nil {
t.Fatalf("Unable to create a DNS client: %v", err)
}
client.Subdomain = testSubdomain
zone, err := client.GetHostedZone(getTestZone())
if err != nil {
t.Fatalf("Unable to get zone entry: %s", err)
}
existingRecordset, err := client.GetTxtRecordSet(zone)
if err != nil {
t.Fatalf("Unable to get TXT entry: %s", err)
}
// otctools.PrintResource(t, existingRecordset.Records)
if len(existingRecordset.Records) != 1 {
t.Fatalf("There must be one value records. Test preconditions not given. %d", len(existingRecordset.Records))
}
txtValue := fmt.Sprintf("\"challenge test value %d\"", time.Now().UnixNano())
changedRecords := append(existingRecordset.Records, txtValue)
changedRecordset, err := client.UpdateTxtRecordValues(zone, existingRecordset, changedRecords)
if err != nil {
t.Fatalf("Unable to update TXT entry: %s", err)
}
// otctools.PrintResource(t, changedRecordset)
assert.Equal(t, 2, len(changedRecordset.Records), "There must be two value records. One was added.")
t.Log("TestUpdateTxtRecordSetAddValue end")
}
//
//
//
func TestDeleteTxtRecordValue(t *testing.T) {
t.Log("TestDeleteTxtRecordValue start")
time.Sleep(sleepTime)
client, err := NewDNSV2Client()
if err != nil {
t.Fatalf("Unable to create a DNS client: %v", err)
}
client.Subdomain = testSubdomain
zone, err := client.GetHostedZone(getTestZone())
if err != nil {
t.Fatalf("Unable to get zone entry: %s", err)
}
existingRecordset, err := client.GetTxtRecordSet(zone)
if err != nil {
t.Fatalf("Unable to get TXT entry: %s", err)
}
var originalNumberOfRecords int = len(existingRecordset.Records)
if originalNumberOfRecords < 1 {
t.Fatalf("There must be at least one value record. Test preconditions not given. Run TestUpdateTxtRecordSetAddValue to add one value. %d", originalNumberOfRecords)
}
deleteValue := existingRecordset.Records[0]
t.Log(fmt.Sprintf("Deleting value: %s", deleteValue))
changedRecordset, err := client.DeleteTxtRecordValue(zone, deleteValue, false)
if err != nil {
t.Fatalf("Unable to update TXT entry: %s", err)
}
var changedNumberOfRecords int = len(changedRecordset.Records)
t.Log(fmt.Sprintf("Original number of records: %d, Changed number of records: %d", originalNumberOfRecords, changedNumberOfRecords))
// otctools.PrintResource(t, changedRecordset)
assert.Equal(t, originalNumberOfRecords, changedNumberOfRecords+1, "There must be one record less after value deletion.")
t.Log("TestDeleteTxtRecordValue end")
}
//
//
//
func TestDeleteTxtRecordValueLastOne(t *testing.T) {
t.Log("TestDeleteTxtRecordValueLastOne start")
client, err := NewDNSV2Client()
if err != nil {
t.Fatalf("Unable to create a DNS client: %v", err)
}
client.Subdomain = testSubdomain
zone, err := client.GetHostedZone(getTestZone())
if err != nil {
t.Fatalf("Unable to get zone entry: %s", err)
}
existingRecordset, err := client.GetTxtRecordSet(zone)
if err != nil {
t.Fatalf("Unable to get TXT entry: %s", err)
}
if existingRecordset == nil {
t.Fatalf("There must be a test record created. Test preconditions not given. Call TestNewTxtRecordSet to create one.")
}
var originalNumberOfRecords int = len(existingRecordset.Records)
if originalNumberOfRecords != 1 {
t.Fatalf("There must be at exactly one value record. Test preconditions not given. %d", originalNumberOfRecords)
}
deleteValue := existingRecordset.Records[0]
t.Log(fmt.Sprintf("Deleting value: %s. Record count: %d", deleteValue, len(existingRecordset.Records)))
{
var pChangedRecordset *recordsets.RecordSet = nil
var err error
pChangedRecordset, err = client.DeleteTxtRecordValue(zone, deleteValue, true)
if err != nil {
t.Fatalf("Unable to update TXT entry: %s", err)
}
// Must print "tools.go:<line>: null"
otctools.PrintResource(t, pChangedRecordset)
var isNil bool = (pChangedRecordset == nil)
assert.Equal(t, true, isNil, "The returned recordset must nil1.")
// I don't know why this does not work:
// assert.Equal(t, true, pChangedRecordset, "The returned recordset must nil1.")
// It reports:
// Error: Not equal:
// expected: <nil>(<nil>)
// actual : *recordsets.RecordSet((*recordsets.RecordSet)(nil))
}
t.Log("TestDeleteTxtRecordValueLastOne end")
}
// ===========================================================================
// RecordSets 2/2
// ===========================================================================
//
// Deletes the whole recordset.
//
// If needed a recordset to delete is created by the test.
//
func TestCreateGetDeleteTxtRecordSet(t *testing.T) {
t.Log("TestCreateGetDeleteTxtRecordSet start")
client, err := NewDNSV2Client()
if err != nil {
t.Fatalf("Unable to create a DNS client: %v", err)
}
client.Subdomain = testSubdomain
pZone, err := client.GetHostedZone(getTestZone())
if err != nil {
t.Fatalf("Unable to get zone entry: %s", err)
}
testRecordsetExist, err := client.HasTxtRecordSet(pZone)
if err != nil {
t.Fatalf("Unable to check existence of test recordset: %s", err)
}
var testRecordset *recordsets.RecordSet
if testRecordsetExist {
testRecordset, err = client.GetTxtRecordSet(pZone)
if err != nil {
t.Fatalf("Unable to get TXT entry: %s", err)
}
} else {
txtValue := fmt.Sprintf("\"challenge test value %d\"", time.Now().UnixNano())
testRecordset, err = client.NewTxtRecordSet(pZone, txtValue)
if err != nil {
t.Fatalf("Unable to create TXT entry: %s", err)
}
}
// otctools.PrintResource(t, pRecordset)
err = client.DeleteRecordSet(pZone, testRecordset)
if err != nil {
t.Fatalf("Unable to delete recordset: %s", err)
}
t.Log("TestCreateGetDeleteTxtRecordSet end")
}