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.curoverse.com/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
30 "git.curoverse.com/arvados.git/lib/cloud"
31 "git.curoverse.com/arvados.git/lib/dispatchcloud/test"
32 "git.curoverse.com/arvados.git/sdk/go/arvados"
33 "git.curoverse.com/arvados.git/sdk/go/config"
34 "github.com/aws/aws-sdk-go/aws"
35 "github.com/aws/aws-sdk-go/service/ec2"
36 "github.com/sirupsen/logrus"
37 check "gopkg.in/check.v1"
40 var live = flag.String("live-ec2-cfg", "", "Test with real EC2 API, provide config file")
42 // Gocheck boilerplate
43 func Test(t *testing.T) {
47 type EC2InstanceSetSuite struct{}
49 var _ = check.Suite(&EC2InstanceSetSuite{})
51 type testConfig struct {
52 ImageIDForTestSuite string
53 DriverParameters json.RawMessage
59 func (e *ec2stub) ImportKeyPair(input *ec2.ImportKeyPairInput) (*ec2.ImportKeyPairOutput, error) {
63 func (e *ec2stub) DescribeKeyPairs(input *ec2.DescribeKeyPairsInput) (*ec2.DescribeKeyPairsOutput, error) {
64 return &ec2.DescribeKeyPairsOutput{}, nil
67 func (e *ec2stub) RunInstances(input *ec2.RunInstancesInput) (*ec2.Reservation, error) {
68 return &ec2.Reservation{Instances: []*ec2.Instance{&ec2.Instance{
69 InstanceId: aws.String("i-123"),
70 Tags: input.TagSpecifications[0].Tags,
74 func (e *ec2stub) DescribeInstances(input *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) {
75 return &ec2.DescribeInstancesOutput{}, nil
78 func (e *ec2stub) CreateTags(input *ec2.CreateTagsInput) (*ec2.CreateTagsOutput, error) {
82 func (e *ec2stub) TerminateInstances(input *ec2.TerminateInstancesInput) (*ec2.TerminateInstancesOutput, error) {
86 func GetInstanceSet() (cloud.InstanceSet, cloud.ImageID, arvados.Cluster, error) {
87 cluster := arvados.Cluster{
88 InstanceTypes: arvados.InstanceTypeMap(map[string]arvados.InstanceType{
89 "tiny": arvados.InstanceType{
91 ProviderType: "t2.micro",
98 "tiny-with-extra-scratch": arvados.InstanceType{
100 ProviderType: "t2.micro",
105 AddedScratch: 20000000000,
107 "tiny-preemptible": arvados.InstanceType{
109 ProviderType: "t2.micro",
112 Scratch: 10000000000,
118 var exampleCfg testConfig
119 err := config.LoadFile(&exampleCfg, *live)
121 return nil, cloud.ImageID(""), cluster, err
124 ap, err := newEC2InstanceSet(exampleCfg.DriverParameters, "test123", logrus.StandardLogger())
125 return ap, cloud.ImageID(exampleCfg.ImageIDForTestSuite), cluster, err
127 ap := ec2InstanceSet{
128 ec2config: ec2InstanceSetConfig{},
129 dispatcherID: "test123",
130 logger: logrus.StandardLogger(),
132 keys: make(map[string]string),
134 return &ap, cloud.ImageID("blob"), cluster, nil
137 func (*EC2InstanceSetSuite) TestCreate(c *check.C) {
138 ap, img, cluster, err := GetInstanceSet()
140 c.Fatal("Error making provider", err)
143 pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
144 c.Assert(err, check.IsNil)
146 inst, err := ap.Create(cluster.InstanceTypes["tiny"],
147 img, map[string]string{
148 "TestTagName": "test tag value",
149 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
151 c.Assert(err, check.IsNil)
154 c.Check(tags["TestTagName"], check.Equals, "test tag value")
155 c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
159 func (*EC2InstanceSetSuite) TestCreateWithExtraScratch(c *check.C) {
160 ap, img, cluster, err := GetInstanceSet()
162 c.Fatal("Error making provider", err)
165 pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
166 c.Assert(err, check.IsNil)
168 inst, err := ap.Create(cluster.InstanceTypes["tiny-with-extra-scratch"],
169 img, map[string]string{
170 "TestTagName": "test tag value",
171 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
173 c.Assert(err, check.IsNil)
176 c.Check(tags["TestTagName"], check.Equals, "test tag value")
177 c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
181 func (*EC2InstanceSetSuite) TestCreatePreemptible(c *check.C) {
182 ap, img, cluster, err := GetInstanceSet()
184 c.Fatal("Error making provider", err)
187 pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
188 c.Assert(err, check.IsNil)
190 inst, err := ap.Create(cluster.InstanceTypes["tiny-preemptible"],
191 img, map[string]string{
192 "TestTagName": "test tag value",
193 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
195 c.Assert(err, check.IsNil)
198 c.Check(tags["TestTagName"], check.Equals, "test tag value")
199 c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
203 func (*EC2InstanceSetSuite) TestTagInstances(c *check.C) {
204 ap, _, _, err := GetInstanceSet()
206 c.Fatal("Error making provider", err)
209 l, err := ap.Instances(nil)
210 c.Assert(err, check.IsNil)
212 for _, i := range l {
214 tg["TestTag2"] = "123 test tag 2"
215 c.Check(i.SetTags(tg), check.IsNil)
219 func (*EC2InstanceSetSuite) TestListInstances(c *check.C) {
220 ap, _, _, err := GetInstanceSet()
222 c.Fatal("Error making provider: ", err)
225 l, err := ap.Instances(nil)
227 c.Assert(err, check.IsNil)
229 for _, i := range l {
231 c.Logf("%v %v %v", i.String(), i.Address(), tg)
235 func (*EC2InstanceSetSuite) TestDestroyInstances(c *check.C) {
236 ap, _, _, err := GetInstanceSet()
238 c.Fatal("Error making provider", err)
241 l, err := ap.Instances(nil)
242 c.Assert(err, check.IsNil)
244 for _, i := range l {
245 c.Check(i.Destroy(), check.IsNil)