14291: EC2 driver WIP
[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.curoverse.com/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 azconfig.yml:
13 //
14 // ImageIDForTestSuite: ami-xxxxxxxxxxxxxxxxx
15 // DriverParameters:
16 //       AccessKeyID: XXXXXXXXXXXXXX
17 //       SecretAccessKey: xxxxxxxxxxxxxxxxxxxx
18 //       Region: us-east-1
19 //       SecurityGroupId: sg-xxxxxxxx
20 //       SubnetId: subnet-xxxxxxxx
21 //       AdminUsername: crunch
22
23 package ec2
24
25 import (
26         "encoding/json"
27         "flag"
28         "log"
29         "testing"
30
31         "git.curoverse.com/arvados.git/lib/cloud"
32         "git.curoverse.com/arvados.git/sdk/go/arvados"
33         "git.curoverse.com/arvados.git/sdk/go/config"
34         "github.com/sirupsen/logrus"
35         "golang.org/x/crypto/ssh"
36         check "gopkg.in/check.v1"
37 )
38
39 var live = flag.String("live-ec2-cfg", "", "Test with real EC2 API, provide config file")
40
41 // Gocheck boilerplate
42 func Test(t *testing.T) {
43         check.TestingT(t)
44 }
45
46 type EC2InstanceSetSuite struct{}
47
48 var _ = check.Suite(&EC2InstanceSetSuite{})
49
50 type testConfig struct {
51         ImageIDForTestSuite string
52         DriverParameters    json.RawMessage
53 }
54
55 func GetInstanceSet() (cloud.InstanceSet, cloud.ImageID, arvados.Cluster, error) {
56         cluster := arvados.Cluster{
57                 InstanceTypes: arvados.InstanceTypeMap(map[string]arvados.InstanceType{
58                         "tiny": arvados.InstanceType{
59                                 Name:         "tiny",
60                                 ProviderType: "m1.small",
61                                 VCPUs:        1,
62                                 RAM:          4000000000,
63                                 Scratch:      10000000000,
64                                 Price:        .02,
65                                 Preemptible:  false,
66                         },
67                 })}
68         if *live != "" {
69                 var exampleCfg testConfig
70                 err := config.LoadFile(&exampleCfg, *live)
71                 if err != nil {
72                         return nil, cloud.ImageID(""), cluster, err
73                 }
74
75                 ap, err := newEC2InstanceSet(exampleCfg.DriverParameters, "test123", logrus.StandardLogger())
76                 return ap, cloud.ImageID(exampleCfg.ImageIDForTestSuite), cluster, err
77         }
78         ap := ec2InstanceSet{
79                 ec2config:    ec2InstanceSetConfig{},
80                 dispatcherID: "test123",
81                 logger:       logrus.StandardLogger(),
82         }
83         return &ap, cloud.ImageID("blob"), cluster, nil
84 }
85
86 var testKey = []byte(`ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDLQS1ExT2+WjA0d/hntEAyAtgeN1W2ik2QX8c2zO6HjlPHWXL92r07W0WMuDib40Pcevpi1BXeBWXA9ZB5KKMJB+ukaAu22KklnQuUmNvk6ZXnPKSkGxuCYvPQb08WhHf3p1VxiKfP3iauedBDM4x9/bkJohlBBQiFXzNUcQ+a6rKiMzmJN2gbL8ncyUzc+XQ5q4JndTwTGtOlzDiGOc9O4z5Dd76wtAVJneOuuNpwfFRVHThpJM6VThpCZOnl8APaceWXKeuwOuCae3COZMz++xQfxOfZ9Z8aIwo+TlQhsRaNfZ4Vjrop6ej8dtfZtgUFKfbXEOYaHrGrWGotFDTD example@example`)
87
88 func (*EC2InstanceSetSuite) TestCreate(c *check.C) {
89         ap, img, cluster, err := GetInstanceSet()
90         if err != nil {
91                 c.Fatal("Error making provider", err)
92         }
93
94         pk, _, _, _, err := ssh.ParseAuthorizedKey(testKey)
95         c.Assert(err, check.IsNil)
96
97         inst, err := ap.Create(cluster.InstanceTypes["tiny"],
98                 img, map[string]string{
99                         "TestTagName": "test tag value",
100                 }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
101
102         c.Assert(err, check.IsNil)
103
104         tags := inst.Tags()
105         c.Check(tags["TestTagName"], check.Equals, "test tag value")
106         c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
107
108 }
109
110 func (*EC2InstanceSetSuite) TestListInstances(c *check.C) {
111         ap, _, _, err := GetInstanceSet()
112         if err != nil {
113                 c.Fatal("Error making provider", err)
114         }
115
116         l, err := ap.Instances(nil)
117
118         c.Assert(err, check.IsNil)
119
120         for _, i := range l {
121                 tg := i.Tags()
122                 log.Printf("%v %v %v", i.String(), i.Address(), tg)
123         }
124 }
125
126 func (*EC2InstanceSetSuite) TestDestroyInstances(c *check.C) {
127         ap, _, _, err := GetInstanceSet()
128         if err != nil {
129                 c.Fatal("Error making provider", err)
130         }
131
132         l, err := ap.Instances(nil)
133         c.Assert(err, check.IsNil)
134
135         for _, i := range l {
136                 c.Check(i.Destroy(), check.IsNil)
137         }
138 }