|
| 1 | +package manifests |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + "sigs.k8s.io/yaml" |
| 10 | + |
| 11 | + "github.com/openshift/installer/pkg/asset" |
| 12 | + "github.com/openshift/installer/pkg/asset/installconfig" |
| 13 | + "github.com/openshift/installer/pkg/types" |
| 14 | + "github.com/openshift/installer/pkg/types/network" |
| 15 | +) |
| 16 | + |
| 17 | +func TestGenerateCloudProviderConfig(t *testing.T) { |
| 18 | + cases := []struct { |
| 19 | + name string |
| 20 | + installConfig *types.InstallConfig |
| 21 | + expectedCloudCfg map[string]string |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "aws: empty ipFamily - default IPv4", |
| 25 | + installConfig: icBuild.build( |
| 26 | + icBuild.forAWS(), |
| 27 | + icBuild.withAWSRegion("us-east-1"), |
| 28 | + ), |
| 29 | + expectedCloudCfg: map[string]string{ |
| 30 | + cloudProviderConfigDataKey: `[Global] |
| 31 | +`, |
| 32 | + }, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "aws: ipFamily IPv4", |
| 36 | + installConfig: icBuild.build( |
| 37 | + icBuild.forAWS(), |
| 38 | + icBuild.withAWSRegion("us-east-1"), |
| 39 | + icBuild.withAWSIPFamily(network.IPv4), |
| 40 | + ), |
| 41 | + expectedCloudCfg: map[string]string{ |
| 42 | + cloudProviderConfigDataKey: `[Global] |
| 43 | +`, |
| 44 | + }, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "aws: ipFamily DualStackIPv4Primary", |
| 48 | + installConfig: icBuild.build( |
| 49 | + icBuild.forAWS(), |
| 50 | + icBuild.withAWSRegion("us-east-1"), |
| 51 | + icBuild.withAWSIPFamily(network.DualStackIPv4Primary), |
| 52 | + ), |
| 53 | + expectedCloudCfg: map[string]string{ |
| 54 | + cloudProviderConfigDataKey: `[Global] |
| 55 | +NodeIPFamilies=ipv4 |
| 56 | +NodeIPFamilies=ipv6 |
| 57 | +`, |
| 58 | + }, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "aws: ipFamily DualStackIPv6Primary", |
| 62 | + installConfig: icBuild.build( |
| 63 | + icBuild.forAWS(), |
| 64 | + icBuild.withAWSRegion("us-east-1"), |
| 65 | + icBuild.withAWSIPFamily(network.DualStackIPv6Primary), |
| 66 | + ), |
| 67 | + expectedCloudCfg: map[string]string{ |
| 68 | + cloudProviderConfigDataKey: `[Global] |
| 69 | +NodeIPFamilies=ipv6 |
| 70 | +NodeIPFamilies=ipv4 |
| 71 | +`, |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + for _, tc := range cases { |
| 77 | + t.Run(tc.name, func(t *testing.T) { |
| 78 | + parents := asset.Parents{} |
| 79 | + parents.Add( |
| 80 | + installconfig.MakeAsset(tc.installConfig), |
| 81 | + &installconfig.ClusterID{ |
| 82 | + InfraID: "test-infra-id", |
| 83 | + }, |
| 84 | + ) |
| 85 | + |
| 86 | + cloudConfig := &CloudProviderConfig{} |
| 87 | + |
| 88 | + err := cloudConfig.Generate(context.Background(), parents) |
| 89 | + if !assert.NoError(t, err, "failed to generate asset") { |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + if tc.expectedCloudCfg == nil { |
| 94 | + assert.Nil(t, cloudConfig.ConfigMap) |
| 95 | + assert.Len(t, cloudConfig.Files(), 0) |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + if !assert.NotNil(t, cloudConfig.ConfigMap) { |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + assert.Equal(t, "openshift-config", cloudConfig.ConfigMap.Namespace) |
| 104 | + assert.Equal(t, "cloud-provider-config", cloudConfig.ConfigMap.Name) |
| 105 | + assert.Equal(t, tc.expectedCloudCfg, cloudConfig.ConfigMap.Data) |
| 106 | + |
| 107 | + if !assert.Len(t, cloudConfig.Files(), 1, "expected 1 file to be generated") { |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + assert.Equal(t, cloudConfig.Files()[0].Filename, cloudProviderConfigFileName) |
| 112 | + |
| 113 | + var actualCloudConfig corev1.ConfigMap |
| 114 | + err = yaml.Unmarshal(cloudConfig.Files()[0].Data, &actualCloudConfig) |
| 115 | + if !assert.NoError(t, err, "failed to unmarshal cloud-provider-config manifest") { |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + assert.Equal(t, tc.expectedCloudCfg, actualCloudConfig.Data) |
| 120 | + }) |
| 121 | + } |
| 122 | +} |
0 commit comments