1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
6 // How to manually run individual tests against the real cloud:
8 // $ go test -v git.arvados.org/arvados.git/lib/cloud/ec2 -live-ec2-cfg ec2config.yml -check.f=TestCreate
10 // Tests should be run individually and in the order they are listed in the file:
12 // Example azconfig.yml:
14 // ImageIDForTestSuite: ami-xxxxxxxxxxxxxxxxx
16 // AccessKeyID: XXXXXXXXXXXXXX
17 // SecretAccessKey: xxxxxxxxxxxxxxxxxxxx
19 // SecurityGroupIDs: [sg-xxxxxxxx]
20 // SubnetID: subnet-xxxxxxxx
21 // AdminUsername: crunch
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"
42 var live = flag.String("live-ec2-cfg", "", "Test with real EC2 API, provide config file")
44 // Gocheck boilerplate
45 func Test(t *testing.T) {
49 type EC2InstanceSetSuite struct{}
51 var _ = check.Suite(&EC2InstanceSetSuite{})
53 type testConfig struct {
54 ImageIDForTestSuite string
55 DriverParameters json.RawMessage
61 func (e *ec2stub) ImportKeyPair(input *ec2.ImportKeyPairInput) (*ec2.ImportKeyPairOutput, error) {
65 func (e *ec2stub) DescribeKeyPairs(input *ec2.DescribeKeyPairsInput) (*ec2.DescribeKeyPairsOutput, error) {
66 return &ec2.DescribeKeyPairsOutput{}, nil
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,
76 func (e *ec2stub) DescribeInstances(input *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) {
77 return &ec2.DescribeInstancesOutput{}, nil
80 func (e *ec2stub) CreateTags(input *ec2.CreateTagsInput) (*ec2.CreateTagsOutput, error) {
84 func (e *ec2stub) TerminateInstances(input *ec2.TerminateInstancesInput) (*ec2.TerminateInstancesOutput, error) {
88 func GetInstanceSet() (cloud.InstanceSet, cloud.ImageID, arvados.Cluster, error) {
89 cluster := arvados.Cluster{
90 InstanceTypes: arvados.InstanceTypeMap(map[string]arvados.InstanceType{
93 ProviderType: "t2.micro",
100 "tiny-with-extra-scratch": {
102 ProviderType: "t2.micro",
107 AddedScratch: 20000000000,
109 "tiny-preemptible": {
111 ProviderType: "t2.micro",
114 Scratch: 10000000000,
120 var exampleCfg testConfig
121 err := config.LoadFile(&exampleCfg, *live)
123 return nil, cloud.ImageID(""), cluster, err
126 ap, err := newEC2InstanceSet(exampleCfg.DriverParameters, "test123", nil, logrus.StandardLogger())
127 return ap, cloud.ImageID(exampleCfg.ImageIDForTestSuite), cluster, err
129 ap := ec2InstanceSet{
130 ec2config: ec2InstanceSetConfig{},
131 instanceSetID: "test123",
132 logger: logrus.StandardLogger(),
134 keys: make(map[string]string),
136 return &ap, cloud.ImageID("blob"), cluster, nil
139 func (*EC2InstanceSetSuite) TestCreate(c *check.C) {
140 ap, img, cluster, err := GetInstanceSet()
142 c.Fatal("Error making provider", err)
145 pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
146 c.Assert(err, check.IsNil)
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)
153 c.Assert(err, check.IsNil)
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)
161 func (*EC2InstanceSetSuite) TestCreateWithExtraScratch(c *check.C) {
162 ap, img, cluster, err := GetInstanceSet()
164 c.Fatal("Error making provider", err)
167 pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
168 c.Assert(err, check.IsNil)
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)
175 c.Assert(err, check.IsNil)
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)
183 func (*EC2InstanceSetSuite) TestCreatePreemptible(c *check.C) {
184 ap, img, cluster, err := GetInstanceSet()
186 c.Fatal("Error making provider", err)
189 pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
190 c.Assert(err, check.IsNil)
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)
197 c.Assert(err, check.IsNil)
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)
205 func (*EC2InstanceSetSuite) TestTagInstances(c *check.C) {
206 ap, _, _, err := GetInstanceSet()
208 c.Fatal("Error making provider", err)
211 l, err := ap.Instances(nil)
212 c.Assert(err, check.IsNil)
214 for _, i := range l {
216 tg["TestTag2"] = "123 test tag 2"
217 c.Check(i.SetTags(tg), check.IsNil)
221 func (*EC2InstanceSetSuite) TestListInstances(c *check.C) {
222 ap, _, _, err := GetInstanceSet()
224 c.Fatal("Error making provider: ", err)
227 l, err := ap.Instances(nil)
229 c.Assert(err, check.IsNil)
231 for _, i := range l {
233 c.Logf("%v %v %v", i.String(), i.Address(), tg)
237 func (*EC2InstanceSetSuite) TestDestroyInstances(c *check.C) {
238 ap, _, _, err := GetInstanceSet()
240 c.Fatal("Error making provider", err)
243 l, err := ap.Instances(nil)
244 c.Assert(err, check.IsNil)
246 for _, i := range l {
247 c.Check(i.Destroy(), check.IsNil)
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)
257 quotaError := awserr.New("InsufficientInstanceCapacity", "", nil)
258 wrapped = wrapError(quotaError, nil)
259 _, ok = wrapped.(cloud.QuotaError)
260 c.Check(ok, check.Equals, true)