Merge branch '20541-less-fetching-mounts'
[arvados.git] / lib / cloud / ec2 / ec2_test.go
index 2b4b19850476c439c17e484c3c6863804e0311c5..ede7f9de5d2cc8b3dd1ddb17416782c3ec21aee7 100644 (file)
@@ -57,15 +57,19 @@ type testConfig struct {
 }
 
 type ec2stub struct {
-       c       *check.C
-       reftime time.Time
+       c                     *check.C
+       reftime               time.Time
+       importKeyPairCalls    []*ec2.ImportKeyPairInput
+       describeKeyPairsCalls []*ec2.DescribeKeyPairsInput
 }
 
 func (e *ec2stub) ImportKeyPair(input *ec2.ImportKeyPairInput) (*ec2.ImportKeyPairOutput, error) {
+       e.importKeyPairCalls = append(e.importKeyPairCalls, input)
        return nil, nil
 }
 
 func (e *ec2stub) DescribeKeyPairs(input *ec2.DescribeKeyPairsInput) (*ec2.DescribeKeyPairsOutput, error) {
+       e.describeKeyPairsCalls = append(e.describeKeyPairsCalls, input)
        return &ec2.DescribeKeyPairsOutput{}, nil
 }
 
@@ -85,13 +89,13 @@ func (e *ec2stub) DescribeInstances(input *ec2.DescribeInstancesInput) (*ec2.Des
                                InstanceLifecycle: aws.String("spot"),
                                InstanceType:      aws.String("t2.micro"),
                                PrivateIpAddress:  aws.String("10.1.2.3"),
-                               State:             &ec2.InstanceState{Name: aws.String("running")},
+                               State:             &ec2.InstanceState{Name: aws.String("running"), Code: aws.Int64(16)},
                        }, {
                                InstanceId:        aws.String("i-124"),
                                InstanceLifecycle: aws.String("spot"),
                                InstanceType:      aws.String("t2.micro"),
                                PrivateIpAddress:  aws.String("10.1.2.4"),
-                               State:             &ec2.InstanceState{Name: aws.String("running")},
+                               State:             &ec2.InstanceState{Name: aws.String("running"), Code: aws.Int64(16)},
                        }},
                }},
        }, nil
@@ -191,9 +195,6 @@ func GetInstanceSet(c *check.C) (*ec2InstanceSet, cloud.ImageID, arvados.Cluster
                return ap.(*ec2InstanceSet), cloud.ImageID(exampleCfg.ImageIDForTestSuite), cluster
        }
        ap := ec2InstanceSet{
-               ec2config: ec2InstanceSetConfig{
-                       SpotPriceUpdateInterval: arvados.Duration(time.Hour),
-               },
                instanceSetID: "test123",
                logger:        logrus.StandardLogger(),
                client:        &ec2stub{c: c, reftime: time.Now().UTC()},
@@ -216,16 +217,18 @@ func (*EC2InstanceSetSuite) TestCreate(c *check.C) {
        c.Check(tags["TestTagName"], check.Equals, "test tag value")
        c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
 
+       if *live == "" {
+               c.Check(ap.client.(*ec2stub).describeKeyPairsCalls, check.HasLen, 1)
+               c.Check(ap.client.(*ec2stub).importKeyPairCalls, check.HasLen, 1)
+       }
 }
 
 func (*EC2InstanceSetSuite) TestCreateWithExtraScratch(c *check.C) {
        ap, img, cluster := GetInstanceSet(c)
-       pk, _ := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
-
        inst, err := ap.Create(cluster.InstanceTypes["tiny-with-extra-scratch"],
                img, map[string]string{
                        "TestTagName": "test tag value",
-               }, "umask 0600; echo -n test-file-data >/var/run/test-file", pk)
+               }, "umask 0600; echo -n test-file-data >/var/run/test-file", nil)
 
        c.Assert(err, check.IsNil)
 
@@ -233,6 +236,12 @@ func (*EC2InstanceSetSuite) TestCreateWithExtraScratch(c *check.C) {
        c.Check(tags["TestTagName"], check.Equals, "test tag value")
        c.Logf("inst.String()=%v Address()=%v Tags()=%v", inst.String(), inst.Address(), tags)
 
+       if *live == "" {
+               // Should not have called key pair APIs, because
+               // publickey arg was nil
+               c.Check(ap.client.(*ec2stub).describeKeyPairsCalls, check.HasLen, 0)
+               c.Check(ap.client.(*ec2stub).importKeyPairCalls, check.HasLen, 0)
+       }
 }
 
 func (*EC2InstanceSetSuite) TestCreatePreemptible(c *check.C) {
@@ -299,6 +308,7 @@ func (*EC2InstanceSetSuite) TestInstancePriceHistory(c *check.C) {
                }
        }()
 
+       ap.ec2config.SpotPriceUpdateInterval = arvados.Duration(time.Hour)
        ap.ec2config.EBSPrice = 0.1 // $/GiB/month
        inst1, err := ap.Create(cluster.InstanceTypes["tiny-preemptible"], img, tags, "true", pk)
        c.Assert(err, check.IsNil)
@@ -318,7 +328,8 @@ func (*EC2InstanceSetSuite) TestInstancePriceHistory(c *check.C) {
                instances, err = ap.Instances(tags)
                running := 0
                for _, inst := range instances {
-                       if *inst.(*ec2Instance).instance.InstanceLifecycle == "spot" {
+                       ec2i := inst.(*ec2Instance).instance
+                       if *ec2i.InstanceLifecycle == "spot" && *ec2i.State.Code&16 != 0 {
                                running++
                        }
                }
@@ -326,7 +337,7 @@ func (*EC2InstanceSetSuite) TestInstancePriceHistory(c *check.C) {
                        c.Logf("instances are running, and identifiable as spot instances")
                        break
                }
-               c.Logf("waiting for instances to be identifiable as spot instances...")
+               c.Logf("waiting for instances to reach running state so their availability zone becomes visible...")
                time.Sleep(10 * time.Second)
        }