Skip to content

Commit

Permalink
types: unit tests for InstallConfig validation
Browse files Browse the repository at this point in the history
  • Loading branch information
staebler committed Dec 13, 2018
1 parent 5cbe0f7 commit 5bddc88
Show file tree
Hide file tree
Showing 10 changed files with 879 additions and 17 deletions.
70 changes: 70 additions & 0 deletions pkg/types/aws/validation/machinepool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package validation

import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/openshift/installer/pkg/types/aws"
)

func TestValidateMachinePool(t *testing.T) {
cases := []struct {
name string
pool *aws.MachinePool
valid bool
}{
{
name: "empty",
pool: &aws.MachinePool{},
valid: true,
},
{
name: "valid iops",
pool: &aws.MachinePool{
EC2RootVolume: aws.EC2RootVolume{
IOPS: 10,
},
},
valid: true,
},
{
name: "invalid iops",
pool: &aws.MachinePool{
EC2RootVolume: aws.EC2RootVolume{
IOPS: -10,
},
},
valid: false,
},
{
name: "valid size",
pool: &aws.MachinePool{
EC2RootVolume: aws.EC2RootVolume{
Size: 10,
},
},
valid: true,
},
{
name: "invalid size",
pool: &aws.MachinePool{
EC2RootVolume: aws.EC2RootVolume{
Size: -10,
},
},
valid: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
err := ValidateMachinePool(tc.pool, field.NewPath("test-path")).ToAggregate()
if tc.valid {
assert.NoError(t, err)
} else {
assert.Error(t, err)
}
})
}
}
80 changes: 80 additions & 0 deletions pkg/types/aws/validation/platform_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package validation

import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/openshift/installer/pkg/ipnet"
"github.com/openshift/installer/pkg/types/aws"
)

func TestValidatePlatform(t *testing.T) {
cases := []struct {
name string
platform *aws.Platform
valid bool
}{
{
name: "minimal",
platform: &aws.Platform{
Region: "us-east-1",
},
valid: true,
},
{
name: "invalid region",
platform: &aws.Platform{
Region: "bad-region",
},
valid: false,
},
{
name: "valid machine pool",
platform: &aws.Platform{
Region: "us-east-1",
DefaultMachinePlatform: &aws.MachinePool{},
},
valid: true,
},
{
name: "invalid machine pool",
platform: &aws.Platform{
Region: "us-east-1",
DefaultMachinePlatform: &aws.MachinePool{
EC2RootVolume: aws.EC2RootVolume{
IOPS: -10,
},
},
},
valid: false,
},
{
name: "valid CIDR",
platform: &aws.Platform{
Region: "us-east-1",
VPCCIDRBlock: ipnet.MustParseCIDR("192.168.0.0/16"),
},
valid: true,
},
{
name: "invalid CIDR",
platform: &aws.Platform{
Region: "us-east-1",
VPCCIDRBlock: ipnet.MustParseCIDR("0.0.0.0/16"),
},
valid: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
err := ValidatePlatform(tc.platform, field.NewPath("test-path")).ToAggregate()
if tc.valid {
assert.NoError(t, err)
} else {
assert.Error(t, err)
}
})
}
}
48 changes: 48 additions & 0 deletions pkg/types/libvirt/validation/machinepool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package validation

import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/openshift/installer/pkg/types/libvirt"
)

func TestValidateMachinePool(t *testing.T) {
cases := []struct {
name string
pool *libvirt.MachinePool
valid bool
}{
{
name: "empty",
pool: &libvirt.MachinePool{},
valid: true,
},
{
name: "valid image",
pool: &libvirt.MachinePool{
Image: "https://example.com/rhcos-qemu.qcow2",
},
valid: true,
},
{
name: "invalid image",
pool: &libvirt.MachinePool{
Image: "bad-image",
},
valid: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
err := ValidateMachinePool(tc.pool, field.NewPath("test-path")).ToAggregate()
if tc.valid {
assert.NoError(t, err)
} else {
assert.Error(t, err)
}
})
}
}
92 changes: 92 additions & 0 deletions pkg/types/libvirt/validation/platform_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package validation

import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/openshift/installer/pkg/ipnet"
"github.com/openshift/installer/pkg/types/libvirt"
)

func validPlatform() *libvirt.Platform {
return &libvirt.Platform{
URI: "qemu+tcp://192.168.122.1/system",
Network: libvirt.Network{
IfName: "tt0",
IPRange: *ipnet.MustParseCIDR("10.0.0.0/16"),
},
}
}

func TestValidatePlatform(t *testing.T) {
cases := []struct {
name string
platform *libvirt.Platform
valid bool
}{
{
name: "minimal",
platform: validPlatform(),
valid: true,
},
{
name: "invalid uri",
platform: func() *libvirt.Platform {
p := validPlatform()
p.URI = "bad-uri"
return p
}(),
valid: false,
},
{
name: "missing interface name",
platform: func() *libvirt.Platform {
p := validPlatform()
p.Network.IfName = ""
return p
}(),
valid: false,
},
{
name: "missing ip range",
platform: func() *libvirt.Platform {
p := validPlatform()
p.Network.IPRange = ipnet.IPNet{}
return p
}(),
valid: false,
},
{
name: "valid machine pool",
platform: func() *libvirt.Platform {
p := validPlatform()
p.DefaultMachinePlatform = &libvirt.MachinePool{}
return p
}(),
valid: true,
},
{
name: "invalid machine pool",
platform: func() *libvirt.Platform {
p := validPlatform()
p.DefaultMachinePlatform = &libvirt.MachinePool{
Image: "bad-image",
}
return p
}(),
valid: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
err := ValidatePlatform(tc.platform, field.NewPath("test-path")).ToAggregate()
if tc.valid {
assert.NoError(t, err)
} else {
assert.Error(t, err)
}
})
}
}
34 changes: 34 additions & 0 deletions pkg/types/openstack/validation/machinepool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package validation

import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/openshift/installer/pkg/types/openstack"
)

func TestValidateMachinePool(t *testing.T) {
cases := []struct {
name string
pool *openstack.MachinePool
valid bool
}{
{
name: "empty",
pool: &openstack.MachinePool{},
valid: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
err := ValidateMachinePool(tc.pool, field.NewPath("test-path")).ToAggregate()
if tc.valid {
assert.NoError(t, err)
} else {
assert.Error(t, err)
}
})
}
}
9 changes: 9 additions & 0 deletions pkg/types/openstack/validation/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
// ValidatePlatform checks that the specified platform is valid.
func ValidatePlatform(p *openstack.Platform, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if p.Region == "" {
allErrs = append(allErrs, field.Required(fldPath.Child("region"), "must specfify region"))
}
if p.DefaultMachinePlatform != nil {
allErrs = append(allErrs, ValidateMachinePool(p.DefaultMachinePlatform, fldPath.Child("defaultMachinePlatform"))...)
}
Expand All @@ -19,5 +22,11 @@ func ValidatePlatform(p *openstack.Platform, fldPath *field.Path) field.ErrorLis
if err := validate.URI(p.BaseImage); err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("baseImage"), p.BaseImage, err.Error()))
}
if p.Cloud == "" {
allErrs = append(allErrs, field.Required(fldPath.Child("cloud"), "must specify cloud"))
}
if p.ExternalNetwork == "" {
allErrs = append(allErrs, field.Required(fldPath.Child("externalNetwork"), "must specify external network"))
}
return allErrs
}
Loading

0 comments on commit 5bddc88

Please sign in to comment.