2019-04-09 20:56:12 +00:00
|
|
|
package sakuracloud
|
|
|
|
|
|
|
|
import (
|
2022-08-04 13:56:46 +00:00
|
|
|
"context"
|
2019-04-09 20:56:12 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2022-08-04 13:56:46 +00: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 20:56:12 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
func setupTest(t *testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
t.Setenv("SAKURACLOUD_FAKE_MODE", "1")
|
|
|
|
|
|
|
|
createDummyZone(t, fakeCaller())
|
2019-04-09 20:56:12 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
func fakeCaller() iaas.APICaller {
|
|
|
|
return api.NewCallerWithOptions(&api.CallerOptions{
|
|
|
|
Options: &client.Options{
|
|
|
|
AccessToken: "dummy",
|
|
|
|
AccessTokenSecret: "dummy",
|
|
|
|
},
|
|
|
|
FakeMode: true,
|
|
|
|
})
|
2019-04-09 20:56:12 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
func createDummyZone(t *testing.T, caller iaas.APICaller) {
|
2021-11-01 23:52:38 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
ctx := context.Background()
|
2019-04-09 20:56:12 +00:00
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
dnsOp := iaas.NewDNSOp(caller)
|
2019-04-09 20:56:12 +00:00
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
// cleanup
|
|
|
|
zones, err := dnsOp.Find(ctx, &iaas.FindCondition{})
|
|
|
|
require.NoError(t, err)
|
2019-04-09 20:56:12 +00:00
|
|
|
|
2022-08-04 13:56:46 +00: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 20:56:12 +00:00
|
|
|
}
|
2021-11-01 23:52:38 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 13:56:46 +00: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 20:56:12 +00:00
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Token = "token1"
|
|
|
|
config.Secret = "secret1"
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
t.Run("addTXTRecord", func(t *testing.T) {
|
|
|
|
err = p.addTXTRecord("test.example.com", "example.com", "dummyValue", 10)
|
|
|
|
require.NoError(t, err)
|
2019-04-09 20:56:12 +00:00
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
updZone, e := p.getHostedZone("example.com")
|
|
|
|
require.NoError(t, e)
|
|
|
|
require.NotNil(t, updZone)
|
2019-04-09 20:56:12 +00:00
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
require.Len(t, updZone.Records, 1)
|
|
|
|
})
|
2019-04-09 20:56:12 +00:00
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
t.Run("cleanupTXTRecord", func(t *testing.T) {
|
|
|
|
err = p.cleanupTXTRecord("test.example.com", "example.com", "dummyValue")
|
|
|
|
require.NoError(t, err)
|
2019-04-09 20:56:12 +00:00
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
updZone, e := p.getHostedZone("example.com")
|
|
|
|
require.NoError(t, e)
|
|
|
|
require.NotNil(t, updZone)
|
2019-04-09 20:56:12 +00:00
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
require.Len(t, updZone.Records, 0)
|
|
|
|
})
|
2019-04-09 20:56:12 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
func TestDNSProvider_concurrentAddAndCleanupRecords(t *testing.T) {
|
|
|
|
setupTest(t)
|
2019-04-09 20:56:12 +00:00
|
|
|
|
|
|
|
dummyRecordCount := 10
|
|
|
|
|
|
|
|
var providers []*DNSProvider
|
|
|
|
for i := 0; i < dummyRecordCount; i++ {
|
|
|
|
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 13:56:46 +00:00
|
|
|
t.Run("addTXTRecord", func(t *testing.T) {
|
|
|
|
wg.Add(len(providers))
|
2019-04-09 20:56:12 +00:00
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
for i, p := range providers {
|
|
|
|
go func(j int, client *DNSProvider) {
|
|
|
|
err := client.addTXTRecord(fmt.Sprintf("test%d.example.com", j), "example.com", "dummyValue", 10)
|
|
|
|
require.NoError(t, err)
|
|
|
|
wg.Done()
|
|
|
|
}(i, p)
|
|
|
|
}
|
2019-04-09 20:56:12 +00:00
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
wg.Wait()
|
2019-04-09 20:56:12 +00:00
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
updZone, err := providers[0].getHostedZone("example.com")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, updZone)
|
2019-04-09 20:56:12 +00:00
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
require.Len(t, updZone.Records, dummyRecordCount)
|
|
|
|
})
|
2019-04-09 20:56:12 +00:00
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
t.Run("cleanupTXTRecord", func(t *testing.T) {
|
|
|
|
wg.Add(len(providers))
|
2019-04-09 20:56:12 +00:00
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
for i, p := range providers {
|
|
|
|
go func(i int, client *DNSProvider) {
|
|
|
|
err := client.cleanupTXTRecord(fmt.Sprintf("test%d.example.com", i), "example.com", "dummyValue")
|
|
|
|
require.NoError(t, err)
|
|
|
|
wg.Done()
|
|
|
|
}(i, p)
|
2019-04-09 20:56:12 +00:00
|
|
|
}
|
2021-11-01 23:52:38 +00:00
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
wg.Wait()
|
2019-04-09 20:56:12 +00:00
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
updZone, err := providers[0].getHostedZone("example.com")
|
2019-04-09 20:56:12 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-04 13:56:46 +00:00
|
|
|
require.NotNil(t, updZone)
|
2019-04-09 20:56:12 +00:00
|
|
|
|
2022-08-04 13:56:46 +00:00
|
|
|
require.Len(t, updZone.Records, 0)
|
|
|
|
})
|
2019-04-09 20:56:12 +00:00
|
|
|
}
|