sigs.k8s.io/external-dns@v0.14.1/provider/tencentcloud/tencent_cloud_test.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package tencentcloud
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
    24  	dnspod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod/v20210323"
    25  	"sigs.k8s.io/external-dns/endpoint"
    26  	"sigs.k8s.io/external-dns/plan"
    27  	"sigs.k8s.io/external-dns/provider"
    28  	"sigs.k8s.io/external-dns/provider/tencentcloud/cloudapi"
    29  
    30  	privatedns "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns/v20201028"
    31  )
    32  
    33  func NewMockTencentCloudProvider(domainFilter endpoint.DomainFilter, zoneIDFilter provider.ZoneIDFilter, zoneType string) *TencentCloudProvider {
    34  	cfg := tencentCloudConfig{
    35  		// SecretId:  "",
    36  		// SecretKey: "",
    37  		RegionId: "ap-shanghai",
    38  		VPCId:    "vpc-abcdefg",
    39  	}
    40  
    41  	zoneId1 := common.StringPtr(cloudapi.RandStringRunes(8))
    42  
    43  	privateZones := []*privatedns.PrivateZone{
    44  		{
    45  			ZoneId: zoneId1,
    46  			Domain: common.StringPtr("external-dns-test.com"),
    47  			VpcSet: []*privatedns.VpcInfo{
    48  				{
    49  					UniqVpcId: common.StringPtr("vpc-abcdefg"),
    50  					Region:    common.StringPtr("ap-shanghai"),
    51  				},
    52  			},
    53  		},
    54  	}
    55  
    56  	zoneRecordId1 := common.StringPtr(cloudapi.RandStringRunes(8))
    57  	zoneRecordId2 := common.StringPtr(cloudapi.RandStringRunes(8))
    58  	privateZoneRecords := map[string][]*privatedns.PrivateZoneRecord{
    59  		*zoneId1: {
    60  			{
    61  				ZoneId:      zoneId1,
    62  				RecordId:    zoneRecordId1,
    63  				SubDomain:   common.StringPtr("nginx"),
    64  				RecordType:  common.StringPtr("TXT"),
    65  				RecordValue: common.StringPtr("heritage=external-dns,external-dns/owner=default"),
    66  				TTL:         common.Int64Ptr(300),
    67  			},
    68  			{
    69  				ZoneId:      zoneId1,
    70  				RecordId:    zoneRecordId2,
    71  				SubDomain:   common.StringPtr("nginx"),
    72  				RecordType:  common.StringPtr("A"),
    73  				RecordValue: common.StringPtr("10.10.10.10"),
    74  				TTL:         common.Int64Ptr(300),
    75  			},
    76  		},
    77  	}
    78  
    79  	dnsDomainId1 := common.Uint64Ptr(cloudapi.RandUint64())
    80  	dnsPodDomains := []*dnspod.DomainListItem{
    81  		{
    82  			DomainId: dnsDomainId1,
    83  			Name:     common.StringPtr("external-dns-test.com"),
    84  		},
    85  	}
    86  	dnsDomainRecordId1 := common.Uint64Ptr(cloudapi.RandUint64())
    87  	dnsDomainRecordId2 := common.Uint64Ptr(cloudapi.RandUint64())
    88  	dnspodRecords := map[string][]*dnspod.RecordListItem{
    89  		"external-dns-test.com": {
    90  			{
    91  				RecordId: dnsDomainRecordId1,
    92  				Value:    common.StringPtr("heritage=external-dns,external-dns/owner=default"),
    93  				Name:     common.StringPtr("nginx"),
    94  				Type:     common.StringPtr("TXT"),
    95  				TTL:      common.Uint64Ptr(300),
    96  			},
    97  			{
    98  				RecordId: dnsDomainRecordId2,
    99  				Name:     common.StringPtr("nginx"),
   100  				Type:     common.StringPtr("A"),
   101  				Value:    common.StringPtr("10.10.10.10"),
   102  				TTL:      common.Uint64Ptr(300),
   103  			},
   104  		},
   105  	}
   106  
   107  	var apiService cloudapi.TencentAPIService = cloudapi.NewMockService(privateZones, privateZoneRecords, dnsPodDomains, dnspodRecords)
   108  
   109  	tencentCloudProvider := &TencentCloudProvider{
   110  		domainFilter: domainFilter,
   111  		zoneIDFilter: zoneIDFilter,
   112  		apiService:   apiService,
   113  		vpcID:        cfg.VPCId,
   114  		privateZone:  zoneType == "private",
   115  	}
   116  
   117  	return tencentCloudProvider
   118  }
   119  
   120  func TestTencentPrivateProvider_Records(t *testing.T) {
   121  	p := NewMockTencentCloudProvider(endpoint.NewDomainFilter([]string{"external-dns-test.com"}), provider.NewZoneIDFilter([]string{}), "private")
   122  	endpoints, err := p.Records(context.Background())
   123  	if err != nil {
   124  		t.Errorf("Failed to get records: %v", err)
   125  	} else {
   126  		if len(endpoints) != 2 {
   127  			t.Errorf("Incorrect number of records: %d", len(endpoints))
   128  		}
   129  		for _, endpoint := range endpoints {
   130  			t.Logf("Endpoint for %+v", *endpoint)
   131  		}
   132  	}
   133  
   134  	// Test for Create、UpdateOld、UpdateNew、Delete
   135  	// The base record will be created.
   136  	changes := &plan.Changes{
   137  		Create: []*endpoint.Endpoint{
   138  			{
   139  				DNSName:    "redis.external-dns-test.com",
   140  				RecordType: "A",
   141  				RecordTTL:  300,
   142  				Targets:    endpoint.NewTargets("4.3.2.1"),
   143  			},
   144  		},
   145  		UpdateOld: []*endpoint.Endpoint{
   146  			{
   147  				DNSName:    "nginx.external-dns-test.com",
   148  				RecordType: "A",
   149  				RecordTTL:  300,
   150  				Targets:    endpoint.NewTargets("10.10.10.10"),
   151  			},
   152  		},
   153  		UpdateNew: []*endpoint.Endpoint{
   154  			{
   155  				DNSName:    "tencent.external-dns-test.com",
   156  				RecordType: "A",
   157  				RecordTTL:  600,
   158  				Targets:    endpoint.NewTargets("1.2.3.4", "5.6.7.8"),
   159  			},
   160  		},
   161  		Delete: []*endpoint.Endpoint{
   162  			{
   163  				DNSName:    "nginx.external-dns-test.com",
   164  				RecordType: "TXT",
   165  				RecordTTL:  300,
   166  				Targets:    endpoint.NewTargets("\"heritage=external-dns,external-dns/owner=default\""),
   167  			},
   168  		},
   169  	}
   170  	if err := p.ApplyChanges(context.Background(), changes); err != nil {
   171  		t.Errorf("Failed to get records: %v", err)
   172  	}
   173  	endpoints, err = p.Records(context.Background())
   174  	if err != nil {
   175  		t.Errorf("Failed to get records: %v", err)
   176  	} else {
   177  		if len(endpoints) != 3 {
   178  			t.Errorf("Incorrect number of records: %d", len(endpoints))
   179  		}
   180  		for _, endpoint := range endpoints {
   181  			t.Logf("Endpoint for %+v", *endpoint)
   182  		}
   183  	}
   184  
   185  	// Test for Delete one target
   186  	changes = &plan.Changes{
   187  		Delete: []*endpoint.Endpoint{
   188  			{
   189  				DNSName:    "tencent.external-dns-test.com",
   190  				RecordType: "A",
   191  				RecordTTL:  600,
   192  				Targets:    endpoint.NewTargets("5.6.7.8"),
   193  			},
   194  		},
   195  	}
   196  	if err := p.ApplyChanges(context.Background(), changes); err != nil {
   197  		t.Errorf("Failed to get records: %v", err)
   198  	}
   199  	endpoints, err = p.Records(context.Background())
   200  	if err != nil {
   201  		t.Errorf("Failed to get records: %v", err)
   202  	} else {
   203  		if len(endpoints) != 3 {
   204  			t.Errorf("Incorrect number of records: %d", len(endpoints))
   205  		}
   206  		for _, endpoint := range endpoints {
   207  			t.Logf("Endpoint for %+v", *endpoint)
   208  		}
   209  	}
   210  
   211  	// Test for Delete another target
   212  	changes = &plan.Changes{
   213  		Create: []*endpoint.Endpoint{
   214  			{
   215  				DNSName:    "redis.external-dns-test.com",
   216  				RecordType: "A",
   217  				RecordTTL:  300,
   218  				Targets:    endpoint.NewTargets("5.6.7.8"),
   219  			},
   220  		},
   221  	}
   222  	if err := p.ApplyChanges(context.Background(), changes); err != nil {
   223  		t.Errorf("Failed to get records: %v", err)
   224  	}
   225  	endpoints, err = p.Records(context.Background())
   226  	if err != nil {
   227  		t.Errorf("Failed to get records: %v", err)
   228  	} else {
   229  		if len(endpoints) != 3 {
   230  			t.Errorf("Incorrect number of records: %d", len(endpoints))
   231  		}
   232  		for _, endpoint := range endpoints {
   233  			t.Logf("Endpoint for %+v", *endpoint)
   234  		}
   235  	}
   236  
   237  	// Test for Delete another target
   238  	changes = &plan.Changes{
   239  		Delete: []*endpoint.Endpoint{
   240  			{
   241  				DNSName:    "tencent.external-dns-test.com",
   242  				RecordType: "A",
   243  				RecordTTL:  600,
   244  				Targets:    endpoint.NewTargets("1.2.3.4"),
   245  			},
   246  		},
   247  	}
   248  	if err := p.ApplyChanges(context.Background(), changes); err != nil {
   249  		t.Errorf("Failed to get records: %v", err)
   250  	}
   251  	endpoints, err = p.Records(context.Background())
   252  	if err != nil {
   253  		t.Errorf("Failed to get records: %v", err)
   254  	} else {
   255  		if len(endpoints) != 2 {
   256  			t.Errorf("Incorrect number of records: %d", len(endpoints))
   257  		}
   258  		for _, endpoint := range endpoints {
   259  			t.Logf("Endpoint for %+v", *endpoint)
   260  		}
   261  	}
   262  }
   263  
   264  func TestTencentPublicProvider_Records(t *testing.T) {
   265  	p := NewMockTencentCloudProvider(endpoint.NewDomainFilter([]string{"external-dns-test.com"}), provider.NewZoneIDFilter([]string{}), "public")
   266  	endpoints, err := p.Records(context.Background())
   267  	if err != nil {
   268  		t.Errorf("Failed to get records: %v", err)
   269  	} else {
   270  		if len(endpoints) != 2 {
   271  			t.Errorf("Incorrect number of records: %d", len(endpoints))
   272  		}
   273  		for _, endpoint := range endpoints {
   274  			t.Logf("Endpoint for %+v", *endpoint)
   275  		}
   276  	}
   277  
   278  	// Test for Create、UpdateOld、UpdateNew、Delete
   279  	changes := &plan.Changes{
   280  		Create: []*endpoint.Endpoint{
   281  			{
   282  				DNSName:    "redis.external-dns-test.com",
   283  				RecordType: "A",
   284  				RecordTTL:  300,
   285  				Targets:    endpoint.NewTargets("4.3.2.1"),
   286  			},
   287  		},
   288  		UpdateOld: []*endpoint.Endpoint{
   289  			{
   290  				DNSName:    "nginx.external-dns-test.com",
   291  				RecordType: "A",
   292  				RecordTTL:  300,
   293  				Targets:    endpoint.NewTargets("10.10.10.10"),
   294  			},
   295  		},
   296  		UpdateNew: []*endpoint.Endpoint{
   297  			{
   298  				DNSName:    "tencent.external-dns-test.com",
   299  				RecordType: "A",
   300  				RecordTTL:  600,
   301  				Targets:    endpoint.NewTargets("1.2.3.4", "5.6.7.8"),
   302  			},
   303  		},
   304  		Delete: []*endpoint.Endpoint{
   305  			{
   306  				DNSName:    "nginx.external-dns-test.com",
   307  				RecordType: "TXT",
   308  				RecordTTL:  300,
   309  				Targets:    endpoint.NewTargets("\"heritage=external-dns,external-dns/owner=default\""),
   310  			},
   311  		},
   312  	}
   313  	if err := p.ApplyChanges(context.Background(), changes); err != nil {
   314  		t.Errorf("Failed to get records: %v", err)
   315  	}
   316  	endpoints, err = p.Records(context.Background())
   317  	if err != nil {
   318  		t.Errorf("Failed to get records: %v", err)
   319  	} else {
   320  		if len(endpoints) != 2 {
   321  			t.Errorf("Incorrect number of records: %d", len(endpoints))
   322  		}
   323  		for _, endpoint := range endpoints {
   324  			t.Logf("Endpoint for %+v", *endpoint)
   325  		}
   326  	}
   327  
   328  	// Test for Delete one target
   329  	changes = &plan.Changes{
   330  		Delete: []*endpoint.Endpoint{
   331  			{
   332  				DNSName:    "tencent.external-dns-test.com",
   333  				RecordType: "A",
   334  				RecordTTL:  600,
   335  				Targets:    endpoint.NewTargets("5.6.7.8"),
   336  			},
   337  		},
   338  	}
   339  	if err := p.ApplyChanges(context.Background(), changes); err != nil {
   340  		t.Errorf("Failed to get records: %v", err)
   341  	}
   342  	endpoints, err = p.Records(context.Background())
   343  	if err != nil {
   344  		t.Errorf("Failed to get records: %v", err)
   345  	} else {
   346  		if len(endpoints) != 2 {
   347  			t.Errorf("Incorrect number of records: %d", len(endpoints))
   348  		}
   349  		for _, endpoint := range endpoints {
   350  			t.Logf("Endpoint for %+v", *endpoint)
   351  		}
   352  	}
   353  
   354  	// Test for Delete another target
   355  	changes = &plan.Changes{
   356  		Create: []*endpoint.Endpoint{
   357  			{
   358  				DNSName:    "redis.external-dns-test.com",
   359  				RecordType: "A",
   360  				RecordTTL:  300,
   361  				Targets:    endpoint.NewTargets("5.6.7.8"),
   362  			},
   363  		},
   364  	}
   365  	if err := p.ApplyChanges(context.Background(), changes); err != nil {
   366  		t.Errorf("Failed to get records: %v", err)
   367  	}
   368  	endpoints, err = p.Records(context.Background())
   369  	if err != nil {
   370  		t.Errorf("Failed to get records: %v", err)
   371  	} else {
   372  		if len(endpoints) != 2 {
   373  			t.Errorf("Incorrect number of records: %d", len(endpoints))
   374  		}
   375  		for _, endpoint := range endpoints {
   376  			t.Logf("Endpoint for %+v", *endpoint)
   377  		}
   378  	}
   379  
   380  	// Test for Delete another target
   381  	changes = &plan.Changes{
   382  		Delete: []*endpoint.Endpoint{
   383  			{
   384  				DNSName:    "tencent.external-dns-test.com",
   385  				RecordType: "A",
   386  				RecordTTL:  600,
   387  				Targets:    endpoint.NewTargets("1.2.3.4"),
   388  			},
   389  		},
   390  	}
   391  	if err := p.ApplyChanges(context.Background(), changes); err != nil {
   392  		t.Errorf("Failed to get records: %v", err)
   393  	}
   394  	endpoints, err = p.Records(context.Background())
   395  	if err != nil {
   396  		t.Errorf("Failed to get records: %v", err)
   397  	} else {
   398  		if len(endpoints) != 1 {
   399  			t.Errorf("Incorrect number of records: %d", len(endpoints))
   400  		}
   401  		for _, endpoint := range endpoints {
   402  			t.Logf("Endpoint for %+v", *endpoint)
   403  		}
   404  	}
   405  }