Fix 2.4.2 upgrade notes formatting refs #19330
[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.arvados.org/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 //       SecurityGroupIDs: [sg-xxxxxxxx]
20 //       SubnetID: subnet-xxxxxxxx
21 //       AdminUsername: crunch
22
23 package ec2
24
25 import (
26         "encoding/json"
27         "flag"
28         "sync/atomic"
29         "testing"
30
31         "git.arvados.org/arvados.git/lib/cloud"
32         "git.arvados.org/arvados.git/lib/dispatchcloud/test"
33         "git.arvados.org/arvados.git/sdk/go/arvados"
34         "git.arvados.org/arvados.git/sdk/go/config"
35         "github.com/aws/aws-sdk-go/aws"
36         "github.com/aws/aws-sdk-go/aws/awserr"
37         "github.com/aws/aws-sdk-go/service/ec2"
38         "github.com/sirupsen/logrus"
39         check "gopkg.in/check.v1"
40 )
41
42 var live = flag.String("live-ec2-cfg", "", "Test with real EC2 API, provide config file")
43
44 // Gocheck boilerplate
45 func Test(t *testing.T) {
46         check.TestingT(t)
47 }
48
49 type EC2InstanceSetSuite struct{}
50
51 var _ = check.Suite(&EC2InstanceSetSuite{})
52
53 type testConfig struct {
54         ImageIDForTestSuite string
55         DriverParameters    json.RawMessage
56 }
57
58 type ec2stub struct {
59 }
60
61 func (e *ec2stub) ImportKeyPair(input *ec2.ImportKeyPairInput) (*ec2.ImportKeyPairOutput, error) {
62         return nil, nil
63 }
64
65 func (e *ec2stub) DescribeKeyPairs(input *ec2.DescribeKeyPairsInput) (*ec2.DescribeKeyPairsOutput, error) {
66         return &ec2.DescribeKeyPairsOutput{}, nil
67 }
68
69 func (e *ec2stub) RunInstances(input *ec2.RunInstancesInput) (*ec2.Reservation, error) {
70         return &ec2.Reservation{Instances: []*ec2.Instance{{
71                 InstanceId: aws.String("i-123"),
72                 Tags:       input.TagSpecifications[0].Tags,
73         }}}, nil
74 }
75
76 func (e *ec2stub) DescribeInstances(input *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) {
77         return &ec2.DescribeInstancesOutput{}, nil
78 }
79
80 func (e *ec2stub) CreateTags(input *ec2.CreateTagsInput) (*ec2.CreateTagsOutput, error) {
81         return nil, nil
82 }
83
84 func (e *ec2stub) TerminateInstances(input *ec2.TerminateInstancesInput) (*ec2.TerminateInstancesOutput, error) {
85         return nil, nil
86 }
87
88 func GetInstanceSet() (cloud.InstanceSet, cloud.ImageID, arvados.Cluster, error) {
89         cluster := arvados.Cluster{
90                 InstanceTypes: arvados.InstanceTypeMap(map[string]arvados.InstanceType{
91                         "tiny": {
92                                 Name:         "tiny",
93                                 ProviderType: "t2.micro",
94                                 VCPUs:        1,
95                                 RAM:          4000000000,
96                                 Scratch:      10000000000,
97                                 Price:        .02,
98                                 Preemptible:  false,
99                         },
100                         "tiny-with-extra-scratch": {
101                                 Name:         "tiny",
102                                 ProviderType: "t2.micro",
103                                 VCPUs:        1,
104                                 RAM:          4000000000,
105                                 Price:        .02,
106                                 Preemptible:  false,
107                                 AddedScratch: 20000000000,
108                         },
109                         "tiny-preemptible": {
110                                 Name:         "tiny",
111                                 ProviderType: "t2.micro",
112                                 VCPUs:        1,
113                                 RAM:          4000000000,
114                                 Scratch:      10000000000,
115                                 Price:        .02,
116                                 Preemptible:  true,
117                         },
118                 })}
119         if *live != "" {
120                 var exampleCfg testConfig
121                 err := config.LoadFile(&exampleCfg, *live)
122                 if err != nil {
123                         return nil, cloud.ImageID(""), cluster, err
124                 }
125
126                 ap, err := newEC2InstanceSet(exampleCfg.DriverParameters, "test123", nil, logrus.StandardLogger())
127                 return ap, cloud.ImageID(exampleCfg.ImageIDForTestSuite), cluster, err
128         }
129         ap := ec2InstanceSet{
130                 ec2config:     ec2InstanceSetConfig{},
131                 instanceSetID: "test123",
132                 logger:        logrus.StandardLogger(),
133                 client:        &ec2stub{},
134                 keys:          make(map[string]string),
135         }
136         return &ap, cloud.ImageID("blob"), cluster, nil
137 }
138
139 func (*EC2InstanceSetSuite) TestCreate(c *check.C) {
140         ap, img, cluster, err := GetInstanceSet()
141         if err != nil {
142                 c.Fatal("Error making provider", err)
143         }
144
145         pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
146         c.Assert(err, check.IsNil)
147
148         inst, err := ap.Create(cluster.InstanceTypes["tiny"],
149                 img, map[string]string{
150                         "TestTagName": "test tag value",
151                 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
152
153         c.Assert(err, check.IsNil)
154
155         tags := inst.Tags()
156         c.Check(tags["TestTagName"], check.Equals, "test tag value")
157         c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
158
159 }
160
161 func (*EC2InstanceSetSuite) TestCreateWithExtraScratch(c *check.C) {
162         ap, img, cluster, err := GetInstanceSet()
163         if err != nil {
164                 c.Fatal("Error making provider", err)
165         }
166
167         pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
168         c.Assert(err, check.IsNil)
169
170         inst, err := ap.Create(cluster.InstanceTypes["tiny-with-extra-scratch"],
171                 img, map[string]string{
172                         "TestTagName": "test tag value",
173                 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
174
175         c.Assert(err, check.IsNil)
176
177         tags := inst.Tags()
178         c.Check(tags["TestTagName"], check.Equals, "test tag value")
179         c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
180
181 }
182
183 func (*EC2InstanceSetSuite) TestCreatePreemptible(c *check.C) {
184         ap, img, cluster, err := GetInstanceSet()
185         if err != nil {
186                 c.Fatal("Error making provider", err)
187         }
188
189         pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
190         c.Assert(err, check.IsNil)
191
192         inst, err := ap.Create(cluster.InstanceTypes["tiny-preemptible"],
193                 img, map[string]string{
194                         "TestTagName": "test tag value",
195                 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
196
197         c.Assert(err, check.IsNil)
198
199         tags := inst.Tags()
200         c.Check(tags["TestTagName"], check.Equals, "test tag value")
201         c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
202
203 }
204
205 func (*EC2InstanceSetSuite) TestTagInstances(c *check.C) {
206         ap, _, _, err := GetInstanceSet()
207         if err != nil {
208                 c.Fatal("Error making provider", err)
209         }
210
211         l, err := ap.Instances(nil)
212         c.Assert(err, check.IsNil)
213
214         for _, i := range l {
215                 tg := i.Tags()
216                 tg["TestTag2"] = "123 test tag 2"
217                 c.Check(i.SetTags(tg), check.IsNil)
218         }
219 }
220
221 func (*EC2InstanceSetSuite) TestListInstances(c *check.C) {
222         ap, _, _, err := GetInstanceSet()
223         if err != nil {
224                 c.Fatal("Error making provider: ", err)
225         }
226
227         l, err := ap.Instances(nil)
228
229         c.Assert(err, check.IsNil)
230
231         for _, i := range l {
232                 tg := i.Tags()
233                 c.Logf("%v %v %v", i.String(), i.Address(), tg)
234         }
235 }
236
237 func (*EC2InstanceSetSuite) TestDestroyInstances(c *check.C) {
238         ap, _, _, err := GetInstanceSet()
239         if err != nil {
240                 c.Fatal("Error making provider", err)
241         }
242
243         l, err := ap.Instances(nil)
244         c.Assert(err, check.IsNil)
245
246         for _, i := range l {
247                 c.Check(i.Destroy(), check.IsNil)
248         }
249 }
250
251 func (*EC2InstanceSetSuite) TestWrapError(c *check.C) {
252         retryError := awserr.New("Throttling", "", nil)
253         wrapped := wrapError(retryError, &atomic.Value{})
254         _, ok := wrapped.(cloud.RateLimitError)
255         c.Check(ok, check.Equals, true)
256
257         quotaError := awserr.New("InsufficientInstanceCapacity", "", nil)
258         wrapped = wrapError(quotaError, nil)
259         _, ok = wrapped.(cloud.QuotaError)
260         c.Check(ok, check.Equals, true)
261 }