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