|
| 1 | +// Copyright 2024 StreamNative, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cloud |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "math/rand" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 26 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 27 | + "k8s.io/apimachinery/pkg/api/errors" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | +) |
| 30 | + |
| 31 | +var saGeneratedName = fmt.Sprintf("t-%d-%d", rand.Intn(1000), rand.Intn(100)) |
| 32 | + |
| 33 | +func TestServiceAccountBinding(t *testing.T) { |
| 34 | + resource.Test(t, resource.TestCase{ |
| 35 | + PreCheck: func() { |
| 36 | + testAccPreCheck(t) |
| 37 | + }, |
| 38 | + ProviderFactories: testAccProviderFactories, |
| 39 | + CheckDestroy: testCheckServiceAccountBindingDestroy, |
| 40 | + Steps: []resource.TestStep{ |
| 41 | + { |
| 42 | + Config: testResourceDataSourceServiceAccountBinding( |
| 43 | + "sndev", |
| 44 | + saGeneratedName, |
| 45 | + "gcp-shared-usc1-test", |
| 46 | + "streamnative"), |
| 47 | + Check: resource.ComposeTestCheckFunc( |
| 48 | + testCheckServiceAccountBindingExists("streamnative_service_account_binding.test-service-account-binding"), |
| 49 | + ), |
| 50 | + }, |
| 51 | + }, |
| 52 | + }) |
| 53 | +} |
| 54 | + |
| 55 | +func TestServiceAccountBindingRemovedExternally(t *testing.T) { |
| 56 | + // This test case is to simulate the situation that the service account binding is removed externally |
| 57 | + // and the terraform state still has the resource |
| 58 | + resource.Test(t, resource.TestCase{ |
| 59 | + PreCheck: func() { |
| 60 | + testAccPreCheck(t) |
| 61 | + }, |
| 62 | + ProviderFactories: testAccProviderFactories, |
| 63 | + CheckDestroy: testCheckServiceAccountBindingDestroy, |
| 64 | + Steps: []resource.TestStep{ |
| 65 | + { |
| 66 | + Config: testResourceDataSourceServiceAccountBinding( |
| 67 | + "sndev", |
| 68 | + saGeneratedName, |
| 69 | + "gcp-shared-usc1-test", |
| 70 | + "streamnative"), |
| 71 | + Check: resource.ComposeTestCheckFunc( |
| 72 | + testCheckServiceAccountBindingExists("streamnative_service_account_binding.test-service-account-binding"), |
| 73 | + ), |
| 74 | + }, |
| 75 | + { |
| 76 | + PreConfig: func() { |
| 77 | + meta := testAccProvider.Meta() |
| 78 | + clientSet, err := getClientSet(getFactoryFromMeta(meta)) |
| 79 | + if err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + err = clientSet.CloudV1alpha1(). |
| 83 | + ServiceAccountBindings("sndev"). |
| 84 | + Delete(context.Background(), saGeneratedName+".streamnative.gcp-shared-usc1-test", metav1.DeleteOptions{}) |
| 85 | + if err != nil { |
| 86 | + t.Fatal(err) |
| 87 | + } |
| 88 | + }, |
| 89 | + Config: testResourceDataSourceServiceAccountBinding( |
| 90 | + "sndev", |
| 91 | + saGeneratedName, |
| 92 | + "gcp-shared-usc1-test", |
| 93 | + "streamnative"), |
| 94 | + PlanOnly: true, |
| 95 | + ExpectNonEmptyPlan: true, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }) |
| 99 | +} |
| 100 | + |
| 101 | +func testCheckServiceAccountBindingDestroy(s *terraform.State) error { |
| 102 | + // Add a sleep for wait the service account binding to be deleted |
| 103 | + time.Sleep(5 * time.Second) |
| 104 | + for _, rs := range s.RootModule().Resources { |
| 105 | + if rs.Type != "streamnative_service_account_binding" { |
| 106 | + continue |
| 107 | + } |
| 108 | + meta := testAccProvider.Meta() |
| 109 | + clientSet, err := getClientSet(getFactoryFromMeta(meta)) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + organizationServiceAccountBinding := strings.Split(rs.Primary.ID, "/") |
| 114 | + _, err = clientSet.CloudV1alpha1(). |
| 115 | + ServiceAccountBindings(organizationServiceAccountBinding[0]). |
| 116 | + Get(context.Background(), organizationServiceAccountBinding[1], metav1.GetOptions{}) |
| 117 | + if err != nil { |
| 118 | + if errors.IsNotFound(err) { |
| 119 | + return nil |
| 120 | + } |
| 121 | + return err |
| 122 | + } |
| 123 | + return fmt.Errorf(`ERROR_RESOURCE_SERVICE_ACCOUNT_BINDING_STILL_EXISTS: "%s"`, rs.Primary.ID) |
| 124 | + } |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +func testCheckServiceAccountBindingExists(name string) resource.TestCheckFunc { |
| 129 | + return func(s *terraform.State) error { |
| 130 | + rs, ok := s.RootModule().Resources[name] |
| 131 | + if !ok { |
| 132 | + return fmt.Errorf(`ERROR_RESOURCE_SERVICE_ACCOUNT_BINDING_NOT_FOUND: "%s"`, name) |
| 133 | + } |
| 134 | + if rs.Primary.ID == "" { |
| 135 | + return fmt.Errorf(`ERROR_RESOURCE_SERVICE_ACCOUNT_BINDING_ID_NOT_SET`) |
| 136 | + } |
| 137 | + meta := testAccProvider.Meta() |
| 138 | + clientSet, err := getClientSet(getFactoryFromMeta(meta)) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + organizationCluster := strings.Split(rs.Primary.ID, "/") |
| 143 | + serviceAccountBinding, err := clientSet.CloudV1alpha1(). |
| 144 | + ServiceAccountBindings(organizationCluster[0]). |
| 145 | + Get(context.Background(), organizationCluster[1], metav1.GetOptions{}) |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + length := len(serviceAccountBinding.Status.Conditions) |
| 150 | + // the IAM |
| 151 | + if serviceAccountBinding.Status.Conditions[0].Type != "IAMAccountReady" || serviceAccountBinding.Status.Conditions[0].Status != "True" || |
| 152 | + serviceAccountBinding.Status.Conditions[length-1].Type != "Ready" || serviceAccountBinding.Status.Conditions[length-1].Status != "True" { |
| 153 | + return fmt.Errorf(`ERROR_RESOURCE_SERVICE_ACCOUNT_BINDING_NOT_READY: "%s"`, rs.Primary.ID) |
| 154 | + } |
| 155 | + return nil |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func testResourceDataSourceServiceAccountBinding(organization, name, poolMemberName, poolMemberNamespace string) string { |
| 160 | + return fmt.Sprintf(` |
| 161 | +provider "streamnative" { |
| 162 | +} |
| 163 | +
|
| 164 | +resource "streamnative_service_account" "test-service-account" { |
| 165 | + organization = "%s" |
| 166 | + name = "%s" |
| 167 | + admin = %t |
| 168 | +} |
| 169 | +
|
| 170 | +resource "streamnative_service_account_binding" "test-service-account-binding" { |
| 171 | + organization = "%s" |
| 172 | + service_account_name = streamnative_service_account.test-service-account.name |
| 173 | + pool_member_name = "%s" |
| 174 | + pool_member_namespace = "%s" |
| 175 | + enable_iam_account_creation = true |
| 176 | +} |
| 177 | +
|
| 178 | +data "streamnative_service_account_binding" "test-service-account-binding" { |
| 179 | + depends_on = [streamnative_service_account_binding.test-service-account-binding] |
| 180 | + organization = streamnative_service_account_binding.test-service-account-binding.organization |
| 181 | + name = streamnative_service_account_binding.test-service-account-binding.name |
| 182 | +} |
| 183 | +
|
| 184 | +`, organization, name, true, organization, poolMemberName, poolMemberNamespace) |
| 185 | +} |
0 commit comments