2019-04-09 22:56:12 +02:00
|
|
|
package sakuracloud
|
|
|
|
|
|
|
|
import (
|
2022-08-04 15:56:46 +02:00
|
|
|
"context"
|
2019-04-09 22:56:12 +02:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2022-08-04 15:56:46 +02:00
|
|
|
client "github.com/sacloud/api-client-go"
|
|
|
|
"github.com/sacloud/iaas-api-go"
|
|
|
|
"github.com/sacloud/iaas-api-go/helper/api"
|
2019-04-09 22:56:12 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-08-04 15:56:46 +02:00
|
|
|
func setupTest(t *testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
t.Setenv("SAKURACLOUD_FAKE_MODE", "1")
|
|
|
|
|
|
|
|
createDummyZone(t, fakeCaller())
|
2019-04-09 22:56:12 +02:00
|
|
|
}
|
|
|
|
|
2022-08-04 15:56:46 +02:00
|
|
|
func fakeCaller() iaas.APICaller {
|
|
|
|
return api.NewCallerWithOptions(&api.CallerOptions{
|
|
|
|
Options: &client.Options{
|
|
|
|
AccessToken: "dummy",
|
|
|
|
AccessTokenSecret: "dummy",
|
|
|
|
},
|
|
|
|
FakeMode: true,
|
|
|
|
})
|
2019-04-09 22:56:12 +02:00
|
|
|
}
|
|
|
|
|
2022-08-04 15:56:46 +02:00
|
|
|
func createDummyZone(t *testing.T, caller iaas.APICaller) {
|
2021-11-02 01:52:38 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2022-08-04 15:56:46 +02:00
|
|
|
ctx := context.Background()
|
2019-04-09 22:56:12 +02:00
|
|
|
|
2022-08-04 15:56:46 +02:00
|
|
|
dnsOp := iaas.NewDNSOp(caller)
|
2019-04-09 22:56:12 +02:00
|
|
|
|
2022-08-04 15:56:46 +02:00
|
|
|
// cleanup
|
|
|
|
zones, err := dnsOp.Find(ctx, &iaas.FindCondition{})
|
|
|
|
require.NoError(t, err)
|
2019-04-09 22:56:12 +02:00
|
|
|
|
2022-08-04 15:56:46 +02:00
|
|
|
for _, zone := range zones.DNS {
|
|
|
|
if zone.Name == "example.com" {
|
|
|
|
err = dnsOp.Delete(ctx, zone.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
break
|
2019-04-09 22:56:12 +02:00
|
|
|
}
|
2021-11-02 01:52:38 +02:00
|
|
|
}
|
|
|
|
|
2022-08-04 15:56:46 +02:00
|
|
|
// create dummy zone
|
|
|
|
_, err = iaas.NewDNSOp(caller).Create(context.Background(), &iaas.DNSCreateRequest{Name: "example.com"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_addAndCleanupRecords(t *testing.T) {
|
|
|
|
setupTest(t)
|
2019-04-09 22:56:12 +02:00
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Token = "token1"
|
|
|
|
config.Secret = "secret1"
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-08-04 15:56:46 +02:00
|
|
|
t.Run("addTXTRecord", func(t *testing.T) {
|
2022-11-25 19:12:21 +02:00
|
|
|
err = p.addTXTRecord("test.example.com.", "dummyValue", 10)
|
2022-08-04 15:56:46 +02:00
|
|
|
require.NoError(t, err)
|
2019-04-09 22:56:12 +02:00
|
|
|
|
2022-11-25 19:12:21 +02:00
|
|
|
updZone, e := p.getHostedZone("test.example.com.")
|
2022-08-04 15:56:46 +02:00
|
|
|
require.NoError(t, e)
|
|
|
|
require.NotNil(t, updZone)
|
2019-04-09 22:56:12 +02:00
|
|
|
|
2022-08-04 15:56:46 +02:00
|
|
|
require.Len(t, updZone.Records, 1)
|
|
|
|
})
|
2019-04-09 22:56:12 +02:00
|
|
|
|
2022-08-04 15:56:46 +02:00
|
|
|
t.Run("cleanupTXTRecord", func(t *testing.T) {
|
2022-11-25 19:12:21 +02:00
|
|
|
err = p.cleanupTXTRecord("test.example.com.", "dummyValue")
|
2022-08-04 15:56:46 +02:00
|
|
|
require.NoError(t, err)
|
2019-04-09 22:56:12 +02:00
|
|
|
|
2022-11-25 19:12:21 +02:00
|
|
|
updZone, e := p.getHostedZone("test.example.com.")
|
2022-08-04 15:56:46 +02:00
|
|
|
require.NoError(t, e)
|
|
|
|
require.NotNil(t, updZone)
|
2019-04-09 22:56:12 +02:00
|
|
|
|
2023-10-31 15:08:50 +02:00
|
|
|
require.Empty(t, updZone.Records)
|
2022-08-04 15:56:46 +02:00
|
|
|
})
|
2019-04-09 22:56:12 +02:00
|
|
|
}
|
|
|
|
|
2022-08-04 15:56:46 +02:00
|
|
|
func TestDNSProvider_concurrentAddAndCleanupRecords(t *testing.T) {
|
|
|
|
setupTest(t)
|
2019-04-09 22:56:12 +02:00
|
|
|
|
|
|
|
dummyRecordCount := 10
|
|
|
|
|
|
|
|
var providers []*DNSProvider
|
2024-03-20 21:30:35 +02:00
|
|
|
for range dummyRecordCount {
|
2019-04-09 22:56:12 +02:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Token = "token3"
|
|
|
|
config.Secret = "secret3"
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
providers = append(providers, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2022-08-04 15:56:46 +02:00
|
|
|
t.Run("addTXTRecord", func(t *testing.T) {
|
|
|
|
wg.Add(len(providers))
|
2019-04-09 22:56:12 +02:00
|
|
|
|
2022-08-04 15:56:46 +02:00
|
|
|
for i, p := range providers {
|
|
|
|
go func(j int, client *DNSProvider) {
|
2022-11-25 19:12:21 +02:00
|
|
|
err := client.addTXTRecord(fmt.Sprintf("test%d.example.com.", j), "dummyValue", 10)
|
2022-08-04 15:56:46 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
wg.Done()
|
|
|
|
}(i, p)
|
|
|
|
}
|
2019-04-09 22:56:12 +02:00
|
|
|
|
2022-08-04 15:56:46 +02:00
|
|
|
wg.Wait()
|
2019-04-09 22:56:12 +02:00
|
|
|
|
2022-11-25 19:12:21 +02:00
|
|
|
updZone, err := providers[0].getHostedZone("example.com.")
|
2022-08-04 15:56:46 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, updZone)
|
2019-04-09 22:56:12 +02:00
|
|
|
|
2022-08-04 15:56:46 +02:00
|
|
|
require.Len(t, updZone.Records, dummyRecordCount)
|
|
|
|
})
|
2019-04-09 22:56:12 +02:00
|
|
|
|
2022-08-04 15:56:46 +02:00
|
|
|
t.Run("cleanupTXTRecord", func(t *testing.T) {
|
|
|
|
wg.Add(len(providers))
|
2019-04-09 22:56:12 +02:00
|
|
|
|
2022-08-04 15:56:46 +02:00
|
|
|
for i, p := range providers {
|
|
|
|
go func(i int, client *DNSProvider) {
|
2022-11-25 19:12:21 +02:00
|
|
|
err := client.cleanupTXTRecord(fmt.Sprintf("test%d.example.com.", i), "dummyValue")
|
2022-08-04 15:56:46 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
wg.Done()
|
|
|
|
}(i, p)
|
2019-04-09 22:56:12 +02:00
|
|
|
}
|
2021-11-02 01:52:38 +02:00
|
|
|
|
2022-08-04 15:56:46 +02:00
|
|
|
wg.Wait()
|
2019-04-09 22:56:12 +02:00
|
|
|
|
2022-11-25 19:12:21 +02:00
|
|
|
updZone, err := providers[0].getHostedZone("example.com.")
|
2019-04-09 22:56:12 +02:00
|
|
|
require.NoError(t, err)
|
2022-08-04 15:56:46 +02:00
|
|
|
require.NotNil(t, updZone)
|
2019-04-09 22:56:12 +02:00
|
|
|
|
2023-10-31 15:08:50 +02:00
|
|
|
require.Empty(t, updZone.Records)
|
2022-08-04 15:56:46 +02:00
|
|
|
})
|
2019-04-09 22:56:12 +02:00
|
|
|
}
|