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/lib/dispatchcloud/test"
47 "git.curoverse.com/arvados.git/sdk/go/arvados"
48 "git.curoverse.com/arvados.git/sdk/go/config"
49 "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-06-01/compute"
50 "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-06-01/network"
51 "github.com/Azure/azure-sdk-for-go/storage"
52 "github.com/Azure/go-autorest/autorest"
53 "github.com/Azure/go-autorest/autorest/azure"
54 "github.com/Azure/go-autorest/autorest/to"
55 "github.com/sirupsen/logrus"
56 "golang.org/x/crypto/ssh"
57 check "gopkg.in/check.v1"
60 // Gocheck boilerplate
61 func Test(t *testing.T) {
65 type AzureInstanceSetSuite struct{}
67 var _ = check.Suite(&AzureInstanceSetSuite{})
69 type VirtualMachinesClientStub struct{}
71 func (*VirtualMachinesClientStub) createOrUpdate(ctx context.Context,
72 resourceGroupName string,
74 parameters compute.VirtualMachine) (result compute.VirtualMachine, err error) {
75 parameters.ID = &VMName
76 parameters.Name = &VMName
77 return parameters, nil
80 func (*VirtualMachinesClientStub) delete(ctx context.Context, resourceGroupName string, VMName string) (result *http.Response, err error) {
84 func (*VirtualMachinesClientStub) listComplete(ctx context.Context, resourceGroupName string) (result compute.VirtualMachineListResultIterator, err error) {
85 return compute.VirtualMachineListResultIterator{}, nil
88 type InterfacesClientStub struct{}
90 func (*InterfacesClientStub) createOrUpdate(ctx context.Context,
91 resourceGroupName string,
93 parameters network.Interface) (result network.Interface, err error) {
94 parameters.ID = to.StringPtr(nicName)
95 (*parameters.IPConfigurations)[0].PrivateIPAddress = to.StringPtr("192.168.5.5")
96 return parameters, nil
99 func (*InterfacesClientStub) delete(ctx context.Context, resourceGroupName string, VMName string) (result *http.Response, err error) {
103 func (*InterfacesClientStub) listComplete(ctx context.Context, resourceGroupName string) (result network.InterfaceListResultIterator, err error) {
104 return network.InterfaceListResultIterator{}, nil
107 type BlobContainerStub struct{}
109 func (*BlobContainerStub) GetBlobReference(name string) *storage.Blob {
113 func (*BlobContainerStub) ListBlobs(params storage.ListBlobsParameters) (storage.BlobListResponse, error) {
114 return storage.BlobListResponse{}, nil
117 type testConfig struct {
118 ImageIDForTestSuite string
119 DriverParameters json.RawMessage
122 var live = flag.String("live-azure-cfg", "", "Test with real azure API, provide config file")
124 func GetInstanceSet() (cloud.InstanceSet, cloud.ImageID, arvados.Cluster, error) {
125 cluster := arvados.Cluster{
126 InstanceTypes: arvados.InstanceTypeMap(map[string]arvados.InstanceType{
127 "tiny": arvados.InstanceType{
129 ProviderType: "Standard_D1_v2",
132 Scratch: 10000000000,
138 var exampleCfg testConfig
139 err := config.LoadFile(&exampleCfg, *live)
141 return nil, cloud.ImageID(""), cluster, err
144 ap, err := newAzureInstanceSet(exampleCfg.DriverParameters, "test123", logrus.StandardLogger())
145 return ap, cloud.ImageID(exampleCfg.ImageIDForTestSuite), cluster, err
147 ap := azureInstanceSet{
148 azconfig: azureInstanceSetConfig{
149 BlobContainer: "vhds",
151 dispatcherID: "test123",
152 namePrefix: "compute-test123-",
153 logger: logrus.StandardLogger(),
154 deleteNIC: make(chan string),
155 deleteBlob: make(chan storage.Blob),
157 ap.ctx, ap.stopFunc = context.WithCancel(context.Background())
158 ap.vmClient = &VirtualMachinesClientStub{}
159 ap.netClient = &InterfacesClientStub{}
160 ap.blobcont = &BlobContainerStub{}
161 return &ap, cloud.ImageID("blob"), cluster, nil
164 func (*AzureInstanceSetSuite) TestCreate(c *check.C) {
165 ap, img, cluster, err := GetInstanceSet()
167 c.Fatal("Error making provider", err)
170 pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
171 c.Assert(err, check.IsNil)
173 inst, err := ap.Create(cluster.InstanceTypes["tiny"],
174 img, map[string]string{
175 "TestTagName": "test tag value",
176 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
178 c.Assert(err, check.IsNil)
181 c.Check(tags["TestTagName"], check.Equals, "test tag value")
182 c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
186 func (*AzureInstanceSetSuite) TestListInstances(c *check.C) {
187 ap, _, _, err := GetInstanceSet()
189 c.Fatal("Error making provider", err)
192 l, err := ap.Instances(nil)
194 c.Assert(err, check.IsNil)
196 for _, i := range l {
198 log.Printf("%v %v %v", i.String(), i.Address(), tg)
202 func (*AzureInstanceSetSuite) TestManageNics(c *check.C) {
203 ap, _, _, err := GetInstanceSet()
205 c.Fatal("Error making provider", err)
208 ap.(*azureInstanceSet).manageNics()
212 func (*AzureInstanceSetSuite) TestManageBlobs(c *check.C) {
213 ap, _, _, err := GetInstanceSet()
215 c.Fatal("Error making provider", err)
218 ap.(*azureInstanceSet).manageBlobs()
222 func (*AzureInstanceSetSuite) TestDestroyInstances(c *check.C) {
223 ap, _, _, err := GetInstanceSet()
225 c.Fatal("Error making provider", err)
228 l, err := ap.Instances(nil)
229 c.Assert(err, check.IsNil)
231 for _, i := range l {
232 c.Check(i.Destroy(), check.IsNil)
236 func (*AzureInstanceSetSuite) TestDeleteFake(c *check.C) {
237 ap, _, _, err := GetInstanceSet()
239 c.Fatal("Error making provider", err)
242 _, err = ap.(*azureInstanceSet).netClient.delete(context.Background(), "fakefakefake", "fakefakefake")
244 de, ok := err.(autorest.DetailedError)
246 rq := de.Original.(*azure.RequestError)
248 log.Printf("%v %q %q", rq.Response.StatusCode, rq.ServiceError.Code, rq.ServiceError.Message)
252 func (*AzureInstanceSetSuite) TestWrapError(c *check.C) {
253 retryError := autorest.DetailedError{
254 Original: &azure.RequestError{
255 DetailedError: autorest.DetailedError{
256 Response: &http.Response{
258 Header: map[string][]string{"Retry-After": []string{"123"}},
261 ServiceError: &azure.ServiceError{},
264 wrapped := wrapAzureError(retryError)
265 _, ok := wrapped.(cloud.RateLimitError)
266 c.Check(ok, check.Equals, true)
268 quotaError := autorest.DetailedError{
269 Original: &azure.RequestError{
270 DetailedError: autorest.DetailedError{
271 Response: &http.Response{
275 ServiceError: &azure.ServiceError{
276 Message: "No more quota",
280 wrapped = wrapAzureError(quotaError)
281 _, ok = wrapped.(cloud.QuotaError)
282 c.Check(ok, check.Equals, true)
285 func (*AzureInstanceSetSuite) TestSetTags(c *check.C) {
286 ap, _, _, err := GetInstanceSet()
288 c.Fatal("Error making provider", err)
290 l, err := ap.Instances(nil)
291 c.Assert(err, check.IsNil)
294 err = l[0].SetTags(map[string]string{"foo": "bar"})
296 c.Fatal("Error setting tags", err)
299 l, err = ap.Instances(nil)
300 c.Assert(err, check.IsNil)
304 log.Printf("tags are %v", tg)
308 func (*AzureInstanceSetSuite) TestSSH(c *check.C) {
309 ap, _, _, err := GetInstanceSet()
311 c.Fatal("Error making provider", err)
313 l, err := ap.Instances(nil)
314 c.Assert(err, check.IsNil)
317 sshclient, err := SetupSSHClient(c, l[0])
318 c.Assert(err, check.IsNil)
319 defer sshclient.Conn.Close()
321 sess, err := sshclient.NewSession()
322 c.Assert(err, check.IsNil)
324 _, err = sess.Output("find /var/run/test-file -maxdepth 0 -user root -perm 0600")
325 c.Assert(err, check.IsNil)
327 sess, err = sshclient.NewSession()
328 c.Assert(err, check.IsNil)
330 out, err := sess.Output("sudo cat /var/run/test-file")
331 c.Assert(err, check.IsNil)
332 c.Check(string(out), check.Equals, "test-file-data")
336 func SetupSSHClient(c *check.C, inst cloud.Instance) (*ssh.Client, error) {
337 addr := inst.Address() + ":2222"
339 return nil, errors.New("instance has no address")
342 f, err := os.Open("azconfig_sshkey")
343 c.Assert(err, check.IsNil)
345 keybytes, err := ioutil.ReadAll(f)
346 c.Assert(err, check.IsNil)
348 priv, err := ssh.ParsePrivateKey(keybytes)
349 c.Assert(err, check.IsNil)
351 var receivedKey ssh.PublicKey
352 client, err := ssh.Dial("tcp", addr, &ssh.ClientConfig{
354 Auth: []ssh.AuthMethod{
355 ssh.PublicKeys(priv),
357 HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
361 Timeout: time.Minute,
366 } else if receivedKey == nil {
367 return nil, errors.New("BUG: key was never provided to HostKeyCallback")
370 err = inst.VerifyHostKey(receivedKey, client)
371 c.Assert(err, check.IsNil)