|
| 1 | +package test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "fmt" |
| 7 | + "time" |
| 8 | + helper "github.com/cloudposse/test-helpers/pkg/atmos/component-helper" |
| 9 | + awsHelper "github.com/cloudposse/test-helpers/pkg/aws" |
| 10 | + "github.com/cloudposse/test-helpers/pkg/atmos" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + networkingv1 "k8s.io/api/networking/v1" |
| 14 | + |
| 15 | + |
| 16 | + "k8s.io/client-go/informers" |
| 17 | + "k8s.io/client-go/tools/cache" |
| 18 | +) |
| 19 | + |
| 20 | +type ComponentSuite struct { |
| 21 | + helper.TestSuite |
| 22 | +} |
| 23 | + |
| 24 | +func (s *ComponentSuite) TestBasic() { |
| 25 | + const component = "eks/alb-controller/basic" |
| 26 | + const stack = "default-test" |
| 27 | + const awsRegion = "us-east-2" |
| 28 | + |
| 29 | + randomID := strings.ToLower(random.UniqueId()) |
| 30 | + |
| 31 | + controllerNamespace := fmt.Sprintf("alb-controller-%s", randomID) |
| 32 | + |
| 33 | + input := map[string]interface{}{ |
| 34 | + "kubernetes_namespace": controllerNamespace, |
| 35 | + } |
| 36 | + |
| 37 | + defer s.DestroyAtmosComponent(s.T(), component, stack, nil) |
| 38 | + options, _ := s.DeployAtmosComponent(s.T(), component, stack, nil) |
| 39 | + assert.NotNil(s.T(), options) |
| 40 | + |
| 41 | + type Metadata struct { |
| 42 | + AppVersion string `json:"app_version"` |
| 43 | + Chart string `json:"chart"` |
| 44 | + FirstDeployed float64 `json:"first_deployed"` |
| 45 | + LastDeployed float64 `json:"last_deployed"` |
| 46 | + Name string `json:"name"` |
| 47 | + Namespace string `json:"namespace"` |
| 48 | + Notes string `json:"notes"` |
| 49 | + Revision int `json:"revision"` |
| 50 | + Values string `json:"values"` |
| 51 | + Version string `json:"version"` |
| 52 | + } |
| 53 | + |
| 54 | + metadataArray := []Metadata{} |
| 55 | + |
| 56 | + atmos.OutputStruct(s.T(), options, "metadata", &metadataArray) |
| 57 | + |
| 58 | + metadata := metadataArray[0] |
| 59 | + |
| 60 | + assert.Equal(s.T(), metadata.AppVersion, "v2.7.1") |
| 61 | + assert.Equal(s.T(), metadata.Chart, "aws-load-balancer-controller") |
| 62 | + assert.NotNil(s.T(), metadata.FirstDeployed) |
| 63 | + assert.NotNil(s.T(), metadata.LastDeployed) |
| 64 | + assert.Equal(s.T(), metadata.Name, "aws-load-balancer-controller") |
| 65 | + assert.Equal(s.T(), metadata.Namespace, controllerNamespace) |
| 66 | + assert.Equal(s.T(), metadata.Notes, "AWS Load Balancer controller installed!\n") |
| 67 | + assert.Equal(s.T(), metadata.Revision, 1) |
| 68 | + assert.NotNil(s.T(), metadata.Values) |
| 69 | + assert.Equal(s.T(), metadata.Version, "1.7.1") |
| 70 | + |
| 71 | + clusterOptions := s.GetAtmosOptions("eks/cluster", stack, nil) |
| 72 | + clusrerId := atmos.Output(s.T(), clusterOptions, "eks_cluster_id") |
| 73 | + cluster := awsHelper.GetEksCluster(s.T(), context.Background(), awsRegion, clusrerId) |
| 74 | + clientset, err := awsHelper.NewK8SClientset(cluster) |
| 75 | + assert.NoError(s.T(), err) |
| 76 | + assert.NotNil(s.T(), clientset) |
| 77 | + |
| 78 | + namespaces, err := clientset.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{}) |
| 79 | + assert.NoError(s.T(), err) |
| 80 | + assert.NotNil(s.T(), namespaces) |
| 81 | + assert.Equal(s.T(), len(namespaces.Items), 5) |
| 82 | + |
| 83 | + deployment, err := clientset.AppsV1().Deployments(controllerNamespace).Get(context.Background(), "aws-load-balancer-controller", metav1.GetOptions{}) |
| 84 | + assert.NoError(s.T(), err) |
| 85 | + assert.NotNil(s.T(), deployment) |
| 86 | + assert.Equal(s.T(), deployment.Name, "aws-load-balancer-controller") |
| 87 | + assert.Equal(s.T(), deployment.Namespace, controllerNamespace) |
| 88 | + assert.Equal(s.T(), *deployment.Spec.Replicas, int32(2)) |
| 89 | + |
| 90 | + |
| 91 | + ingressNamespace := fmt.Sprintf("example-%s", randomID) |
| 92 | + ingressName := fmt.Sprintf("example-ingress-%s", randomID) |
| 93 | + |
| 94 | + pathType := networkingv1.PathTypeImplementationSpecific |
| 95 | + |
| 96 | + ingress := &networkingv1.Ingress{ |
| 97 | + ObjectMeta: metav1.ObjectMeta{ |
| 98 | + Name: ingressName, |
| 99 | + Annotations: map[string]string{ |
| 100 | + "alb.ingress.kubernetes.io/load-balancer-name": ingressName, |
| 101 | + "alb.ingress.kubernetes.io/group.name": "example-group", |
| 102 | + "external-dns.alpha.kubernetes.io/hostname": "example.com", |
| 103 | + }, |
| 104 | + }, |
| 105 | + Spec: networkingv1.IngressSpec{ |
| 106 | + DefaultBackend: &networkingv1.IngressBackend{ |
| 107 | + Service: &networkingv1.IngressServiceBackend{ |
| 108 | + Name: "default", |
| 109 | + Port: networkingv1.ServiceBackendPort{ |
| 110 | + Name: "use-annotation", |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + TLS: []networkingv1.IngressTLS{ |
| 115 | + { |
| 116 | + Hosts: []string{"example.com"}, |
| 117 | + }, |
| 118 | + }, |
| 119 | + Rules: []networkingv1.IngressRule{ |
| 120 | + { |
| 121 | + Host: "example.com", |
| 122 | + IngressRuleValue: networkingv1.IngressRuleValue{ |
| 123 | + HTTP: &networkingv1.HTTPIngressRuleValue{ |
| 124 | + Paths: []networkingv1.HTTPIngressPath{ |
| 125 | + { |
| 126 | + Path: "/", |
| 127 | + PathType: &pathType, |
| 128 | + Backend: networkingv1.IngressBackend{ |
| 129 | + Service: &networkingv1.IngressServiceBackend{ |
| 130 | + Name: "default", |
| 131 | + Port: networkingv1.ServiceBackendPort{ |
| 132 | + Number: 80, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + } |
| 144 | + |
| 145 | + defer clientset.NetworkingV1().Ingresses(ingressNamespace).Delete(context.Background(), ingressName, metav1.DeleteOptions{}) |
| 146 | + _, err = clientset.NetworkingV1().Ingresses(ingressNamespace).Create(context.Background(), ingress, metav1.CreateOptions{}) |
| 147 | + assert.NoError(s.T(), err) |
| 148 | + assert.NotNil(s.T(), ingress) |
| 149 | + |
| 150 | + // Wait for the Ingress to be updated with the LoadBalancer metadata |
| 151 | + factory := informers.NewSharedInformerFactory(clientset, 0) |
| 152 | + informer := factory.Networking().V1().Ingresses().Informer() |
| 153 | + |
| 154 | + stopChannel := make(chan struct{}) |
| 155 | + |
| 156 | + informer.AddEventHandler(cache.ResourceEventHandlerFuncs{ |
| 157 | + UpdateFunc: func(oldObj, newObj interface{}) { |
| 158 | + _, ok := oldObj.(*networkingv1.Ingress) |
| 159 | + if !ok { |
| 160 | + return |
| 161 | + } |
| 162 | + newIngress, ok := newObj.(*networkingv1.Ingress) |
| 163 | + if !ok { |
| 164 | + return |
| 165 | + } |
| 166 | + |
| 167 | + // Check if the Ingress's LoadBalancer status has been populated |
| 168 | + if len(newIngress.Status.LoadBalancer.Ingress[0].Hostname) > 0 { |
| 169 | + fmt.Printf("Ingress %s is ready\n", newIngress.Name) |
| 170 | + close(stopChannel) |
| 171 | + } else { |
| 172 | + fmt.Printf("Ingress %s is not ready yet\n", newIngress.Name) |
| 173 | + } |
| 174 | + }, |
| 175 | + }) |
| 176 | + |
| 177 | + go informer.Run(stopChannel) |
| 178 | + |
| 179 | + select { |
| 180 | + case <-stopChannel: |
| 181 | + msg := "All ingress have joined the EKS cluster" |
| 182 | + fmt.Println(msg) |
| 183 | + case <-time.After(1 * time.Minute): |
| 184 | + msg := "Not all worker nodes have joined the EKS cluster" |
| 185 | + fmt.Println(msg) |
| 186 | + } |
| 187 | + |
| 188 | + ingressStatus, err := clientset.NetworkingV1().Ingresses(ingressNamespace).Get(context.Background(), ingressName, metav1.GetOptions{}) |
| 189 | + assert.NoError(s.T(), err) |
| 190 | + assert.Equal(s.T(), ingressStatus.Name, ingressName) |
| 191 | + assert.NotEmpty(s.T(), ingressStatus.Status.LoadBalancer.Ingress[0].Hostname) |
| 192 | +} |
| 193 | + |
| 194 | +func (s *ComponentSuite) TestEnabledFlag() { |
| 195 | + const component = "eks/alb-controller/disabled" |
| 196 | + const stack = "default-test" |
| 197 | + s.VerifyEnabledFlag(component, stack, nil) |
| 198 | +} |
| 199 | + |
| 200 | +func (s *ComponentSuite) SetupSuite() { |
| 201 | + s.TestSuite.InitConfig() |
| 202 | + s.TestSuite.Config.ComponentDestDir = "components/terraform/eks/alb-controller" |
| 203 | + s.TestSuite.SetupSuite() |
| 204 | +} |
| 205 | + |
| 206 | +func TestRunSuite(t *testing.T) { |
| 207 | + suite := new(ComponentSuite) |
| 208 | + suite.AddDependency(t, "vpc", "default-test", nil) |
| 209 | + suite.AddDependency(t, "eks/cluster", "default-test", nil) |
| 210 | + helper.Run(t, suite) |
| 211 | +} |
0 commit comments