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 {
73 vmParameters compute.VirtualMachine
76 func (stub *VirtualMachinesClientStub) createOrUpdate(ctx context.Context,
77 resourceGroupName string,
79 parameters compute.VirtualMachine) (result compute.VirtualMachine, err error) {
80 parameters.ID = &VMName
81 parameters.Name = &VMName
82 stub.vmParameters = parameters
83 return parameters, nil
86 func (*VirtualMachinesClientStub) delete(ctx context.Context, resourceGroupName string, VMName string) (result *http.Response, err error) {
90 func (*VirtualMachinesClientStub) listComplete(ctx context.Context, resourceGroupName string) (result compute.VirtualMachineListResultIterator, err error) {
91 return compute.VirtualMachineListResultIterator{}, nil
94 type InterfacesClientStub struct{}
96 func (*InterfacesClientStub) createOrUpdate(ctx context.Context,
97 resourceGroupName string,
99 parameters network.Interface) (result network.Interface, err error) {
100 parameters.ID = to.StringPtr(nicName)
101 (*parameters.IPConfigurations)[0].PrivateIPAddress = to.StringPtr("192.168.5.5")
102 return parameters, nil
105 func (*InterfacesClientStub) delete(ctx context.Context, resourceGroupName string, VMName string) (result *http.Response, err error) {
109 func (*InterfacesClientStub) listComplete(ctx context.Context, resourceGroupName string) (result network.InterfaceListResultIterator, err error) {
110 return network.InterfaceListResultIterator{}, nil
113 type BlobContainerStub struct{}
115 func (*BlobContainerStub) GetBlobReference(name string) *storage.Blob {
119 func (*BlobContainerStub) ListBlobs(params storage.ListBlobsParameters) (storage.BlobListResponse, error) {
120 return storage.BlobListResponse{}, nil
123 type testConfig struct {
124 ImageIDForTestSuite string
125 DriverParameters json.RawMessage
128 var live = flag.String("live-azure-cfg", "", "Test with real azure API, provide config file")
130 func GetInstanceSet() (*azureInstanceSet, cloud.ImageID, arvados.Cluster, error) {
131 cluster := arvados.Cluster{
132 InstanceTypes: arvados.InstanceTypeMap(map[string]arvados.InstanceType{
135 ProviderType: "Standard_D1_v2",
138 Scratch: 10000000000,
144 ProviderType: "Standard_D1_v2",
147 Scratch: 10000000000,
153 var exampleCfg testConfig
154 err := config.LoadFile(&exampleCfg, *live)
156 return nil, cloud.ImageID(""), cluster, err
159 ap, err := newAzureInstanceSet(exampleCfg.DriverParameters, "test123", nil, logrus.StandardLogger(), nil)
160 return ap.(*azureInstanceSet), cloud.ImageID(exampleCfg.ImageIDForTestSuite), cluster, err
162 ap := azureInstanceSet{
163 azconfig: azureInstanceSetConfig{
164 BlobContainer: "vhds",
166 dispatcherID: "test123",
167 namePrefix: testNamePrefix,
168 logger: logrus.StandardLogger(),
169 deleteNIC: make(chan string),
170 deleteBlob: make(chan storage.Blob),
171 deleteDisk: make(chan compute.Disk),
173 ap.ctx, ap.stopFunc = context.WithCancel(context.Background())
174 ap.vmClient = &VirtualMachinesClientStub{}
175 ap.netClient = &InterfacesClientStub{}
176 ap.blobcont = &BlobContainerStub{}
177 return &ap, cloud.ImageID("blob"), cluster, nil
180 func (*AzureInstanceSetSuite) TestCreate(c *check.C) {
181 ap, img, cluster, err := GetInstanceSet()
183 c.Fatal("Error making provider", err)
186 pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
187 c.Assert(err, check.IsNil)
189 inst, err := ap.Create(cluster.InstanceTypes["tiny"],
190 img, map[string]string{
191 "TestTagName": "test tag value",
192 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
194 c.Assert(err, check.IsNil)
197 c.Check(tags["TestTagName"], check.Equals, "test tag value")
198 c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
200 c.Check(ap.vmClient.(*VirtualMachinesClientStub).vmParameters.VirtualMachineProperties.OsProfile.LinuxConfiguration.SSH, check.NotNil)
203 instPreemptable, err := ap.Create(cluster.InstanceTypes["tinyp"],
204 img, map[string]string{
205 "TestTagName": "test tag value",
206 }, "umask 0600; echo -n test-file-data >/var/run/test-file", nil)
208 c.Assert(err, check.IsNil)
210 tags = instPreemptable.Tags()
211 c.Check(tags["TestTagName"], check.Equals, "test tag value")
212 c.Logf("instPreemptable.String()=%v Address()=%v Tags()=%v", instPreemptable.String(), instPreemptable.Address(), tags)
214 // Should not have set SSH option, because publickey
216 c.Check(ap.vmClient.(*VirtualMachinesClientStub).vmParameters.VirtualMachineProperties.OsProfile.LinuxConfiguration.SSH, check.IsNil)
220 func (*AzureInstanceSetSuite) TestListInstances(c *check.C) {
221 ap, _, _, err := GetInstanceSet()
223 c.Fatal("Error making provider", err)
226 l, err := ap.Instances(nil)
228 c.Assert(err, check.IsNil)
230 for _, i := range l {
232 log.Printf("%v %v %v", i.String(), i.Address(), tg)
236 func (*AzureInstanceSetSuite) TestManageNics(c *check.C) {
237 ap, _, _, err := GetInstanceSet()
239 c.Fatal("Error making provider", err)
246 func (*AzureInstanceSetSuite) TestManageBlobs(c *check.C) {
247 ap, _, _, err := GetInstanceSet()
249 c.Fatal("Error making provider", err)
256 func (*AzureInstanceSetSuite) TestDestroyInstances(c *check.C) {
257 ap, _, _, err := GetInstanceSet()
259 c.Fatal("Error making provider", err)
262 l, err := ap.Instances(nil)
263 c.Assert(err, check.IsNil)
265 for _, i := range filterInstances(c, l) {
266 c.Check(i.Destroy(), check.IsNil)
270 func (*AzureInstanceSetSuite) TestDeleteFake(c *check.C) {
271 ap, _, _, err := GetInstanceSet()
273 c.Fatal("Error making provider", err)
276 _, err = ap.netClient.delete(context.Background(), "fakefakefake", "fakefakefake")
278 de, ok := err.(autorest.DetailedError)
280 rq := de.Original.(*azure.RequestError)
282 log.Printf("%v %q %q", rq.Response.StatusCode, rq.ServiceError.Code, rq.ServiceError.Message)
286 func (*AzureInstanceSetSuite) TestWrapError(c *check.C) {
287 retryError := autorest.DetailedError{
288 Original: &azure.RequestError{
289 DetailedError: autorest.DetailedError{
290 Response: &http.Response{
292 Header: map[string][]string{"Retry-After": {"123"}},
295 ServiceError: &azure.ServiceError{},
298 wrapped := wrapAzureError(retryError)
299 _, ok := wrapped.(cloud.RateLimitError)
300 c.Check(ok, check.Equals, true)
302 quotaError := autorest.DetailedError{
303 Original: &azure.RequestError{
304 DetailedError: autorest.DetailedError{
305 Response: &http.Response{
309 ServiceError: &azure.ServiceError{
310 Message: "No more quota",
314 wrapped = wrapAzureError(quotaError)
315 _, ok = wrapped.(cloud.QuotaError)
316 c.Check(ok, check.Equals, true)
319 func (*AzureInstanceSetSuite) TestSetTags(c *check.C) {
320 ap, _, _, err := GetInstanceSet()
322 c.Fatal("Error making provider", err)
325 l, err := ap.Instances(nil)
326 c.Assert(err, check.IsNil)
327 l = filterInstances(c, l)
329 err = l[0].SetTags(map[string]string{"foo": "bar"})
331 c.Fatal("Error setting tags", err)
335 l, err = ap.Instances(nil)
336 c.Assert(err, check.IsNil)
337 l = filterInstances(c, l)
341 log.Printf("tags are %v", tg)
345 func (*AzureInstanceSetSuite) TestSSH(c *check.C) {
346 ap, _, _, err := GetInstanceSet()
348 c.Fatal("Error making provider", err)
350 l, err := ap.Instances(nil)
351 c.Assert(err, check.IsNil)
352 l = filterInstances(c, l)
355 sshclient, err := SetupSSHClient(c, l[0])
356 c.Assert(err, check.IsNil)
357 defer sshclient.Conn.Close()
359 sess, err := sshclient.NewSession()
360 c.Assert(err, check.IsNil)
362 _, err = sess.Output("find /var/run/test-file -maxdepth 0 -user root -perm 0600")
363 c.Assert(err, check.IsNil)
365 sess, err = sshclient.NewSession()
366 c.Assert(err, check.IsNil)
368 out, err := sess.Output("sudo cat /var/run/test-file")
369 c.Assert(err, check.IsNil)
370 c.Check(string(out), check.Equals, "test-file-data")
374 func SetupSSHClient(c *check.C, inst cloud.Instance) (*ssh.Client, error) {
375 addr := inst.Address() + ":2222"
377 return nil, errors.New("instance has no address")
380 f, err := os.Open("azconfig_sshkey")
381 c.Assert(err, check.IsNil)
383 keybytes, err := ioutil.ReadAll(f)
384 c.Assert(err, check.IsNil)
386 priv, err := ssh.ParsePrivateKey(keybytes)
387 c.Assert(err, check.IsNil)
389 var receivedKey ssh.PublicKey
390 client, err := ssh.Dial("tcp", addr, &ssh.ClientConfig{
392 Auth: []ssh.AuthMethod{
393 ssh.PublicKeys(priv),
395 HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
399 Timeout: time.Minute,
404 } else if receivedKey == nil {
405 return nil, errors.New("BUG: key was never provided to HostKeyCallback")
408 err = inst.VerifyHostKey(receivedKey, client)
409 c.Assert(err, check.IsNil)
414 func filterInstances(c *check.C, instances []cloud.Instance) []cloud.Instance {
415 var r []cloud.Instance
416 for _, i := range instances {
417 if !strings.HasPrefix(i.String(), testNamePrefix) {
418 c.Logf("ignoring instance %s", i)