github.com/hugorut/terraform@v1.1.3/src/backend/remote-state/azure/client_test.go (about) 1 package azure 2 3 import ( 4 "context" 5 "os" 6 "testing" 7 8 "github.com/hugorut/terraform/src/backend" 9 "github.com/hugorut/terraform/src/legacy/helper/acctest" 10 "github.com/hugorut/terraform/src/states/remote" 11 "github.com/tombuildsstuff/giovanni/storage/2018-11-09/blob/blobs" 12 ) 13 14 func TestRemoteClient_impl(t *testing.T) { 15 var _ remote.Client = new(RemoteClient) 16 var _ remote.ClientLocker = new(RemoteClient) 17 } 18 19 func TestRemoteClientAccessKeyBasic(t *testing.T) { 20 testAccAzureBackend(t) 21 rs := acctest.RandString(4) 22 res := testResourceNames(rs, "testState") 23 armClient := buildTestClient(t, res) 24 25 ctx := context.TODO() 26 err := armClient.buildTestResources(ctx, &res) 27 defer armClient.destroyTestResources(ctx, res) 28 if err != nil { 29 t.Fatalf("Error creating Test Resources: %q", err) 30 } 31 32 b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 33 "storage_account_name": res.storageAccountName, 34 "container_name": res.storageContainerName, 35 "key": res.storageKeyName, 36 "access_key": res.storageAccountAccessKey, 37 "environment": os.Getenv("ARM_ENVIRONMENT"), 38 "endpoint": os.Getenv("ARM_ENDPOINT"), 39 })).(*Backend) 40 41 state, err := b.StateMgr(backend.DefaultStateName) 42 if err != nil { 43 t.Fatal(err) 44 } 45 46 remote.TestClient(t, state.(*remote.State).Client) 47 } 48 49 func TestRemoteClientManagedServiceIdentityBasic(t *testing.T) { 50 testAccAzureBackendRunningInAzure(t) 51 rs := acctest.RandString(4) 52 res := testResourceNames(rs, "testState") 53 armClient := buildTestClient(t, res) 54 55 ctx := context.TODO() 56 err := armClient.buildTestResources(ctx, &res) 57 defer armClient.destroyTestResources(ctx, res) 58 if err != nil { 59 t.Fatalf("Error creating Test Resources: %q", err) 60 } 61 62 b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 63 "storage_account_name": res.storageAccountName, 64 "container_name": res.storageContainerName, 65 "key": res.storageKeyName, 66 "resource_group_name": res.resourceGroup, 67 "use_msi": true, 68 "subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"), 69 "tenant_id": os.Getenv("ARM_TENANT_ID"), 70 "environment": os.Getenv("ARM_ENVIRONMENT"), 71 "endpoint": os.Getenv("ARM_ENDPOINT"), 72 })).(*Backend) 73 74 state, err := b.StateMgr(backend.DefaultStateName) 75 if err != nil { 76 t.Fatal(err) 77 } 78 79 remote.TestClient(t, state.(*remote.State).Client) 80 } 81 82 func TestRemoteClientSasTokenBasic(t *testing.T) { 83 testAccAzureBackend(t) 84 rs := acctest.RandString(4) 85 res := testResourceNames(rs, "testState") 86 armClient := buildTestClient(t, res) 87 88 ctx := context.TODO() 89 err := armClient.buildTestResources(ctx, &res) 90 defer armClient.destroyTestResources(ctx, res) 91 if err != nil { 92 t.Fatalf("Error creating Test Resources: %q", err) 93 } 94 95 sasToken, err := buildSasToken(res.storageAccountName, res.storageAccountAccessKey) 96 if err != nil { 97 t.Fatalf("Error building SAS Token: %+v", err) 98 } 99 100 b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 101 "storage_account_name": res.storageAccountName, 102 "container_name": res.storageContainerName, 103 "key": res.storageKeyName, 104 "sas_token": *sasToken, 105 "environment": os.Getenv("ARM_ENVIRONMENT"), 106 "endpoint": os.Getenv("ARM_ENDPOINT"), 107 })).(*Backend) 108 109 state, err := b.StateMgr(backend.DefaultStateName) 110 if err != nil { 111 t.Fatal(err) 112 } 113 114 remote.TestClient(t, state.(*remote.State).Client) 115 } 116 117 func TestRemoteClientServicePrincipalBasic(t *testing.T) { 118 testAccAzureBackend(t) 119 rs := acctest.RandString(4) 120 res := testResourceNames(rs, "testState") 121 armClient := buildTestClient(t, res) 122 123 ctx := context.TODO() 124 err := armClient.buildTestResources(ctx, &res) 125 defer armClient.destroyTestResources(ctx, res) 126 if err != nil { 127 t.Fatalf("Error creating Test Resources: %q", err) 128 } 129 130 b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 131 "storage_account_name": res.storageAccountName, 132 "container_name": res.storageContainerName, 133 "key": res.storageKeyName, 134 "resource_group_name": res.resourceGroup, 135 "subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"), 136 "tenant_id": os.Getenv("ARM_TENANT_ID"), 137 "client_id": os.Getenv("ARM_CLIENT_ID"), 138 "client_secret": os.Getenv("ARM_CLIENT_SECRET"), 139 "environment": os.Getenv("ARM_ENVIRONMENT"), 140 "endpoint": os.Getenv("ARM_ENDPOINT"), 141 })).(*Backend) 142 143 state, err := b.StateMgr(backend.DefaultStateName) 144 if err != nil { 145 t.Fatal(err) 146 } 147 148 remote.TestClient(t, state.(*remote.State).Client) 149 } 150 151 func TestRemoteClientAccessKeyLocks(t *testing.T) { 152 testAccAzureBackend(t) 153 rs := acctest.RandString(4) 154 res := testResourceNames(rs, "testState") 155 armClient := buildTestClient(t, res) 156 157 ctx := context.TODO() 158 err := armClient.buildTestResources(ctx, &res) 159 defer armClient.destroyTestResources(ctx, res) 160 if err != nil { 161 t.Fatalf("Error creating Test Resources: %q", err) 162 } 163 164 b1 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 165 "storage_account_name": res.storageAccountName, 166 "container_name": res.storageContainerName, 167 "key": res.storageKeyName, 168 "access_key": res.storageAccountAccessKey, 169 "environment": os.Getenv("ARM_ENVIRONMENT"), 170 "endpoint": os.Getenv("ARM_ENDPOINT"), 171 })).(*Backend) 172 173 b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 174 "storage_account_name": res.storageAccountName, 175 "container_name": res.storageContainerName, 176 "key": res.storageKeyName, 177 "access_key": res.storageAccountAccessKey, 178 "environment": os.Getenv("ARM_ENVIRONMENT"), 179 "endpoint": os.Getenv("ARM_ENDPOINT"), 180 })).(*Backend) 181 182 s1, err := b1.StateMgr(backend.DefaultStateName) 183 if err != nil { 184 t.Fatal(err) 185 } 186 187 s2, err := b2.StateMgr(backend.DefaultStateName) 188 if err != nil { 189 t.Fatal(err) 190 } 191 192 remote.TestRemoteLocks(t, s1.(*remote.State).Client, s2.(*remote.State).Client) 193 } 194 195 func TestRemoteClientServicePrincipalLocks(t *testing.T) { 196 testAccAzureBackend(t) 197 rs := acctest.RandString(4) 198 res := testResourceNames(rs, "testState") 199 armClient := buildTestClient(t, res) 200 201 ctx := context.TODO() 202 err := armClient.buildTestResources(ctx, &res) 203 defer armClient.destroyTestResources(ctx, res) 204 if err != nil { 205 t.Fatalf("Error creating Test Resources: %q", err) 206 } 207 208 b1 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 209 "storage_account_name": res.storageAccountName, 210 "container_name": res.storageContainerName, 211 "key": res.storageKeyName, 212 "resource_group_name": res.resourceGroup, 213 "subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"), 214 "tenant_id": os.Getenv("ARM_TENANT_ID"), 215 "client_id": os.Getenv("ARM_CLIENT_ID"), 216 "client_secret": os.Getenv("ARM_CLIENT_SECRET"), 217 "environment": os.Getenv("ARM_ENVIRONMENT"), 218 "endpoint": os.Getenv("ARM_ENDPOINT"), 219 })).(*Backend) 220 221 b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 222 "storage_account_name": res.storageAccountName, 223 "container_name": res.storageContainerName, 224 "key": res.storageKeyName, 225 "resource_group_name": res.resourceGroup, 226 "subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"), 227 "tenant_id": os.Getenv("ARM_TENANT_ID"), 228 "client_id": os.Getenv("ARM_CLIENT_ID"), 229 "client_secret": os.Getenv("ARM_CLIENT_SECRET"), 230 "environment": os.Getenv("ARM_ENVIRONMENT"), 231 "endpoint": os.Getenv("ARM_ENDPOINT"), 232 })).(*Backend) 233 234 s1, err := b1.StateMgr(backend.DefaultStateName) 235 if err != nil { 236 t.Fatal(err) 237 } 238 239 s2, err := b2.StateMgr(backend.DefaultStateName) 240 if err != nil { 241 t.Fatal(err) 242 } 243 244 remote.TestRemoteLocks(t, s1.(*remote.State).Client, s2.(*remote.State).Client) 245 } 246 247 func TestPutMaintainsMetaData(t *testing.T) { 248 testAccAzureBackend(t) 249 rs := acctest.RandString(4) 250 res := testResourceNames(rs, "testState") 251 armClient := buildTestClient(t, res) 252 253 ctx := context.TODO() 254 err := armClient.buildTestResources(ctx, &res) 255 defer armClient.destroyTestResources(ctx, res) 256 if err != nil { 257 t.Fatalf("Error creating Test Resources: %q", err) 258 } 259 260 headerName := "acceptancetest" 261 expectedValue := "f3b56bad-33ad-4b93-a600-7a66e9cbd1eb" 262 263 client, err := armClient.getBlobClient(ctx) 264 if err != nil { 265 t.Fatalf("Error building Blob Client: %+v", err) 266 } 267 268 _, err = client.PutBlockBlob(ctx, res.storageAccountName, res.storageContainerName, res.storageKeyName, blobs.PutBlockBlobInput{}) 269 if err != nil { 270 t.Fatalf("Error Creating Block Blob: %+v", err) 271 } 272 273 blobReference, err := client.GetProperties(ctx, res.storageAccountName, res.storageContainerName, res.storageKeyName, blobs.GetPropertiesInput{}) 274 if err != nil { 275 t.Fatalf("Error loading MetaData: %+v", err) 276 } 277 278 blobReference.MetaData[headerName] = expectedValue 279 opts := blobs.SetMetaDataInput{ 280 MetaData: blobReference.MetaData, 281 } 282 _, err = client.SetMetaData(ctx, res.storageAccountName, res.storageContainerName, res.storageKeyName, opts) 283 if err != nil { 284 t.Fatalf("Error setting MetaData: %+v", err) 285 } 286 287 // update the metadata using the Backend 288 remoteClient := RemoteClient{ 289 keyName: res.storageKeyName, 290 containerName: res.storageContainerName, 291 accountName: res.storageAccountName, 292 293 giovanniBlobClient: *client, 294 } 295 296 bytes := []byte(acctest.RandString(20)) 297 err = remoteClient.Put(bytes) 298 if err != nil { 299 t.Fatalf("Error putting data: %+v", err) 300 } 301 302 // Verify it still exists 303 blobReference, err = client.GetProperties(ctx, res.storageAccountName, res.storageContainerName, res.storageKeyName, blobs.GetPropertiesInput{}) 304 if err != nil { 305 t.Fatalf("Error loading MetaData: %+v", err) 306 } 307 308 if blobReference.MetaData[headerName] != expectedValue { 309 t.Fatalf("%q was not set to %q in the MetaData: %+v", headerName, expectedValue, blobReference.MetaData) 310 } 311 }