14291: Working on instance creation
[arvados.git] / lib / cloud / ec2 / ec2_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4 //
5 //
6 // How to manually run individual tests against the real cloud:
7 //
8 // $ go test -v git.curoverse.com/arvados.git/lib/cloud/ec2 -live-ec2-cfg ec2config.yml -check.f=TestCreate
9 //
10 // Tests should be run individually and in the order they are listed in the file:
11 //
12 // Example azconfig.yml:
13 //
14 // ImageIDForTestSuite: ami-xxxxxxxxxxxxxxxxx
15 // DriverParameters:
16 //       AccessKeyID: XXXXXXXXXXXXXX
17 //       SecretAccessKey: xxxxxxxxxxxxxxxxxxxx
18 //       Region: us-east-1
19 //       SecurityGroupId: sg-xxxxxxxx
20 //       SubnetId: subnet-xxxxxxxx
21 //       AdminUsername: crunch
22 //       KeyPairName: arvados-dispatcher-keypair
23
24 package ec2
25
26 import (
27         "encoding/json"
28         "flag"
29         "log"
30         "testing"
31
32         "git.curoverse.com/arvados.git/lib/cloud"
33         "git.curoverse.com/arvados.git/sdk/go/arvados"
34         "git.curoverse.com/arvados.git/sdk/go/config"
35         "github.com/sirupsen/logrus"
36         "golang.org/x/crypto/ssh"
37         check "gopkg.in/check.v1"
38 )
39
40 var live = flag.String("live-ec2-cfg", "", "Test with real EC2 API, provide config file")
41
42 // Gocheck boilerplate
43 func Test(t *testing.T) {
44         check.TestingT(t)
45 }
46
47 type EC2InstanceSetSuite struct{}
48
49 var _ = check.Suite(&EC2InstanceSetSuite{})
50
51 type testConfig struct {
52         ImageIDForTestSuite string
53         DriverParameters    json.RawMessage
54 }
55
56 func GetInstanceSet() (cloud.InstanceSet, cloud.ImageID, arvados.Cluster, error) {
57         cluster := arvados.Cluster{
58                 InstanceTypes: arvados.InstanceTypeMap(map[string]arvados.InstanceType{
59                         "tiny": arvados.InstanceType{
60                                 Name:         "tiny",
61                                 ProviderType: "m1.small",
62                                 VCPUs:        1,
63                                 RAM:          4000000000,
64                                 Scratch:      10000000000,
65                                 Price:        .02,
66                                 Preemptible:  false,
67                         },
68                 })}
69         if *live != "" {
70                 var exampleCfg testConfig
71                 err := config.LoadFile(&exampleCfg, *live)
72                 if err != nil {
73                         return nil, cloud.ImageID(""), cluster, err
74                 }
75
76                 ap, err := newEC2InstanceSet(exampleCfg.DriverParameters, "test123", logrus.StandardLogger())
77                 return ap, cloud.ImageID(exampleCfg.ImageIDForTestSuite), cluster, err
78         }
79         ap := ec2InstanceSet{
80                 ec2config:    ec2InstanceSetConfig{},
81                 dispatcherID: "test123",
82                 logger:       logrus.StandardLogger(),
83         }
84         return &ap, cloud.ImageID("blob"), cluster, nil
85 }
86
87 var testKey = []byte(`ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDLQS1ExT2+WjA0d/hntEAyAtgeN1W2ik2QX8c2zO6HjlPHWXL92r07W0WMuDib40Pcevpi1BXeBWXA9ZB5KKMJB+ukaAu22KklnQuUmNvk6ZXnPKSkGxuCYvPQb08WhHf3p1VxiKfP3iauedBDM4x9/bkJohlBBQiFXzNUcQ+a6rKiMzmJN2gbL8ncyUzc+XQ5q4JndTwTGtOlzDiGOc9O4z5Dd76wtAVJneOuuNpwfFRVHThpJM6VThpCZOnl8APaceWXKeuwOuCae3COZMz++xQfxOfZ9Z8aIwo+TlQhsRaNfZ4Vjrop6ej8dtfZtgUFKfbXEOYaHrGrWGotFDTD example@example`)
88
89 func (*EC2InstanceSetSuite) TestCreate(c *check.C) {
90         ap, img, cluster, err := GetInstanceSet()
91         if err != nil {
92                 c.Fatal("Error making provider", err)
93         }
94
95         pk, _, _, _, err := ssh.ParseAuthorizedKey(testKey)
96         c.Assert(err, check.IsNil)
97
98         inst, err := ap.Create(cluster.InstanceTypes["tiny"],
99                 img, map[string]string{
100                         "TestTagName": "test tag value",
101                 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
102
103         c.Assert(err, check.IsNil)
104
105         tags := inst.Tags()
106         c.Check(tags["TestTagName"], check.Equals, "test tag value")
107         c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
108
109 }
110
111 func (*EC2InstanceSetSuite) TestListInstances(c *check.C) {
112         ap, _, _, err := GetInstanceSet()
113         if err != nil {
114                 c.Fatal("Error making provider", err)
115         }
116
117         l, err := ap.Instances(nil)
118
119         c.Assert(err, check.IsNil)
120
121         for _, i := range l {
122                 tg := i.Tags()
123                 log.Printf("%v %v %v", i.String(), i.Address(), tg)
124         }
125 }
126
127 func (*EC2InstanceSetSuite) TestDestroyInstances(c *check.C) {
128         ap, _, _, err := GetInstanceSet()
129         if err != nil {
130                 c.Fatal("Error making provider", err)
131         }
132
133         l, err := ap.Instances(nil)
134         c.Assert(err, check.IsNil)
135
136         for _, i := range l {
137                 c.Check(i.Destroy(), check.IsNil)
138         }
139 }