16736: Fixes typo.
[arvados.git] / lib / cloud / ec2 / ec2_test.go
index ab6c6b597b399af717d0b18f7d0f546c25d846e5..6aa6e857ff59b278aa3a54292ab461527502d84b 100644 (file)
@@ -5,7 +5,7 @@
 //
 // How to manually run individual tests against the real cloud:
 //
-// $ go test -v git.curoverse.com/arvados.git/lib/cloud/ec2 -live-ec2-cfg ec2config.yml -check.f=TestCreate
+// $ go test -v git.arvados.org/arvados.git/lib/cloud/ec2 -live-ec2-cfg ec2config.yml -check.f=TestCreate
 //
 // Tests should be run individually and in the order they are listed in the file:
 //
 //       AccessKeyID: XXXXXXXXXXXXXX
 //       SecretAccessKey: xxxxxxxxxxxxxxxxxxxx
 //       Region: us-east-1
-//       SecurityGroupId: sg-xxxxxxxx
-//       SubnetId: subnet-xxxxxxxx
+//       SecurityGroupIDs: [sg-xxxxxxxx]
+//       SubnetID: subnet-xxxxxxxx
 //       AdminUsername: crunch
-//       KeyPairName: arvados-dispatcher-keypair
 
 package ec2
 
 import (
        "encoding/json"
        "flag"
-       "log"
        "testing"
 
-       "git.curoverse.com/arvados.git/lib/cloud"
-       "git.curoverse.com/arvados.git/sdk/go/arvados"
-       "git.curoverse.com/arvados.git/sdk/go/config"
+       "git.arvados.org/arvados.git/lib/cloud"
+       "git.arvados.org/arvados.git/lib/dispatchcloud/test"
+       "git.arvados.org/arvados.git/sdk/go/arvados"
+       "git.arvados.org/arvados.git/sdk/go/config"
+       "github.com/aws/aws-sdk-go/aws"
+       "github.com/aws/aws-sdk-go/service/ec2"
        "github.com/sirupsen/logrus"
-       "golang.org/x/crypto/ssh"
        check "gopkg.in/check.v1"
 )
 
@@ -53,31 +53,60 @@ type testConfig struct {
        DriverParameters    json.RawMessage
 }
 
