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/2019-07-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{
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),
159 deleteDisk: make(chan compute.Disk),
161 ap.ctx, ap.stopFunc = context.WithCancel(context.Background())
162 ap.vmClient = &VirtualMachinesClientStub{}
163 ap.netClient = &InterfacesClientStub{}
164 ap.blobcont = &BlobContainerStub{}
165 return &ap, cloud.ImageID("blob"), cluster, nil
168 func (*AzureInstanceSetSuite) TestCreate(c *check.C) {
169 ap, img, cluster, err := GetInstanceSet()
171 c.Fatal("Error making provider", err)
174 pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
175 c.Assert(err, check.IsNil)
177 inst, err := ap.Create(cluster.InstanceTypes["tiny"],
178 img, map[string]string{
179 "TestTagName": "test tag value",
180 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
182 c.Assert(err, check.IsNil)
185 c.Check(tags["TestTagName"], check.Equals, "test tag value")
186 c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
190 func (*AzureInstanceSetSuite) TestListInstances(c *check.C) {
191 ap, _, _, err := GetInstanceSet()
193 c.Fatal("Error making provider", err)
196 l, err := ap.Instances(nil)
198 c.Assert(err, check.IsNil)
200 for _, i := range l {
202 log.Printf("%v %v %v", i.String(), i.Address(), tg)
206 func (*AzureInstanceSetSuite) TestManageNics(c *check.C) {
207 ap, _, _, err := GetInstanceSet()
209 c.Fatal("Error making provider", err)
212 ap.(*azureInstanceSet).manageNics()
216 func (*AzureInstanceSetSuite) TestManageBlobs(c *check.C) {
217 ap, _, _, err := GetInstanceSet()
219 c.Fatal("Error making provider", err)
222 ap.(*azureInstanceSet).manageBlobs()
226 func (*AzureInstanceSetSuite) TestDestroyInstances(c *check.C) {
227 ap, _, _, err := GetInstanceSet()
229 c.Fatal("Error making provider", err)
232 l, err := ap.Instances(nil)
233 c.Assert(err, check.IsNil)
235 for _, i := range filterInstances(c, l) {
236 c.Check(i.Destroy(), check.IsNil)
240 func (*AzureInstanceSetSuite) TestDeleteFake(c *check.C) {
241 ap, _, _, err := GetInstanceSet()
243 c.Fatal("Error making provider", err)
246 _, err = ap.(*azureInstanceSet).netClient.delete(context.Background(), "fakefakefake", "fakefakefake")
248 de, ok := err.(autorest.DetailedError)
250 rq := de.Original.(*azure.RequestError)
252 log.Printf("%v %q %q", rq.Response.StatusCode, rq.ServiceError.Code, rq.ServiceError.Message)
256 func (*AzureInstanceSetSuite) TestWrapError(c *check.C) {
257 retryError := autorest.DetailedError{
258 Original: &azure.RequestError{
259 DetailedError: autorest.DetailedError{
260 Response: &http.Response{
262 Header: map[string][]string{"Retry-After": {"123"}},
265 ServiceError: &azure.ServiceError{},
268 wrapped := wrapAzureError(retryError)
269 _, ok := wrapped.(cloud.RateLimitError)
270 c.Check(ok, check.Equals, true)
272 quotaError := autorest.DetailedError{
273 Original: &azure.RequestError{
274 DetailedError: autorest.DetailedError{
275 Response: &http.Response{
279 ServiceError: &azure.ServiceError{
280 Message: "No more quota",
284 wrapped = wrapAzureError(quotaError)
285 _, ok = wrapped.(cloud.QuotaError)
286 c.Check(ok, check.Equals, true)
289 func (*AzureInstanceSetSuite) TestSetTags(c *check.C) {
290 ap, _, _, err := GetInstanceSet()
292 c.Fatal("Error making provider", err)
295 l, err := ap.Instances(nil)
296 c.Assert(err, check.IsNil)
297 l = filterInstances(c, l)
299 err = l[0].SetTags(map[string]string{"foo": "bar"})
301 c.Fatal("Error setting tags", err)
305 l, err = ap.Instances(nil)
306 c.Assert(err, check.IsNil)
307 l = filterInstances(c, l)
311 log.Printf("tags are %v", tg)
315 func (*AzureInstanceSetSuite) TestSSH(c *check.C) {
316 ap, _, _, err := GetInstanceSet()
318 c.Fatal("Error making provider", err)
320 l, err := ap.Instances(nil)
321 c.Assert(err, check.IsNil)
322 l = filterInstances(c, l)
325 sshclient, err := SetupSSHClient(c, l[0])
326 c.Assert(err, check.IsNil)
327 defer sshclient.Conn.Close()
329 sess, err := sshclient.NewSession()
330 c.Assert(err, check.IsNil)
332 _, err = sess.Output("find /var/run/test-file -maxdepth 0 -user root -perm 0600")
333 c.Assert(err, check.IsNil)
335 sess, err = sshclient.NewSession()
336 c.Assert(err, check.IsNil)
338 out, err := sess.Output("sudo cat /var/run/test-file")
339 c.Assert(err, check.IsNil)
340 c.Check(string(out), check.Equals, "test-file-data")
344 func SetupSSHClient(c *check.C, inst cloud.Instance) (*ssh.Client, error) {
345 addr := inst.Address() + ":2222"
347 return nil, errors.New("instance has no address")
350 f, err := os.Open("azconfig_sshkey")
351 c.Assert(err, check.IsNil)
353 keybytes, err := ioutil.ReadAll(f)
354 c.Assert(err, check.IsNil)
356 priv, err := ssh.ParsePrivateKey(keybytes)
357 c.Assert(err, check.IsNil)
359 var receivedKey ssh.PublicKey
360 client, err := ssh.Dial("tcp", addr, &ssh.ClientConfig{
362 Auth: []ssh.AuthMethod{
363 ssh.PublicKeys(priv),
365 HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
369 Timeout: time.Minute,
374 } else if receivedKey == nil {
375 return nil, errors.New("BUG: key was never provided to HostKeyCallback")
378 err = inst.VerifyHostKey(receivedKey, client)
379 c.Assert(err, check.IsNil)
384 func filterInstances(c *check.C, instances []cloud.Instance) []cloud.Instance {
385 var r []cloud.Instance
386 for _, i := range instances {
387 if !strings.HasPrefix(i.String(), testNamePrefix) {
388 c.Logf("ignoring instance %s", i)