2016-09-07 11:15:53 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2016-09-07 11:15:53 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-09-27 12:19:23 -04:00
|
|
|
privateIP = "10.0.0.1"
|
|
|
|
publicIP = "192.168.1.1"
|
|
|
|
privateDNS = "private.dns.test"
|
|
|
|
publicDNS = "public.dns.test"
|
|
|
|
sshHostTemplate = "custom.host.value"
|
2016-09-07 11:15:53 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSSHHost(t *testing.T) {
|
|
|
|
origSshHostSleepDuration := sshHostSleepDuration
|
|
|
|
defer func() { sshHostSleepDuration = origSshHostSleepDuration }()
|
|
|
|
sshHostSleepDuration = 0
|
|
|
|
|
|
|
|
var cases = []struct {
|
2017-11-21 21:49:38 -05:00
|
|
|
allowTries int
|
|
|
|
vpcId string
|
|
|
|
sshInterface string
|
2016-09-07 11:15:53 -04:00
|
|
|
|
2019-09-27 12:19:23 -04:00
|
|
|
ok bool
|
|
|
|
wantHost string
|
|
|
|
sshHostOverride string
|
2016-09-07 11:15:53 -04:00
|
|
|
}{
|
2019-09-27 12:19:23 -04:00
|
|
|
{1, "", "", true, publicDNS, ""},
|
|
|
|
{1, "", "private_ip", true, privateIP, ""},
|
|
|
|
{1, "vpc-id", "", true, publicIP, ""},
|
|
|
|
{1, "vpc-id", "private_ip", true, privateIP, ""},
|
|
|
|
{1, "vpc-id", "private_dns", true, privateDNS, ""},
|
|
|
|
{1, "vpc-id", "public_dns", true, publicDNS, ""},
|
|
|
|
{1, "vpc-id", "public_ip", true, publicIP, ""},
|
|
|
|
{2, "", "", true, publicDNS, ""},
|
|
|
|
{2, "", "private_ip", true, privateIP, ""},
|
|
|
|
{2, "vpc-id", "", true, publicIP, ""},
|
|
|
|
{2, "vpc-id", "private_ip", true, privateIP, ""},
|
|
|
|
{2, "vpc-id", "private_dns", true, privateDNS, ""},
|
|
|
|
{2, "vpc-id", "public_dns", true, publicDNS, ""},
|
|
|
|
{2, "vpc-id", "public_ip", true, publicIP, ""},
|
|
|
|
{3, "", "", false, "", ""},
|
|
|
|
{3, "", "private_ip", false, "", ""},
|
|
|
|
{3, "vpc-id", "", false, "", ""},
|
|
|
|
{3, "vpc-id", "private_ip", false, "", ""},
|
|
|
|
{3, "vpc-id", "private_dns", false, "", ""},
|
|
|
|
{3, "vpc-id", "public_dns", false, "", ""},
|
|
|
|
{3, "vpc-id", "public_ip", false, "", ""},
|
|
|
|
{1, "", "", true, sshHostTemplate, sshHostTemplate},
|
|
|
|
{1, "vpc-id", "", true, sshHostTemplate, sshHostTemplate},
|
|
|
|
{2, "vpc-id", "private_dns", true, sshHostTemplate, sshHostTemplate},
|
2016-09-07 11:15:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2019-09-27 12:19:23 -04:00
|
|
|
testSSHHost(t, c.allowTries, c.vpcId, c.sshInterface, c.ok, c.wantHost,
|
|
|
|
c.sshHostOverride)
|
2016-09-07 11:15:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 12:19:23 -04:00
|
|
|
func testSSHHost(t *testing.T, allowTries int, vpcId string, sshInterface string,
|
|
|
|
ok bool, wantHost string, sshHostOverride string) {
|
|
|
|
t.Logf("allowTries=%d vpcId=%s sshInterface=%s ok=%t wantHost=%q sshHostOverride=%s",
|
|
|
|
allowTries, vpcId, sshInterface, ok, wantHost, sshHostOverride)
|
2016-09-07 11:15:53 -04:00
|
|
|
|
|
|
|
e := &fakeEC2Describer{
|
|
|
|
allowTries: allowTries,
|
|
|
|
vpcId: vpcId,
|
|
|
|
privateIP: privateIP,
|
|
|
|
publicIP: publicIP,
|
2017-11-21 21:49:38 -05:00
|
|
|
privateDNS: privateDNS,
|
2016-09-07 11:15:53 -04:00
|
|
|
publicDNS: publicDNS,
|
|
|
|
}
|
|
|
|
|
2019-09-27 12:19:23 -04:00
|
|
|
f := SSHHost(e, sshInterface, sshHostOverride)
|
2016-09-07 11:15:53 -04:00
|
|
|
st := &multistep.BasicStateBag{}
|
|
|
|
st.Put("instance", &ec2.Instance{
|
|
|
|
InstanceId: aws.String("instance-id"),
|
|
|
|
})
|
|
|
|
|
|
|
|
host, err := f(st)
|
|
|
|
|
|
|
|
if e.tries > allowTries {
|
|
|
|
t.Fatalf("got %d ec2 DescribeInstances tries, want %d", e.tries, allowTries)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ok && err != nil:
|
|
|
|
t.Fatalf("expected no error, got %+v", err)
|
|
|
|
case !ok && err == nil:
|
|
|
|
t.Fatalf("expected error, got none and host %s", host)
|
|
|
|
}
|
|
|
|
|
|
|
|
if host != wantHost {
|
|
|
|
t.Fatalf("got host %s, want %s", host, wantHost)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeEC2Describer struct {
|
|
|
|
allowTries int
|
|
|
|
tries int
|
|
|
|
|
2017-11-21 21:49:38 -05:00
|
|
|
vpcId string
|
|
|
|
privateIP, publicIP, privateDNS, publicDNS string
|
2016-09-07 11:15:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *fakeEC2Describer) DescribeInstances(in *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) {
|
|
|
|
d.tries++
|
|
|
|
|
|
|
|
instance := &ec2.Instance{
|
|
|
|
InstanceId: aws.String("instance-id"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.vpcId != "" {
|
|
|
|
instance.VpcId = aws.String(d.vpcId)
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.tries >= d.allowTries {
|
|
|
|
instance.PublicIpAddress = aws.String(d.publicIP)
|
|
|
|
instance.PrivateIpAddress = aws.String(d.privateIP)
|
|
|
|
instance.PublicDnsName = aws.String(d.publicDNS)
|
2017-11-21 21:49:38 -05:00
|
|
|
instance.PrivateDnsName = aws.String(d.privateDNS)
|
2016-09-07 11:15:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
out := &ec2.DescribeInstancesOutput{
|
|
|
|
Reservations: []*ec2.Reservation{
|
|
|
|
{
|
|
|
|
Instances: []*ec2.Instance{instance},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|