20259: Add documentation for banner and tooltip features
[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 ec2config.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         "time"
31
32         "git.arvados.org/arvados.git/lib/cloud"
33         "git.arvados.org/arvados.git/lib/dispatchcloud/test"
34         "git.arvados.org/arvados.git/sdk/go/arvados"
35         "git.arvados.org/arvados.git/sdk/go/config"
36         "github.com/aws/aws-sdk-go/aws"
37         "github.com/aws/aws-sdk-go/aws/awserr"
38         "github.com/aws/aws-sdk-go/service/ec2"
39         "github.com/sirupsen/logrus"
40         check "gopkg.in/check.v1"
41 )
42
43 var live = flag.String("live-ec2-cfg", "", "Test with real EC2 API, provide config file")
44
45 // Gocheck boilerplate
46 func Test(t *testing.T) {
47         check.TestingT(t)
48 }
49
50 type EC2InstanceSetSuite struct{}
51
52 var _ = check.Suite(&EC2InstanceSetSuite{})
53
54 type testConfig struct {
55         ImageIDForTestSuite string
56         DriverParameters    json.RawMessage
57 }
58
59 type ec2stub struct {
60         c       *check.C
61         reftime time.Time
62 }
63
64 func (e *ec2stub) ImportKeyPair(input *ec2.ImportKeyPairInput) (*ec2.ImportKeyPairOutput, error) {
65         return nil, nil
66 }
67
68 func (e *ec2stub) DescribeKeyPairs(input *ec2.DescribeKeyPairsInput) (*ec2.DescribeKeyPairsOutput, error) {
69         return &ec2.DescribeKeyPairsOutput{}, nil
70 }
71
72 func (e *ec2stub) RunInstances(input *ec2.RunInstancesInput) (*ec2.Reservation, error) {
73         return &ec2.Reservation{Instances: []*ec2.Instance{{
74                 InstanceId:   aws.String("i-123"),
75                 InstanceType: aws.String("t2.micro"),
76                 Tags:         input.TagSpecifications[0].Tags,
77         }}}, nil
78 }
79
80 func (e *ec2stub) DescribeInstances(input *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) {
81         return &ec2.DescribeInstancesOutput{
82                 Reservations: []*ec2.Reservation{{
83                         Instances: []*ec2.Instance{{
84                                 InstanceId:        aws.String("i-123"),
85                                 InstanceLifecycle: aws.String("spot"),
86                                 InstanceType:      aws.String("t2.micro"),
87                                 PrivateIpAddress:  aws.String("10.1.2.3"),
88                                 State:             &ec2.InstanceState{Name: aws.String("running"), Code: aws.Int64(16)},
89                         }, {
90                                 InstanceId:        aws.String("i-124"),
91                                 InstanceLifecycle: aws.String("spot"),
92                                 InstanceType:      aws.String("t2.micro"),
93                                 PrivateIpAddress:  aws.String("10.1.2.4"),
94                                 State:             &ec2.InstanceState{Name: aws.String("running"), Code: aws.Int64(16)},
95                         }},
96                 }},
97         }, nil
98 }
99
100 func (e *ec2stub) DescribeInstanceStatusPages(input *ec2.DescribeInstanceStatusInput, fn func(*ec2.DescribeInstanceStatusOutput, bool) bool) error {
101         fn(&ec2.DescribeInstanceStatusOutput{
102                 InstanceStatuses: []*ec2.InstanceStatus{{
103                         InstanceId:       aws.String("i-123"),
104                         AvailabilityZone: aws.String("aa-east-1a"),
105                 }, {
106                         InstanceId:       aws.String("i-124"),
107                         AvailabilityZone: aws.String("aa-east-1a"),
108                 }},
109         }, true)
110         return nil
111 }
112
113 func (e *ec2stub) DescribeSpotPriceHistoryPages(input *ec2.DescribeSpotPriceHistoryInput, fn func(*ec2.DescribeSpotPriceHistoryOutput, bool) bool) error {
114         if !fn(&ec2.DescribeSpotPriceHistoryOutput{
115                 SpotPriceHistory: []*ec2.SpotPrice{
116                         &ec2.SpotPrice{
117                                 InstanceType:     aws.String("t2.micro"),
118                                 AvailabilityZone: aws.String("aa-east-1a"),
119                                 SpotPrice:        aws.String("0.005"),
120                                 Timestamp:        aws.Time(e.reftime.Add(-9 * time.Minute)),
121                         },
122                         &ec2.SpotPrice{
123                                 InstanceType:     aws.String("t2.micro"),
124                                 AvailabilityZone: aws.String("aa-east-1a"),
125                                 SpotPrice:        aws.String("0.015"),
126                                 Timestamp:        aws.Time(e.reftime.Add(-5 * time.Minute)),
127                         },
128                 },
129         }, false) {
130                 return nil
131         }
132         fn(&ec2.DescribeSpotPriceHistoryOutput{
133                 SpotPriceHistory: []*ec2.SpotPrice{
134                         &ec2.SpotPrice{
135                                 InstanceType:     aws.String("t2.micro"),
136                                 AvailabilityZone: aws.String("aa-east-1a"),
137                                 SpotPrice:        aws.String("0.01"),
138                                 Timestamp:        aws.Time(e.reftime.Add(-2 * time.Minute)),
139                         },
140                 },
141         }, true)
142         return nil
143 }
144
145 func (e *ec2stub) CreateTags(input *ec2.CreateTagsInput) (*ec2.CreateTagsOutput, error) {
146         return nil, nil
147 }
148
149 func (e *ec2stub) TerminateInstances(input *ec2.TerminateInstancesInput) (*ec2.TerminateInstancesOutput, error) {
150         return nil, nil
151 }
152
153 func GetInstanceSet(c *check.C) (*ec2InstanceSet, cloud.ImageID, arvados.Cluster) {
154         cluster := arvados.Cluster{
155                 InstanceTypes: arvados.InstanceTypeMap(map[string]arvados.InstanceType{
156                         "tiny": {
157                                 Name:         "tiny",
158                                 ProviderType: "t2.micro",
159                                 VCPUs:        1,
160                                 RAM:          4000000000,
161                                 Scratch:      10000000000,
162                                 Price:        .02,
163                                 Preemptible:  false,
164                         },
165                         "tiny-with-extra-scratch": {
166                                 Name:         "tiny-with-extra-scratch",
167                                 ProviderType: "t2.micro",
168                                 VCPUs:        1,
169                                 RAM:          4000000000,
170                                 Price:        .02,
171                                 Preemptible:  false,
172                                 AddedScratch: 20000000000,
173                         },
174                         "tiny-preemptible": {
175                                 Name:         "tiny-preemptible",
176                                 ProviderType: "t2.micro",
177                                 VCPUs:        1,
178                                 RAM:          4000000000,
179                                 Scratch:      10000000000,
180                                 Price:        .02,
181                                 Preemptible:  true,
182                         },
183                 })}
184         if *live != "" {
185                 var exampleCfg testConfig
186                 err := config.LoadFile(&exampleCfg, *live)
187                 c.Assert(err, check.IsNil)
188
189                 ap, err := newEC2InstanceSet(exampleCfg.DriverParameters, "test123", nil, logrus.StandardLogger())
190                 c.Assert(err, check.IsNil)
191                 return ap.(*ec2InstanceSet), cloud.ImageID(exampleCfg.ImageIDForTestSuite), cluster
192         }
193         ap := ec2InstanceSet{
194                 instanceSetID: "test123",
195                 logger:        logrus.StandardLogger(),
196                 client:        &ec2stub{c: c, reftime: time.Now().UTC()},
197                 keys:          make(map[string]string),
198         }
199         return &ap, cloud.ImageID("blob"), cluster
200 }
201
202 func (*EC2InstanceSetSuite) TestCreate(c *check.C) {
203         ap, img, cluster := GetInstanceSet(c)
204         pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
205
206         inst, err := ap.Create(cluster.InstanceTypes["tiny"],
207                 img, map[string]string{
208                         "TestTagName": "test tag value",
209                 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
210         c.Assert(err, check.IsNil)
211
212         tags := inst.Tags()
213         c.Check(tags["TestTagName"], check.Equals, "test tag value")
214         c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
215
216 }
217
218 func (*EC2InstanceSetSuite) TestCreateWithExtraScratch(c *check.C) {
219         ap, img, cluster := GetInstanceSet(c)
220         pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
221
222         inst, err := ap.Create(cluster.InstanceTypes["tiny-with-extra-scratch"],
223                 img, map[string]string{
224                         "TestTagName": "test tag value",
225                 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
226
227         c.Assert(err, check.IsNil)
228
229         tags := inst.Tags()
230         c.Check(tags["TestTagName"], check.Equals, "test tag value")
231         c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
232
233 }
234
235 func (*EC2InstanceSetSuite) TestCreatePreemptible(c *check.C) {
236         ap, img, cluster := GetInstanceSet(c)
237         pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
238
239         inst, err := ap.Create(cluster.InstanceTypes["tiny-preemptible"],
240                 img, map[string]string{
241                         "TestTagName": "test tag value",
242                 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
243
244         c.Assert(err, check.IsNil)
245
246         tags := inst.Tags()
247         c.Check(tags["TestTagName"], check.Equals, "test tag value")
248         c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
249
250 }
251
252 func (*EC2InstanceSetSuite) TestTagInstances(c *check.C) {
253         ap, _, _ := GetInstanceSet(c)
254         l, err := ap.Instances(nil)
255         c.Assert(err, check.IsNil)
256
257         for _, i := range l {
258                 tg := i.Tags()
259                 tg["TestTag2"] = "123 test tag 2"
260                 c.Check(i.SetTags(tg), check.IsNil)
261         }
262 }
263
264 func (*EC2InstanceSetSuite) TestListInstances(c *check.C) {
265         ap, _, _ := GetInstanceSet(c)
266         l, err := ap.Instances(nil)
267         c.Assert(err, check.IsNil)
268
269         for _, i := range l {
270                 tg := i.Tags()
271                 c.Logf("%v %v %v", i.String(), i.Address(), tg)
272         }
273 }
274
275 func (*EC2InstanceSetSuite) TestDestroyInstances(c *check.C) {
276         ap, _, _ := GetInstanceSet(c)
277         l, err := ap.Instances(nil)
278         c.Assert(err, check.IsNil)
279
280         for _, i := range l {
281                 c.Check(i.Destroy(), check.IsNil)
282         }
283 }
284
285 func (*EC2InstanceSetSuite) TestInstancePriceHistory(c *check.C) {
286         ap, img, cluster := GetInstanceSet(c)
287         pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
288         tags := cloud.InstanceTags{"arvados-ec2-driver": "test"}
289
290         defer func() {
291                 instances, err := ap.Instances(tags)
292                 c.Assert(err, check.IsNil)
293                 for _, inst := range instances {
294                         c.Logf("cleanup: destroy instance %s", inst)
295                         c.Check(inst.Destroy(), check.IsNil)
296                 }
297         }()
298
299         ap.ec2config.SpotPriceUpdateInterval = arvados.Duration(time.Hour)
300         ap.ec2config.EBSPrice = 0.1 // $/GiB/month
301         inst1, err := ap.Create(cluster.InstanceTypes["tiny-preemptible"], img, tags, "true", pk)
302         c.Assert(err, check.IsNil)
303         defer inst1.Destroy()
304         inst2, err := ap.Create(cluster.InstanceTypes["tiny-preemptible"], img, tags, "true", pk)
305         c.Assert(err, check.IsNil)
306         defer inst2.Destroy()
307
308         // in live mode, we need to wait for the instances to reach
309         // running state before we can discover their availability
310         // zones and look up the appropriate prices.
311         var instances []cloud.Instance
312         for deadline := time.Now().Add(5 * time.Minute); ; {
313                 if deadline.Before(time.Now()) {
314                         c.Fatal("timed out")
315                 }
316                 instances, err = ap.Instances(tags)
317                 running := 0
318                 for _, inst := range instances {
319                         ec2i := inst.(*ec2Instance).instance
320                         if *ec2i.InstanceLifecycle == "spot" && *ec2i.State.Code&16 != 0 {
321                                 running++
322                         }
323                 }
324                 if running >= 2 {
325                         c.Logf("instances are running, and identifiable as spot instances")
326                         break
327                 }
328                 c.Logf("waiting for instances to reach running state so their availability zone becomes visible...")
329                 time.Sleep(10 * time.Second)
330         }
331
332         for _, inst := range instances {
333                 hist := inst.PriceHistory(arvados.InstanceType{})
334                 c.Logf("%s price history: %v", inst.ID(), hist)
335                 c.Check(len(hist) > 0, check.Equals, true)
336
337                 histWithScratch := inst.PriceHistory(arvados.InstanceType{AddedScratch: 640 << 30})
338                 c.Logf("%s price history with 640 GiB scratch: %v", inst.ID(), histWithScratch)
339
340                 for i, ip := range hist {
341                         c.Check(ip.Price, check.Not(check.Equals), 0.0)
342                         if i > 0 {
343                                 c.Check(ip.StartTime.Before(hist[i-1].StartTime), check.Equals, true)
344                         }
345                         c.Check(ip.Price < histWithScratch[i].Price, check.Equals, true)
346                 }
347         }
348 }
349
350 func (*EC2InstanceSetSuite) TestWrapError(c *check.C) {
351         retryError := awserr.New("Throttling", "", nil)
352         wrapped := wrapError(retryError, &atomic.Value{})
353         _, ok := wrapped.(cloud.RateLimitError)
354         c.Check(ok, check.Equals, true)
355
356         quotaError := awserr.New("InsufficientInstanceCapacity", "", nil)
357         wrapped = wrapError(quotaError, nil)
358         _, ok = wrapped.(cloud.QuotaError)
359         c.Check(ok, check.Equals, true)
360 }