mirror of
https://github.com/openshift/installer.git
synced 2026-02-05 15:47:14 +01:00
pkg/asset/manifests/network_test.go: rework tests
Reworks the tests for producing the CNO config. Introduces the assert package to test for equality for and reworks the logic to use structs rather than literals for comparison. Also adapts the tests to the refacoring for ipv4.InternalJoinSubnet support.
This commit is contained in:
@@ -1,42 +1,21 @@
|
||||
package manifests
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/utils/ptr"
|
||||
|
||||
configv1 "github.com/openshift/api/config/v1"
|
||||
operatorv1 "github.com/openshift/api/operator/v1"
|
||||
"github.com/openshift/installer/pkg/asset"
|
||||
"github.com/openshift/installer/pkg/asset/installconfig"
|
||||
"github.com/openshift/installer/pkg/ipnet"
|
||||
"github.com/openshift/installer/pkg/types"
|
||||
awstypes "github.com/openshift/installer/pkg/types/aws"
|
||||
"github.com/openshift/installer/pkg/types/aws"
|
||||
"github.com/openshift/installer/pkg/types/powervs"
|
||||
)
|
||||
|
||||
const stubDefaultNetworkConfigOVNWithMTU = `apiVersion: operator.openshift.io/v1
|
||||
kind: Network
|
||||
metadata:
|
||||
creationTimestamp: null
|
||||
name: cluster
|
||||
spec:
|
||||
clusterNetwork: null
|
||||
defaultNetwork:
|
||||
ovnKubernetesConfig:
|
||||
egressIPConfig: {}
|
||||
mtu: 1000
|
||||
type: OVNKubernetes
|
||||
disableNetworkDiagnostics: false
|
||||
managementState: Managed
|
||||
observedConfig: null
|
||||
serviceNetwork: null
|
||||
unsupportedConfigOverrides: null
|
||||
status:
|
||||
readyReplicas: 0
|
||||
`
|
||||
|
||||
func validInstallConfigWithMTU(ic *types.InstallConfig) *installconfig.InstallConfig {
|
||||
return &installconfig.InstallConfig{
|
||||
AssetBase: installconfig.AssetBase{
|
||||
@@ -45,27 +24,41 @@ func validInstallConfigWithMTU(ic *types.InstallConfig) *installconfig.InstallCo
|
||||
}
|
||||
}
|
||||
|
||||
func TestNetworking_GenerateCustomNetworkConfigMTU(t *testing.T) {
|
||||
type args struct {
|
||||
ic *installconfig.InstallConfig
|
||||
func stubDefaultNetworkConfigOVN() *operatorv1.Network {
|
||||
return &operatorv1.Network{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: "operator.openshift.io/v1",
|
||||
Kind: "Network",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "cluster",
|
||||
},
|
||||
Spec: operatorv1.NetworkSpec{
|
||||
ClusterNetwork: []operatorv1.ClusterNetworkEntry{},
|
||||
ServiceNetwork: []string{},
|
||||
OperatorSpec: operatorv1.OperatorSpec{ManagementState: operatorv1.Managed},
|
||||
DefaultNetwork: operatorv1.DefaultNetworkDefinition{
|
||||
Type: operatorv1.NetworkTypeOVNKubernetes,
|
||||
OVNKubernetesConfig: &operatorv1.OVNKubernetesConfig{},
|
||||
},
|
||||
},
|
||||
Status: operatorv1.NetworkStatus{},
|
||||
}
|
||||
}
|
||||
|
||||
func TestNetworking_GenerateCustomNetworkConfigMTU(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
no *Networking
|
||||
args args
|
||||
want *operatorv1.DefaultNetworkDefinition
|
||||
ic *installconfig.InstallConfig
|
||||
want *operatorv1.Network
|
||||
wantErr bool
|
||||
wantErrExp string
|
||||
}{
|
||||
{
|
||||
name: "mtu unset OVNKubernetes",
|
||||
no: &Networking{},
|
||||
args: args{ic: &installconfig.InstallConfig{}},
|
||||
wantErr: false,
|
||||
}, {
|
||||
name: "mtu unset OVNKubernetes edge defaults",
|
||||
no: &Networking{},
|
||||
args: args{ic: validInstallConfigWithMTU(&types.InstallConfig{
|
||||
ic: validInstallConfigWithMTU(&types.InstallConfig{
|
||||
Networking: &types.Networking{
|
||||
NetworkType: "OVNKubernetes",
|
||||
},
|
||||
@@ -74,34 +67,32 @@ func TestNetworking_GenerateCustomNetworkConfigMTU(t *testing.T) {
|
||||
Name: "edge",
|
||||
},
|
||||
},
|
||||
Platform: types.Platform{AWS: &awstypes.Platform{}},
|
||||
})},
|
||||
want: &operatorv1.DefaultNetworkDefinition{
|
||||
Type: operatorv1.NetworkTypeOVNKubernetes,
|
||||
OVNKubernetesConfig: &operatorv1.OVNKubernetesConfig{
|
||||
MTU: ptr.To(uint32(1200)),
|
||||
},
|
||||
},
|
||||
Platform: types.Platform{AWS: &aws.Platform{}},
|
||||
}),
|
||||
want: func() *operatorv1.Network {
|
||||
ovn := stubDefaultNetworkConfigOVN()
|
||||
ovn.Spec.DefaultNetwork.OVNKubernetesConfig.MTU = ptr.To(uint32(1200))
|
||||
return ovn
|
||||
}(),
|
||||
}, {
|
||||
name: "mtu set OVNKubernetes",
|
||||
no: &Networking{},
|
||||
args: args{ic: validInstallConfigWithMTU(&types.InstallConfig{
|
||||
ic: validInstallConfigWithMTU(&types.InstallConfig{
|
||||
Networking: &types.Networking{
|
||||
ClusterNetworkMTU: 1500,
|
||||
NetworkType: "OVNKubernetes",
|
||||
},
|
||||
Platform: types.Platform{AWS: &awstypes.Platform{}},
|
||||
})},
|
||||
want: &operatorv1.DefaultNetworkDefinition{
|
||||
Type: operatorv1.NetworkTypeOVNKubernetes,
|
||||
OVNKubernetesConfig: &operatorv1.OVNKubernetesConfig{
|
||||
MTU: ptr.To(uint32(1500)),
|
||||
},
|
||||
},
|
||||
Platform: types.Platform{AWS: &aws.Platform{}},
|
||||
}),
|
||||
want: func() *operatorv1.Network {
|
||||
ovn := stubDefaultNetworkConfigOVN()
|
||||
ovn.Spec.DefaultNetwork.OVNKubernetesConfig.MTU = ptr.To(uint32(1500))
|
||||
return ovn
|
||||
}(),
|
||||
}, {
|
||||
name: "mtu set OVNKubernetes edge",
|
||||
no: &Networking{},
|
||||
args: args{ic: validInstallConfigWithMTU(&types.InstallConfig{
|
||||
ic: validInstallConfigWithMTU(&types.InstallConfig{
|
||||
Networking: &types.Networking{
|
||||
ClusterNetworkMTU: 1500,
|
||||
NetworkType: "OVNKubernetes",
|
||||
@@ -111,18 +102,17 @@ func TestNetworking_GenerateCustomNetworkConfigMTU(t *testing.T) {
|
||||
Name: "edge",
|
||||
},
|
||||
},
|
||||
Platform: types.Platform{AWS: &awstypes.Platform{}},
|
||||
})},
|
||||
want: &operatorv1.DefaultNetworkDefinition{
|
||||
Type: operatorv1.NetworkTypeOVNKubernetes,
|
||||
OVNKubernetesConfig: &operatorv1.OVNKubernetesConfig{
|
||||
MTU: ptr.To(uint32(1500)),
|
||||
},
|
||||
},
|
||||
Platform: types.Platform{AWS: &aws.Platform{}},
|
||||
}),
|
||||
want: func() *operatorv1.Network {
|
||||
ovn := stubDefaultNetworkConfigOVN()
|
||||
ovn.Spec.DefaultNetwork.OVNKubernetesConfig.MTU = ptr.To(uint32(1500))
|
||||
return ovn
|
||||
}(),
|
||||
}, {
|
||||
name: "mtu set OVNKubernetes edge high",
|
||||
no: &Networking{},
|
||||
args: args{ic: validInstallConfigWithMTU(&types.InstallConfig{
|
||||
ic: validInstallConfigWithMTU(&types.InstallConfig{
|
||||
Networking: &types.Networking{
|
||||
ClusterNetworkMTU: 8000,
|
||||
NetworkType: "OVNKubernetes",
|
||||
@@ -132,23 +122,51 @@ func TestNetworking_GenerateCustomNetworkConfigMTU(t *testing.T) {
|
||||
Name: "edge",
|
||||
},
|
||||
},
|
||||
Platform: types.Platform{AWS: &awstypes.Platform{}},
|
||||
})},
|
||||
want: &operatorv1.DefaultNetworkDefinition{
|
||||
Type: operatorv1.NetworkTypeOVNKubernetes,
|
||||
OVNKubernetesConfig: &operatorv1.OVNKubernetesConfig{
|
||||
MTU: ptr.To(uint32(8000)),
|
||||
Platform: types.Platform{AWS: &aws.Platform{}},
|
||||
}),
|
||||
want: func() *operatorv1.Network {
|
||||
ovn := stubDefaultNetworkConfigOVN()
|
||||
ovn.Spec.DefaultNetwork.OVNKubernetesConfig.MTU = ptr.To(uint32(8000))
|
||||
return ovn
|
||||
}(),
|
||||
},
|
||||
{
|
||||
name: "PowerVS has host routing enabled",
|
||||
no: &Networking{},
|
||||
ic: validInstallConfigWithMTU(&types.InstallConfig{
|
||||
Networking: &types.Networking{
|
||||
NetworkType: "OVNKubernetes",
|
||||
},
|
||||
},
|
||||
Platform: types.Platform{PowerVS: &powervs.Platform{}},
|
||||
}),
|
||||
want: func() *operatorv1.Network {
|
||||
ovn := stubDefaultNetworkConfigOVN()
|
||||
ovn.Spec.DefaultNetwork.OVNKubernetesConfig.GatewayConfig = &operatorv1.GatewayConfig{RoutingViaHost: true}
|
||||
return ovn
|
||||
}(),
|
||||
},
|
||||
{
|
||||
name: "Custom OVN V4 InternalJoinSubnet",
|
||||
no: &Networking{},
|
||||
ic: validInstallConfigWithMTU(&types.InstallConfig{
|
||||
Networking: &types.Networking{
|
||||
NetworkType: "OVNKubernetes",
|
||||
OVNKubernetesConfig: &types.OVNKubernetesConfig{
|
||||
IPv4: &types.IPv4OVNKubernetesConfig{InternalJoinSubnet: ipnet.MustParseCIDR("101.64.0.0/16")},
|
||||
},
|
||||
},
|
||||
Platform: types.Platform{AWS: &aws.Platform{}},
|
||||
}),
|
||||
want: func() *operatorv1.Network {
|
||||
ovn := stubDefaultNetworkConfigOVN()
|
||||
ovn.Spec.DefaultNetwork.OVNKubernetesConfig.IPv4 = &operatorv1.IPv4OVNKubernetesConfig{InternalJoinSubnet: "101.64.0.0/16"}
|
||||
return ovn
|
||||
}(),
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
no := &Networking{
|
||||
Config: tt.no.Config,
|
||||
FileList: tt.no.FileList,
|
||||
}
|
||||
got, err := no.GenerateCustomNetworkConfigMTU(tt.args.ic)
|
||||
got, err := clusterNetworkOperatorConfig(tt.ic, []configv1.ClusterNetworkEntry{}, []string{})
|
||||
if (err != nil) != tt.wantErr {
|
||||
if tt.wantErrExp != "" {
|
||||
assert.Regexp(t, tt.wantErrExp, err)
|
||||
@@ -157,80 +175,7 @@ func TestNetworking_GenerateCustomNetworkConfigMTU(t *testing.T) {
|
||||
t.Errorf("Networking.GenerateCustomNetworkConfigMTU() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
dumpDN := ""
|
||||
if got.OVNKubernetesConfig != nil {
|
||||
t.Errorf("Networking.GenerateCustomNetworkConfigMTU() = %v (%v), want %v", got, *got.OVNKubernetesConfig, tt.want)
|
||||
dumpDN = fmt.Sprintf("%v", *got.OVNKubernetesConfig)
|
||||
if got.OVNKubernetesConfig.MTU != nil {
|
||||
dumpDN += fmt.Sprintf(" mtu[%v]", *got.OVNKubernetesConfig.MTU)
|
||||
}
|
||||
} else if got.OpenShiftSDNConfig != nil {
|
||||
t.Errorf("Networking.GenerateCustomNetworkConfigMTU() = %v (%v), want %v", got, *got.OpenShiftSDNConfig, tt.want)
|
||||
dumpDN = fmt.Sprintf("%v", *got.OpenShiftSDNConfig)
|
||||
if got.OpenShiftSDNConfig.MTU != nil {
|
||||
dumpDN += fmt.Sprintf(" mtu[%v]", *got.OpenShiftSDNConfig.MTU)
|
||||
}
|
||||
}
|
||||
|
||||
t.Errorf("Networking.GenerateCustomNetworkConfigMTU() = %v (debug: %s), want %v", got, dumpDN, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNetworking_generateCustomCnoConfig(t *testing.T) {
|
||||
type fields struct {
|
||||
Config *configv1.Network
|
||||
FileList []*asset.File
|
||||
}
|
||||
type args struct {
|
||||
defaultNetwork *operatorv1.DefaultNetworkDefinition
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want []byte
|
||||
wantErr bool
|
||||
wantErrExp string
|
||||
}{
|
||||
{
|
||||
name: "custom OVNKubernetes",
|
||||
fields: fields{},
|
||||
args: args{defaultNetwork: &operatorv1.DefaultNetworkDefinition{
|
||||
Type: operatorv1.NetworkTypeOVNKubernetes,
|
||||
OVNKubernetesConfig: &operatorv1.OVNKubernetesConfig{
|
||||
MTU: ptr.To(uint32(1000)),
|
||||
},
|
||||
}},
|
||||
want: []byte(stubDefaultNetworkConfigOVNWithMTU),
|
||||
}, {
|
||||
name: "invalid config",
|
||||
fields: fields{},
|
||||
args: args{defaultNetwork: nil},
|
||||
wantErr: true,
|
||||
wantErrExp: `defaultNetwork must be specified`,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
no := &Networking{
|
||||
Config: tt.fields.Config,
|
||||
FileList: tt.fields.FileList,
|
||||
}
|
||||
got, err := no.generateCustomCnoConfig(tt.args.defaultNetwork)
|
||||
if (err != nil) != tt.wantErr {
|
||||
if tt.wantErrExp != "" {
|
||||
assert.Regexp(t, tt.wantErrExp, err)
|
||||
return
|
||||
}
|
||||
t.Errorf("Networking.generateCustomCnoConfig() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("Networking.generateCustomCnoConfig() got =>\n[%v],\n want =>\n[%v]", string(got), string(tt.want))
|
||||
}
|
||||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user