mirror of https://github.com/kubernetes/kops.git
Add unit tests
This commit is contained in:
parent
1912db0f11
commit
e2bf86a0d3
|
|
@ -19,6 +19,10 @@ package validation
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"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"
|
||||||
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
|
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
|
||||||
|
|
||||||
|
|
@ -102,28 +106,39 @@ func TestValidateInstanceGroupSpec(t *testing.T) {
|
||||||
{
|
{
|
||||||
Input: kops.InstanceGroupSpec{
|
Input: kops.InstanceGroupSpec{
|
||||||
MachineType: "t2.micro",
|
MachineType: "t2.micro",
|
||||||
|
Image: "ami-073c8c0760395aab8",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Input: kops.InstanceGroupSpec{
|
Input: kops.InstanceGroupSpec{
|
||||||
MachineType: "t2.invalidType",
|
MachineType: "t2.invalidType",
|
||||||
|
Image: "ami-073c8c0760395aab8",
|
||||||
},
|
},
|
||||||
ExpectedErrors: []string{"Invalid value::test-nodes.spec.machineType"},
|
ExpectedErrors: []string{"Invalid value::test-nodes.spec.machineType"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Input: kops.InstanceGroupSpec{
|
Input: kops.InstanceGroupSpec{
|
||||||
MachineType: "m5.large",
|
MachineType: "m4.large",
|
||||||
Image: "k8s-1.9-debian-stretch-amd64-hvm-ebs-2018-03-11",
|
Image: "ami-073c8c0760395aab8",
|
||||||
},
|
},
|
||||||
ExpectedErrors: []string{},
|
ExpectedErrors: []string{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Input: kops.InstanceGroupSpec{
|
Input: kops.InstanceGroupSpec{
|
||||||
MachineType: "c5.large",
|
MachineType: "c5.large",
|
||||||
Image: "k8s-1.9-debian-stretch-amd64-hvm-ebs-2018-03-11",
|
Image: "ami-073c8c0760395aab8",
|
||||||
},
|
},
|
||||||
ExpectedErrors: []string{},
|
ExpectedErrors: []string{},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Input: kops.InstanceGroupSpec{
|
||||||
|
MachineType: "a1.large",
|
||||||
|
Image: "ami-073c8c0760395aab8",
|
||||||
|
},
|
||||||
|
ExpectedErrors: []string{
|
||||||
|
"Invalid value::test-nodes.spec.machineType",
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Input: kops.InstanceGroupSpec{
|
Input: kops.InstanceGroupSpec{
|
||||||
SpotDurationInMinutes: fi.Int64(55),
|
SpotDurationInMinutes: fi.Int64(55),
|
||||||
|
|
@ -182,6 +197,18 @@ func TestValidateInstanceGroupSpec(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
cloud := awsup.BuildMockAWSCloud("us-east-1", "abc")
|
cloud := awsup.BuildMockAWSCloud("us-east-1", "abc")
|
||||||
|
mockEC2 := &mockec2.MockEC2{}
|
||||||
|
cloud.MockEC2 = mockEC2
|
||||||
|
|
||||||
|
mockEC2.Images = append(mockEC2.Images, &ec2.Image{
|
||||||
|
CreationDate: aws.String("2016-10-21T20:07:19.000Z"),
|
||||||
|
ImageId: aws.String("ami-073c8c0760395aab8"),
|
||||||
|
Name: aws.String("focal"),
|
||||||
|
OwnerId: aws.String(awsup.WellKnownAccountUbuntu),
|
||||||
|
RootDeviceName: aws.String("/dev/xvda"),
|
||||||
|
Architecture: aws.String("x86_64"),
|
||||||
|
})
|
||||||
|
|
||||||
for _, g := range grid {
|
for _, g := range grid {
|
||||||
ig := &kops.InstanceGroup{
|
ig := &kops.InstanceGroup{
|
||||||
ObjectMeta: v1.ObjectMeta{
|
ObjectMeta: v1.ObjectMeta{
|
||||||
|
|
@ -195,6 +222,82 @@ func TestValidateInstanceGroupSpec(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMixedInstancePolicies(t *testing.T) {
|
||||||
|
grid := []struct {
|
||||||
|
Input kops.InstanceGroupSpec
|
||||||
|
ExpectedErrors []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Input: kops.InstanceGroupSpec{
|
||||||
|
MachineType: "m4.large",
|
||||||
|
Image: "ami-073c8c0760395aab8",
|
||||||
|
MixedInstancesPolicy: &kops.MixedInstancesPolicySpec{
|
||||||
|
Instances: []string{
|
||||||
|
"m4.large",
|
||||||
|
"t3.medium",
|
||||||
|
"c5.large",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ExpectedErrors: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Input: kops.InstanceGroupSpec{
|
||||||
|
MachineType: "m4.large",
|
||||||
|
Image: "ami-073c8c0760395aab8",
|
||||||
|
MixedInstancesPolicy: &kops.MixedInstancesPolicySpec{
|
||||||
|
Instances: []string{
|
||||||
|
"a1.large",
|
||||||
|
"c4.large",
|
||||||
|
"c5.large",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ExpectedErrors: []string{"Invalid value::spec.mixedInstancesPolicy.instances[0]"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Input: kops.InstanceGroupSpec{
|
||||||
|
MachineType: "m4.large",
|
||||||
|
Image: "ami-073c8c0760395aab8",
|
||||||
|
MixedInstancesPolicy: &kops.MixedInstancesPolicySpec{
|
||||||
|
Instances: []string{
|
||||||
|
"t3.medium",
|
||||||
|
"c4.large",
|
||||||
|
"c5.large",
|
||||||
|
},
|
||||||
|
OnDemandAboveBase: fi.Int64(231),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ExpectedErrors: []string{"Invalid value::spec.mixedInstancesPolicy.onDemandAboveBase"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cloud := awsup.BuildMockAWSCloud("us-east-1", "abc")
|
||||||
|
mockEC2 := &mockec2.MockEC2{}
|
||||||
|
cloud.MockEC2 = mockEC2
|
||||||
|
|
||||||
|
mockEC2.Images = append(mockEC2.Images, &ec2.Image{
|
||||||
|
CreationDate: aws.String("2016-10-21T20:07:19.000Z"),
|
||||||
|
ImageId: aws.String("ami-073c8c0760395aab8"),
|
||||||
|
Name: aws.String("focal"),
|
||||||
|
OwnerId: aws.String(awsup.WellKnownAccountUbuntu),
|
||||||
|
RootDeviceName: aws.String("/dev/xvda"),
|
||||||
|
Architecture: aws.String("x86_64"),
|
||||||
|
})
|
||||||
|
|
||||||
|
for _, g := range grid {
|
||||||
|
ig := &kops.InstanceGroup{
|
||||||
|
ObjectMeta: v1.ObjectMeta{
|
||||||
|
Name: "test-nodes",
|
||||||
|
},
|
||||||
|
Spec: g.Input,
|
||||||
|
}
|
||||||
|
errs := awsValidateInstanceGroup(ig, cloud)
|
||||||
|
|
||||||
|
testErrors(t, g.Input, errs, g.ExpectedErrors)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestInstanceMetadataOptions(t *testing.T) {
|
func TestInstanceMetadataOptions(t *testing.T) {
|
||||||
cloud := awsup.BuildMockAWSCloud("us-east-1", "abc")
|
cloud := awsup.BuildMockAWSCloud("us-east-1", "abc")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue