github.com/openshift/installer@v1.4.17/pkg/asset/manifests/network_test.go (about)

     1  package manifests
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"k8s.io/utils/ptr"
    10  
    11  	configv1 "github.com/openshift/api/config/v1"
    12  	operatorv1 "github.com/openshift/api/operator/v1"
    13  	"github.com/openshift/installer/pkg/asset"
    14  	"github.com/openshift/installer/pkg/asset/installconfig"
    15  	"github.com/openshift/installer/pkg/types"
    16  	awstypes "github.com/openshift/installer/pkg/types/aws"
    17  )
    18  
    19  const stubDefaultNetworkConfigOVNWithMTU = `apiVersion: operator.openshift.io/v1
    20  kind: Network
    21  metadata:
    22    creationTimestamp: null
    23    name: cluster
    24  spec:
    25    clusterNetwork: null
    26    defaultNetwork:
    27      ovnKubernetesConfig:
    28        egressIPConfig: {}
    29        mtu: 1000
    30      type: OVNKubernetes
    31    disableNetworkDiagnostics: false
    32    managementState: Managed
    33    observedConfig: null
    34    serviceNetwork: null
    35    unsupportedConfigOverrides: null
    36  status:
    37    readyReplicas: 0
    38  `
    39  
    40  func validInstallConfigWithMTU(ic *types.InstallConfig) *installconfig.InstallConfig {
    41  	return &installconfig.InstallConfig{
    42  		AssetBase: installconfig.AssetBase{
    43  			Config: ic,
    44  		},
    45  	}
    46  }
    47  
    48  func TestNetworking_GenerateCustomNetworkConfigMTU(t *testing.T) {
    49  	type args struct {
    50  		ic *installconfig.InstallConfig
    51  	}
    52  	tests := []struct {
    53  		name       string
    54  		no         *Networking
    55  		args       args
    56  		want       *operatorv1.DefaultNetworkDefinition
    57  		wantErr    bool
    58  		wantErrExp string
    59  	}{
    60  		{
    61  			name:    "mtu unset OVNKubernetes",
    62  			no:      &Networking{},
    63  			args:    args{ic: &installconfig.InstallConfig{}},
    64  			wantErr: false,
    65  		}, {
    66  			name: "mtu unset OVNKubernetes edge defaults",
    67  			no:   &Networking{},
    68  			args: args{ic: validInstallConfigWithMTU(&types.InstallConfig{
    69  				Networking: &types.Networking{
    70  					NetworkType: "OVNKubernetes",
    71  				},
    72  				Compute: []types.MachinePool{
    73  					{
    74  						Name: "edge",
    75  					},
    76  				},
    77  				Platform: types.Platform{AWS: &awstypes.Platform{}},
    78  			})},
    79  			want: &operatorv1.DefaultNetworkDefinition{
    80  				Type: operatorv1.NetworkTypeOVNKubernetes,
    81  				OVNKubernetesConfig: &operatorv1.OVNKubernetesConfig{
    82  					MTU: ptr.To(uint32(1200)),
    83  				},
    84  			},
    85  		}, {
    86  			name: "mtu set OVNKubernetes",
    87  			no:   &Networking{},
    88  			args: args{ic: validInstallConfigWithMTU(&types.InstallConfig{
    89  				Networking: &types.Networking{
    90  					ClusterNetworkMTU: 1500,
    91  					NetworkType:       "OVNKubernetes",
    92  				},
    93  				Platform: types.Platform{AWS: &awstypes.Platform{}},
    94  			})},
    95  			want: &operatorv1.DefaultNetworkDefinition{
    96  				Type: operatorv1.NetworkTypeOVNKubernetes,
    97  				OVNKubernetesConfig: &operatorv1.OVNKubernetesConfig{
    98  					MTU: ptr.To(uint32(1500)),
    99  				},
   100  			},
   101  		}, {
   102  			name: "mtu set OVNKubernetes edge",
   103  			no:   &Networking{},
   104  			args: args{ic: validInstallConfigWithMTU(&types.InstallConfig{
   105  				Networking: &types.Networking{
   106  					ClusterNetworkMTU: 1500,
   107  					NetworkType:       "OVNKubernetes",
   108  				},
   109  				Compute: []types.MachinePool{
   110  					{
   111  						Name: "edge",
   112  					},
   113  				},
   114  				Platform: types.Platform{AWS: &awstypes.Platform{}},
   115  			})},
   116  			want: &operatorv1.DefaultNetworkDefinition{
   117  				Type: operatorv1.NetworkTypeOVNKubernetes,
   118  				OVNKubernetesConfig: &operatorv1.OVNKubernetesConfig{
   119  					MTU: ptr.To(uint32(1500)),
   120  				},
   121  			},
   122  		}, {
   123  			name: "mtu set OVNKubernetes edge high",
   124  			no:   &Networking{},
   125  			args: args{ic: validInstallConfigWithMTU(&types.InstallConfig{
   126  				Networking: &types.Networking{
   127  					ClusterNetworkMTU: 8000,
   128  					NetworkType:       "OVNKubernetes",
   129  				},
   130  				Compute: []types.MachinePool{
   131  					{
   132  						Name: "edge",
   133  					},
   134  				},
   135  				Platform: types.Platform{AWS: &awstypes.Platform{}},
   136  			})},
   137  			want: &operatorv1.DefaultNetworkDefinition{
   138  				Type: operatorv1.NetworkTypeOVNKubernetes,
   139  				OVNKubernetesConfig: &operatorv1.OVNKubernetesConfig{
   140  					MTU: ptr.To(uint32(8000)),
   141  				},
   142  			},
   143  		},
   144  	}
   145  	for _, tt := range tests {
   146  		t.Run(tt.name, func(t *testing.T) {
   147  			no := &Networking{
   148  				Config:   tt.no.Config,
   149  				FileList: tt.no.FileList,
   150  			}
   151  			got, err := no.GenerateCustomNetworkConfigMTU(tt.args.ic)
   152  			if (err != nil) != tt.wantErr {
   153  				if tt.wantErrExp != "" {
   154  					assert.Regexp(t, tt.wantErrExp, err)
   155  					return
   156  				}
   157  				t.Errorf("Networking.GenerateCustomNetworkConfigMTU() error = %v, wantErr %v", err, tt.wantErr)
   158  				return
   159  			}
   160  			if !reflect.DeepEqual(got, tt.want) {
   161  				dumpDN := ""
   162  				if got.OVNKubernetesConfig != nil {
   163  					t.Errorf("Networking.GenerateCustomNetworkConfigMTU() = %v (%v), want %v", got, *got.OVNKubernetesConfig, tt.want)
   164  					dumpDN = fmt.Sprintf("%v", *got.OVNKubernetesConfig)
   165  					if got.OVNKubernetesConfig.MTU != nil {
   166  						dumpDN += fmt.Sprintf(" mtu[%v]", *got.OVNKubernetesConfig.MTU)
   167  					}
   168  				} else if got.OpenShiftSDNConfig != nil {
   169  					t.Errorf("Networking.GenerateCustomNetworkConfigMTU() = %v (%v), want %v", got, *got.OpenShiftSDNConfig, tt.want)
   170  					dumpDN = fmt.Sprintf("%v", *got.OpenShiftSDNConfig)
   171  					if got.OpenShiftSDNConfig.MTU != nil {
   172  						dumpDN += fmt.Sprintf(" mtu[%v]", *got.OpenShiftSDNConfig.MTU)
   173  					}
   174  				}
   175  
   176  				t.Errorf("Networking.GenerateCustomNetworkConfigMTU() = %v (debug: %s), want %v", got, dumpDN, tt.want)
   177  			}
   178  		})
   179  	}
   180  }
   181  
   182  func TestNetworking_generateCustomCnoConfig(t *testing.T) {
   183  	type fields struct {
   184  		Config   *configv1.Network
   185  		FileList []*asset.File
   186  	}
   187  	type args struct {
   188  		defaultNetwork *operatorv1.DefaultNetworkDefinition
   189  	}
   190  	tests := []struct {
   191  		name       string
   192  		fields     fields
   193  		args       args
   194  		want       []byte
   195  		wantErr    bool
   196  		wantErrExp string
   197  	}{
   198  		{
   199  			name:   "custom OVNKubernetes",
   200  			fields: fields{},
   201  			args: args{defaultNetwork: &operatorv1.DefaultNetworkDefinition{
   202  				Type: operatorv1.NetworkTypeOVNKubernetes,
   203  				OVNKubernetesConfig: &operatorv1.OVNKubernetesConfig{
   204  					MTU: ptr.To(uint32(1000)),
   205  				},
   206  			}},
   207  			want: []byte(stubDefaultNetworkConfigOVNWithMTU),
   208  		}, {
   209  			name:       "invalid config",
   210  			fields:     fields{},
   211  			args:       args{defaultNetwork: nil},
   212  			wantErr:    true,
   213  			wantErrExp: `defaultNetwork must be specified`,
   214  		},
   215  	}
   216  	for _, tt := range tests {
   217  		t.Run(tt.name, func(t *testing.T) {
   218  			no := &Networking{
   219  				Config:   tt.fields.Config,
   220  				FileList: tt.fields.FileList,
   221  			}
   222  			got, err := no.generateCustomCnoConfig(tt.args.defaultNetwork)
   223  			if (err != nil) != tt.wantErr {
   224  				if tt.wantErrExp != "" {
   225  					assert.Regexp(t, tt.wantErrExp, err)
   226  					return
   227  				}
   228  				t.Errorf("Networking.generateCustomCnoConfig() error = %v, wantErr %v", err, tt.wantErr)
   229  				return
   230  			}
   231  			if !reflect.DeepEqual(got, tt.want) {
   232  				t.Errorf("Networking.generateCustomCnoConfig() got =>\n[%v],\n want =>\n[%v]", string(got), string(tt.want))
   233  			}
   234  		})
   235  	}
   236  }