Fix 2829 : aws unit test are unit again

Signed-off-by: Jean-Laurent de Morlhon <jeanlaurent@morlhon.net>
This commit is contained in:
Jean-Laurent de Morlhon 2016-01-15 13:38:01 +01:00
parent 438f91c6ba
commit 6023e13169
3 changed files with 43 additions and 170 deletions

View File

@ -1,8 +1,6 @@
package amazonec2 package amazonec2
import ( import (
"io/ioutil"
"os"
"testing" "testing"
"errors" "errors"
@ -10,20 +8,12 @@ import (
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
"github.com/docker/machine/commands/commandstest" "github.com/docker/machine/commands/commandstest"
"github.com/docker/machine/commands/mcndirs"
"github.com/docker/machine/libmachine/drivers"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
const ( const (
testSSHPort = 22 testSSHPort = 22
testDockerPort = 2376 testDockerPort = 2376
testStoreDir = ".store-test"
machineTestName = "test-host"
machineTestDriverName = "none"
machineTestStorePath = "/test/path"
machineTestCaCert = "test-cert"
machineTestPrivateKey = "test-key"
) )
var ( var (
@ -34,85 +24,17 @@ var (
} }
) )
func cleanup() error {
return os.RemoveAll(testStoreDir)
}
func getTestStorePath() (string, error) {
tmpDir, err := ioutil.TempDir("", "machine-test-")
if err != nil {
return "", err
}
mcndirs.BaseDir = tmpDir
return tmpDir, nil
}
func getDefaultTestDriverFlags() *commandstest.FakeFlagger {
return &commandstest.FakeFlagger{
Data: map[string]interface{}{
"name": "test",
"url": "unix:///var/run/docker.sock",
"swarm": false,
"swarm-host": "",
"swarm-master": false,
"swarm-discovery": "",
"amazonec2-ami": "ami-12345",
"amazonec2-access-key": "abcdefg",
"amazonec2-secret-key": "12345",
"amazonec2-session-token": "",
"amazonec2-instance-type": "t1.micro",
"amazonec2-vpc-id": "vpc-12345",
"amazonec2-subnet-id": "subnet-12345",
"amazonec2-security-group": "docker-machine-test",
"amazonec2-region": "us-east-1",
"amazonec2-zone": "e",
"amazonec2-root-size": 10,
"amazonec2-iam-instance-profile": "",
"amazonec2-ssh-user": "ubuntu",
"amazonec2-request-spot-instance": false,
"amazonec2-spot-price": "",
"amazonec2-private-address-only": false,
"amazonec2-use-private-address": false,
"amazonec2-monitoring": false,
},
}
}
func getTestDriver() (*Driver, error) {
storePath, err := getTestStorePath()
if err != nil {
return nil, err
}
defer cleanup()
d := NewDriver(machineTestName, storePath)
d.SetConfigFromFlags(getDefaultTestDriverFlags())
return d, nil
}
func TestConfigureSecurityGroupPermissionsEmpty(t *testing.T) { func TestConfigureSecurityGroupPermissionsEmpty(t *testing.T) {
d, err := getTestDriver() driver := NewTestDriver()
if err != nil {
t.Fatal(err)
}
defer cleanup()
group := securityGroup perms := driver.configureSecurityGroupPermissions(securityGroup)
perms := d.configureSecurityGroupPermissions(group)
if len(perms) != 2 { assert.Len(t, perms, 2)
t.Fatalf("expected 2 permissions; received %d", len(perms))
}
} }
func TestConfigureSecurityGroupPermissionsSshOnly(t *testing.T) { func TestConfigureSecurityGroupPermissionsSshOnly(t *testing.T) {
d, err := getTestDriver() driver := NewTestDriver()
if err != nil {
t.Fatal(err)
}
defer cleanup()
group := securityGroup group := securityGroup
group.IpPermissions = []*ec2.IpPermission{ group.IpPermissions = []*ec2.IpPermission{
{ {
IpProtocol: aws.String("tcp"), IpProtocol: aws.String("tcp"),
@ -121,26 +43,15 @@ func TestConfigureSecurityGroupPermissionsSshOnly(t *testing.T) {
}, },
} }
perms := d.configureSecurityGroupPermissions(group) perms := driver.configureSecurityGroupPermissions(group)
if len(perms) != 1 {
t.Fatalf("expected 1 permission; received %d", len(perms))
}
receivedPort := *perms[0].FromPort assert.Len(t, perms, 1)
if receivedPort != testDockerPort { assert.Equal(t, testDockerPort, *perms[0].FromPort)
t.Fatalf("expected permission on port %d; received port %d", testDockerPort, receivedPort)
}
} }
func TestConfigureSecurityGroupPermissionsDockerOnly(t *testing.T) { func TestConfigureSecurityGroupPermissionsDockerOnly(t *testing.T) {
d, err := getTestDriver() driver := NewTestDriver()
if err != nil {
t.Fatal(err)
}
defer cleanup()
group := securityGroup group := securityGroup
group.IpPermissions = []*ec2.IpPermission{ group.IpPermissions = []*ec2.IpPermission{
{ {
IpProtocol: aws.String("tcp"), IpProtocol: aws.String("tcp"),
@ -149,26 +60,15 @@ func TestConfigureSecurityGroupPermissionsDockerOnly(t *testing.T) {
}, },
} }
perms := d.configureSecurityGroupPermissions(group) perms := driver.configureSecurityGroupPermissions(group)
if len(perms) != 1 {
t.Fatalf("expected 1 permission; received %d", len(perms))
}
receivedPort := *perms[0].FromPort assert.Len(t, perms, 1)
if receivedPort != testSSHPort { assert.Equal(t, testSSHPort, *perms[0].FromPort)
t.Fatalf("expected permission on port %d; received port %d", testSSHPort, receivedPort)
}
} }
func TestConfigureSecurityGroupPermissionsDockerAndSsh(t *testing.T) { func TestConfigureSecurityGroupPermissionsDockerAndSsh(t *testing.T) {
d, err := getTestDriver() driver := NewTestDriver()
if err != nil {
t.Fatal(err)
}
defer cleanup()
group := securityGroup group := securityGroup
group.IpPermissions = []*ec2.IpPermission{ group.IpPermissions = []*ec2.IpPermission{
{ {
IpProtocol: aws.String("tcp"), IpProtocol: aws.String("tcp"),
@ -182,69 +82,35 @@ func TestConfigureSecurityGroupPermissionsDockerAndSsh(t *testing.T) {
}, },
} }
perms := d.configureSecurityGroupPermissions(group) perms := driver.configureSecurityGroupPermissions(group)
if len(perms) != 0 {
t.Fatalf("expected 0 permissions; received %d", len(perms))
}
}
func TestAwsRegionList(t *testing.T) { assert.Empty(t, perms)
} }
func TestValidateAwsRegionValid(t *testing.T) { func TestValidateAwsRegionValid(t *testing.T) {
regions := []string{"eu-west-1", "eu-central-1"} regions := []string{"eu-west-1", "eu-central-1"}
for _, v := range regions { for _, region := range regions {
r, err := validateAwsRegion(v) validatedRegion, err := validateAwsRegion(region)
if err != nil {
t.Fatal(err)
}
if v != r { assert.NoError(t, err)
t.Fatal("Wrong region returned") assert.Equal(t, region, validatedRegion)
}
} }
} }
func TestValidateAwsRegionInvalid(t *testing.T) { func TestValidateAwsRegionInvalid(t *testing.T) {
regions := []string{"eu-west-2", "eu-central-2"} regions := []string{"eu-west-2", "eu-central-2"}
for _, v := range regions { for _, region := range regions {
r, err := validateAwsRegion(v) _, err := validateAwsRegion(region)
if err == nil {
t.Fatal("No error returned")
}
if v == r { assert.EqualError(t, err, "Invalid region specified")
t.Fatal("Wrong region returned")
} }
} }
}
func TestSetConfigFromFlags(t *testing.T) {
driver, err := getTestDriver()
if err != nil {
t.Fatal(err)
}
checkFlags := &drivers.CheckDriverOptions{
FlagsValues: map[string]interface{}{
"amazonec2-region": "us-west-2",
},
CreateFlags: driver.GetCreateFlags(),
}
driver.SetConfigFromFlags(checkFlags)
assert.NoError(t, err)
assert.Empty(t, checkFlags.InvalidFlags)
}
func TestFindDefaultVPC(t *testing.T) { func TestFindDefaultVPC(t *testing.T) {
driver := NewDriver("machineFoo", "path") driver := NewDriver("machineFoo", "path")
driver.clientFactory = func() Ec2Client { driver.clientFactory = func() Ec2Client { return &fakeEC2WithLogin{} }
return &fakeEC2WithLogin{}
}
vpc, err := driver.getDefaultVPCId() vpc, err := driver.getDefaultVPCId()
@ -283,8 +149,7 @@ func TestDescribeAccountAttributeFails(t *testing.T) {
} }
func TestAccessKeyIsMandatory(t *testing.T) { func TestAccessKeyIsMandatory(t *testing.T) {
driver := NewDriver("machineFoo", "path") driver := NewTestDriver()
driver.clientFactory = func() Ec2Client { return &fakeEC2{} }
driver.awsCredentials = &cliCredentials{} driver.awsCredentials = &cliCredentials{}
options := &commandstest.FakeFlagger{ options := &commandstest.FakeFlagger{
Data: map[string]interface{}{ Data: map[string]interface{}{
@ -300,8 +165,7 @@ func TestAccessKeyIsMandatory(t *testing.T) {
} }
func TestAccessKeyIsMandatoryEvenIfSecretKeyIsPassed(t *testing.T) { func TestAccessKeyIsMandatoryEvenIfSecretKeyIsPassed(t *testing.T) {
driver := NewDriver("machineFoo", "path") driver := NewTestDriver()
driver.clientFactory = func() Ec2Client { return &fakeEC2{} }
driver.awsCredentials = &cliCredentials{} driver.awsCredentials = &cliCredentials{}
options := &commandstest.FakeFlagger{ options := &commandstest.FakeFlagger{
Data: map[string]interface{}{ Data: map[string]interface{}{
@ -318,8 +182,7 @@ func TestAccessKeyIsMandatoryEvenIfSecretKeyIsPassed(t *testing.T) {
} }
func TestSecretKeyIsMandatory(t *testing.T) { func TestSecretKeyIsMandatory(t *testing.T) {
driver := NewDriver("machineFoo", "path") driver := NewTestDriver()
driver.clientFactory = func() Ec2Client { return &fakeEC2{} }
driver.awsCredentials = &cliCredentials{} driver.awsCredentials = &cliCredentials{}
options := &commandstest.FakeFlagger{ options := &commandstest.FakeFlagger{
Data: map[string]interface{}{ Data: map[string]interface{}{
@ -336,8 +199,7 @@ func TestSecretKeyIsMandatory(t *testing.T) {
} }
func TestLoadingFromCredentialsWorked(t *testing.T) { func TestLoadingFromCredentialsWorked(t *testing.T) {
driver := NewDriver("machineFoo", "path") driver := NewCustomTestDriver(&fakeEC2WithLogin{})
driver.clientFactory = func() Ec2Client { return &fakeEC2WithLogin{} }
driver.awsCredentials = &fileCredentials{} driver.awsCredentials = &fileCredentials{}
options := &commandstest.FakeFlagger{ options := &commandstest.FakeFlagger{
Data: map[string]interface{}{ Data: map[string]interface{}{
@ -356,8 +218,7 @@ func TestLoadingFromCredentialsWorked(t *testing.T) {
} }
func TestPassingBothCLIArgWorked(t *testing.T) { func TestPassingBothCLIArgWorked(t *testing.T) {
driver := NewDriver("machineFoo", "path") driver := NewCustomTestDriver(&fakeEC2WithLogin{})
driver.clientFactory = func() Ec2Client { return &fakeEC2WithLogin{} }
driver.awsCredentials = &cliCredentials{} driver.awsCredentials = &cliCredentials{}
options := &commandstest.FakeFlagger{ options := &commandstest.FakeFlagger{
Data: map[string]interface{}{ Data: map[string]interface{}{

View File

@ -41,5 +41,5 @@ func validateAwsRegion(region string) (string, error) {
} }
} }
return "", errors.New("invalid region specified") return "", errors.New("Invalid region specified")
} }

View File

@ -88,3 +88,15 @@ func (f *fakeEC2WithLogin) DescribeAccountAttributes(input *ec2.DescribeAccountA
}, },
}, nil }, nil
} }
func NewTestDriver() *Driver {
driver := NewDriver("machineFoo", "path")
driver.clientFactory = func() Ec2Client { return &fakeEC2{} }
return driver
}
func NewCustomTestDriver(ec2Client Ec2Client) *Driver {
driver := NewDriver("machineFoo", "path")
driver.clientFactory = func() Ec2Client { return ec2Client }
return driver
}