github.phpd.cn/hashicorp/packer@v1.3.2/builder/amazon/common/ssh_test.go (about) 1 package common 2 3 import ( 4 "testing" 5 6 "github.com/aws/aws-sdk-go/aws" 7 "github.com/aws/aws-sdk-go/service/ec2" 8 "github.com/hashicorp/packer/helper/multistep" 9 ) 10 11 const ( 12 privateIP = "10.0.0.1" 13 publicIP = "192.168.1.1" 14 privateDNS = "private.dns.test" 15 publicDNS = "public.dns.test" 16 ) 17 18 func TestSSHHost(t *testing.T) { 19 origSshHostSleepDuration := sshHostSleepDuration 20 defer func() { sshHostSleepDuration = origSshHostSleepDuration }() 21 sshHostSleepDuration = 0 22 23 var cases = []struct { 24 allowTries int 25 vpcId string 26 sshInterface string 27 28 ok bool 29 wantHost string 30 }{ 31 {1, "", "", true, publicDNS}, 32 {1, "", "private_ip", true, privateIP}, 33 {1, "vpc-id", "", true, publicIP}, 34 {1, "vpc-id", "private_ip", true, privateIP}, 35 {1, "vpc-id", "private_dns", true, privateDNS}, 36 {1, "vpc-id", "public_dns", true, publicDNS}, 37 {1, "vpc-id", "public_ip", true, publicIP}, 38 {2, "", "", true, publicDNS}, 39 {2, "", "private_ip", true, privateIP}, 40 {2, "vpc-id", "", true, publicIP}, 41 {2, "vpc-id", "private_ip", true, privateIP}, 42 {2, "vpc-id", "private_dns", true, privateDNS}, 43 {2, "vpc-id", "public_dns", true, publicDNS}, 44 {2, "vpc-id", "public_ip", true, publicIP}, 45 {3, "", "", false, ""}, 46 {3, "", "private_ip", false, ""}, 47 {3, "vpc-id", "", false, ""}, 48 {3, "vpc-id", "private_ip", false, ""}, 49 {3, "vpc-id", "private_dns", false, ""}, 50 {3, "vpc-id", "public_dns", false, ""}, 51 {3, "vpc-id", "public_ip", false, ""}, 52 } 53 54 for _, c := range cases { 55 testSSHHost(t, c.allowTries, c.vpcId, c.sshInterface, c.ok, c.wantHost) 56 } 57 } 58 59 func testSSHHost(t *testing.T, allowTries int, vpcId string, sshInterface string, ok bool, wantHost string) { 60 t.Logf("allowTries=%d vpcId=%s sshInterface=%s ok=%t wantHost=%q", allowTries, vpcId, sshInterface, ok, wantHost) 61 62 e := &fakeEC2Describer{ 63 allowTries: allowTries, 64 vpcId: vpcId, 65 privateIP: privateIP, 66 publicIP: publicIP, 67 privateDNS: privateDNS, 68 publicDNS: publicDNS, 69 } 70 71 f := SSHHost(e, sshInterface) 72 st := &multistep.BasicStateBag{} 73 st.Put("instance", &ec2.Instance{ 74 InstanceId: aws.String("instance-id"), 75 }) 76 77 host, err := f(st) 78 79 if e.tries > allowTries { 80 t.Fatalf("got %d ec2 DescribeInstances tries, want %d", e.tries, allowTries) 81 } 82 83 switch { 84 case ok && err != nil: 85 t.Fatalf("expected no error, got %+v", err) 86 case !ok && err == nil: 87 t.Fatalf("expected error, got none and host %s", host) 88 } 89 90 if host != wantHost { 91 t.Fatalf("got host %s, want %s", host, wantHost) 92 } 93 } 94 95 type fakeEC2Describer struct { 96 allowTries int 97 tries int 98 99 vpcId string 100 privateIP, publicIP, privateDNS, publicDNS string 101 } 102 103 func (d *fakeEC2Describer) DescribeInstances(in *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) { 104 d.tries++ 105 106 instance := &ec2.Instance{ 107 InstanceId: aws.String("instance-id"), 108 } 109 110 if d.vpcId != "" { 111 instance.VpcId = aws.String(d.vpcId) 112 } 113 114 if d.tries >= d.allowTries { 115 instance.PublicIpAddress = aws.String(d.publicIP) 116 instance.PrivateIpAddress = aws.String(d.privateIP) 117 instance.PublicDnsName = aws.String(d.publicDNS) 118 instance.PrivateDnsName = aws.String(d.privateDNS) 119 } 120 121 out := &ec2.DescribeInstancesOutput{ 122 Reservations: []*ec2.Reservation{ 123 { 124 Instances: []*ec2.Instance{instance}, 125 }, 126 }, 127 } 128 129 return out, nil 130 }