2014-09-09 21:25:35 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2014-09-09 21:25:35 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-10-10 08:18:12 +00:00
|
|
|
package aws
|
2014-09-09 21:25:35 +00:00
|
|
|
|
|
|
|
import (
|
2015-03-27 14:31:16 +00:00
|
|
|
"io"
|
2014-09-09 21:25:35 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2015-06-04 05:54:59 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
|
|
|
"github.com/aws/aws-sdk-go/service/elb"
|
2015-03-05 18:10:56 +00:00
|
|
|
|
2015-06-13 14:24:35 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/autoscaling"
|
2015-06-05 02:03:50 +00:00
|
|
|
"github.com/golang/glog"
|
2015-08-05 22:05:17 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2016-02-29 19:16:42 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2016-04-20 21:41:18 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/sets"
|
2016-02-29 19:16:42 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/mock"
|
2014-09-09 21:25:35 +00:00
|
|
|
)
|
|
|
|
|
2015-05-23 00:12:53 +00:00
|
|
|
const TestClusterId = "clusterid.test"
|
|
|
|
|
2014-09-09 21:25:35 +00:00
|
|
|
func TestReadAWSCloudConfig(t *testing.T) {
|
2015-03-27 14:31:16 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
|
2015-05-23 00:12:53 +00:00
|
|
|
reader io.Reader
|
2016-04-17 20:31:02 +00:00
|
|
|
aws Services
|
2015-03-27 14:31:16 +00:00
|
|
|
|
|
|
|
expectError bool
|
|
|
|
zone string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"No config reader",
|
|
|
|
nil, nil,
|
|
|
|
true, "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Empty config, no metadata",
|
|
|
|
strings.NewReader(""), nil,
|
|
|
|
true, "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"No zone in config, no metadata",
|
|
|
|
strings.NewReader("[global]\n"), nil,
|
|
|
|
true, "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Zone in config, no metadata",
|
|
|
|
strings.NewReader("[global]\nzone = eu-west-1a"), nil,
|
|
|
|
false, "eu-west-1a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"No zone in config, metadata does not have zone",
|
2015-05-23 00:12:53 +00:00
|
|
|
strings.NewReader("[global]\n"), NewFakeAWSServices().withAz(""),
|
2015-03-27 14:31:16 +00:00
|
|
|
true, "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"No zone in config, metadata has zone",
|
2015-05-23 00:12:53 +00:00
|
|
|
strings.NewReader("[global]\n"), NewFakeAWSServices(),
|
|
|
|
false, "us-east-1a",
|
2015-03-27 14:31:16 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"Zone in config should take precedence over metadata",
|
2015-05-23 00:12:53 +00:00
|
|
|
strings.NewReader("[global]\nzone = eu-west-1a"), NewFakeAWSServices(),
|
|
|
|
false, "eu-west-1a",
|
2015-03-27 14:31:16 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Logf("Running test case %s", test.name)
|
2015-09-20 20:09:08 +00:00
|
|
|
var metadata EC2Metadata
|
2015-05-23 00:12:53 +00:00
|
|
|
if test.aws != nil {
|
2015-09-20 20:09:08 +00:00
|
|
|
metadata, _ = test.aws.Metadata()
|
2015-05-23 00:12:53 +00:00
|
|
|
}
|
|
|
|
cfg, err := readAWSCloudConfig(test.reader, metadata)
|
2015-03-27 14:31:16 +00:00
|
|
|
if test.expectError {
|
|
|
|
if err == nil {
|
2015-05-23 00:12:53 +00:00
|
|
|
t.Errorf("Should error for case %s (cfg=%v)", test.name, cfg)
|
2015-03-27 14:31:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Should succeed for case: %s", test.name)
|
|
|
|
}
|
|
|
|
if cfg.Global.Zone != test.zone {
|
|
|
|
t.Errorf("Incorrect zone value (%s vs %s) for case: %s",
|
|
|
|
cfg.Global.Zone, test.zone, test.name)
|
|
|
|
}
|
|
|
|
}
|
2014-09-09 21:25:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-23 00:12:53 +00:00
|
|
|
type FakeAWSServices struct {
|
2016-02-29 15:22:26 +00:00
|
|
|
region string
|
2015-09-02 15:28:55 +00:00
|
|
|
instances []*ec2.Instance
|
2016-02-29 15:22:26 +00:00
|
|
|
selfInstance *ec2.Instance
|
2015-09-02 15:28:55 +00:00
|
|
|
networkInterfacesMacs []string
|
|
|
|
networkInterfacesVpcIDs []string
|
2015-05-23 00:12:53 +00:00
|
|
|
|
|
|
|
ec2 *FakeEC2
|
|
|
|
elb *FakeELB
|
2015-06-13 14:24:35 +00:00
|
|
|
asg *FakeASG
|
2015-05-23 00:12:53 +00:00
|
|
|
metadata *FakeMetadata
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFakeAWSServices() *FakeAWSServices {
|
|
|
|
s := &FakeAWSServices{}
|
2016-02-29 15:22:26 +00:00
|
|
|
s.region = "us-east-1"
|
2015-05-23 00:12:53 +00:00
|
|
|
s.ec2 = &FakeEC2{aws: s}
|
|
|
|
s.elb = &FakeELB{aws: s}
|
2015-06-13 14:24:35 +00:00
|
|
|
s.asg = &FakeASG{aws: s}
|
2015-05-23 00:12:53 +00:00
|
|
|
s.metadata = &FakeMetadata{aws: s}
|
|
|
|
|
2015-09-02 15:28:55 +00:00
|
|
|
s.networkInterfacesMacs = []string{"aa:bb:cc:dd:ee:00", "aa:bb:cc:dd:ee:01"}
|
|
|
|
s.networkInterfacesVpcIDs = []string{"vpc-mac0", "vpc-mac1"}
|
|
|
|
|
2016-02-29 15:22:26 +00:00
|
|
|
selfInstance := &ec2.Instance{}
|
|
|
|
selfInstance.InstanceId = aws.String("i-self")
|
|
|
|
selfInstance.Placement = &ec2.Placement{
|
|
|
|
AvailabilityZone: aws.String("us-east-1a"),
|
|
|
|
}
|
|
|
|
selfInstance.PrivateDnsName = aws.String("ip-172-20-0-100.ec2.internal")
|
|
|
|
selfInstance.PrivateIpAddress = aws.String("192.168.0.1")
|
|
|
|
selfInstance.PublicIpAddress = aws.String("1.2.3.4")
|
|
|
|
s.selfInstance = selfInstance
|
|
|
|
s.instances = []*ec2.Instance{selfInstance}
|
2015-05-23 00:12:53 +00:00
|
|
|
|
|
|
|
var tag ec2.Tag
|
|
|
|
tag.Key = aws.String(TagNameKubernetesCluster)
|
|
|
|
tag.Value = aws.String(TestClusterId)
|
|
|
|
selfInstance.Tags = []*ec2.Tag{&tag}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FakeAWSServices) withAz(az string) *FakeAWSServices {
|
2016-02-29 15:22:26 +00:00
|
|
|
if s.selfInstance.Placement == nil {
|
|
|
|
s.selfInstance.Placement = &ec2.Placement{}
|
|
|
|
}
|
|
|
|
s.selfInstance.Placement.AvailabilityZone = aws.String(az)
|
2015-05-23 00:12:53 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FakeAWSServices) Compute(region string) (EC2, error) {
|
|
|
|
return s.ec2, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FakeAWSServices) LoadBalancing(region string) (ELB, error) {
|
|
|
|
return s.elb, nil
|
|
|
|
}
|
|
|
|
|
2015-06-13 14:24:35 +00:00
|
|
|
func (s *FakeAWSServices) Autoscaling(region string) (ASG, error) {
|
|
|
|
return s.asg, nil
|
|
|
|
}
|
|
|
|
|
2015-09-20 20:09:08 +00:00
|
|
|
func (s *FakeAWSServices) Metadata() (EC2Metadata, error) {
|
|
|
|
return s.metadata, nil
|
2015-05-23 00:12:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilterTags(t *testing.T) {
|
|
|
|
awsServices := NewFakeAWSServices()
|
|
|
|
c, err := newAWSCloud(strings.NewReader("[global]"), awsServices)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error building aws cloud: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.filterTags) != 1 {
|
|
|
|
t.Errorf("unexpected filter tags: %v", c.filterTags)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.filterTags[TagNameKubernetesCluster] != TestClusterId {
|
|
|
|
t.Errorf("unexpected filter tags: %v", c.filterTags)
|
2014-09-09 21:25:35 +00:00
|
|
|
}
|
2015-05-23 00:12:53 +00:00
|
|
|
}
|
2014-09-09 21:25:35 +00:00
|
|
|
|
2015-05-23 00:12:53 +00:00
|
|
|
func TestNewAWSCloud(t *testing.T) {
|
2015-03-27 14:31:16 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
|
2015-05-23 00:12:53 +00:00
|
|
|
reader io.Reader
|
2016-04-17 20:31:02 +00:00
|
|
|
awsServices Services
|
2015-03-27 14:31:16 +00:00
|
|
|
|
|
|
|
expectError bool
|
2016-02-29 15:22:26 +00:00
|
|
|
region string
|
2015-03-27 14:31:16 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"No config reader",
|
2015-05-23 00:12:53 +00:00
|
|
|
nil, NewFakeAWSServices().withAz(""),
|
2015-03-27 14:31:16 +00:00
|
|
|
true, "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Config specified invalid zone",
|
2015-05-23 00:12:53 +00:00
|
|
|
strings.NewReader("[global]\nzone = blahonga"), NewFakeAWSServices(),
|
2015-03-27 14:31:16 +00:00
|
|
|
true, "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Config specifies valid zone",
|
2015-05-23 00:12:53 +00:00
|
|
|
strings.NewReader("[global]\nzone = eu-west-1a"), NewFakeAWSServices(),
|
2016-02-29 15:22:26 +00:00
|
|
|
false, "eu-west-1",
|
2015-03-27 14:31:16 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"Gets zone from metadata when not in config",
|
|
|
|
strings.NewReader("[global]\n"),
|
2015-05-23 00:12:53 +00:00
|
|
|
NewFakeAWSServices(),
|
2016-02-29 15:22:26 +00:00
|
|
|
false, "us-east-1",
|
2015-03-27 14:31:16 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"No zone in config or metadata",
|
2015-05-23 00:12:53 +00:00
|
|
|
strings.NewReader("[global]\n"),
|
|
|
|
NewFakeAWSServices().withAz(""),
|
2015-03-27 14:31:16 +00:00
|
|
|
true, "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Logf("Running test case %s", test.name)
|
2015-05-23 00:12:53 +00:00
|
|
|
c, err := newAWSCloud(test.reader, test.awsServices)
|
2015-03-27 14:31:16 +00:00
|
|
|
if test.expectError {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Should error for case %s", test.name)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
2015-05-23 00:12:53 +00:00
|
|
|
t.Errorf("Should succeed for case: %s, got %v", test.name, err)
|
2016-02-29 15:22:26 +00:00
|
|
|
} else if c.region != test.region {
|
|
|
|
t.Errorf("Incorrect region value (%s vs %s) for case: %s",
|
|
|
|
c.region, test.region, test.name)
|
2015-03-27 14:31:16 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-09 21:25:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type FakeEC2 struct {
|
2015-11-29 11:14:19 +00:00
|
|
|
aws *FakeAWSServices
|
|
|
|
Subnets []*ec2.Subnet
|
|
|
|
DescribeSubnetsInput *ec2.DescribeSubnetsInput
|
|
|
|
RouteTables []*ec2.RouteTable
|
|
|
|
DescribeRouteTablesInput *ec2.DescribeRouteTablesInput
|
2016-02-29 19:16:42 +00:00
|
|
|
mock.Mock
|
2014-09-09 21:25:35 +00:00
|
|
|
}
|
|
|
|
|
2015-06-05 02:03:50 +00:00
|
|
|
func contains(haystack []*string, needle string) bool {
|
2015-04-21 13:50:36 +00:00
|
|
|
for _, s := range haystack {
|
2015-06-05 02:03:50 +00:00
|
|
|
// (deliberately panic if s == nil)
|
|
|
|
if needle == *s {
|
2015-04-21 13:50:36 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-06-05 02:03:50 +00:00
|
|
|
func instanceMatchesFilter(instance *ec2.Instance, filter *ec2.Filter) bool {
|
|
|
|
name := *filter.Name
|
|
|
|
if name == "private-dns-name" {
|
2015-09-20 21:10:57 +00:00
|
|
|
if instance.PrivateDnsName == nil {
|
2015-06-05 02:03:50 +00:00
|
|
|
return false
|
|
|
|
}
|
2015-09-20 21:10:57 +00:00
|
|
|
return contains(filter.Values, *instance.PrivateDnsName)
|
2015-06-05 02:03:50 +00:00
|
|
|
}
|
2016-02-23 14:44:03 +00:00
|
|
|
|
|
|
|
if name == "instance-state-name" {
|
|
|
|
return contains(filter.Values, *instance.State.Name)
|
|
|
|
}
|
|
|
|
|
Change LoadBalancer methods to take api.Service
This is a better abstraction than passing in specific pieces of the
Service that each of the cloudproviders may or may not need. For
instance, many of the providers don't need a region, yet this is passed
in. Similarly many of the providers want a string IP for the load
balancer, but it passes in a converted net ip. Affinity is unused by
AWS. A provider change may also require adding a new parameter which has
an effect on all other cloud provider implementations.
Further, this will simplify adding provider specific load balancer
options, such as with labels or some other metadata. For example, we
could add labels for configuring the details of an AWS elastic load
balancer, such as idle timeout on connections, whether it is
internal or external, cross-zone load balancing, and so on.
Authors: @chbatey, @jsravn
2016-02-17 11:36:50 +00:00
|
|
|
if strings.HasPrefix(name, "tag:") {
|
|
|
|
tagName := name[4:]
|
|
|
|
for _, instanceTag := range instance.Tags {
|
|
|
|
if aws.StringValue(instanceTag.Key) == tagName && contains(filter.Values, aws.StringValue(instanceTag.Value)) {
|
|
|
|
return true
|
2016-02-23 14:44:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-05 02:03:50 +00:00
|
|
|
panic("Unknown filter name: " + name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *FakeEC2) DescribeInstances(request *ec2.DescribeInstancesInput) ([]*ec2.Instance, error) {
|
2015-05-14 19:18:25 +00:00
|
|
|
matches := []*ec2.Instance{}
|
2015-05-23 00:12:53 +00:00
|
|
|
for _, instance := range self.aws.instances {
|
2015-09-20 21:10:57 +00:00
|
|
|
if request.InstanceIds != nil {
|
|
|
|
if instance.InstanceId == nil {
|
2015-06-05 02:03:50 +00:00
|
|
|
glog.Warning("Instance with no instance id: ", instance)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
found := false
|
2016-02-29 15:22:26 +00:00
|
|
|
for _, instanceID := range request.InstanceIds {
|
|
|
|
if *instanceID == *instance.InstanceId {
|
2015-06-05 02:03:50 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
continue
|
|
|
|
}
|
2015-04-21 13:50:36 +00:00
|
|
|
}
|
2015-06-05 02:03:50 +00:00
|
|
|
if request.Filters != nil {
|
|
|
|
allMatch := true
|
|
|
|
for _, filter := range request.Filters {
|
|
|
|
if !instanceMatchesFilter(instance, filter) {
|
|
|
|
allMatch = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !allMatch {
|
|
|
|
continue
|
|
|
|
}
|
2015-03-10 04:15:53 +00:00
|
|
|
}
|
2015-04-21 13:50:36 +00:00
|
|
|
matches = append(matches, instance)
|
2015-03-10 04:15:53 +00:00
|
|
|
}
|
2015-05-14 19:18:25 +00:00
|
|
|
|
|
|
|
return matches, nil
|
2015-03-10 04:15:53 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 19:47:49 +00:00
|
|
|
type FakeMetadata struct {
|
2015-05-23 00:12:53 +00:00
|
|
|
aws *FakeAWSServices
|
2015-03-26 19:47:49 +00:00
|
|
|
}
|
|
|
|
|
2015-09-20 20:09:08 +00:00
|
|
|
func (self *FakeMetadata) GetMetadata(key string) (string, error) {
|
2015-09-02 15:28:55 +00:00
|
|
|
networkInterfacesPrefix := "network/interfaces/macs/"
|
2016-02-29 15:22:26 +00:00
|
|
|
i := self.aws.selfInstance
|
2015-03-10 04:15:53 +00:00
|
|
|
if key == "placement/availability-zone" {
|
2016-02-29 15:22:26 +00:00
|
|
|
az := ""
|
|
|
|
if i.Placement != nil {
|
|
|
|
az = aws.StringValue(i.Placement.AvailabilityZone)
|
|
|
|
}
|
|
|
|
return az, nil
|
2015-03-26 19:47:49 +00:00
|
|
|
} else if key == "instance-id" {
|
2016-02-29 15:22:26 +00:00
|
|
|
return aws.StringValue(i.InstanceId), nil
|
2015-07-02 17:37:00 +00:00
|
|
|
} else if key == "local-hostname" {
|
2016-02-29 15:22:26 +00:00
|
|
|
return aws.StringValue(i.PrivateDnsName), nil
|
2015-10-30 23:37:03 +00:00
|
|
|
} else if key == "local-ipv4" {
|
2016-02-29 15:22:26 +00:00
|
|
|
return aws.StringValue(i.PrivateIpAddress), nil
|
2015-10-30 23:37:03 +00:00
|
|
|
} else if key == "public-ipv4" {
|
2016-02-29 15:22:26 +00:00
|
|
|
return aws.StringValue(i.PublicIpAddress), nil
|
2015-09-02 15:28:55 +00:00
|
|
|
} else if strings.HasPrefix(key, networkInterfacesPrefix) {
|
|
|
|
if key == networkInterfacesPrefix {
|
2015-09-20 20:09:08 +00:00
|
|
|
return strings.Join(self.aws.networkInterfacesMacs, "/\n") + "/\n", nil
|
2015-09-02 15:28:55 +00:00
|
|
|
} else {
|
|
|
|
keySplit := strings.Split(key, "/")
|
|
|
|
macParam := keySplit[3]
|
|
|
|
if len(keySplit) == 5 && keySplit[4] == "vpc-id" {
|
|
|
|
for i, macElem := range self.aws.networkInterfacesMacs {
|
|
|
|
if macParam == macElem {
|
2015-09-20 20:09:08 +00:00
|
|
|
return self.aws.networkInterfacesVpcIDs[i], nil
|
2015-09-02 15:28:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-20 20:09:08 +00:00
|
|
|
return "", nil
|
2015-09-02 15:28:55 +00:00
|
|
|
}
|
2015-03-10 04:15:53 +00:00
|
|
|
} else {
|
2015-09-20 20:09:08 +00:00
|
|
|
return "", nil
|
2015-03-10 04:15:53 +00:00
|
|
|
}
|
2014-09-09 21:25:35 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 16:01:33 +00:00
|
|
|
func (ec2 *FakeEC2) AttachVolume(request *ec2.AttachVolumeInput) (resp *ec2.VolumeAttachment, err error) {
|
2015-03-26 17:04:10 +00:00
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
2015-05-25 19:05:56 +00:00
|
|
|
func (ec2 *FakeEC2) DetachVolume(request *ec2.DetachVolumeInput) (resp *ec2.VolumeAttachment, err error) {
|
2015-03-26 17:04:10 +00:00
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
2016-02-29 19:16:42 +00:00
|
|
|
func (e *FakeEC2) DescribeVolumes(request *ec2.DescribeVolumesInput) ([]*ec2.Volume, error) {
|
|
|
|
args := e.Called(request)
|
|
|
|
return args.Get(0).([]*ec2.Volume), nil
|
2015-03-26 17:04:10 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 23:53:47 +00:00
|
|
|
func (ec2 *FakeEC2) CreateVolume(request *ec2.CreateVolumeInput) (resp *ec2.Volume, err error) {
|
2015-03-26 17:04:10 +00:00
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
2015-10-07 16:01:33 +00:00
|
|
|
func (ec2 *FakeEC2) DeleteVolume(request *ec2.DeleteVolumeInput) (resp *ec2.DeleteVolumeOutput, err error) {
|
2015-03-26 17:04:10 +00:00
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
2015-06-05 02:03:50 +00:00
|
|
|
func (ec2 *FakeEC2) DescribeSecurityGroups(request *ec2.DescribeSecurityGroupsInput) ([]*ec2.SecurityGroup, error) {
|
2015-05-23 00:12:53 +00:00
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ec2 *FakeEC2) CreateSecurityGroup(*ec2.CreateSecurityGroupInput) (*ec2.CreateSecurityGroupOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
2015-06-05 02:03:50 +00:00
|
|
|
func (ec2 *FakeEC2) DeleteSecurityGroup(*ec2.DeleteSecurityGroupInput) (*ec2.DeleteSecurityGroupOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
2015-05-23 00:12:53 +00:00
|
|
|
func (ec2 *FakeEC2) AuthorizeSecurityGroupIngress(*ec2.AuthorizeSecurityGroupIngressInput) (*ec2.AuthorizeSecurityGroupIngressOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
2015-06-05 02:03:50 +00:00
|
|
|
func (ec2 *FakeEC2) RevokeSecurityGroupIngress(*ec2.RevokeSecurityGroupIngressInput) (*ec2.RevokeSecurityGroupIngressOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
2015-10-01 20:38:11 +00:00
|
|
|
func (ec2 *FakeEC2) DescribeSubnets(request *ec2.DescribeSubnetsInput) ([]*ec2.Subnet, error) {
|
|
|
|
ec2.DescribeSubnetsInput = request
|
|
|
|
return ec2.Subnets, nil
|
2015-05-23 00:12:53 +00:00
|
|
|
}
|
|
|
|
|
2015-06-05 02:03:50 +00:00
|
|
|
func (ec2 *FakeEC2) CreateTags(*ec2.CreateTagsInput) (*ec2.CreateTagsOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
2015-11-29 11:14:19 +00:00
|
|
|
func (ec2 *FakeEC2) DescribeRouteTables(request *ec2.DescribeRouteTablesInput) ([]*ec2.RouteTable, error) {
|
|
|
|
ec2.DescribeRouteTablesInput = request
|
|
|
|
return ec2.RouteTables, nil
|
2015-06-12 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FakeEC2) CreateRoute(request *ec2.CreateRouteInput) (*ec2.CreateRouteOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FakeEC2) DeleteRoute(request *ec2.DeleteRouteInput) (*ec2.DeleteRouteOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
2015-06-13 04:34:43 +00:00
|
|
|
func (s *FakeEC2) ModifyInstanceAttribute(request *ec2.ModifyInstanceAttributeInput) (*ec2.ModifyInstanceAttributeOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
2015-05-23 00:12:53 +00:00
|
|
|
type FakeELB struct {
|
|
|
|
aws *FakeAWSServices
|
Change LoadBalancer methods to take api.Service
This is a better abstraction than passing in specific pieces of the
Service that each of the cloudproviders may or may not need. For
instance, many of the providers don't need a region, yet this is passed
in. Similarly many of the providers want a string IP for the load
balancer, but it passes in a converted net ip. Affinity is unused by
AWS. A provider change may also require adding a new parameter which has
an effect on all other cloud provider implementations.
Further, this will simplify adding provider specific load balancer
options, such as with labels or some other metadata. For example, we
could add labels for configuring the details of an AWS elastic load
balancer, such as idle timeout on connections, whether it is
internal or external, cross-zone load balancing, and so on.
Authors: @chbatey, @jsravn
2016-02-17 11:36:50 +00:00
|
|
|
mock.Mock
|
2015-05-23 00:12:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ec2 *FakeELB) CreateLoadBalancer(*elb.CreateLoadBalancerInput) (*elb.CreateLoadBalancerOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
2015-06-13 18:45:38 +00:00
|
|
|
|
Change LoadBalancer methods to take api.Service
This is a better abstraction than passing in specific pieces of the
Service that each of the cloudproviders may or may not need. For
instance, many of the providers don't need a region, yet this is passed
in. Similarly many of the providers want a string IP for the load
balancer, but it passes in a converted net ip. Affinity is unused by
AWS. A provider change may also require adding a new parameter which has
an effect on all other cloud provider implementations.
Further, this will simplify adding provider specific load balancer
options, such as with labels or some other metadata. For example, we
could add labels for configuring the details of an AWS elastic load
balancer, such as idle timeout on connections, whether it is
internal or external, cross-zone load balancing, and so on.
Authors: @chbatey, @jsravn
2016-02-17 11:36:50 +00:00
|
|
|
func (ec2 *FakeELB) DeleteLoadBalancer(input *elb.DeleteLoadBalancerInput) (*elb.DeleteLoadBalancerOutput, error) {
|
2015-05-23 00:12:53 +00:00
|
|
|
panic("Not implemented")
|
|
|
|
}
|
2015-06-13 18:45:38 +00:00
|
|
|
|
Change LoadBalancer methods to take api.Service
This is a better abstraction than passing in specific pieces of the
Service that each of the cloudproviders may or may not need. For
instance, many of the providers don't need a region, yet this is passed
in. Similarly many of the providers want a string IP for the load
balancer, but it passes in a converted net ip. Affinity is unused by
AWS. A provider change may also require adding a new parameter which has
an effect on all other cloud provider implementations.
Further, this will simplify adding provider specific load balancer
options, such as with labels or some other metadata. For example, we
could add labels for configuring the details of an AWS elastic load
balancer, such as idle timeout on connections, whether it is
internal or external, cross-zone load balancing, and so on.
Authors: @chbatey, @jsravn
2016-02-17 11:36:50 +00:00
|
|
|
func (ec2 *FakeELB) DescribeLoadBalancers(input *elb.DescribeLoadBalancersInput) (*elb.DescribeLoadBalancersOutput, error) {
|
|
|
|
args := ec2.Called(input)
|
|
|
|
return args.Get(0).(*elb.DescribeLoadBalancersOutput), nil
|
2015-05-23 00:12:53 +00:00
|
|
|
}
|
|
|
|
func (ec2 *FakeELB) RegisterInstancesWithLoadBalancer(*elb.RegisterInstancesWithLoadBalancerInput) (*elb.RegisterInstancesWithLoadBalancerOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
2015-06-13 18:45:38 +00:00
|
|
|
|
2015-05-23 00:12:53 +00:00
|
|
|
func (ec2 *FakeELB) DeregisterInstancesFromLoadBalancer(*elb.DeregisterInstancesFromLoadBalancerInput) (*elb.DeregisterInstancesFromLoadBalancerOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
2015-06-13 18:45:38 +00:00
|
|
|
func (ec2 *FakeELB) DetachLoadBalancerFromSubnets(*elb.DetachLoadBalancerFromSubnetsInput) (*elb.DetachLoadBalancerFromSubnetsOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ec2 *FakeELB) AttachLoadBalancerToSubnets(*elb.AttachLoadBalancerToSubnetsInput) (*elb.AttachLoadBalancerToSubnetsOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ec2 *FakeELB) CreateLoadBalancerListeners(*elb.CreateLoadBalancerListenersInput) (*elb.CreateLoadBalancerListenersOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ec2 *FakeELB) DeleteLoadBalancerListeners(*elb.DeleteLoadBalancerListenersInput) (*elb.DeleteLoadBalancerListenersOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ec2 *FakeELB) ApplySecurityGroupsToLoadBalancer(*elb.ApplySecurityGroupsToLoadBalancerInput) (*elb.ApplySecurityGroupsToLoadBalancerOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
2015-07-31 04:24:46 +00:00
|
|
|
func (elb *FakeELB) ConfigureHealthCheck(*elb.ConfigureHealthCheckInput) (*elb.ConfigureHealthCheckOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
2016-04-20 21:41:18 +00:00
|
|
|
func (elb *FakeELB) CreateLoadBalancerPolicy(*elb.CreateLoadBalancerPolicyInput) (*elb.CreateLoadBalancerPolicyOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (elb *FakeELB) SetLoadBalancerPoliciesForBackendServer(*elb.SetLoadBalancerPoliciesForBackendServerInput) (*elb.SetLoadBalancerPoliciesForBackendServerOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
2015-06-13 14:24:35 +00:00
|
|
|
type FakeASG struct {
|
|
|
|
aws *FakeAWSServices
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *FakeASG) UpdateAutoScalingGroup(*autoscaling.UpdateAutoScalingGroupInput) (*autoscaling.UpdateAutoScalingGroupOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *FakeASG) DescribeAutoScalingGroups(*autoscaling.DescribeAutoScalingGroupsInput) (*autoscaling.DescribeAutoScalingGroupsOutput, error) {
|
|
|
|
panic("Not implemented")
|
|
|
|
}
|
|
|
|
|
2016-04-17 20:31:02 +00:00
|
|
|
func mockInstancesResp(selfInstance *ec2.Instance, instances []*ec2.Instance) (*Cloud, *FakeAWSServices) {
|
2016-02-29 15:22:26 +00:00
|
|
|
awsServices := NewFakeAWSServices()
|
|
|
|
awsServices.instances = instances
|
|
|
|
awsServices.selfInstance = selfInstance
|
|
|
|
awsCloud, err := newAWSCloud(nil, awsServices)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return awsCloud, awsServices
|
2015-03-10 04:15:53 +00:00
|
|
|
}
|
|
|
|
|
2016-04-17 20:31:02 +00:00
|
|
|
func mockAvailabilityZone(availabilityZone string) *Cloud {
|
2015-05-23 00:12:53 +00:00
|
|
|
awsServices := NewFakeAWSServices().withAz(availabilityZone)
|
2016-02-29 15:22:26 +00:00
|
|
|
awsCloud, err := newAWSCloud(nil, awsServices)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2015-03-10 04:15:53 +00:00
|
|
|
}
|
2016-02-29 15:22:26 +00:00
|
|
|
return awsCloud
|
2014-09-09 21:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestList(t *testing.T) {
|
2015-05-14 23:53:47 +00:00
|
|
|
// TODO this setup is not very clean and could probably be improved
|
|
|
|
var instance0 ec2.Instance
|
|
|
|
var instance1 ec2.Instance
|
|
|
|
var instance2 ec2.Instance
|
|
|
|
var instance3 ec2.Instance
|
|
|
|
|
|
|
|
//0
|
|
|
|
tag0 := ec2.Tag{
|
2015-05-14 19:18:25 +00:00
|
|
|
Key: aws.String("Name"),
|
|
|
|
Value: aws.String("foo"),
|
2015-05-14 23:53:47 +00:00
|
|
|
}
|
|
|
|
instance0.Tags = []*ec2.Tag{&tag0}
|
2015-09-20 21:10:57 +00:00
|
|
|
instance0.InstanceId = aws.String("instance0")
|
|
|
|
instance0.PrivateDnsName = aws.String("instance0.ec2.internal")
|
2016-02-29 15:22:26 +00:00
|
|
|
instance0.Placement = &ec2.Placement{AvailabilityZone: aws.String("us-east-1a")}
|
2015-05-14 23:53:47 +00:00
|
|
|
state0 := ec2.InstanceState{
|
|
|
|
Name: aws.String("running"),
|
|
|
|
}
|
|
|
|
instance0.State = &state0
|
|
|
|
|
|
|
|
//1
|
|
|
|
tag1 := ec2.Tag{
|
2015-05-14 19:18:25 +00:00
|
|
|
Key: aws.String("Name"),
|
|
|
|
Value: aws.String("bar"),
|
2015-05-14 23:53:47 +00:00
|
|
|
}
|
|
|
|
instance1.Tags = []*ec2.Tag{&tag1}
|
2015-09-20 21:10:57 +00:00
|
|
|
instance1.InstanceId = aws.String("instance1")
|
|
|
|
instance1.PrivateDnsName = aws.String("instance1.ec2.internal")
|
2016-02-29 15:22:26 +00:00
|
|
|
instance1.Placement = &ec2.Placement{AvailabilityZone: aws.String("us-east-1a")}
|
2015-05-14 23:53:47 +00:00
|
|
|
state1 := ec2.InstanceState{
|
|
|
|
Name: aws.String("running"),
|
|
|
|
}
|
|
|
|
instance1.State = &state1
|
|
|
|
|
|
|
|
//2
|
|
|
|
tag2 := ec2.Tag{
|
2015-05-14 19:18:25 +00:00
|
|
|
Key: aws.String("Name"),
|
|
|
|
Value: aws.String("baz"),
|
2015-05-14 23:53:47 +00:00
|
|
|
}
|
|
|
|
instance2.Tags = []*ec2.Tag{&tag2}
|
2015-09-20 21:10:57 +00:00
|
|
|
instance2.InstanceId = aws.String("instance2")
|
|
|
|
instance2.PrivateDnsName = aws.String("instance2.ec2.internal")
|
2016-02-29 15:22:26 +00:00
|
|
|
instance2.Placement = &ec2.Placement{AvailabilityZone: aws.String("us-east-1a")}
|
2015-05-14 23:53:47 +00:00
|
|
|
state2 := ec2.InstanceState{
|
|
|
|
Name: aws.String("running"),
|
|
|
|
}
|
|
|
|
instance2.State = &state2
|
|
|
|
|
|
|
|
//3
|
|
|
|
tag3 := ec2.Tag{
|
2015-05-14 19:18:25 +00:00
|
|
|
Key: aws.String("Name"),
|
|
|
|
Value: aws.String("quux"),
|
2015-05-14 23:53:47 +00:00
|
|
|
}
|
|
|
|
instance3.Tags = []*ec2.Tag{&tag3}
|
2015-09-20 21:10:57 +00:00
|
|
|
instance3.InstanceId = aws.String("instance3")
|
|
|
|
instance3.PrivateDnsName = aws.String("instance3.ec2.internal")
|
2016-02-29 15:22:26 +00:00
|
|
|
instance3.Placement = &ec2.Placement{AvailabilityZone: aws.String("us-east-1a")}
|
2015-05-14 23:53:47 +00:00
|
|
|
state3 := ec2.InstanceState{
|
|
|
|
Name: aws.String("running"),
|
|
|
|
}
|
|
|
|
instance3.State = &state3
|
2014-09-09 21:25:35 +00:00
|
|
|
|
2015-05-14 23:53:47 +00:00
|
|
|
instances := []*ec2.Instance{&instance0, &instance1, &instance2, &instance3}
|
2016-02-29 15:22:26 +00:00
|
|
|
aws, _ := mockInstancesResp(&instance0, instances)
|
2014-09-09 21:25:35 +00:00
|
|
|
|
|
|
|
table := []struct {
|
|
|
|
input string
|
|
|
|
expect []string
|
|
|
|
}{
|
|
|
|
{"blahonga", []string{}},
|
2015-07-02 17:37:00 +00:00
|
|
|
{"quux", []string{"instance3.ec2.internal"}},
|
|
|
|
{"a", []string{"instance1.ec2.internal", "instance2.ec2.internal"}},
|
2014-09-09 21:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range table {
|
|
|
|
result, err := aws.List(item.input)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected call with %v to succeed, failed with %s", item.input, err)
|
|
|
|
}
|
|
|
|
if e, a := item.expect, result; !reflect.DeepEqual(e, a) {
|
|
|
|
t.Errorf("Expected %v, got %v", e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-20 04:00:18 +00:00
|
|
|
func testHasNodeAddress(t *testing.T, addrs []api.NodeAddress, addressType api.NodeAddressType, address string) {
|
|
|
|
for _, addr := range addrs {
|
|
|
|
if addr.Type == addressType && addr.Address == address {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Errorf("Did not find expected address: %s:%s in %v", addressType, address, addrs)
|
|
|
|
}
|
|
|
|
|
2015-03-13 22:09:31 +00:00
|
|
|
func TestNodeAddresses(t *testing.T) {
|
2015-03-04 23:32:47 +00:00
|
|
|
// Note these instances have the same name
|
|
|
|
// (we test that this produces an error)
|
2015-05-14 23:53:47 +00:00
|
|
|
var instance0 ec2.Instance
|
|
|
|
var instance1 ec2.Instance
|
2016-02-03 21:43:47 +00:00
|
|
|
var instance2 ec2.Instance
|
2015-05-14 23:53:47 +00:00
|
|
|
|
|
|
|
//0
|
2016-02-29 15:22:26 +00:00
|
|
|
instance0.InstanceId = aws.String("i-0")
|
2015-09-20 21:10:57 +00:00
|
|
|
instance0.PrivateDnsName = aws.String("instance-same.ec2.internal")
|
|
|
|
instance0.PrivateIpAddress = aws.String("192.168.0.1")
|
|
|
|
instance0.PublicIpAddress = aws.String("1.2.3.4")
|
2015-05-14 23:53:47 +00:00
|
|
|
instance0.InstanceType = aws.String("c3.large")
|
2016-02-29 15:22:26 +00:00
|
|
|
instance0.Placement = &ec2.Placement{AvailabilityZone: aws.String("us-east-1a")}
|
2015-05-14 23:53:47 +00:00
|
|
|
state0 := ec2.InstanceState{
|
|
|
|
Name: aws.String("running"),
|
|
|
|
}
|
|
|
|
instance0.State = &state0
|
|
|
|
|
|
|
|
//1
|
2016-02-29 15:22:26 +00:00
|
|
|
instance1.InstanceId = aws.String("i-1")
|
2015-09-20 21:10:57 +00:00
|
|
|
instance1.PrivateDnsName = aws.String("instance-same.ec2.internal")
|
|
|
|
instance1.PrivateIpAddress = aws.String("192.168.0.2")
|
2015-05-14 23:53:47 +00:00
|
|
|
instance1.InstanceType = aws.String("c3.large")
|
2016-02-29 15:22:26 +00:00
|
|
|
instance1.Placement = &ec2.Placement{AvailabilityZone: aws.String("us-east-1a")}
|
2015-05-14 23:53:47 +00:00
|
|
|
state1 := ec2.InstanceState{
|
|
|
|
Name: aws.String("running"),
|
|
|
|
}
|
|
|
|
instance1.State = &state1
|
|
|
|
|
2016-02-03 21:43:47 +00:00
|
|
|
//2
|
2016-02-29 15:22:26 +00:00
|
|
|
instance2.InstanceId = aws.String("i-2")
|
2016-02-03 21:43:47 +00:00
|
|
|
instance2.PrivateDnsName = aws.String("instance-other.ec2.internal")
|
|
|
|
instance2.PrivateIpAddress = aws.String("192.168.0.1")
|
|
|
|
instance2.PublicIpAddress = aws.String("1.2.3.4")
|
|
|
|
instance2.InstanceType = aws.String("c3.large")
|
2016-02-29 15:22:26 +00:00
|
|
|
instance2.Placement = &ec2.Placement{AvailabilityZone: aws.String("us-east-1a")}
|
2016-02-03 21:43:47 +00:00
|
|
|
state2 := ec2.InstanceState{
|
|
|
|
Name: aws.String("running"),
|
|
|
|
}
|
|
|
|
instance2.State = &state2
|
|
|
|
|
|
|
|
instances := []*ec2.Instance{&instance0, &instance1, &instance2}
|
2015-05-14 19:18:25 +00:00
|
|
|
|
2016-02-29 15:22:26 +00:00
|
|
|
aws1, _ := mockInstancesResp(&instance0, []*ec2.Instance{&instance0})
|
2015-07-02 17:37:00 +00:00
|
|
|
_, err1 := aws1.NodeAddresses("instance-mismatch.ec2.internal")
|
2014-09-09 21:25:35 +00:00
|
|
|
if err1 == nil {
|
|
|
|
t.Errorf("Should error when no instance found")
|
|
|
|
}
|
|
|
|
|
2016-02-29 15:22:26 +00:00
|
|
|
aws2, _ := mockInstancesResp(&instance2, instances)
|
2015-07-02 17:37:00 +00:00
|
|
|
_, err2 := aws2.NodeAddresses("instance-same.ec2.internal")
|
2014-09-09 21:25:35 +00:00
|
|
|
if err2 == nil {
|
|
|
|
t.Errorf("Should error when multiple instances found")
|
|
|
|
}
|
|
|
|
|
2016-02-29 15:22:26 +00:00
|
|
|
aws3, _ := mockInstancesResp(&instance0, instances[0:1])
|
2015-07-02 17:37:00 +00:00
|
|
|
addrs3, err3 := aws3.NodeAddresses("instance-same.ec2.internal")
|
2014-09-09 21:25:35 +00:00
|
|
|
if err3 != nil {
|
|
|
|
t.Errorf("Should not error when instance found")
|
|
|
|
}
|
2015-04-20 04:00:18 +00:00
|
|
|
if len(addrs3) != 3 {
|
|
|
|
t.Errorf("Should return exactly 3 NodeAddresses")
|
2014-09-09 21:25:35 +00:00
|
|
|
}
|
2015-04-20 04:00:18 +00:00
|
|
|
testHasNodeAddress(t, addrs3, api.NodeInternalIP, "192.168.0.1")
|
|
|
|
testHasNodeAddress(t, addrs3, api.NodeLegacyHostIP, "192.168.0.1")
|
|
|
|
testHasNodeAddress(t, addrs3, api.NodeExternalIP, "1.2.3.4")
|
2015-10-30 23:37:03 +00:00
|
|
|
|
2016-02-29 15:22:26 +00:00
|
|
|
// Fetch from metadata
|
|
|
|
aws4, fakeServices := mockInstancesResp(&instance0, []*ec2.Instance{&instance0})
|
|
|
|
fakeServices.selfInstance.PublicIpAddress = aws.String("2.3.4.5")
|
|
|
|
fakeServices.selfInstance.PrivateIpAddress = aws.String("192.168.0.2")
|
2015-10-30 23:37:03 +00:00
|
|
|
|
2016-02-29 15:22:26 +00:00
|
|
|
addrs4, err4 := aws4.NodeAddresses(*instance0.PrivateDnsName)
|
2015-10-30 23:37:03 +00:00
|
|
|
if err4 != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err4)
|
|
|
|
}
|
|
|
|
testHasNodeAddress(t, addrs4, api.NodeInternalIP, "192.168.0.2")
|
|
|
|
testHasNodeAddress(t, addrs4, api.NodeExternalIP, "2.3.4.5")
|
2014-09-09 21:25:35 +00:00
|
|
|
}
|
2015-03-10 04:15:53 +00:00
|
|
|
|
|
|
|
func TestGetRegion(t *testing.T) {
|
2016-02-29 15:22:26 +00:00
|
|
|
aws := mockAvailabilityZone("us-west-2e")
|
2015-03-10 04:15:53 +00:00
|
|
|
zones, ok := aws.Zones()
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Unexpected missing zones impl")
|
|
|
|
}
|
|
|
|
zone, err := zones.GetZone()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error %v", err)
|
|
|
|
}
|
|
|
|
if zone.Region != "us-west-2" {
|
|
|
|
t.Errorf("Unexpected region: %s", zone.Region)
|
|
|
|
}
|
|
|
|
if zone.FailureDomain != "us-west-2e" {
|
|
|
|
t.Errorf("Unexpected FailureDomain: %s", zone.FailureDomain)
|
|
|
|
}
|
|
|
|
}
|
2015-09-02 15:28:55 +00:00
|
|
|
|
|
|
|
func TestFindVPCID(t *testing.T) {
|
|
|
|
awsServices := NewFakeAWSServices()
|
|
|
|
c, err := newAWSCloud(strings.NewReader("[global]"), awsServices)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error building aws cloud: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vpcID, err := c.findVPCID()
|
|
|
|
if err != nil {
|
2016-03-23 00:26:50 +00:00
|
|
|
t.Errorf("Unexpected error: %v", err)
|
2015-09-02 15:28:55 +00:00
|
|
|
}
|
|
|
|
if vpcID != "vpc-mac0" {
|
|
|
|
t.Errorf("Unexpected vpcID: %s", vpcID)
|
|
|
|
}
|
|
|
|
}
|
2015-09-21 18:44:26 +00:00
|
|
|
|
2015-10-01 20:38:11 +00:00
|
|
|
func constructSubnets(subnetsIn map[int]map[string]string) (subnetsOut []*ec2.Subnet) {
|
|
|
|
for i := range subnetsIn {
|
|
|
|
subnetsOut = append(
|
|
|
|
subnetsOut,
|
|
|
|
constructSubnet(
|
|
|
|
subnetsIn[i]["id"],
|
|
|
|
subnetsIn[i]["az"],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func constructSubnet(id string, az string) *ec2.Subnet {
|
|
|
|
return &ec2.Subnet{
|
|
|
|
SubnetId: &id,
|
|
|
|
AvailabilityZone: &az,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-29 11:14:19 +00:00
|
|
|
func constructRouteTables(routeTablesIn map[string]bool) (routeTablesOut []*ec2.RouteTable) {
|
2016-02-25 22:25:35 +00:00
|
|
|
routeTablesOut = append(routeTablesOut,
|
|
|
|
&ec2.RouteTable{
|
|
|
|
Associations: []*ec2.RouteTableAssociation{{Main: aws.Bool(true)}},
|
|
|
|
Routes: []*ec2.Route{{
|
|
|
|
DestinationCidrBlock: aws.String("0.0.0.0/0"),
|
|
|
|
GatewayId: aws.String("igw-main"),
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
|
2015-11-29 11:14:19 +00:00
|
|
|
for subnetID := range routeTablesIn {
|
|
|
|
routeTablesOut = append(
|
|
|
|
routeTablesOut,
|
|
|
|
constructRouteTable(
|
|
|
|
subnetID,
|
|
|
|
routeTablesIn[subnetID],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func constructRouteTable(subnetID string, public bool) *ec2.RouteTable {
|
|
|
|
var gatewayID string
|
|
|
|
if public {
|
|
|
|
gatewayID = "igw-" + subnetID[len(subnetID)-8:8]
|
|
|
|
} else {
|
|
|
|
gatewayID = "vgw-" + subnetID[len(subnetID)-8:8]
|
|
|
|
}
|
|
|
|
return &ec2.RouteTable{
|
|
|
|
Associations: []*ec2.RouteTableAssociation{{SubnetId: aws.String(subnetID)}},
|
|
|
|
Routes: []*ec2.Route{{
|
|
|
|
DestinationCidrBlock: aws.String("0.0.0.0/0"),
|
|
|
|
GatewayId: aws.String(gatewayID),
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 20:38:11 +00:00
|
|
|
func TestSubnetIDsinVPC(t *testing.T) {
|
|
|
|
awsServices := NewFakeAWSServices()
|
|
|
|
c, err := newAWSCloud(strings.NewReader("[global]"), awsServices)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error building aws cloud: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// test with 3 subnets from 3 different AZs
|
|
|
|
subnets := make(map[int]map[string]string)
|
|
|
|
subnets[0] = make(map[string]string)
|
|
|
|
subnets[0]["id"] = "subnet-a0000001"
|
|
|
|
subnets[0]["az"] = "af-south-1a"
|
|
|
|
subnets[1] = make(map[string]string)
|
|
|
|
subnets[1]["id"] = "subnet-b0000001"
|
|
|
|
subnets[1]["az"] = "af-south-1b"
|
|
|
|
subnets[2] = make(map[string]string)
|
|
|
|
subnets[2]["id"] = "subnet-c0000001"
|
|
|
|
subnets[2]["az"] = "af-south-1c"
|
|
|
|
awsServices.ec2.Subnets = constructSubnets(subnets)
|
|
|
|
|
2015-11-29 11:14:19 +00:00
|
|
|
routeTables := map[string]bool{
|
|
|
|
"subnet-a0000001": true,
|
|
|
|
"subnet-b0000001": true,
|
|
|
|
"subnet-c0000001": true,
|
|
|
|
}
|
|
|
|
awsServices.ec2.RouteTables = constructRouteTables(routeTables)
|
|
|
|
|
2016-03-04 17:15:12 +00:00
|
|
|
result, err := c.findELBSubnets(false)
|
2015-10-01 20:38:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error listing subnets: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result) != 3 {
|
|
|
|
t.Errorf("Expected 3 subnets but got %d", len(result))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
result_set := make(map[string]bool)
|
|
|
|
for _, v := range result {
|
|
|
|
result_set[v] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range subnets {
|
|
|
|
if !result_set[subnets[i]["id"]] {
|
|
|
|
t.Errorf("Expected subnet%d '%s' in result: %v", i, subnets[i]["id"], result)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 22:25:35 +00:00
|
|
|
// test implicit routing table - when subnets are not explicitly linked to a table they should use main
|
|
|
|
awsServices.ec2.RouteTables = constructRouteTables(map[string]bool{})
|
|
|
|
|
2016-03-04 17:15:12 +00:00
|
|
|
result, err = c.findELBSubnets(false)
|
2016-02-25 22:25:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error listing subnets: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result) != 3 {
|
|
|
|
t.Errorf("Expected 3 subnets but got %d", len(result))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
result_set = make(map[string]bool)
|
|
|
|
for _, v := range result {
|
|
|
|
result_set[v] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range subnets {
|
|
|
|
if !result_set[subnets[i]["id"]] {
|
|
|
|
t.Errorf("Expected subnet%d '%s' in result: %v", i, subnets[i]["id"], result)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 20:38:11 +00:00
|
|
|
// test with 4 subnets from 3 different AZs
|
|
|
|
// add duplicate az subnet
|
|
|
|
subnets[3] = make(map[string]string)
|
|
|
|
subnets[3]["id"] = "subnet-c0000002"
|
|
|
|
subnets[3]["az"] = "af-south-1c"
|
|
|
|
awsServices.ec2.Subnets = constructSubnets(subnets)
|
2015-11-29 11:14:19 +00:00
|
|
|
routeTables["subnet-c0000002"] = true
|
|
|
|
awsServices.ec2.RouteTables = constructRouteTables(routeTables)
|
2015-10-01 20:38:11 +00:00
|
|
|
|
2016-03-04 17:15:12 +00:00
|
|
|
result, err = c.findELBSubnets(false)
|
2015-10-01 20:38:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error listing subnets: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result) != 3 {
|
|
|
|
t.Errorf("Expected 3 subnets but got %d", len(result))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-29 11:14:19 +00:00
|
|
|
// test with 6 subnets from 3 different AZs
|
|
|
|
// with 3 private subnets
|
|
|
|
subnets[4] = make(map[string]string)
|
|
|
|
subnets[4]["id"] = "subnet-d0000001"
|
|
|
|
subnets[4]["az"] = "af-south-1a"
|
|
|
|
subnets[5] = make(map[string]string)
|
|
|
|
subnets[5]["id"] = "subnet-d0000002"
|
|
|
|
subnets[5]["az"] = "af-south-1b"
|
|
|
|
|
|
|
|
awsServices.ec2.Subnets = constructSubnets(subnets)
|
|
|
|
routeTables["subnet-a0000001"] = false
|
|
|
|
routeTables["subnet-b0000001"] = false
|
|
|
|
routeTables["subnet-c0000001"] = false
|
|
|
|
routeTables["subnet-c0000002"] = true
|
|
|
|
routeTables["subnet-d0000001"] = true
|
|
|
|
routeTables["subnet-d0000002"] = true
|
|
|
|
awsServices.ec2.RouteTables = constructRouteTables(routeTables)
|
2016-03-04 17:15:12 +00:00
|
|
|
result, err = c.findELBSubnets(false)
|
2015-11-29 11:14:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error listing subnets: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result) != 3 {
|
|
|
|
t.Errorf("Expected 3 subnets but got %d", len(result))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []*string{aws.String("subnet-c0000002"), aws.String("subnet-d0000001"), aws.String("subnet-d0000002")}
|
|
|
|
for _, s := range result {
|
|
|
|
if !contains(expected, s) {
|
|
|
|
t.Errorf("Unexpected subnet '%s' found", s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-10-01 20:38:11 +00:00
|
|
|
}
|
2016-01-26 11:01:15 +00:00
|
|
|
|
2016-01-26 13:57:52 +00:00
|
|
|
func TestIpPermissionExistsHandlesMultipleGroupIds(t *testing.T) {
|
2016-01-26 15:13:25 +00:00
|
|
|
oldIpPermission := ec2.IpPermission{
|
|
|
|
UserIdGroupPairs: []*ec2.UserIdGroupPair{
|
|
|
|
{GroupId: aws.String("firstGroupId")},
|
|
|
|
{GroupId: aws.String("secondGroupId")},
|
|
|
|
{GroupId: aws.String("thirdGroupId")},
|
2016-01-26 11:01:15 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-01-26 15:13:25 +00:00
|
|
|
existingIpPermission := ec2.IpPermission{
|
|
|
|
UserIdGroupPairs: []*ec2.UserIdGroupPair{
|
|
|
|
{GroupId: aws.String("secondGroupId")},
|
2016-01-26 11:01:15 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-01-26 15:13:25 +00:00
|
|
|
newIpPermission := ec2.IpPermission{
|
|
|
|
UserIdGroupPairs: []*ec2.UserIdGroupPair{
|
|
|
|
{GroupId: aws.String("fourthGroupId")},
|
2016-01-26 11:01:15 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-01-26 13:57:52 +00:00
|
|
|
equals := ipPermissionExists(&existingIpPermission, &oldIpPermission, false)
|
2016-01-26 11:01:15 +00:00
|
|
|
if !equals {
|
|
|
|
t.Errorf("Should have been considered equal since first is in the second array of groups")
|
|
|
|
}
|
|
|
|
|
2016-01-26 13:57:52 +00:00
|
|
|
equals = ipPermissionExists(&newIpPermission, &oldIpPermission, false)
|
2016-01-26 11:01:15 +00:00
|
|
|
if equals {
|
|
|
|
t.Errorf("Should have not been considered equal since first is not in the second array of groups")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 14:18:46 +00:00
|
|
|
func TestIpPermissionExistsHandlesRangeSubsets(t *testing.T) {
|
|
|
|
// Two existing scenarios we'll test against
|
|
|
|
emptyIpPermission := ec2.IpPermission{}
|
|
|
|
|
|
|
|
oldIpPermission := ec2.IpPermission{
|
|
|
|
IpRanges: []*ec2.IpRange{
|
|
|
|
{CidrIp: aws.String("10.0.0.0/8")},
|
|
|
|
{CidrIp: aws.String("192.168.1.0/24")},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Two already existing ranges and a new one
|
|
|
|
existingIpPermission := ec2.IpPermission{
|
|
|
|
IpRanges: []*ec2.IpRange{
|
|
|
|
{CidrIp: aws.String("10.0.0.0/8")},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
existingIpPermission2 := ec2.IpPermission{
|
|
|
|
IpRanges: []*ec2.IpRange{
|
|
|
|
{CidrIp: aws.String("192.168.1.0/24")},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
newIpPermission := ec2.IpPermission{
|
|
|
|
IpRanges: []*ec2.IpRange{
|
|
|
|
{CidrIp: aws.String("172.16.0.0/16")},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
exists := ipPermissionExists(&emptyIpPermission, &emptyIpPermission, false)
|
|
|
|
if !exists {
|
|
|
|
t.Errorf("Should have been considered existing since we're comparing a range array against itself")
|
|
|
|
}
|
|
|
|
exists = ipPermissionExists(&oldIpPermission, &oldIpPermission, false)
|
|
|
|
if !exists {
|
|
|
|
t.Errorf("Should have been considered existing since we're comparing a range array against itself")
|
|
|
|
}
|
|
|
|
|
|
|
|
exists = ipPermissionExists(&existingIpPermission, &oldIpPermission, false)
|
|
|
|
if !exists {
|
|
|
|
t.Errorf("Should have been considered existing since 10.* is in oldIpPermission's array of ranges")
|
|
|
|
}
|
|
|
|
exists = ipPermissionExists(&existingIpPermission2, &oldIpPermission, false)
|
|
|
|
if !exists {
|
|
|
|
t.Errorf("Should have been considered existing since 192.* is in oldIpPermission2's array of ranges")
|
|
|
|
}
|
|
|
|
|
|
|
|
exists = ipPermissionExists(&newIpPermission, &emptyIpPermission, false)
|
|
|
|
if exists {
|
|
|
|
t.Errorf("Should have not been considered existing since we compared against a missing array of ranges")
|
|
|
|
}
|
|
|
|
exists = ipPermissionExists(&newIpPermission, &oldIpPermission, false)
|
|
|
|
if exists {
|
|
|
|
t.Errorf("Should have not been considered existing since 172.* is not in oldIpPermission's array of ranges")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-26 13:57:52 +00:00
|
|
|
func TestIpPermissionExistsHandlesMultipleGroupIdsWithUserIds(t *testing.T) {
|
2016-01-26 15:13:25 +00:00
|
|
|
oldIpPermission := ec2.IpPermission{
|
|
|
|
UserIdGroupPairs: []*ec2.UserIdGroupPair{
|
|
|
|
{GroupId: aws.String("firstGroupId"), UserId: aws.String("firstUserId")},
|
|
|
|
{GroupId: aws.String("secondGroupId"), UserId: aws.String("secondUserId")},
|
|
|
|
{GroupId: aws.String("thirdGroupId"), UserId: aws.String("thirdUserId")},
|
2016-01-26 11:01:15 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-01-26 15:13:25 +00:00
|
|
|
existingIpPermission := ec2.IpPermission{
|
|
|
|
UserIdGroupPairs: []*ec2.UserIdGroupPair{
|
|
|
|
{GroupId: aws.String("secondGroupId"), UserId: aws.String("secondUserId")},
|
2016-01-26 11:01:15 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-01-26 15:13:25 +00:00
|
|
|
newIpPermission := ec2.IpPermission{
|
|
|
|
UserIdGroupPairs: []*ec2.UserIdGroupPair{
|
|
|
|
{GroupId: aws.String("secondGroupId"), UserId: aws.String("anotherUserId")},
|
2016-01-26 11:01:15 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-01-26 13:57:52 +00:00
|
|
|
equals := ipPermissionExists(&existingIpPermission, &oldIpPermission, true)
|
2016-01-26 11:01:15 +00:00
|
|
|
if !equals {
|
|
|
|
t.Errorf("Should have been considered equal since first is in the second array of groups")
|
|
|
|
}
|
|
|
|
|
2016-01-26 13:57:52 +00:00
|
|
|
equals = ipPermissionExists(&newIpPermission, &oldIpPermission, true)
|
2016-01-26 11:01:15 +00:00
|
|
|
if equals {
|
|
|
|
t.Errorf("Should have not been considered equal since first is not in the second array of groups")
|
|
|
|
}
|
|
|
|
}
|
2016-02-23 14:44:03 +00:00
|
|
|
func TestFindInstanceByNodeNameExcludesTerminatedInstances(t *testing.T) {
|
|
|
|
awsServices := NewFakeAWSServices()
|
|
|
|
|
|
|
|
nodeName := "my-dns.internal"
|
|
|
|
|
|
|
|
var tag ec2.Tag
|
|
|
|
tag.Key = aws.String(TagNameKubernetesCluster)
|
|
|
|
tag.Value = aws.String(TestClusterId)
|
|
|
|
tags := []*ec2.Tag{&tag}
|
|
|
|
|
|
|
|
var runningInstance ec2.Instance
|
|
|
|
runningInstance.InstanceId = aws.String("i-running")
|
|
|
|
runningInstance.PrivateDnsName = aws.String(nodeName)
|
|
|
|
runningInstance.State = &ec2.InstanceState{Code: aws.Int64(16), Name: aws.String("running")}
|
|
|
|
runningInstance.Tags = tags
|
|
|
|
|
|
|
|
var terminatedInstance ec2.Instance
|
|
|
|
terminatedInstance.InstanceId = aws.String("i-terminated")
|
|
|
|
terminatedInstance.PrivateDnsName = aws.String(nodeName)
|
|
|
|
terminatedInstance.State = &ec2.InstanceState{Code: aws.Int64(48), Name: aws.String("terminated")}
|
|
|
|
terminatedInstance.Tags = tags
|
|
|
|
|
|
|
|
instances := []*ec2.Instance{&terminatedInstance, &runningInstance}
|
|
|
|
awsServices.instances = append(awsServices.instances, instances...)
|
|
|
|
|
|
|
|
c, err := newAWSCloud(strings.NewReader("[global]"), awsServices)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error building aws cloud: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
instance, err := c.findInstanceByNodeName(nodeName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to find instance: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if *instance.InstanceId != "i-running" {
|
|
|
|
t.Errorf("Expected running instance but got %v", *instance.InstanceId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-06 16:58:03 +00:00
|
|
|
func TestFindInstancesByNodeNameCached(t *testing.T) {
|
2016-02-23 14:44:03 +00:00
|
|
|
awsServices := NewFakeAWSServices()
|
|
|
|
|
|
|
|
nodeNameOne := "my-dns.internal"
|
|
|
|
nodeNameTwo := "my-dns-two.internal"
|
|
|
|
|
|
|
|
var tag ec2.Tag
|
|
|
|
tag.Key = aws.String(TagNameKubernetesCluster)
|
|
|
|
tag.Value = aws.String(TestClusterId)
|
|
|
|
tags := []*ec2.Tag{&tag}
|
|
|
|
|
|
|
|
var runningInstance ec2.Instance
|
|
|
|
runningInstance.InstanceId = aws.String("i-running")
|
|
|
|
runningInstance.PrivateDnsName = aws.String(nodeNameOne)
|
|
|
|
runningInstance.State = &ec2.InstanceState{Code: aws.Int64(16), Name: aws.String("running")}
|
|
|
|
runningInstance.Tags = tags
|
|
|
|
|
|
|
|
var secondInstance ec2.Instance
|
|
|
|
|
|
|
|
secondInstance.InstanceId = aws.String("i-running")
|
|
|
|
secondInstance.PrivateDnsName = aws.String(nodeNameTwo)
|
|
|
|
secondInstance.State = &ec2.InstanceState{Code: aws.Int64(48), Name: aws.String("running")}
|
|
|
|
secondInstance.Tags = tags
|
|
|
|
|
|
|
|
var terminatedInstance ec2.Instance
|
|
|
|
terminatedInstance.InstanceId = aws.String("i-terminated")
|
|
|
|
terminatedInstance.PrivateDnsName = aws.String(nodeNameOne)
|
|
|
|
terminatedInstance.State = &ec2.InstanceState{Code: aws.Int64(48), Name: aws.String("terminated")}
|
|
|
|
terminatedInstance.Tags = tags
|
|
|
|
|
|
|
|
instances := []*ec2.Instance{&secondInstance, &runningInstance, &terminatedInstance}
|
|
|
|
awsServices.instances = append(awsServices.instances, instances...)
|
|
|
|
|
|
|
|
c, err := newAWSCloud(strings.NewReader("[global]"), awsServices)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error building aws cloud: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-06 16:58:03 +00:00
|
|
|
nodeNames := sets.NewString(nodeNameOne)
|
|
|
|
returnedInstances, errr := c.getInstancesByNodeNamesCached(nodeNames)
|
2016-02-23 14:44:03 +00:00
|
|
|
|
|
|
|
if errr != nil {
|
|
|
|
t.Errorf("Failed to find instance: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(returnedInstances) != 1 {
|
|
|
|
t.Errorf("Expected a single isntance but found: %v", returnedInstances)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *returnedInstances[0].PrivateDnsName != nodeNameOne {
|
|
|
|
t.Errorf("Expected node name %v but got %v", nodeNameOne, returnedInstances[0].PrivateDnsName)
|
|
|
|
}
|
|
|
|
}
|
2016-02-29 19:16:42 +00:00
|
|
|
|
|
|
|
func TestGetVolumeLabels(t *testing.T) {
|
|
|
|
awsServices := NewFakeAWSServices()
|
|
|
|
c, err := newAWSCloud(strings.NewReader("[global]"), awsServices)
|
|
|
|
assert.Nil(t, err, "Error building aws cloud: %v", err)
|
|
|
|
volumeId := aws.String("vol-VolumeId")
|
|
|
|
expectedVolumeRequest := &ec2.DescribeVolumesInput{VolumeIds: []*string{volumeId}}
|
|
|
|
awsServices.ec2.On("DescribeVolumes", expectedVolumeRequest).Return([]*ec2.Volume{
|
|
|
|
{
|
|
|
|
VolumeId: volumeId,
|
2016-03-05 13:11:30 +00:00
|
|
|
AvailabilityZone: aws.String("us-east-1a"),
|
2016-02-29 19:16:42 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
labels, err := c.GetVolumeLabels(*volumeId)
|
|
|
|
|
|
|
|
assert.Nil(t, err, "Error creating Volume %v", err)
|
|
|
|
assert.Equal(t, map[string]string{
|
|
|
|
unversioned.LabelZoneFailureDomain: "us-east-1a",
|
|
|
|
unversioned.LabelZoneRegion: "us-east-1"}, labels)
|
|
|
|
awsServices.ec2.AssertExpectations(t)
|
|
|
|
}
|
Change LoadBalancer methods to take api.Service
This is a better abstraction than passing in specific pieces of the
Service that each of the cloudproviders may or may not need. For
instance, many of the providers don't need a region, yet this is passed
in. Similarly many of the providers want a string IP for the load
balancer, but it passes in a converted net ip. Affinity is unused by
AWS. A provider change may also require adding a new parameter which has
an effect on all other cloud provider implementations.
Further, this will simplify adding provider specific load balancer
options, such as with labels or some other metadata. For example, we
could add labels for configuring the details of an AWS elastic load
balancer, such as idle timeout on connections, whether it is
internal or external, cross-zone load balancing, and so on.
Authors: @chbatey, @jsravn
2016-02-17 11:36:50 +00:00
|
|
|
|
|
|
|
func (self *FakeELB) expectDescribeLoadBalancers(loadBalancerName string) {
|
|
|
|
self.On("DescribeLoadBalancers", &elb.DescribeLoadBalancersInput{LoadBalancerNames: []*string{aws.String(loadBalancerName)}}).Return(&elb.DescribeLoadBalancersOutput{
|
|
|
|
LoadBalancerDescriptions: []*elb.LoadBalancerDescription{{}},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDescribeLoadBalancerOnDelete(t *testing.T) {
|
|
|
|
awsServices := NewFakeAWSServices()
|
|
|
|
c, _ := newAWSCloud(strings.NewReader("[global]"), awsServices)
|
|
|
|
awsServices.elb.expectDescribeLoadBalancers("aid")
|
|
|
|
|
|
|
|
c.EnsureLoadBalancerDeleted(&api.Service{ObjectMeta: api.ObjectMeta{Name: "myservice", UID: "id"}})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDescribeLoadBalancerOnUpdate(t *testing.T) {
|
|
|
|
awsServices := NewFakeAWSServices()
|
|
|
|
c, _ := newAWSCloud(strings.NewReader("[global]"), awsServices)
|
|
|
|
awsServices.elb.expectDescribeLoadBalancers("aid")
|
|
|
|
|
|
|
|
c.UpdateLoadBalancer(&api.Service{ObjectMeta: api.ObjectMeta{Name: "myservice", UID: "id"}}, []string{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDescribeLoadBalancerOnGet(t *testing.T) {
|
|
|
|
awsServices := NewFakeAWSServices()
|
|
|
|
c, _ := newAWSCloud(strings.NewReader("[global]"), awsServices)
|
|
|
|
awsServices.elb.expectDescribeLoadBalancers("aid")
|
|
|
|
|
|
|
|
c.GetLoadBalancer(&api.Service{ObjectMeta: api.ObjectMeta{Name: "myservice", UID: "id"}})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDescribeLoadBalancerOnEnsure(t *testing.T) {
|
|
|
|
awsServices := NewFakeAWSServices()
|
|
|
|
c, _ := newAWSCloud(strings.NewReader("[global]"), awsServices)
|
|
|
|
awsServices.elb.expectDescribeLoadBalancers("aid")
|
|
|
|
|
2016-05-17 23:55:04 +00:00
|
|
|
c.EnsureLoadBalancer(&api.Service{ObjectMeta: api.ObjectMeta{Name: "myservice", UID: "id"}}, []string{})
|
Change LoadBalancer methods to take api.Service
This is a better abstraction than passing in specific pieces of the
Service that each of the cloudproviders may or may not need. For
instance, many of the providers don't need a region, yet this is passed
in. Similarly many of the providers want a string IP for the load
balancer, but it passes in a converted net ip. Affinity is unused by
AWS. A provider change may also require adding a new parameter which has
an effect on all other cloud provider implementations.
Further, this will simplify adding provider specific load balancer
options, such as with labels or some other metadata. For example, we
could add labels for configuring the details of an AWS elastic load
balancer, such as idle timeout on connections, whether it is
internal or external, cross-zone load balancing, and so on.
Authors: @chbatey, @jsravn
2016-02-17 11:36:50 +00:00
|
|
|
}
|
2016-03-25 17:36:06 +00:00
|
|
|
|
2016-05-10 15:40:34 +00:00
|
|
|
func TestBuildListener(t *testing.T) {
|
2016-03-25 17:36:06 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
|
2016-05-02 21:43:26 +00:00
|
|
|
lbPort int64
|
2016-06-07 17:36:53 +00:00
|
|
|
portName string
|
2016-05-02 21:43:26 +00:00
|
|
|
instancePort int64
|
|
|
|
backendProtocolAnnotation string
|
|
|
|
certAnnotation string
|
2016-06-07 17:36:53 +00:00
|
|
|
sslPortAnnotation string
|
2016-03-25 17:36:06 +00:00
|
|
|
|
|
|
|
expectError bool
|
|
|
|
lbProtocol string
|
|
|
|
instanceProtocol string
|
|
|
|
certID string
|
|
|
|
}{
|
|
|
|
{
|
2016-05-02 21:43:26 +00:00
|
|
|
"No cert or BE protocol annotation, passthrough",
|
2016-06-07 17:36:53 +00:00
|
|
|
80, "", 7999, "", "", "",
|
2016-03-25 17:36:06 +00:00
|
|
|
false, "tcp", "tcp", "",
|
|
|
|
},
|
2016-05-10 15:53:44 +00:00
|
|
|
{
|
|
|
|
"Cert annotation without BE protocol specified, SSL->TCP",
|
2016-06-07 17:36:53 +00:00
|
|
|
80, "", 8000, "", "cert", "",
|
2016-05-10 15:53:44 +00:00
|
|
|
false, "ssl", "tcp", "cert",
|
|
|
|
},
|
2016-03-25 17:36:06 +00:00
|
|
|
{
|
2016-05-02 21:43:26 +00:00
|
|
|
"BE protocol without cert annotation, passthrough",
|
2016-06-07 17:36:53 +00:00
|
|
|
443, "", 8001, "https", "", "",
|
2016-05-02 21:43:26 +00:00
|
|
|
false, "tcp", "tcp", "",
|
2016-03-25 17:36:06 +00:00
|
|
|
},
|
|
|
|
{
|
2016-05-02 21:43:26 +00:00
|
|
|
"Invalid cert annotation, bogus backend protocol",
|
2016-06-07 17:36:53 +00:00
|
|
|
443, "", 8002, "bacon", "foo", "",
|
|
|
|
true, "tcp", "tcp", "",
|
2016-03-25 17:36:06 +00:00
|
|
|
},
|
|
|
|
{
|
2016-05-02 21:43:26 +00:00
|
|
|
"Invalid cert annotation, protocol followed by equal sign",
|
2016-06-07 17:36:53 +00:00
|
|
|
443, "", 8003, "http=", "=", "",
|
|
|
|
true, "tcp", "tcp", "",
|
2016-03-25 17:36:06 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"HTTPS->HTTPS",
|
2016-06-07 17:36:53 +00:00
|
|
|
443, "", 8004, "https", "cert", "",
|
2016-03-25 17:36:06 +00:00
|
|
|
false, "https", "https", "cert",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"HTTPS->HTTP",
|
2016-06-07 17:36:53 +00:00
|
|
|
443, "", 8005, "http", "cert", "",
|
2016-03-25 17:36:06 +00:00
|
|
|
false, "https", "http", "cert",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"SSL->SSL",
|
2016-06-07 17:36:53 +00:00
|
|
|
443, "", 8006, "ssl", "cert", "",
|
2016-03-25 17:36:06 +00:00
|
|
|
false, "ssl", "ssl", "cert",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"SSL->TCP",
|
2016-06-07 17:36:53 +00:00
|
|
|
443, "", 8007, "tcp", "cert", "",
|
2016-03-25 17:36:06 +00:00
|
|
|
false, "ssl", "tcp", "cert",
|
|
|
|
},
|
2016-06-07 17:36:53 +00:00
|
|
|
{
|
|
|
|
"Port in whitelist",
|
|
|
|
1234, "", 8008, "tcp", "cert", "1234,5678",
|
|
|
|
false, "ssl", "tcp", "cert",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Port not in whitelist, passthrough",
|
|
|
|
443, "", 8009, "tcp", "cert", "1234,5678",
|
|
|
|
false, "tcp", "tcp", "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Named port in whitelist",
|
|
|
|
1234, "bar", 8010, "tcp", "cert", "foo,bar",
|
|
|
|
false, "ssl", "tcp", "cert",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Named port not in whitelist, passthrough",
|
|
|
|
443, "", 8011, "tcp", "cert", "foo,bar",
|
|
|
|
false, "tcp", "tcp", "",
|
|
|
|
},
|
2016-03-25 17:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Logf("Running test case %s", test.name)
|
|
|
|
annotations := make(map[string]string)
|
2016-05-02 21:43:26 +00:00
|
|
|
if test.backendProtocolAnnotation != "" {
|
|
|
|
annotations[ServiceAnnotationLoadBalancerBEProtocol] = test.backendProtocolAnnotation
|
|
|
|
}
|
|
|
|
if test.certAnnotation != "" {
|
|
|
|
annotations[ServiceAnnotationLoadBalancerCertificate] = test.certAnnotation
|
2016-03-25 17:36:06 +00:00
|
|
|
}
|
2016-06-07 17:36:53 +00:00
|
|
|
ports := getPortSets(test.sslPortAnnotation)
|
2016-05-10 15:53:44 +00:00
|
|
|
l, err := buildListener(api.ServicePort{
|
2016-05-02 23:20:50 +00:00
|
|
|
NodePort: int32(test.instancePort),
|
|
|
|
Port: int32(test.lbPort),
|
2016-06-07 17:36:53 +00:00
|
|
|
Name: test.portName,
|
2016-03-25 17:36:06 +00:00
|
|
|
Protocol: api.Protocol("tcp"),
|
2016-06-07 17:36:53 +00:00
|
|
|
}, annotations, ports)
|
2016-03-25 17:36:06 +00:00
|
|
|
if test.expectError {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Should error for case %s", test.name)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Should succeed for case: %s, got %v", test.name, err)
|
|
|
|
} else {
|
|
|
|
var cert *string
|
|
|
|
if test.certID != "" {
|
|
|
|
cert = &test.certID
|
|
|
|
}
|
|
|
|
expected := &elb.Listener{
|
|
|
|
InstancePort: &test.instancePort,
|
|
|
|
InstanceProtocol: &test.instanceProtocol,
|
|
|
|
LoadBalancerPort: &test.lbPort,
|
|
|
|
Protocol: &test.lbProtocol,
|
|
|
|
SSLCertificateId: cert,
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(l, expected) {
|
2016-05-02 21:43:26 +00:00
|
|
|
t.Errorf("Incorrect listener (%v vs expected %v) for case: %s",
|
2016-03-25 17:36:06 +00:00
|
|
|
l, expected, test.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-20 21:41:18 +00:00
|
|
|
|
|
|
|
func TestProxyProtocolEnabled(t *testing.T) {
|
|
|
|
policies := sets.NewString(ProxyProtocolPolicyName, "FooBarFoo")
|
|
|
|
fakeBackend := &elb.BackendServerDescription{
|
|
|
|
InstancePort: aws.Int64(80),
|
|
|
|
PolicyNames: stringSetToPointers(policies),
|
|
|
|
}
|
|
|
|
result := proxyProtocolEnabled(fakeBackend)
|
|
|
|
assert.True(t, result, "expected to find %s in %s", ProxyProtocolPolicyName, policies)
|
|
|
|
|
|
|
|
policies = sets.NewString("FooBarFoo")
|
|
|
|
fakeBackend = &elb.BackendServerDescription{
|
|
|
|
InstancePort: aws.Int64(80),
|
|
|
|
PolicyNames: []*string{
|
|
|
|
aws.String("FooBarFoo"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
result = proxyProtocolEnabled(fakeBackend)
|
|
|
|
assert.False(t, result, "did not expect to find %s in %s", ProxyProtocolPolicyName, policies)
|
|
|
|
|
|
|
|
policies = sets.NewString()
|
|
|
|
fakeBackend = &elb.BackendServerDescription{
|
|
|
|
InstancePort: aws.Int64(80),
|
|
|
|
}
|
|
|
|
result = proxyProtocolEnabled(fakeBackend)
|
|
|
|
assert.False(t, result, "did not expect to find %s in %s", ProxyProtocolPolicyName, policies)
|
|
|
|
}
|