19320: Comment re future use of spot attr in priceKey.
[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")},
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")},
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                 ec2config: ec2InstanceSetConfig{
195                         SpotPriceUpdateInterval: arvados.Duration(time.Hour),
196                 },
197                 instanceSetID: "test123",
198                 logger:        logrus.StandardLogger(),
199                 client:        &ec2stub{c: c, reftime: time.Now().UTC()},
200                 keys:          make(map[string]string),
201         }
202         return &ap, cloud.ImageID("blob"), cluster
203 }
204
205 func (*EC2InstanceSetSuite) TestCreate(c *check.C) {
206         ap, img, cluster := GetInstanceSet(c)
207         pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
208
209         inst, err := ap.Create(cluster.InstanceTypes["tiny"],
210                 img, map[string]string{
211                         "TestTagName": "test tag value",
212                 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
213         c.Assert(err, check.IsNil)
214
215         tags := inst.Tags()
216         c.Check(tags["TestTagName"], check.Equals, "test tag value")
217         c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
218
219 }
220
221 func (*EC2InstanceSetSuite) TestCreateWithExtraScratch(c *check.C) {
222         ap, img, cluster := GetInstanceSet(c)
223         pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
224
225         inst, err := ap.Create(cluster.InstanceTypes["tiny-with-extra-scratch"],
226                 img, map[string]string{
227                         "TestTagName": "test tag value",
228                 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
229
230         c.Assert(err, check.IsNil)
231
232         tags := inst.Tags()
233         c.Check(tags["TestTagName"], check.Equals, "test tag value")
234         c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
235
236 }
237
238 func (*EC2InstanceSetSuite) TestCreatePreemptible(c *check.C) {
239         ap, img, cluster := GetInstanceSet(c)
240         pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
241
242         inst, err := ap.Create(cluster.InstanceTypes["tiny-preemptible"],
243                 img, map[string]string{
244                         "TestTagName": "test tag value",
245                 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
246
247         c.Assert(err, check.IsNil)
248
249         tags := inst.Tags()
250         c.Check(tags["TestTagName"], check.Equals, "test tag value")
251         c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
252
253 }
254
255 func (*EC2InstanceSetSuite) TestTagInstances(c *check.C) {
256         ap, _, _ := GetInstanceSet(c)
257         l, err := ap.Instances(nil)
258         c.Assert(err, check.IsNil)
259
260         for _, i := range l {
261                 tg := i.Tags()
262                 tg["TestTag2"] = "123 test tag 2"
263                 c.Check(i.SetTags(tg), check.IsNil)
264         }
265 }
266
267 func (*EC2InstanceSetSuite) TestListInstances(c *check.C) {
268         ap, _, _ := GetInstanceSet(c)
269         l, err := ap.Instances(nil)
270         c.Assert(err, check.IsNil)
271
272         for _, i := range l {
273                 tg := i.Tags()
274                 c.Logf("%v %v %v", i.String(), i.Address(), tg)
275         }
276 }
277
278 func (*EC2InstanceSetSuite) TestDestroyInstances(c *check.C) {
279         ap, _, _ := GetInstanceSet(c)
280         l, err := ap.Instances(nil)
281         c.Assert(err, check.IsNil)
282
283         for _, i := range l {
284                 c.Check(i.Destroy(), check.IsNil)
285         }
286 }
287
288 func (*EC2InstanceSetSuite) TestInstancePriceHistory(c *check.C) {
289         ap, img, cluster := GetInstanceSet(c)
290         pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
291         tags := cloud.InstanceTags{"arvados-ec2-driver": "test"}
292
293         defer func() {
294                 instances, err := ap.Instances(tags)
295                 c.Assert(err, check.IsNil)
296                 for _, inst := range instances {
297                         c.Logf("cleanup: destroy instance %s", inst)
298                         c.Check(inst.Destroy(), check.IsNil)
299                 }
300         }()
301
302         ap.ec2config.EBSPrice = 0.1 // $/GiB/month
303         inst1, err := ap.Create(cluster.InstanceTypes["tiny-preemptible"], img, tags, "true", pk)
304         c.Assert(err, check.IsNil)
305         defer inst1.Destroy()
306         inst2, err := ap.Create(cluster.InstanceTypes["tiny-preemptible"], img, tags, "true", pk)
307         c.Assert(err, check.IsNil)
308         defer inst2.Destroy()
309
310         // in live mode, we need to wait for the instances to reach
311         // running state before we can discover their availability
312         // zones and look up the appropriate prices.
313         var instances []cloud.Instance
314         for deadline := time.Now().Add(5 * time.Minute); ; {
315                 if deadline.Before(time.Now()) {
316                         c.Fatal("timed out")
317                 }
318                 instances, err = ap.Instances(tags)
319                 running := 0
320                 for _, inst := range instances {
321                         if *inst.(*ec2Instance).instance.InstanceLifecycle == "spot" {
322                                 running++
323                         }
324                 }
325                 if running >= 2 {
326                         c.Logf("instances are running, and identifiable as spot instances")
327                         break
328                 }
329                 c.Logf("waiting for instances to be identifiable as spot instances...")
330                 time.Sleep(10 * time.Second)
331         }
332
333         for _, inst := range instances {
334                 hist := inst.PriceHistory(arvados.InstanceType{})
335                 c.Logf("%s price history: %v", inst.ID(), hist)
336                 c.Check(len(hist) > 0, check.Equals, true)
337
338                 histWithScratch := inst.PriceHistory(arvados.InstanceType{AddedScratch: 640 << 30})
339                 c.Logf("%s price history with 640 GiB scratch: %v", inst.ID(), histWithScratch)
340
341                 for i, ip := range hist {
342                         c.Check(ip.Price, check.Not(check.Equals), 0.0)
343                         if i > 0 {
344                                 c.Check(ip.StartTime.Before(hist[i-1].StartTime), check.Equals, true)
345                         }
346                         c.Check(ip.Price < histWithScratch[i].Price, check.Equals, true)
347                 }
348         }
349 }
350
351 func (*EC2InstanceSetSuite) TestWrapError(c *check.C) {
352         retryError := awserr.New("Throttling", "", nil)
353         wrapped := wrapError(retryError, &atomic.Value{})
354         _, ok := wrapped.(cloud.RateLimitError)
355         c.Check(ok, check.Equals, true)
356
357         quotaError := awserr.New("InsufficientInstanceCapacity", "", nil)
358         wrapped = wrapError(quotaError, nil)
359         _, ok = wrapped.(cloud.QuotaError)
360         c.Check(ok, check.Equals, true)
361 }