Add super-simple test for VPC task

This commit is contained in:
Justin Santa Barbara 2016-12-09 23:16:59 -05:00
parent c474bc28b3
commit d8f1a0679d
3 changed files with 138 additions and 18 deletions

View File

@ -30,6 +30,18 @@ type vpcInfo struct {
attributes ec2.DescribeVpcAttributeOutput
}
func (m *MockEC2) FindVpc(id string) *ec2.Vpc {
vpc := m.Vpcs[id]
if vpc == nil {
return nil
}
copy := vpc.main
copy.Tags = m.getTags(ec2.ResourceTypeVpc, *vpc.main.VpcId)
return &copy
}
func (m *MockEC2) CreateVpcRequest(*ec2.CreateVpcInput) (*request.Request, *ec2.CreateVpcOutput) {
panic("Not implemented")
return nil, nil
@ -46,6 +58,7 @@ func (m *MockEC2) CreateVpc(request *ec2.CreateVpcInput) (*ec2.CreateVpcOutput,
main: ec2.Vpc{
VpcId: s(id),
CidrBlock: request.CidrBlock,
IsDefault: aws.Bool(false),
},
attributes: ec2.DescribeVpcAttributeOutput{
EnableDnsHostnames: &ec2.AttributeBooleanValue{Value: aws.Bool(false)},

View File

@ -96,23 +96,27 @@ func TestElasticIPCreate(t *testing.T) {
{
allTasks := buildTasks()
target := fi.NewDryRunTarget(os.Stderr)
context, err := fi.NewContext(target, cloud, nil, nil, nil, true, allTasks)
if err != nil {
t.Fatalf("error building context: %v", err)
}
if err := context.RunTasks(defaultDeadline); err != nil {
t.Fatalf("unexpected error during Run: %v", err)
}
if target.HasChanges() {
var b bytes.Buffer
if err := target.PrintReport(allTasks, &b); err != nil {
t.Fatalf("error building report: %v", err)
}
t.Fatalf("Target had changes after executing: %v", b.String())
}
checkNoChanges(t, cloud, allTasks)
}
}
func checkNoChanges(t *testing.T, cloud fi.Cloud, allTasks map[string]fi.Task) {
target := fi.NewDryRunTarget(os.Stderr)
context, err := fi.NewContext(target, cloud, nil, nil, nil, true, allTasks)
if err != nil {
t.Fatalf("error building context: %v", err)
}
if err := context.RunTasks(defaultDeadline); err != nil {
t.Fatalf("unexpected error during Run: %v", err)
}
if target.HasChanges() {
var b bytes.Buffer
if err := target.PrintReport(allTasks, &b); err != nil {
t.Fatalf("error building report: %v", err)
}
t.Fatalf("Target had changes after executing: %v", b.String())
}
}

View File

@ -0,0 +1,103 @@
/*
Copyright 2016 The Kubernetes Authors.
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.
*/
package awstasks
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"k8s.io/kops/cloudmock/aws/mockec2"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"reflect"
"testing"
)
func TestVPCCreate(t *testing.T) {
cloud := awsup.BuildMockAWSCloud("us-east-1", "abc")
c := &mockec2.MockEC2{}
cloud.MockEC2 = c
// We define a function so we can rebuild the tasks, because we modify in-place when running
buildTasks := func() map[string]fi.Task {
vpc1 := &VPC{
Name: s("vpc1"),
CIDR: s("172.21.0.0/16"),
}
return map[string]fi.Task{
"vpc1": vpc1,
}
}
{
allTasks := buildTasks()
vpc1 := allTasks["vpc1"].(*VPC)
target := &awsup.AWSAPITarget{
Cloud: cloud,
}
context, err := fi.NewContext(target, cloud, nil, nil, nil, true, allTasks)
if err != nil {
t.Fatalf("error building context: %v", err)
}
if err := context.RunTasks(defaultDeadline); err != nil {
t.Fatalf("unexpected error during Run: %v", err)
}
if fi.StringValue(vpc1.ID) == "" {
t.Fatalf("ID not set after create")
}
if len(c.Vpcs) != 1 {
t.Fatalf("Expected exactly one Vpc; found %v", c.Vpcs)
}
expected := &ec2.Vpc{
CidrBlock: s("172.21.0.0/16"),
IsDefault: fi.Bool(false),
VpcId: vpc1.ID,
Tags: buildTags(map[string]string{
"Name": "vpc1",
}),
}
actual := c.FindVpc(*vpc1.ID)
if actual == nil {
t.Fatalf("VPC created but then not found")
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("Unexpected VPC: expected=%v actual=%v", expected, actual)
}
}
{
allTasks := buildTasks()
checkNoChanges(t, cloud, allTasks)
}
}
func buildTags(tags map[string]string) []*ec2.Tag {
var t []*ec2.Tag
for k, v := range tags {
t = append(t, &ec2.Tag{
Key: aws.String(k),
Value: aws.String(v),
})
}
return t
}