github.com/ojiry/terraform@v0.8.2-0.20161218223921-e50cec712c4a/state/remote/azure_test.go (about) 1 package remote 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 "testing" 8 9 mainStorage "github.com/Azure/azure-sdk-for-go/storage" 10 "github.com/hashicorp/terraform/helper/acctest" 11 riviera "github.com/jen20/riviera/azure" 12 "github.com/jen20/riviera/storage" 13 "github.com/satori/uuid" 14 ) 15 16 func TestAzureClient_impl(t *testing.T) { 17 var _ Client = new(AzureClient) 18 } 19 20 // This test creates a bucket in Azure and populates it. 21 // It may incur costs, so it will only run if Azure credential environment 22 // variables are present. 23 func TestAzureClient(t *testing.T) { 24 config := getAzureConfig(t) 25 26 setup(t, config) 27 defer teardown(t, config) 28 29 client, err := azureFactory(config) 30 if err != nil { 31 t.Fatalf("Error for valid config: %v", err) 32 } 33 34 testClient(t, client) 35 } 36 37 // This test is the same as TestAzureClient with the addition of passing an 38 // empty string in the lease_id, we expect the client to pass tests 39 func TestAzureClientEmptyLease(t *testing.T) { 40 config := getAzureConfig(t) 41 config["lease_id"] = "" 42 43 setup(t, config) 44 defer teardown(t, config) 45 46 client, err := azureFactory(config) 47 if err != nil { 48 t.Fatalf("Error for valid config: %v", err) 49 } 50 51 testClient(t, client) 52 } 53 54 // This test is the same as TestAzureClient with the addition of using the 55 // lease_id config option 56 func TestAzureClientLease(t *testing.T) { 57 leaseID := uuid.NewV4().String() 58 config := getAzureConfig(t) 59 config["lease_id"] = leaseID 60 61 setup(t, config) 62 defer teardown(t, config) 63 64 client, err := azureFactory(config) 65 if err != nil { 66 t.Fatalf("Error for valid config: %v", err) 67 } 68 azureClient := client.(*AzureClient) 69 70 // put empty blob so we can acquire lease against it 71 err = azureClient.blobClient.CreateBlockBlob(azureClient.containerName, azureClient.keyName) 72 if err != nil { 73 t.Fatalf("Error creating blob for leasing: %v", err) 74 } 75 76 _, err = azureClient.blobClient.AcquireLease(azureClient.containerName, azureClient.keyName, -1, leaseID) 77 if err != nil { 78 t.Fatalf("Error acquiring lease: %v", err) 79 } 80 81 // no need to release lease as blob is deleted in testing 82 testClient(t, client) 83 } 84 85 func getAzureConfig(t *testing.T) map[string]string { 86 config := map[string]string{ 87 "arm_subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"), 88 "arm_client_id": os.Getenv("ARM_CLIENT_ID"), 89 "arm_client_secret": os.Getenv("ARM_CLIENT_SECRET"), 90 "arm_tenant_id": os.Getenv("ARM_TENANT_ID"), 91 } 92 93 for k, v := range config { 94 if v == "" { 95 t.Skipf("skipping; %s must be set", strings.ToUpper(k)) 96 } 97 } 98 99 rs := acctest.RandString(8) 100 101 config["resource_group_name"] = fmt.Sprintf("terraform-%s", rs) 102 config["storage_account_name"] = fmt.Sprintf("terraform%s", rs) 103 config["container_name"] = "terraform" 104 config["key"] = "test.tfstate" 105 106 return config 107 } 108 109 func setup(t *testing.T, conf map[string]string) { 110 creds, err := getCredentialsFromConf(conf) 111 if err != nil { 112 t.Fatalf("Error getting credentials from conf: %v", err) 113 } 114 rivieraClient, err := getRivieraClient(creds) 115 if err != nil { 116 t.Fatalf("Error instantiating the riviera client: %v", err) 117 } 118 119 // Create resource group 120 r := rivieraClient.NewRequest() 121 r.Command = riviera.CreateResourceGroup{ 122 Name: conf["resource_group_name"], 123 Location: riviera.WestUS, 124 } 125 response, err := r.Execute() 126 if err != nil { 127 t.Fatalf("Error creating a resource group: %v", err) 128 } 129 if !response.IsSuccessful() { 130 t.Fatalf("Error creating a resource group: %v", response.Error.Error()) 131 } 132 133 // Create storage account 134 r = rivieraClient.NewRequest() 135 r.Command = storage.CreateStorageAccount{ 136 ResourceGroupName: conf["resource_group_name"], 137 Name: conf["storage_account_name"], 138 AccountType: riviera.String("Standard_LRS"), 139 Location: riviera.WestUS, 140 } 141 response, err = r.Execute() 142 if err != nil { 143 t.Fatalf("Error creating a storage account: %v", err) 144 } 145 if !response.IsSuccessful() { 146 t.Fatalf("Error creating a storage account: %v", response.Error.Error()) 147 } 148 149 // Create container 150 accessKey, err := getStorageAccountAccessKey(conf, conf["resource_group_name"], conf["storage_account_name"]) 151 if err != nil { 152 t.Fatalf("Error creating a storage account: %v", err) 153 } 154 storageClient, err := mainStorage.NewBasicClient(conf["storage_account_name"], accessKey) 155 if err != nil { 156 t.Fatalf("Error creating storage client for storage account %q: %s", conf["storage_account_name"], err) 157 } 158 blobClient := storageClient.GetBlobService() 159 _, err = blobClient.CreateContainerIfNotExists(conf["container_name"], mainStorage.ContainerAccessTypePrivate) 160 if err != nil { 161 t.Fatalf("Couldn't create container with name %s: %s.", conf["container_name"], err) 162 } 163 } 164 165 func teardown(t *testing.T, conf map[string]string) { 166 creds, err := getCredentialsFromConf(conf) 167 if err != nil { 168 t.Fatalf("Error getting credentials from conf: %v", err) 169 } 170 rivieraClient, err := getRivieraClient(creds) 171 if err != nil { 172 t.Fatalf("Error instantiating the riviera client: %v", err) 173 } 174 175 r := rivieraClient.NewRequest() 176 r.Command = riviera.DeleteResourceGroup{ 177 Name: conf["resource_group_name"], 178 } 179 response, err := r.Execute() 180 if err != nil { 181 t.Fatalf("Error deleting the resource group: %v", err) 182 } 183 if !response.IsSuccessful() { 184 t.Fatalf("Error deleting the resource group: %v", err) 185 } 186 } 187 188 func getRivieraClient(credentials *riviera.AzureResourceManagerCredentials) (*riviera.Client, error) { 189 rivieraClient, err := riviera.NewClient(credentials) 190 if err != nil { 191 return nil, fmt.Errorf("Error creating Riviera client: %s", err) 192 } 193 194 request := rivieraClient.NewRequest() 195 request.Command = riviera.RegisterResourceProvider{ 196 Namespace: "Microsoft.Storage", 197 } 198 199 response, err := request.Execute() 200 if err != nil { 201 return nil, fmt.Errorf("Cannot request provider registration for Azure Resource Manager: %s.", err) 202 } 203 204 if !response.IsSuccessful() { 205 return nil, fmt.Errorf("Credentials for acessing the Azure Resource Manager API are likely " + 206 "to be incorrect, or\n the service principal does not have permission to use " + 207 "the Azure Service Management\n API.") 208 } 209 210 return rivieraClient, nil 211 }