1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
6 // How to manually run individual tests against the real cloud:
8 // $ go test -v git.arvados.org/arvados.git/lib/cloud/azure -live-azure-cfg azconfig.yml -check.f=TestCreate
10 // Tests should be run individually and in the order they are listed in the file:
12 // Example azconfig.yml:
14 // ImageIDForTestSuite: "https://example.blob.core.windows.net/system/Microsoft.Compute/Images/images/zzzzz-compute-osDisk.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.vhd"
16 // SubscriptionID: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
17 // ClientID: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
18 // Location: centralus
19 // CloudEnvironment: AzurePublicCloud
20 // ClientSecret: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
21 // TenantId: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
22 // ResourceGroup: zzzzz
24 // Subnet: zzzzz-subnet-private
25 // StorageAccount: example
26 // BlobContainer: vhds
27 // DeleteDanglingResourcesAfter: 20s
28 // AdminUsername: crunch
46 "git.arvados.org/arvados.git/lib/cloud"
47 "git.arvados.org/arvados.git/lib/dispatchcloud/test"
48 "git.arvados.org/arvados.git/sdk/go/arvados"
49 "git.arvados.org/arvados.git/sdk/go/config"
50 "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-06-01/compute"
51 "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-06-01/network"
52 "github.com/Azure/azure-sdk-for-go/storage"
53 "github.com/Azure/go-autorest/autorest"
54 "github.com/Azure/go-autorest/autorest/azure"
55 "github.com/Azure/go-autorest/autorest/to"
56 "github.com/sirupsen/logrus"
57 "golang.org/x/crypto/ssh"
58 check "gopkg.in/check.v1"
61 // Gocheck boilerplate
62 func Test(t *testing.T) {
66 type AzureInstanceSetSuite struct{}
68 var _ = check.Suite(&AzureInstanceSetSuite{})
70 const testNamePrefix = "compute-test123-"
72 type VirtualMachinesClientStub struct{}
74 func (*VirtualMachinesClientStub) createOrUpdate(ctx context.Context,
75 resourceGroupName string,
77 parameters compute.VirtualMachine) (result compute.VirtualMachine, err error) {
78 parameters.ID = &VMName
79 parameters.Name = &VMName
80 return parameters, nil
83 func (*VirtualMachinesClientStub) delete(ctx context.Context, resourceGroupName string, VMName string) (result *http.Response, err error) {
87 func (*VirtualMachinesClientStub) listComplete(ctx context.Context, resourceGroupName string) (result compute.VirtualMachineListResultIterator, err error) {
88 return compute.VirtualMachineListResultIterator{}, nil
91 type InterfacesClientStub struct{}
93 func (*InterfacesClientStub) createOrUpdate(ctx context.Context,
94 resourceGroupName string,
96 parameters network.Interface) (result network.Interface, err error) {
97 parameters.ID = to.StringPtr(nicName)
98 (*parameters.IPConfigurations)[0].PrivateIPAddress = to.StringPtr("192.168.5.5")
99 return parameters, nil
102 func (*InterfacesClientStub) delete(ctx context.Context, resourceGroupName string, VMName string) (result *http.Response, err error) {
106 func (*InterfacesClientStub) listComplete(ctx context.Context, resourceGroupName string) (result network.InterfaceListResultIterator, err error) {
107 return network.InterfaceListResultIterator{}, nil
110 type BlobContainerStub struct{}
112 func (*BlobContainerStub) GetBlobReference(name string) *storage.Blob {
116 func (*BlobContainerStub) ListBlobs(params storage.ListBlobsParameters) (storage.BlobListResponse, error) {
117 return storage.BlobListResponse{}, nil
120 type testConfig struct {
121 ImageIDForTestSuite string
122 DriverParameters json.RawMessage
125 var live = flag.String("live-azure-cfg", "", "Test with real azure API, provide config file")
127 func GetInstanceSet() (cloud.InstanceSet, cloud.ImageID, arvados.Cluster, error) {
128 cluster := arvados.Cluster{
129 InstanceTypes: arvados.InstanceTypeMap(map[string]arvados.InstanceType{
130 "tiny": arvados.InstanceType{
132 ProviderType: "Standard_D1_v2",
135 Scratch: 10000000000,
141 var exampleCfg testConfig
142 err := config.LoadFile(&exampleCfg, *live)
144 return nil, cloud.ImageID(""), cluster, err
147 ap, err := newAzureInstanceSet(exampleCfg.DriverParameters, "test123", nil, logrus.StandardLogger())
148 return ap, cloud.ImageID(exampleCfg.ImageIDForTestSuite), cluster, err
150 ap := azureInstanceSet{
151 azconfig: azureInstanceSetConfig{
152 BlobContainer: "vhds",
154 dispatcherID: "test123",
155 namePrefix: testNamePrefix,
156 logger: logrus.StandardLogger(),
157 deleteNIC: make(chan string),
158 deleteBlob: make(chan storage.Blob),
160 ap.ctx, ap.stopFunc = context.WithCancel(context.Background())
161 ap.vmClient = &VirtualMachinesClientStub{}
162 ap.netClient = &InterfacesClientStub{}
163 ap.blobcont = &BlobContainerStub{}
164 return &ap, cloud.ImageID("blob"), cluster, nil
167 func (*AzureInstanceSetSuite) TestCreate(c *check.C) {
168 ap, img, cluster, err := GetInstanceSet()
170 c.Fatal("Error making provider", err)
173 pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
174 c.Assert(err, check.IsNil)
176 inst, err := ap.Create(cluster.InstanceTypes["tiny"],
177 img, map[string]string{
178 "TestTagName": "test tag value",
179 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
181 c.Assert(err, check.IsNil)
184 c.Check(tags["TestTagName"], check.Equals, "test tag value")
185 c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
189 func (*AzureInstanceSetSuite) TestListInstances(c *check.C) {
190 ap, _, _, err := GetInstanceSet()
192 c.Fatal("Error making provider", err)
195 l, err := ap.Instances(nil)
197 c.Assert(err, check.IsNil)
199 for _, i := range l {
201 log.Printf("%v %v %v", i.String(), i.Address(), tg)
205 func (*AzureInstanceSetSuite) TestManageNics(c *check.C) {
206 ap, _, _, err := GetInstanceSet()
208 c.Fatal("Error making provider", err)
211 ap.(*azureInstanceSet).manageNics()
215 func (*AzureInstanceSetSuite) TestManageBlobs(c *check.C) {
216 ap, _, _, err := GetInstanceSet()
218 c.Fatal("Error making provider", err)
221 ap.(*azureInstanceSet).manageBlobs()
225 func (*AzureInstanceSetSuite) TestDestroyInstances(c *check.C) {
226 ap, _, _, err := GetInstanceSet()
228 c.Fatal("Error making provider", err)
231 l, err := ap.Instances(nil)
232 c.Assert(err, check.IsNil)
234 for _, i := range filterInstances(c, l) {
235 c.Check(i.Destroy(), check.IsNil)
239 func (*AzureInstanceSetSuite) TestDeleteFake(c *check.C) {
240 ap, _, _, err := GetInstanceSet()
242 c.Fatal("Error making provider", err)
245 _, err = ap.(*azureInstanceSet).netClient.delete(context.Background(), "fakefakefake", "fakefakefake")
247 de, ok := err.(autorest.DetailedError)
249 rq := de.Original.(*azure.RequestError)
251 log.Printf("%v %q %q", rq.Response.StatusCode, rq.ServiceError.Code, rq.ServiceError.Message)
255 func (*AzureInstanceSetSuite) TestWrapError(c *check.C) {
256 retryError := autorest.DetailedError{
257 Original: &azure.RequestError{
258 DetailedError: autorest.DetailedError{
259 Response: &http.Response{
261 Header: map[string][]string{"Retry-After": []string{"123"}},
264 ServiceError: &azure.ServiceError{},
267 wrapped := wrapAzureError(retryError)
268 _, ok := wrapped.(cloud.RateLimitError)
269 c.Check(ok, check.Equals, true)
271 quotaError := autorest.DetailedError{
272 Original: &azure.RequestError{
273 DetailedError: autorest.DetailedError{
274 Response: &http.Response{
278 ServiceError: &azure.ServiceError{
279 Message: "No more quota",
283 wrapped = wrapAzureError(quotaError)
284 _, ok = wrapped.(cloud.QuotaError)
285 c.Check(ok, check.Equals, true)
288 func (*AzureInstanceSetSuite) TestSetTags(c *check.C) {
289 ap, _, _, err := GetInstanceSet()
291 c.Fatal("Error making provider", err)
294 l, err := ap.Instances(nil)
295 c.Assert(err, check.IsNil)
296 l = filterInstances(c, l)
298 err = l[0].SetTags(map[string]string{"foo": "bar"})
300 c.Fatal("Error setting tags", err)
304 l, err = ap.Instances(nil)
305 c.Assert(err, check.IsNil)
306 l = filterInstances(c, l)
310 log.Printf("tags are %v", tg)
314 func (*AzureInstanceSetSuite) TestSSH(c *check.C) {
315 ap, _, _, err := GetInstanceSet()
317 c.Fatal("Error making provider", err)
319 l, err := ap.Instances(nil)
320 c.Assert(err, check.IsNil)
321 l = filterInstances(c, l)
324 sshclient, err := SetupSSHClient(c, l[0])
325 c.Assert(err, check.IsNil)
326 defer sshclient.Conn.Close()
328 sess, err := sshclient.NewSession()
329 c.Assert(err, check.IsNil)
331 _, err = sess.Output("find /var/run/test-file -maxdepth 0 -user root -perm 0600")
332 c.Assert(err, check.IsNil)
334 sess, err = sshclient.NewSession()
335 c.Assert(err, check.IsNil)
337 out, err := sess.Output("sudo cat /var/run/test-file")
338 c.Assert(err, check.IsNil)
339 c.Check(string(out), check.Equals, "test-file-data")
343 func SetupSSHClient(c *check.C, inst cloud.Instance) (*ssh.Client, error) {
344 addr := inst.Address() + ":2222"
346 return nil, errors.New("instance has no address")
349 f, err := os.Open("azconfig_sshkey")
350 c.Assert(err, check.IsNil)
352 keybytes, err := ioutil.ReadAll(f)
353 c.Assert(err, check.IsNil)
355 priv, err := ssh.ParsePrivateKey(keybytes)
356 c.Assert(err, check.IsNil)
358 var receivedKey ssh.PublicKey
359 client, err := ssh.Dial("tcp", addr, &ssh.ClientConfig{
361 Auth: []ssh.AuthMethod{
362 ssh.PublicKeys(priv),
364 HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
368 Timeout: time.Minute,
373 } else if receivedKey == nil {
374 return nil, errors.New("BUG: key was never provided to HostKeyCallback")
377 err = inst.VerifyHostKey(receivedKey, client)
378 c.Assert(err, check.IsNil)
383 func filterInstances(c *check.C, instances []cloud.Instance) []cloud.Instance {
384 var r []cloud.Instance
385 for _, i := range instances {
386 if !strings.HasPrefix(i.String(), testNamePrefix) {
387 c.Logf("ignoring instance %s", i)