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 ProviderType: "Standard_D1_v2",
144 Scratch: 10000000000,
150 var exampleCfg testConfig
151 err := config.LoadFile(&exampleCfg, *live)
153 return nil, cloud.ImageID(""), cluster, err
156 ap, err := newAzureInstanceSet(exampleCfg.DriverParameters, "test123", nil, logrus.StandardLogger())
157 return ap, cloud.ImageID(exampleCfg.ImageIDForTestSuite), cluster, err
159 ap := azureInstanceSet{
160 azconfig: azureInstanceSetConfig{
161 BlobContainer: "vhds",
163 dispatcherID: "test123",
164 namePrefix: testNamePrefix,
165 logger: logrus.StandardLogger(),
166 deleteNIC: make(chan string),
167 deleteBlob: make(chan storage.Blob),
168 deleteDisk: make(chan compute.Disk),
170 ap.ctx, ap.stopFunc = context.WithCancel(context.Background())
171 ap.vmClient = &VirtualMachinesClientStub{}
172 ap.netClient = &InterfacesClientStub{}
173 ap.blobcont = &BlobContainerStub{}
174 return &ap, cloud.ImageID("blob"), cluster, nil
177 func (*AzureInstanceSetSuite) TestCreate(c *check.C) {
178 ap, img, cluster, err := GetInstanceSet()
180 c.Fatal("Error making provider", err)
183 pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
184 c.Assert(err, check.IsNil)
186 inst, err := ap.Create(cluster.InstanceTypes["tiny"],
187 img, map[string]string{
188 "TestTagName": "test tag value",
189 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
191 c.Assert(err, check.IsNil)
194 c.Check(tags["TestTagName"], check.Equals, "test tag value")
195 c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
197 instPreemptable, err := ap.Create(cluster.InstanceTypes["tinyp"],
198 img, map[string]string{
199 "TestTagName": "test tag value",
200 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
202 c.Assert(err, check.IsNil)
204 tags = instPreemptable.Tags()
205 c.Check(tags["TestTagName"], check.Equals, "test tag value")
206 c.Logf("instPreemptable.String()=%v Address()=%v Tags()=%v", instPreemptable.String(), instPreemptable.Address(), tags)
210 func (*AzureInstanceSetSuite) TestListInstances(c *check.C) {
211 ap, _, _, err := GetInstanceSet()
213 c.Fatal("Error making provider", err)
216 l, err := ap.Instances(nil)
218 c.Assert(err, check.IsNil)
220 for _, i := range l {
222 log.Printf("%v %v %v", i.String(), i.Address(), tg)
226 func (*AzureInstanceSetSuite) TestManageNics(c *check.C) {
227 ap, _, _, err := GetInstanceSet()
229 c.Fatal("Error making provider", err)
232 ap.(*azureInstanceSet).manageNics()
236 func (*AzureInstanceSetSuite) TestManageBlobs(c *check.C) {
237 ap, _, _, err := GetInstanceSet()
239 c.Fatal("Error making provider", err)
242 ap.(*azureInstanceSet).manageBlobs()
246 func (*AzureInstanceSetSuite) TestDestroyInstances(c *check.C) {
247 ap, _, _, err := GetInstanceSet()
249 c.Fatal("Error making provider", err)
252 l, err := ap.Instances(nil)
253 c.Assert(err, check.IsNil)
255 for _, i := range filterInstances(c, l) {
256 c.Check(i.Destroy(), check.IsNil)
260 func (*AzureInstanceSetSuite) TestDeleteFake(c *check.C) {
261 ap, _, _, err := GetInstanceSet()
263 c.Fatal("Error making provider", err)
266 _, err = ap.(*azureInstanceSet).netClient.delete(context.Background(), "fakefakefake", "fakefakefake")
268 de, ok := err.(autorest.DetailedError)
270 rq := de.Original.(*azure.RequestError)
272 log.Printf("%v %q %q", rq.Response.StatusCode, rq.ServiceError.Code, rq.ServiceError.Message)
276 func (*AzureInstanceSetSuite) TestWrapError(c *check.C) {
277 retryError := autorest.DetailedError{
278 Original: &azure.RequestError{
279 DetailedError: autorest.DetailedError{
280 Response: &http.Response{
282 Header: map[string][]string{"Retry-After": {"123"}},
285 ServiceError: &azure.ServiceError{},
288 wrapped := wrapAzureError(retryError)
289 _, ok := wrapped.(cloud.RateLimitError)
290 c.Check(ok, check.Equals, true)
292 quotaError := autorest.DetailedError{
293 Original: &azure.RequestError{
294 DetailedError: autorest.DetailedError{
295 Response: &http.Response{
299 ServiceError: &azure.ServiceError{
300 Message: "No more quota",
304 wrapped = wrapAzureError(quotaError)
305 _, ok = wrapped.(cloud.QuotaError)
306 c.Check(ok, check.Equals, true)
309 func (*AzureInstanceSetSuite) TestSetTags(c *check.C) {
310 ap, _, _, err := GetInstanceSet()
312 c.Fatal("Error making provider", err)
315 l, err := ap.Instances(nil)
316 c.Assert(err, check.IsNil)
317 l = filterInstances(c, l)
319 err = l[0].SetTags(map[string]string{"foo": "bar"})
321 c.Fatal("Error setting tags", err)
325 l, err = ap.Instances(nil)
326 c.Assert(err, check.IsNil)
327 l = filterInstances(c, l)
331 log.Printf("tags are %v", tg)
335 func (*AzureInstanceSetSuite) TestSSH(c *check.C) {
336 ap, _, _, err := GetInstanceSet()
338 c.Fatal("Error making provider", err)
340 l, err := ap.Instances(nil)
341 c.Assert(err, check.IsNil)
342 l = filterInstances(c, l)
345 sshclient, err := SetupSSHClient(c, l[0])
346 c.Assert(err, check.IsNil)
347 defer sshclient.Conn.Close()
349 sess, err := sshclient.NewSession()
350 c.Assert(err, check.IsNil)
352 _, err = sess.Output("find /var/run/test-file -maxdepth 0 -user root -perm 0600")
353 c.Assert(err, check.IsNil)
355 sess, err = sshclient.NewSession()
356 c.Assert(err, check.IsNil)
358 out, err := sess.Output("sudo cat /var/run/test-file")
359 c.Assert(err, check.IsNil)
360 c.Check(string(out), check.Equals, "test-file-data")
364 func SetupSSHClient(c *check.C, inst cloud.Instance) (*ssh.Client, error) {
365 addr := inst.Address() + ":2222"
367 return nil, errors.New("instance has no address")
370 f, err := os.Open("azconfig_sshkey")
371 c.Assert(err, check.IsNil)
373 keybytes, err := ioutil.ReadAll(f)
374 c.Assert(err, check.IsNil)
376 priv, err := ssh.ParsePrivateKey(keybytes)
377 c.Assert(err, check.IsNil)
379 var receivedKey ssh.PublicKey
380 client, err := ssh.Dial("tcp", addr, &ssh.ClientConfig{
382 Auth: []ssh.AuthMethod{
383 ssh.PublicKeys(priv),
385 HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
389 Timeout: time.Minute,
394 } else if receivedKey == nil {
395 return nil, errors.New("BUG: key was never provided to HostKeyCallback")
398 err = inst.VerifyHostKey(receivedKey, client)
399 c.Assert(err, check.IsNil)
404 func filterInstances(c *check.C, instances []cloud.Instance) []cloud.Instance {
405 var r []cloud.Instance
406 for _, i := range instances {
407 if !strings.HasPrefix(i.String(), testNamePrefix) {
408 c.Logf("ignoring instance %s", i)