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.curoverse.com/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
45 "git.curoverse.com/arvados.git/lib/cloud"
46 "git.curoverse.com/arvados.git/sdk/go/arvados"
47 "git.curoverse.com/arvados.git/sdk/go/config"
48 "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-06-01/compute"
49 "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-06-01/network"
50 "github.com/Azure/azure-sdk-for-go/storage"
51 "github.com/Azure/go-autorest/autorest"
52 "github.com/Azure/go-autorest/autorest/azure"
53 "github.com/Azure/go-autorest/autorest/to"
54 "github.com/sirupsen/logrus"
55 "golang.org/x/crypto/ssh"
56 check "gopkg.in/check.v1"
59 // Gocheck boilerplate
60 func Test(t *testing.T) {
64 type AzureInstanceSetSuite struct{}
66 var _ = check.Suite(&AzureInstanceSetSuite{})
68 type VirtualMachinesClientStub struct{}
70 var testKey = []byte(`ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDLQS1ExT2+WjA0d/hntEAyAtgeN1W2ik2QX8c2zO6HjlPHWXL92r07W0WMuDib40Pcevpi1BXeBWXA9ZB5KKMJB+ukaAu22KklnQuUmNvk6ZXnPKSkGxuCYvPQb08WhHf3p1VxiKfP3iauedBDM4x9/bkJohlBBQiFXzNUcQ+a6rKiMzmJN2gbL8ncyUzc+XQ5q4JndTwTGtOlzDiGOc9O4z5Dd76wtAVJneOuuNpwfFRVHThpJM6VThpCZOnl8APaceWXKeuwOuCae3COZMz++xQfxOfZ9Z8aIwo+TlQhsRaNfZ4Vjrop6ej8dtfZtgUFKfbXEOYaHrGrWGotFDTD example@example`)
72 func (*VirtualMachinesClientStub) createOrUpdate(ctx context.Context,
73 resourceGroupName string,
75 parameters compute.VirtualMachine) (result compute.VirtualMachine, err error) {
76 parameters.ID = &VMName
77 parameters.Name = &VMName
78 return parameters, nil
81 func (*VirtualMachinesClientStub) delete(ctx context.Context, resourceGroupName string, VMName string) (result *http.Response, err error) {
85 func (*VirtualMachinesClientStub) listComplete(ctx context.Context, resourceGroupName string) (result compute.VirtualMachineListResultIterator, err error) {
86 return compute.VirtualMachineListResultIterator{}, nil
89 type InterfacesClientStub struct{}
91 func (*InterfacesClientStub) createOrUpdate(ctx context.Context,
92 resourceGroupName string,
94 parameters network.Interface) (result network.Interface, err error) {
95 parameters.ID = to.StringPtr(nicName)
96 (*parameters.IPConfigurations)[0].PrivateIPAddress = to.StringPtr("192.168.5.5")
97 return parameters, nil
100 func (*InterfacesClientStub) delete(ctx context.Context, resourceGroupName string, VMName string) (result *http.Response, err error) {
104 func (*InterfacesClientStub) listComplete(ctx context.Context, resourceGroupName string) (result network.InterfaceListResultIterator, err error) {
105 return network.InterfaceListResultIterator{}, nil
108 type BlobContainerStub struct{}
110 func (*BlobContainerStub) GetBlobReference(name string) *storage.Blob {
114 func (*BlobContainerStub) ListBlobs(params storage.ListBlobsParameters) (storage.BlobListResponse, error) {
115 return storage.BlobListResponse{}, nil
118 type testConfig struct {
119 ImageIDForTestSuite string
120 DriverParameters json.RawMessage
123 var live = flag.String("live-azure-cfg", "", "Test with real azure API, provide config file")
125 func GetInstanceSet() (cloud.InstanceSet, cloud.ImageID, arvados.Cluster, error) {
126 cluster := arvados.Cluster{
127 InstanceTypes: arvados.InstanceTypeMap(map[string]arvados.InstanceType{
128 "tiny": arvados.InstanceType{
130 ProviderType: "Standard_D1_v2",
133 Scratch: 10000000000,
139 var exampleCfg testConfig
140 err := config.LoadFile(&exampleCfg, *live)
142 return nil, cloud.ImageID(""), cluster, err
145 ap, err := newAzureInstanceSet(exampleCfg.DriverParameters, "test123", logrus.StandardLogger())
146 return ap, cloud.ImageID(exampleCfg.ImageIDForTestSuite), cluster, err
148 ap := azureInstanceSet{
149 azconfig: azureInstanceSetConfig{
150 BlobContainer: "vhds",
152 dispatcherID: "test123",
153 namePrefix: "compute-test123-",
154 logger: logrus.StandardLogger(),
155 deleteNIC: make(chan string),
156 deleteBlob: make(chan storage.Blob),
158 ap.ctx, ap.stopFunc = context.WithCancel(context.Background())
159 ap.vmClient = &VirtualMachinesClientStub{}
160 ap.netClient = &InterfacesClientStub{}
161 ap.blobcont = &BlobContainerStub{}
162 return &ap, cloud.ImageID("blob"), cluster, nil
165 func (*AzureInstanceSetSuite) TestCreate(c *check.C) {
166 ap, img, cluster, err := GetInstanceSet()
168 c.Fatal("Error making provider", err)
171 pk, _, _, _, err := ssh.ParseAuthorizedKey(testKey)
172 c.Assert(err, check.IsNil)
174 inst, err := ap.Create(cluster.InstanceTypes["tiny"],
175 img, map[string]string{
176 "TestTagName": "test tag value",
177 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
179 c.Assert(err, check.IsNil)
182 c.Check(tags["TestTagName"], check.Equals, "test tag value")
183 c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
187 func (*AzureInstanceSetSuite) TestListInstances(c *check.C) {
188 ap, _, _, err := GetInstanceSet()
190 c.Fatal("Error making provider", err)
193 l, err := ap.Instances(nil)
195 c.Assert(err, check.IsNil)
197 for _, i := range l {
199 log.Printf("%v %v %v", i.String(), i.Address(), tg)
203 func (*AzureInstanceSetSuite) TestManageNics(c *check.C) {
204 ap, _, _, err := GetInstanceSet()
206 c.Fatal("Error making provider", err)
209 ap.(*azureInstanceSet).manageNics()
213 func (*AzureInstanceSetSuite) TestManageBlobs(c *check.C) {
214 ap, _, _, err := GetInstanceSet()
216 c.Fatal("Error making provider", err)
219 ap.(*azureInstanceSet).manageBlobs()
223 func (*AzureInstanceSetSuite) TestDestroyInstances(c *check.C) {
224 ap, _, _, err := GetInstanceSet()
226 c.Fatal("Error making provider", err)
229 l, err := ap.Instances(nil)
230 c.Assert(err, check.IsNil)
232 for _, i := range l {
233 c.Check(i.Destroy(), check.IsNil)
237 func (*AzureInstanceSetSuite) TestDeleteFake(c *check.C) {
238 ap, _, _, err := GetInstanceSet()
240 c.Fatal("Error making provider", err)
243 _, err = ap.(*azureInstanceSet).netClient.delete(context.Background(), "fakefakefake", "fakefakefake")
245 de, ok := err.(autorest.DetailedError)
247 rq := de.Original.(*azure.RequestError)
249 log.Printf("%v %q %q", rq.Response.StatusCode, rq.ServiceError.Code, rq.ServiceError.Message)
253 func (*AzureInstanceSetSuite) TestWrapError(c *check.C) {
254 retryError := autorest.DetailedError{
255 Original: &azure.RequestError{
256 DetailedError: autorest.DetailedError{
257 Response: &http.Response{
259 Header: map[string][]string{"Retry-After": []string{"123"}},
262 ServiceError: &azure.ServiceError{},
265 wrapped := wrapAzureError(retryError)
266 _, ok := wrapped.(cloud.RateLimitError)
267 c.Check(ok, check.Equals, true)
269 quotaError := autorest.DetailedError{
270 Original: &azure.RequestError{
271 DetailedError: autorest.DetailedError{
272 Response: &http.Response{
276 ServiceError: &azure.ServiceError{
277 Message: "No more quota",
281 wrapped = wrapAzureError(quotaError)
282 _, ok = wrapped.(cloud.QuotaError)
283 c.Check(ok, check.Equals, true)
286 func (*AzureInstanceSetSuite) TestSetTags(c *check.C) {
287 ap, _, _, err := GetInstanceSet()
289 c.Fatal("Error making provider", err)
291 l, err := ap.Instances(nil)
292 c.Assert(err, check.IsNil)
295 err = l[0].SetTags(map[string]string{"foo": "bar"})
297 c.Fatal("Error setting tags", err)
300 l, err = ap.Instances(nil)
301 c.Assert(err, check.IsNil)
305 log.Printf("tags are %v", tg)
309 func (*AzureInstanceSetSuite) TestSSH(c *check.C) {
310 ap, _, _, err := GetInstanceSet()
312 c.Fatal("Error making provider", err)
314 l, err := ap.Instances(nil)
315 c.Assert(err, check.IsNil)
318 sshclient, err := SetupSSHClient(c, l[0])
319 c.Assert(err, check.IsNil)
320 defer sshclient.Conn.Close()
322 sess, err := sshclient.NewSession()
323 c.Assert(err, check.IsNil)
325 _, err = sess.Output("find /var/run/test-file -maxdepth 0 -user root -perm 0600")
326 c.Assert(err, check.IsNil)
328 sess, err = sshclient.NewSession()
329 c.Assert(err, check.IsNil)
331 out, err := sess.Output("sudo cat /var/run/test-file")
332 c.Assert(err, check.IsNil)
333 c.Check(string(out), check.Equals, "test-file-data")
337 func SetupSSHClient(c *check.C, inst cloud.Instance) (*ssh.Client, error) {
338 addr := inst.Address() + ":2222"
340 return nil, errors.New("instance has no address")
343 f, err := os.Open("azconfig_sshkey")
344 c.Assert(err, check.IsNil)
346 keybytes, err := ioutil.ReadAll(f)
347 c.Assert(err, check.IsNil)
349 priv, err := ssh.ParsePrivateKey(keybytes)
350 c.Assert(err, check.IsNil)
352 var receivedKey ssh.PublicKey
353 client, err := ssh.Dial("tcp", addr, &ssh.ClientConfig{
355 Auth: []ssh.AuthMethod{
356 ssh.PublicKeys(priv),
358 HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
362 Timeout: time.Minute,
367 } else if receivedKey == nil {
368 return nil, errors.New("BUG: key was never provided to HostKeyCallback")
371 err = inst.VerifyHostKey(receivedKey, client)
372 c.Assert(err, check.IsNil)