+type ec2stub struct {
+}
+
+func (e *ec2stub) ImportKeyPair(input *ec2.ImportKeyPairInput) (*ec2.ImportKeyPairOutput, error) {
+       return nil, nil
+}
+
+func (e *ec2stub) DescribeKeyPairs(input *ec2.DescribeKeyPairsInput) (*ec2.DescribeKeyPairsOutput, error) {
+       return &ec2.DescribeKeyPairsOutput{}, nil
+}
+
+func (e *ec2stub) RunInstances(input *ec2.RunInstancesInput) (*ec2.Reservation, error) {
+       return &ec2.Reservation{Instances: []*ec2.Instance{{
+               InstanceId: aws.String("i-123"),
+               Tags:       input.TagSpecifications[0].Tags,
+       }}}, nil
+}
+
+func (e *ec2stub) DescribeInstances(input *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) {
+       return &ec2.DescribeInstancesOutput{}, nil
+}
+
+func (e *ec2stub) CreateTags(input *ec2.CreateTagsInput) (*ec2.CreateTagsOutput, error) {
+       return nil, nil
+}
+
+func (e *ec2stub) TerminateInstances(input *ec2.TerminateInstancesInput) (*ec2.TerminateInstancesOutput, error) {
+       return nil, nil
+}
+
 func GetInstanceSet() (cloud.InstanceSet, cloud.ImageID, arvados.Cluster, error) {
        cluster := arvados.Cluster{
                InstanceTypes: arvados.InstanceTypeMap(map[string]arvados.InstanceType{
-                       "tiny": arvados.InstanceType{
+                       "tiny": {
                                Name:         "tiny",
-                               ProviderType: "m1.small",
+                               ProviderType: "t2.micro",
                                VCPUs:        1,
                                RAM:          4000000000,
                                Scratch:      10000000000,
                                Price:        .02,
                                Preemptible:  false,
                        },
-                       "tiny-with-extra-scratch": arvados.InstanceType{
+                       "tiny-with-extra-scratch": {
                                Name:         "tiny",
-                               ProviderType: "m1.small",
+                               ProviderType: "t2.micro",
                                VCPUs:        1,
                                RAM:          4000000000,
-                               Scratch:      10000000000,
-                               ExtraScratch: 20000000000,
                                Price:        .02,
                                Preemptible:  false,
+                               AddedScratch: 20000000000,
                        },
-                       "tiny-preemptible": arvados.InstanceType{
+                       "tiny-preemptible": {
                                Name:         "tiny",
-                               ProviderType: "m1.small",
+                               ProviderType: "t2.micro",
                                VCPUs:        1,
                                RAM:          4000000000,
                                Scratch:      10000000000,
@@ -92,26 +121,26 @@ func GetInstanceSet() (cloud.InstanceSet, cloud.ImageID, arvados.Cluster, error)
                        return nil, cloud.ImageID(""), cluster, err
                }
 
-               ap, err := newEC2InstanceSet(exampleCfg.DriverParameters, "test123", logrus.StandardLogger())
+               ap, err := newEC2InstanceSet(exampleCfg.DriverParameters, "test123", nil, logrus.StandardLogger())
                return ap, cloud.ImageID(exampleCfg.ImageIDForTestSuite), cluster, err
        }
        ap := ec2InstanceSet{
-               ec2config:    ec2InstanceSetConfig{},
-               dispatcherID: "test123",
-               logger:       logrus.StandardLogger(),
+               ec2config:     ec2InstanceSetConfig{},
+               instanceSetID: "test123",
+               logger:        logrus.StandardLogger(),
+               client:        &ec2stub{},
+               keys:          make(map[string]string),
        }
        return &ap, cloud.ImageID("blob"), cluster, nil
 }
 
-var testKey = []byte(`ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDLQS1ExT2+WjA0d/hntEAyAtgeN1W2ik2QX8c2zO6HjlPHWXL92r07W0WMuDib40Pcevpi1BXeBWXA9ZB5KKMJB+ukaAu22KklnQuUmNvk6ZXnPKSkGxuCYvPQb08WhHf3p1VxiKfP3iauedBDM4x9/bkJohlBBQiFXzNUcQ+a6rKiMzmJN2gbL8ncyUzc+XQ5q4JndTwTGtOlzDiGOc9O4z5Dd76wtAVJneOuuNpwfFRVHThpJM6VThpCZOnl8APaceWXKeuwOuCae3COZMz++xQfxOfZ9Z8aIwo+TlQhsRaNfZ4Vjrop6ej8dtfZtgUFKfbXEOYaHrGrWGotFDTD example@example`)
-
 func (*EC2InstanceSetSuite) TestCreate(c *check.C) {
        ap, img, cluster, err := GetInstanceSet()
        if err != nil {
                c.Fatal("Error making provider", err)
        }
 
-       pk, _, _, _, err := ssh.ParseAuthorizedKey(testKey)
+       pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
        c.Assert(err, check.IsNil)
 
        inst, err := ap.Create(cluster.InstanceTypes["tiny"],
@@ -133,7 +162,7 @@ func (*EC2InstanceSetSuite) TestCreateWithExtraScratch(c *check.C) {
                c.Fatal("Error making provider", err)
        }
 
-       pk, _, _, _, err := ssh.ParseAuthorizedKey(testKey)
+       pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
        c.Assert(err, check.IsNil)
 
        inst, err := ap.Create(cluster.InstanceTypes["tiny-with-extra-scratch"],
@@ -155,7 +184,7 @@ func (*EC2InstanceSetSuite) TestCreatePreemptible(c *check.C) {
                c.Fatal("Error making provider", err)
        }
 
-       pk, _, _, _, err := ssh.ParseAuthorizedKey(testKey)
+       pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
        c.Assert(err, check.IsNil)
 
        inst, err := ap.Create(cluster.InstanceTypes["tiny-preemptible"],
@@ -171,6 +200,22 @@ func (*EC2InstanceSetSuite) TestCreatePreemptible(c *check.C) {
 
 }
 
+func (*EC2InstanceSetSuite) TestTagInstances(c *check.C) {
+       ap, _, _, err := GetInstanceSet()
+       if err != nil {
+               c.Fatal("Error making provider", err)
+       }
+
+       l, err := ap.Instances(nil)
+       c.Assert(err, check.IsNil)
+
+       for _, i := range l {
+               tg := i.Tags()
+               tg["TestTag2"] = "123 test tag 2"
+               c.Check(i.SetTags(tg), check.IsNil)
+       }
+}
+
 func (*EC2InstanceSetSuite) TestListInstances(c *check.C) {
        ap, _, _, err := GetInstanceSet()
        if err != nil {
@@ -183,7 +228,7 @@ func (*EC2InstanceSetSuite) TestListInstances(c *check.C) {
 
        for _, i := range l {
                tg := i.Tags()
-               log.Printf("%v %v %v", i.String(), i.Address(), tg)
+               c.Logf("%v %v %v", i.String(), i.Address(), tg)
        }
 }