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
44 "git.curoverse.com/arvados.git/lib/cloud"
45 "git.curoverse.com/arvados.git/sdk/go/arvados"
46 "git.curoverse.com/arvados.git/sdk/go/config"
47 "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-06-01/compute"
48 "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-06-01/network"
49 "github.com/Azure/azure-sdk-for-go/storage"
50 "github.com/Azure/go-autorest/autorest"
51 "github.com/Azure/go-autorest/autorest/azure"
52 "github.com/Azure/go-autorest/autorest/to"
53 "github.com/jmcvetta/randutil"
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 testConfig struct {
109 ImageIDForTestSuite string
110 DriverParameters json.RawMessage
113 var live = flag.String("live-azure-cfg", "", "Test with real azure API, provide config file")
115 func GetInstanceSet() (cloud.InstanceSet, cloud.ImageID, arvados.Cluster, error) {
116 cluster := arvados.Cluster{
117 InstanceTypes: arvados.InstanceTypeMap(map[string]arvados.InstanceType{
118 "tiny": arvados.InstanceType{
120 ProviderType: "Standard_D1_v2",
123 Scratch: 10000000000,
129 var exampleCfg testConfig
130 err := config.LoadFile(&exampleCfg, *live)
132 return nil, cloud.ImageID(""), cluster, err
135 ap, err := newAzureInstanceSet(exampleCfg.DriverParameters, "test123", logrus.StandardLogger())
136 return ap, cloud.ImageID(exampleCfg.ImageIDForTestSuite), cluster, err
138 ap := azureInstanceSet{
139 azconfig: azureInstanceSetConfig{
140 BlobContainer: "vhds",
142 dispatcherID: "test123",
143 namePrefix: "compute-test123-",
144 logger: logrus.StandardLogger(),
145 deleteNIC: make(chan string),
146 deleteBlob: make(chan storage.Blob),
148 ap.ctx, ap.stopFunc = context.WithCancel(context.Background())
149 ap.vmClient = &VirtualMachinesClientStub{}
150 ap.netClient = &InterfacesClientStub{}
151 return &ap, cloud.ImageID("blob"), cluster, nil
154 func (*AzureInstanceSetSuite) TestCreate(c *check.C) {
155 ap, img, cluster, err := GetInstanceSet()
157 c.Fatal("Error making provider", err)
160 pk, _, _, _, err := ssh.ParseAuthorizedKey(testKey)
161 c.Assert(err, check.IsNil)
163 nodetoken, err := randutil.String(40, "abcdefghijklmnopqrstuvwxyz0123456789")
164 c.Assert(err, check.IsNil)
166 inst, err := ap.Create(cluster.InstanceTypes["tiny"],
167 img, map[string]string{
168 "node-token": nodetoken},
171 c.Assert(err, check.IsNil)
174 log.Printf("Result %v %v %v", inst.String(), inst.Address(), tg)
178 func (*AzureInstanceSetSuite) TestListInstances(c *check.C) {
179 ap, _, _, err := GetInstanceSet()
181 c.Fatal("Error making provider", err)
184 l, err := ap.Instances(nil)
186 c.Assert(err, check.IsNil)
188 for _, i := range l {
190 log.Printf("%v %v %v", i.String(), i.Address(), tg)
194 func (*AzureInstanceSetSuite) TestManageNics(c *check.C) {
195 ap, _, _, err := GetInstanceSet()
197 c.Fatal("Error making provider", err)
200 ap.(*azureInstanceSet).manageNics()
204 func (*AzureInstanceSetSuite) TestManageBlobs(c *check.C) {
205 ap, _, _, err := GetInstanceSet()
207 c.Fatal("Error making provider", err)
210 ap.(*azureInstanceSet).manageBlobs()
214 func (*AzureInstanceSetSuite) TestDestroyInstances(c *check.C) {
215 ap, _, _, err := GetInstanceSet()
217 c.Fatal("Error making provider", err)
220 l, err := ap.Instances(nil)
221 c.Assert(err, check.IsNil)
223 for _, i := range l {
224 c.Check(i.Destroy(), check.IsNil)
228 func (*AzureInstanceSetSuite) TestDeleteFake(c *check.C) {
229 ap, _, _, err := GetInstanceSet()
231 c.Fatal("Error making provider", err)
234 _, err = ap.(*azureInstanceSet).netClient.delete(context.Background(), "fakefakefake", "fakefakefake")
236 de, ok := err.(autorest.DetailedError)
238 rq := de.Original.(*azure.RequestError)
240 log.Printf("%v %q %q", rq.Response.StatusCode, rq.ServiceError.Code, rq.ServiceError.Message)
244 func (*AzureInstanceSetSuite) TestWrapError(c *check.C) {
245 retryError := autorest.DetailedError{
246 Original: &azure.RequestError{
247 DetailedError: autorest.DetailedError{
248 Response: &http.Response{
250 Header: map[string][]string{"Retry-After": []string{"123"}},
253 ServiceError: &azure.ServiceError{},
256 wrapped := wrapAzureError(retryError)
257 _, ok := wrapped.(cloud.RateLimitError)
258 c.Check(ok, check.Equals, true)
260 quotaError := autorest.DetailedError{
261 Original: &azure.RequestError{
262 DetailedError: autorest.DetailedError{
263 Response: &http.Response{
267 ServiceError: &azure.ServiceError{
268 Message: "No more quota",
272 wrapped = wrapAzureError(quotaError)
273 _, ok = wrapped.(cloud.QuotaError)
274 c.Check(ok, check.Equals, true)
277 func (*AzureInstanceSetSuite) TestSetTags(c *check.C) {
278 ap, _, _, err := GetInstanceSet()
280 c.Fatal("Error making provider", err)
282 l, err := ap.Instances(nil)
283 c.Assert(err, check.IsNil)
286 err = l[0].SetTags(map[string]string{"foo": "bar"})
288 c.Fatal("Error setting tags", err)
291 l, err = ap.Instances(nil)
292 c.Assert(err, check.IsNil)
296 log.Printf("tags are %v", tg)
300 func (*AzureInstanceSetSuite) TestSSH(c *check.C) {
301 ap, _, _, err := GetInstanceSet()
303 c.Fatal("Error making provider", err)
305 l, err := ap.Instances(nil)
306 c.Assert(err, check.IsNil)
310 sshclient, err := SetupSSHClient(c, l[0])
311 c.Assert(err, check.IsNil)
313 sess, err := sshclient.NewSession()
314 c.Assert(err, check.IsNil)
316 out, err := sess.Output("cat /home/crunch/node-token")
317 c.Assert(err, check.IsNil)
319 log.Printf("%v", string(out))
321 sshclient.Conn.Close()
325 func SetupSSHClient(c *check.C, inst cloud.Instance) (*ssh.Client, error) {
326 addr := inst.Address() + ":2222"
328 return nil, errors.New("instance has no address")
331 f, err := os.Open("azconfig_sshkey")
332 c.Assert(err, check.IsNil)
334 keybytes, err := ioutil.ReadAll(f)
335 c.Assert(err, check.IsNil)
337 priv, err := ssh.ParsePrivateKey(keybytes)
338 c.Assert(err, check.IsNil)
340 var receivedKey ssh.PublicKey
341 client, err := ssh.Dial("tcp", addr, &ssh.ClientConfig{
343 Auth: []ssh.AuthMethod{
344 ssh.PublicKeys(priv),
346 HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
350 Timeout: time.Minute,
355 } else if receivedKey == nil {
356 return nil, errors.New("BUG: key was never provided to HostKeyCallback")
359 err = inst.VerifyHostKey(receivedKey, client)
360 c.Assert(err, check.IsNil)