|
| 1 | +package test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + "github.com/cloudposse/test-helpers/pkg/atmos" |
| 10 | + "github.com/cloudposse/test-helpers/pkg/helm" |
| 11 | + awsHelper "github.com/cloudposse/test-helpers/pkg/aws" |
| 12 | + helper "github.com/cloudposse/test-helpers/pkg/atmos/component-helper" |
| 13 | + awsTerratest "github.com/gruntwork-io/terratest/modules/aws" |
| 14 | + "github.com/gruntwork-io/terratest/modules/random" |
| 15 | + |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + |
| 18 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 19 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 20 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 21 | + |
| 22 | + "k8s.io/client-go/dynamic" |
| 23 | +) |
| 24 | + |
| 25 | +type ComponentSuite struct { |
| 26 | + helper.TestSuite |
| 27 | +} |
| 28 | + |
| 29 | +func (s *ComponentSuite) TestBasic() { |
| 30 | + const component = "eks/external-dns/basic" |
| 31 | + const stack = "default-test" |
| 32 | + const awsRegion = "us-east-2" |
| 33 | + |
| 34 | + clusterOptions := s.GetAtmosOptions("eks/cluster", stack, nil) |
| 35 | + clusrerId := atmos.Output(s.T(), clusterOptions, "eks_cluster_id") |
| 36 | + cluster := awsHelper.GetEksCluster(s.T(), context.Background(), awsRegion, clusrerId) |
| 37 | + |
| 38 | + dnsDelegatedOptions := s.GetAtmosOptions("dns-delegated", stack, nil) |
| 39 | + delegatedDomainName := atmos.Output(s.T(), dnsDelegatedOptions, "default_domain_name") |
| 40 | + defaultDNSZoneId := atmos.Output(s.T(), dnsDelegatedOptions, "default_dns_zone_id") |
| 41 | + |
| 42 | + defer func() { |
| 43 | + if err := awsHelper.CleanDNSZoneID(s.T(), context.Background(), defaultDNSZoneId, awsRegion); err != nil { |
| 44 | + fmt.Printf("Error cleaning DNS zone %s: %v\n", defaultDNSZoneId, err) |
| 45 | + } |
| 46 | + }() |
| 47 | + |
| 48 | + randomID := strings.ToLower(random.UniqueId()) |
| 49 | + |
| 50 | + namespace := fmt.Sprintf("external-dns-%s", randomID) |
| 51 | + dnsEndpointName := fmt.Sprintf("example-dns-record-%s", randomID) |
| 52 | + dnsRecordHostName := fmt.Sprintf("%s.%s", randomID, delegatedDomainName) |
| 53 | + |
| 54 | + inputs := map[string]interface{}{ |
| 55 | + "kubernetes_namespace": namespace, |
| 56 | + } |
| 57 | + |
| 58 | + defer s.DestroyAtmosComponent(s.T(), component, stack, &inputs) |
| 59 | + options, _ := s.DeployAtmosComponent(s.T(), component, stack, &inputs) |
| 60 | + assert.NotNil(s.T(), options) |
| 61 | + |
| 62 | + metadataArray := []helm.Metadata{} |
| 63 | + |
| 64 | + atmos.OutputStruct(s.T(), options, "metadata", &metadataArray) |
| 65 | + |
| 66 | + metadata := metadataArray[0] |
| 67 | + |
| 68 | + assert.Equal(s.T(), metadata.AppVersion, "0.14.0") |
| 69 | + assert.Equal(s.T(), metadata.Chart, "external-dns") |
| 70 | + assert.NotNil(s.T(), metadata.FirstDeployed) |
| 71 | + assert.NotNil(s.T(), metadata.LastDeployed) |
| 72 | + assert.Equal(s.T(), metadata.Name, "external-dns") |
| 73 | + assert.Equal(s.T(), metadata.Namespace, namespace) |
| 74 | + assert.NotNil(s.T(), metadata.Values) |
| 75 | + assert.Equal(s.T(), metadata.Version, "6.33.0") |
| 76 | + |
| 77 | + |
| 78 | + config, err := awsHelper.NewK8SClientConfig(cluster) |
| 79 | + assert.NoError(s.T(), err) |
| 80 | + assert.NotNil(s.T(), config) |
| 81 | + |
| 82 | + dynamicClient, err := dynamic.NewForConfig(config) |
| 83 | + if err != nil { |
| 84 | + panic(fmt.Errorf("failed to create dynamic client: %v", err)) |
| 85 | + } |
| 86 | + |
| 87 | + // Define the GroupVersionResource for the DNSEndpoint CRD |
| 88 | + dnsEndpointGVR := schema.GroupVersionResource{ |
| 89 | + Group: "externaldns.k8s.io", |
| 90 | + Version: "v1alpha1", |
| 91 | + Resource: "dnsendpoints", |
| 92 | + } |
| 93 | + |
| 94 | + // Create the DNSEndpoint object as an unstructured resource |
| 95 | + dnsEndpoint := &unstructured.Unstructured{ |
| 96 | + Object: map[string]interface{}{ |
| 97 | + "apiVersion": "externaldns.k8s.io/v1alpha1", |
| 98 | + "kind": "DNSEndpoint", |
| 99 | + "metadata": map[string]interface{}{ |
| 100 | + "name": dnsEndpointName, |
| 101 | + "namespace": namespace, |
| 102 | + }, |
| 103 | + "spec": map[string]interface{}{ |
| 104 | + "endpoints": []interface{}{ |
| 105 | + map[string]interface{}{ |
| 106 | + "dnsName": dnsRecordHostName, |
| 107 | + "recordTTL": 300, |
| 108 | + "recordType": "A", |
| 109 | + "targets": []interface{}{ |
| 110 | + "127.0.0.1", |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + // Create the DNSEndpoint resource in the "default" namespace |
| 119 | + _, err = dynamicClient.Resource(dnsEndpointGVR).Namespace(namespace).Create(context.Background(), dnsEndpoint, metav1.CreateOptions{}) |
| 120 | + assert.NoError(s.T(), err) |
| 121 | + |
| 122 | + defer func() { |
| 123 | + if err := dynamicClient.Resource(dnsEndpointGVR).Namespace(namespace).Delete(context.Background(), dnsEndpointName, metav1.DeleteOptions{}); err != nil { |
| 124 | + fmt.Printf("Error deleting external dns %s: %v\n", dnsEndpointName, err) |
| 125 | + } |
| 126 | + }() |
| 127 | + |
| 128 | + // Wait for the DNS record to be created |
| 129 | + // We can't use informers as DNS CRD status is not updated when the record is created |
| 130 | + time.Sleep(2 * time.Minute) |
| 131 | + |
| 132 | + delegatedNSRecord := awsTerratest.GetRoute53Record(s.T(), defaultDNSZoneId, dnsRecordHostName, "A", awsRegion) |
| 133 | + assert.Equal(s.T(), fmt.Sprintf("%s.", dnsRecordHostName), *delegatedNSRecord.Name) |
| 134 | + |
| 135 | + s.DriftTest(component, stack, &inputs) |
| 136 | +} |
| 137 | + |
| 138 | +func (s *ComponentSuite) TestEnabledFlag() { |
| 139 | + const component = "eks/external-dns/disabled" |
| 140 | + const stack = "default-test" |
| 141 | + s.VerifyEnabledFlag(component, stack, nil) |
| 142 | +} |
| 143 | + |
| 144 | +func (s *ComponentSuite) SetupSuite() { |
| 145 | + s.TestSuite.InitConfig() |
| 146 | + s.TestSuite.Config.ComponentDestDir = "components/terraform/eks/external-dns" |
| 147 | + s.TestSuite.SetupSuite() |
| 148 | +} |
| 149 | + |
| 150 | +func TestRunSuite(t *testing.T) { |
| 151 | + suite := new(ComponentSuite) |
| 152 | + suite.AddDependency(t, "vpc", "default-test", nil) |
| 153 | + suite.AddDependency(t, "eks/cluster", "default-test", nil) |
| 154 | + |
| 155 | + subdomain := strings.ToLower(random.UniqueId()) |
| 156 | + inputs := map[string]interface{}{ |
| 157 | + "zone_config": []map[string]interface{}{ |
| 158 | + { |
| 159 | + "subdomain": subdomain, |
| 160 | + "zone_name": "components.cptest.test-automation.app", |
| 161 | + }, |
| 162 | + }, |
| 163 | + } |
| 164 | + suite.AddDependency(t, "dns-delegated", "default-test", &inputs) |
| 165 | + |
| 166 | + randomID := strings.ToLower(random.UniqueId()) |
| 167 | + domainName := fmt.Sprintf("example-%s.net", randomID) |
| 168 | + inputsPrimaryDns := map[string]interface{}{ |
| 169 | + "domain_names": []string{domainName}, |
| 170 | + "record_config": []map[string]interface{}{ |
| 171 | + { |
| 172 | + "root_zone": domainName, |
| 173 | + "name": "", |
| 174 | + "type": "A", |
| 175 | + "ttl": 60, |
| 176 | + "records": []string{"127.0.0.1"}, |
| 177 | + }, |
| 178 | + { |
| 179 | + "root_zone": domainName, |
| 180 | + "name": "www.", |
| 181 | + "type": "CNAME", |
| 182 | + "ttl": 60, |
| 183 | + "records": []string{domainName}, |
| 184 | + }, |
| 185 | + { |
| 186 | + "root_zone": domainName, |
| 187 | + "name": "123456.", |
| 188 | + "type": "CNAME", |
| 189 | + "ttl": 120, |
| 190 | + "records": []string{domainName}, |
| 191 | + }, |
| 192 | + }, |
| 193 | + } |
| 194 | + suite.AddDependency(t, "dns-primary", "default-test", &inputsPrimaryDns) |
| 195 | + |
| 196 | + suite.AddDependency(t, "eks/alb-controller", "default-test", nil) |
| 197 | + helper.Run(t, suite) |
| 198 | +} |
0 commit comments