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