X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/a4fa040579473b598a532ee38173b1a28c6b1694..756b80504e55ff7d9b9ec3f221bd11e231e9c1c6:/lib/cloud/azure/azure_test.go diff --git a/lib/cloud/azure/azure_test.go b/lib/cloud/azure/azure_test.go index 850a3fb427..2f88f73440 100644 --- a/lib/cloud/azure/azure_test.go +++ b/lib/cloud/azure/azure_test.go @@ -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: // @@ -25,6 +25,7 @@ // StorageAccount: example // BlobContainer: vhds // DeleteDanglingResourcesAfter: 20s +// AdminUsername: crunch package azure @@ -38,19 +39,20 @@ 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" "github.com/Azure/go-autorest/autorest/azure" "github.com/Azure/go-autorest/autorest/to" - "github.com/jmcvetta/randutil" "github.com/sirupsen/logrus" "golang.org/x/crypto/ssh" check "gopkg.in/check.v1" @@ -65,16 +67,19 @@ 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 { + vmParameters compute.VirtualMachine +} -func (*VirtualMachinesClientStub) createOrUpdate(ctx context.Context, +func (stub *VirtualMachinesClientStub) createOrUpdate(ctx context.Context, resourceGroupName string, VMName string, parameters compute.VirtualMachine) (result compute.VirtualMachine, err error) { parameters.ID = &VMName parameters.Name = &VMName + stub.vmParameters = parameters return parameters, nil } @@ -105,6 +110,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 @@ -112,10 +127,10 @@ type testConfig struct { var live = flag.String("live-azure-cfg", "", "Test with real azure API, provide config file") -func GetInstanceSet() (cloud.InstanceSet, cloud.ImageID, arvados.Cluster, error) { +func GetInstanceSet() (*azureInstanceSet, 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 +139,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,22 +156,24 @@ func GetInstanceSet() (cloud.InstanceSet, cloud.ImageID, arvados.Cluster, error) return nil, cloud.ImageID(""), cluster, err } - ap, err := newAzureInstanceSet(exampleCfg.DriverParameters, "test123", logrus.StandardLogger()) - return ap, cloud.ImageID(exampleCfg.ImageIDForTestSuite), cluster, err + ap, err := newAzureInstanceSet(exampleCfg.DriverParameters, "test123", nil, logrus.StandardLogger()) + return ap.(*azureInstanceSet), cloud.ImageID(exampleCfg.ImageIDForTestSuite), cluster, err } ap := azureInstanceSet{ azconfig: azureInstanceSetConfig{ 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,22 +183,38 @@ func (*AzureInstanceSetSuite) TestCreate(c *check.C) { c.Fatal("Error making provider", err) } - pk, _, _, _, err := ssh.ParseAuthorizedKey(testKey) - c.Assert(err, check.IsNil) - - nodetoken, err := randutil.String(40, "abcdefghijklmnopqrstuvwxyz0123456789") + pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch") c.Assert(err, check.IsNil) inst, err := ap.Create(cluster.InstanceTypes["tiny"], img, map[string]string{ - "node-token": nodetoken}, - pk) + "TestTagName": "test tag value", + }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk) c.Assert(err, check.IsNil) - tg := inst.Tags() - log.Printf("Result %v %v %v", inst.String(), inst.Address(), tg) + tags := inst.Tags() + c.Check(tags["TestTagName"], check.Equals, "test tag value") + c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags) + if *live == "" { + c.Check(ap.vmClient.(*VirtualMachinesClientStub).vmParameters.VirtualMachineProperties.OsProfile.LinuxConfiguration.SSH, check.NotNil) + } + + 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", nil) + 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) + if *live == "" { + // Should not have set SSH option, because publickey + // arg was nil + c.Check(ap.vmClient.(*VirtualMachinesClientStub).vmParameters.VirtualMachineProperties.OsProfile.LinuxConfiguration.SSH, check.IsNil) + } } func (*AzureInstanceSetSuite) TestListInstances(c *check.C) { @@ -197,7 +239,7 @@ func (*AzureInstanceSetSuite) TestManageNics(c *check.C) { c.Fatal("Error making provider", err) } - ap.(*azureInstanceSet).manageNics() + ap.manageNics() ap.Stop() } @@ -207,7 +249,7 @@ func (*AzureInstanceSetSuite) TestManageBlobs(c *check.C) { c.Fatal("Error making provider", err) } - ap.(*azureInstanceSet).manageBlobs() + ap.manageBlobs() ap.Stop() } @@ -220,7 +262,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) } } @@ -231,7 +273,7 @@ func (*AzureInstanceSetSuite) TestDeleteFake(c *check.C) { c.Fatal("Error making provider", err) } - _, err = ap.(*azureInstanceSet).netClient.delete(context.Background(), "fakefakefake", "fakefakefake") + _, err = ap.netClient.delete(context.Background(), "fakefakefake", "fakefakefake") de, ok := err.(autorest.DetailedError) if ok { @@ -247,7 +289,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{}, @@ -279,17 +321,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() @@ -304,21 +349,25 @@ 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]) c.Assert(err, check.IsNil) + defer sshclient.Conn.Close() sess, err := sshclient.NewSession() c.Assert(err, check.IsNil) - - out, err := sess.Output("cat /home/crunch/node-token") + defer sess.Close() + _, err = sess.Output("find /var/run/test-file -maxdepth 0 -user root -perm 0600") c.Assert(err, check.IsNil) - log.Printf("%v", string(out)) - - sshclient.Conn.Close() + sess, err = sshclient.NewSession() + c.Assert(err, check.IsNil) + defer sess.Close() + out, err := sess.Output("sudo cat /var/run/test-file") + c.Assert(err, check.IsNil) + c.Check(string(out), check.Equals, "test-file-data") } } @@ -361,3 +410,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 +}