19127: do not dump InstanceTypes...Name and InstanceTypes...Scratch,
[arvados.git] / lib / cloud / azure / azure_test.go
index 61649c39800d7ad8e31252313285e0e85fe7b16c..b6aa9a16b6187b4fd5486037076885fe4eb69d19 100644 (file)
@@ -5,7 +5,7 @@
 //
 // How to manually run individual tests against the real cloud:
 //
-// $ go test -v git.curoverse.com/arvados.git/lib/cloud/azure -live-azure-cfg azconfig.yml -check.f=TestCreate
+// $ go test -v git.arvados.org/arvados.git/lib/cloud/azure -live-azure-cfg azconfig.yml -check.f=TestCreate
 //
 // Tests should be run individually and in the order they are listed in the file:
 //
@@ -39,13 +39,15 @@ import (
        "net"
        "net/http"
        "os"
+       "strings"
        "testing"
        "time"
 
-       "git.curoverse.com/arvados.git/lib/cloud"
-       "git.curoverse.com/arvados.git/sdk/go/arvados"
-       "git.curoverse.com/arvados.git/sdk/go/config"
-       "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-06-01/compute"
+       "git.arvados.org/arvados.git/lib/cloud"
+       "git.arvados.org/arvados.git/lib/dispatchcloud/test"
+       "git.arvados.org/arvados.git/sdk/go/arvados"
+       "git.arvados.org/arvados.git/sdk/go/config"
+       "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
        "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-06-01/network"
        "github.com/Azure/azure-sdk-for-go/storage"
        "github.com/Azure/go-autorest/autorest"
@@ -65,9 +67,9 @@ type AzureInstanceSetSuite struct{}
 
 var _ = check.Suite(&AzureInstanceSetSuite{})
 
-type VirtualMachinesClientStub struct{}
+const testNamePrefix = "compute-test123-"
 
-var testKey = []byte(`ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDLQS1ExT2+WjA0d/hntEAyAtgeN1W2ik2QX8c2zO6HjlPHWXL92r07W0WMuDib40Pcevpi1BXeBWXA9ZB5KKMJB+ukaAu22KklnQuUmNvk6ZXnPKSkGxuCYvPQb08WhHf3p1VxiKfP3iauedBDM4x9/bkJohlBBQiFXzNUcQ+a6rKiMzmJN2gbL8ncyUzc+XQ5q4JndTwTGtOlzDiGOc9O4z5Dd76wtAVJneOuuNpwfFRVHThpJM6VThpCZOnl8APaceWXKeuwOuCae3COZMz++xQfxOfZ9Z8aIwo+TlQhsRaNfZ4Vjrop6ej8dtfZtgUFKfbXEOYaHrGrWGotFDTD example@example`)
+type VirtualMachinesClientStub struct{}
 
 func (*VirtualMachinesClientStub) createOrUpdate(ctx context.Context,
        resourceGroupName string,
@@ -105,6 +107,16 @@ func (*InterfacesClientStub) listComplete(ctx context.Context, resourceGroupName
        return network.InterfaceListResultIterator{}, nil
 }
 
+type BlobContainerStub struct{}
+
+func (*BlobContainerStub) GetBlobReference(name string) *storage.Blob {
+       return nil
+}
+
+func (*BlobContainerStub) ListBlobs(params storage.ListBlobsParameters) (storage.BlobListResponse, error) {
+       return storage.BlobListResponse{}, nil
+}
+
 type testConfig struct {
        ImageIDForTestSuite string
        DriverParameters    json.RawMessage
@@ -115,7 +127,7 @@ var live = flag.String("live-azure-cfg", "", "Test with real azure API, provide
 func GetInstanceSet() (cloud.InstanceSet, cloud.ImageID, arvados.Cluster, error) {
        cluster := arvados.Cluster{
                InstanceTypes: arvados.InstanceTypeMap(map[string]arvados.InstanceType{
-                       "tiny": arvados.InstanceType{
+                       "tiny": {
                                Name:         "tiny",
                                ProviderType: "Standard_D1_v2",
                                VCPUs:        1,
@@ -124,6 +136,15 @@ func GetInstanceSet() (cloud.InstanceSet, cloud.ImageID, arvados.Cluster, error)
                                Price:        .02,
                                Preemptible:  false,
                        },
+                       "tinyp": {
+                               Name:         "tiny",
+                               ProviderType: "Standard_D1_v2",
+                               VCPUs:        1,
+                               RAM:          4000000000,
+                               Scratch:      10000000000,
+                               Price:        .002,
+                               Preemptible:  true,
+                       },
                })}
        if *live != "" {
                var exampleCfg testConfig
@@ -132,7 +153,7 @@ func GetInstanceSet() (cloud.InstanceSet, cloud.ImageID, arvados.Cluster, error)
                        return nil, cloud.ImageID(""), cluster, err
                }
 
-               ap, err := newAzureInstanceSet(exampleCfg.DriverParameters, "test123", logrus.StandardLogger())
+               ap, err := newAzureInstanceSet(exampleCfg.DriverParameters, "test123", nil, logrus.StandardLogger())
                return ap, cloud.ImageID(exampleCfg.ImageIDForTestSuite), cluster, err
        }
        ap := azureInstanceSet{
@@ -140,14 +161,16 @@ func GetInstanceSet() (cloud.InstanceSet, cloud.ImageID, arvados.Cluster, error)
                        BlobContainer: "vhds",
                },
                dispatcherID: "test123",
-               namePrefix:   "compute-test123-",
+               namePrefix:   testNamePrefix,
                logger:       logrus.StandardLogger(),
                deleteNIC:    make(chan string),
                deleteBlob:   make(chan storage.Blob),
+               deleteDisk:   make(chan compute.Disk),
        }
        ap.ctx, ap.stopFunc = context.WithCancel(context.Background())
        ap.vmClient = &VirtualMachinesClientStub{}
        ap.netClient = &InterfacesClientStub{}
+       ap.blobcont = &BlobContainerStub{}
        return &ap, cloud.ImageID("blob"), cluster, nil
 }
 
@@ -157,7 +180,7 @@ func (*AzureInstanceSetSuite) TestCreate(c *check.C) {
                c.Fatal("Error making provider", err)
        }
 
-       pk, _, _, _, err := ssh.ParseAuthorizedKey(testKey)
+       pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
        c.Assert(err, check.IsNil)
 
        inst, err := ap.Create(cluster.InstanceTypes["tiny"],
@@ -171,6 +194,17 @@ func (*AzureInstanceSetSuite) TestCreate(c *check.C) {
        c.Check(tags["TestTagName"], check.Equals, "test tag value")
        c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
 
+       instPreemptable, err := ap.Create(cluster.InstanceTypes["tinyp"],
+               img, map[string]string{
+                       "TestTagName": "test tag value",
+               }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
+
+       c.Assert(err, check.IsNil)
+
+       tags = instPreemptable.Tags()
+       c.Check(tags["TestTagName"], check.Equals, "test tag value")
+       c.Logf("instPreemptable.String()=%v Address()=%v Tags()=%v", instPreemptable.String(), instPreemptable.Address(), tags)
+
 }
 
 func (*AzureInstanceSetSuite) TestListInstances(c *check.C) {
@@ -218,7 +252,7 @@ func (*AzureInstanceSetSuite) TestDestroyInstances(c *check.C) {
        l, err := ap.Instances(nil)
        c.Assert(err, check.IsNil)
 
-       for _, i := range l {
+       for _, i := range filterInstances(c, l) {
                c.Check(i.Destroy(), check.IsNil)
        }
 }
@@ -245,7 +279,7 @@ func (*AzureInstanceSetSuite) TestWrapError(c *check.C) {
                        DetailedError: autorest.DetailedError{
                                Response: &http.Response{
                                        StatusCode: 429,
-                                       Header:     map[string][]string{"Retry-After": []string{"123"}},
+                                       Header:     map[string][]string{"Retry-After": {"123"}},
                                },
                        },
                        ServiceError: &azure.ServiceError{},
@@ -277,17 +311,20 @@ func (*AzureInstanceSetSuite) TestSetTags(c *check.C) {
        if err != nil {
                c.Fatal("Error making provider", err)
        }
+
        l, err := ap.Instances(nil)
        c.Assert(err, check.IsNil)
-
+       l = filterInstances(c, l)
        if len(l) > 0 {
                err = l[0].SetTags(map[string]string{"foo": "bar"})
                if err != nil {
                        c.Fatal("Error setting tags", err)
                }
        }
+
        l, err = ap.Instances(nil)
        c.Assert(err, check.IsNil)
+       l = filterInstances(c, l)
 
        if len(l) > 0 {
                tg := l[0].Tags()
@@ -302,6 +339,7 @@ func (*AzureInstanceSetSuite) TestSSH(c *check.C) {
        }
        l, err := ap.Instances(nil)
        c.Assert(err, check.IsNil)
+       l = filterInstances(c, l)
 
        if len(l) > 0 {
                sshclient, err := SetupSSHClient(c, l[0])
@@ -362,3 +400,15 @@ func SetupSSHClient(c *check.C, inst cloud.Instance) (*ssh.Client, error) {
 
        return client, nil
 }
+
+func filterInstances(c *check.C, instances []cloud.Instance) []cloud.Instance {
+       var r []cloud.Instance
+       for _, i := range instances {
+               if !strings.HasPrefix(i.String(), testNamePrefix) {
+                       c.Logf("ignoring instance %s", i)
+                       continue
+               }
+               r = append(r, i)
+       }
+       return r
+}