github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/state/remote/azure_test.go (about) 1 package remote 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 "testing" 8 "time" 9 10 mainStorage "github.com/Azure/azure-sdk-for-go/storage" 11 riviera "github.com/jen20/riviera/azure" 12 "github.com/jen20/riviera/storage" 13 ) 14 15 func TestAzureClient_impl(t *testing.T) { 16 var _ Client = new(AzureClient) 17 } 18 19 func TestAzureClient(t *testing.T) { 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 24 config := map[string]string{ 25 "arm_subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"), 26 "arm_client_id": os.Getenv("ARM_CLIENT_ID"), 27 "arm_client_secret": os.Getenv("ARM_CLIENT_SECRET"), 28 "arm_tenant_id": os.Getenv("ARM_TENANT_ID"), 29 } 30 31 for k, v := range config { 32 if v == "" { 33 t.Skipf("skipping; %s must be set", strings.ToUpper(k)) 34 } 35 } 36 37 config["resource_group_name"] = fmt.Sprintf("terraform-%x", time.Now().Unix()) 38 config["storage_account_name"] = fmt.Sprintf("terraform%x", time.Now().Unix()) 39 config["container_name"] = "terraform" 40 config["key"] = "test.tfstate" 41 42 setup(t, config) 43 defer teardown(t, config) 44 45 client, err := azureFactory(config) 46 if err != nil { 47 t.Fatalf("Error for valid config: %v", err) 48 } 49 50 testClient(t, client) 51 } 52 53 func setup(t *testing.T, conf map[string]string) { 54 creds, err := getCredentialsFromConf(conf) 55 if err != nil { 56 t.Fatalf("Error getting credentials from conf: %v", err) 57 } 58 rivieraClient, err := getRivieraClient(creds) 59 if err != nil { 60 t.Fatalf("Error instantiating the riviera client: %v", err) 61 } 62 63 // Create resource group 64 r := rivieraClient.NewRequest() 65 r.Command = riviera.CreateResourceGroup{ 66 Name: conf["resource_group_name"], 67 Location: riviera.WestUS, 68 } 69 response, err := r.Execute() 70 if err != nil { 71 t.Fatalf("Error creating a resource group: %v", err) 72 } 73 if !response.IsSuccessful() { 74 t.Fatalf("Error creating a resource group: %v", response.Error.Error()) 75 } 76 77 // Create storage account 78 r = rivieraClient.NewRequest() 79 r.Command = storage.CreateStorageAccount{ 80 ResourceGroupName: conf["resource_group_name"], 81 Name: conf["storage_account_name"], 82 AccountType: riviera.String("Standard_LRS"), 83 Location: riviera.WestUS, 84 } 85 response, err = r.Execute() 86 if err != nil { 87 t.Fatalf("Error creating a storage account: %v", err) 88 } 89 if !response.IsSuccessful() { 90 t.Fatalf("Error creating a storage account: %v", response.Error.Error()) 91 } 92 93 // Create container 94 accessKey, err := getStorageAccountAccessKey(conf, conf["resource_group_name"], conf["storage_account_name"]) 95 if err != nil { 96 t.Fatalf("Error creating a storage account: %v", err) 97 } 98 storageClient, err := mainStorage.NewBasicClient(conf["storage_account_name"], accessKey) 99 if err != nil { 100 t.Fatalf("Error creating storage client for storage account %q: %s", conf["storage_account_name"], err) 101 } 102 blobClient := storageClient.GetBlobService() 103 _, err = blobClient.CreateContainerIfNotExists(conf["container_name"], mainStorage.ContainerAccessTypePrivate) 104 if err != nil { 105 t.Fatalf("Couldn't create container with name %s: %s.", conf["container_name"], err) 106 } 107 } 108 109 func teardown(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 r := rivieraClient.NewRequest() 120 r.Command = riviera.DeleteResourceGroup{ 121 Name: conf["resource_group_name"], 122 } 123 response, err := r.Execute() 124 if err != nil { 125 t.Fatalf("Error deleting the resource group: %v", err) 126 } 127 if !response.IsSuccessful() { 128 t.Fatalf("Error deleting the resource group: %v", err) 129 } 130 } 131 132 func getRivieraClient(credentials *riviera.AzureResourceManagerCredentials) (*riviera.Client, error) { 133 rivieraClient, err := riviera.NewClient(credentials) 134 if err != nil { 135 return nil, fmt.Errorf("Error creating Riviera client: %s", err) 136 } 137 138 request := rivieraClient.NewRequest() 139 request.Command = riviera.RegisterResourceProvider{ 140 Namespace: "Microsoft.Storage", 141 } 142 143 response, err := request.Execute() 144 if err != nil { 145 return nil, fmt.Errorf("Cannot request provider registration for Azure Resource Manager: %s.", err) 146 } 147 148 if !response.IsSuccessful() { 149 return nil, fmt.Errorf("Credentials for acessing the Azure Resource Manager API are likely " + 150 "to be incorrect, or\n the service principal does not have permission to use " + 151 "the Azure Service Management\n API.") 152 } 153 154 return rivieraClient, nil 155 }