/
infrastructure_test.go
181 lines (163 loc) · 5.12 KB
/
infrastructure_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package manifests
import (
"testing"
"github.com/ghodss/yaml"
configv1 "github.com/openshift/api/config/v1"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/openshift/installer/pkg/asset"
"github.com/openshift/installer/pkg/asset/installconfig"
"github.com/openshift/installer/pkg/types"
awstypes "github.com/openshift/installer/pkg/types/aws"
)
func TestGenerateInfrastructe(t *testing.T) {
cases := []struct {
name string
installConfig *types.InstallConfig
expectedInfrastructure *configv1.Infrastructure
}{{
name: "vanilla aws",
installConfig: icBuild.build(icBuild.forAWS()),
expectedInfrastructure: infraBuild.build(
infraBuild.forPlatform(configv1.AWSPlatformType),
infraBuild.withAWSPlatformSpec(),
infraBuild.withAWSPlatformStatus(),
),
}, {
name: "service endpoints",
installConfig: icBuild.build(
icBuild.forAWS(),
icBuild.withServiceEndpoint("service", "https://endpoint"),
),
expectedInfrastructure: infraBuild.build(
infraBuild.forPlatform(configv1.AWSPlatformType),
infraBuild.withServiceEndpoint("service", "https://endpoint"),
),
}}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
parents := asset.Parents{}
parents.Add(
&installconfig.ClusterID{
UUID: "test-uuid",
InfraID: "test-infra-id",
},
&installconfig.InstallConfig{Config: tc.installConfig},
&CloudProviderConfig{},
&AdditionalTrustBundleConfig{},
)
infraAsset := &Infrastructure{}
err := infraAsset.Generate(parents)
if !assert.NoError(t, err, "failed to generate asset") {
return
}
if !assert.Len(t, infraAsset.FileList, 1, "expected only one file to be generated") {
return
}
assert.Equal(t, infraAsset.FileList[0].Filename, "manifests/cluster-infrastructure-02-config.yml")
var actualInfra configv1.Infrastructure
err = yaml.Unmarshal(infraAsset.FileList[0].Data, &actualInfra)
if !assert.NoError(t, err, "failed to unmarshal infra manifest") {
return
}
assert.Equal(t, tc.expectedInfrastructure, &actualInfra)
})
}
}
type icOption func(*types.InstallConfig)
type icBuildNamespace struct{}
var icBuild icBuildNamespace
func (icBuildNamespace) build(opts ...icOption) *types.InstallConfig {
ic := &types.InstallConfig{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
},
BaseDomain: "test-domain",
ControlPlane: &types.MachinePool{},
}
for _, opt := range opts {
opt(ic)
}
return ic
}
func (b icBuildNamespace) forAWS() icOption {
return func(ic *types.InstallConfig) {
if ic.Platform.AWS != nil {
return
}
ic.Platform.AWS = &awstypes.Platform{}
}
}
func (b icBuildNamespace) withServiceEndpoint(name, url string) icOption {
return func(ic *types.InstallConfig) {
b.forAWS()(ic)
ic.Platform.AWS.ServiceEndpoints = append(
ic.Platform.AWS.ServiceEndpoints,
awstypes.ServiceEndpoint{
Name: name,
URL: url,
},
)
}
}
type infraOption func(*configv1.Infrastructure)
type infraBuildNamespace struct{}
var infraBuild infraBuildNamespace
func (b infraBuildNamespace) build(opts ...infraOption) *configv1.Infrastructure {
infra := &configv1.Infrastructure{
TypeMeta: metav1.TypeMeta{
APIVersion: configv1.SchemeGroupVersion.String(),
Kind: "Infrastructure",
},
ObjectMeta: metav1.ObjectMeta{
Name: "cluster",
},
Spec: configv1.InfrastructureSpec{
PlatformSpec: configv1.PlatformSpec{},
},
Status: configv1.InfrastructureStatus{
InfrastructureName: "test-infra-id",
APIServerURL: "https://api.test-cluster.test-domain:6443",
APIServerInternalURL: "https://api-int.test-cluster.test-domain:6443",
ControlPlaneTopology: configv1.HighlyAvailableTopologyMode,
InfrastructureTopology: configv1.HighlyAvailableTopologyMode,
PlatformStatus: &configv1.PlatformStatus{},
},
}
for _, opt := range opts {
opt(infra)
}
return infra
}
func (b infraBuildNamespace) forPlatform(platform configv1.PlatformType) infraOption {
return func(infra *configv1.Infrastructure) {
infra.Spec.PlatformSpec.Type = platform
infra.Status.PlatformStatus.Type = platform
infra.Status.Platform = platform
}
}
func (b infraBuildNamespace) withAWSPlatformSpec() infraOption {
return func(infra *configv1.Infrastructure) {
if infra.Spec.PlatformSpec.AWS != nil {
return
}
infra.Spec.PlatformSpec.AWS = &configv1.AWSPlatformSpec{}
}
}
func (b infraBuildNamespace) withAWSPlatformStatus() infraOption {
return func(infra *configv1.Infrastructure) {
if infra.Status.PlatformStatus.AWS != nil {
return
}
infra.Status.PlatformStatus.AWS = &configv1.AWSPlatformStatus{}
}
}
func (b infraBuildNamespace) withServiceEndpoint(name, url string) infraOption {
return func(infra *configv1.Infrastructure) {
b.withAWSPlatformSpec()(infra)
b.withAWSPlatformStatus()(infra)
endpoint := configv1.AWSServiceEndpoint{Name: name, URL: url}
infra.Spec.PlatformSpec.AWS.ServiceEndpoints = append(infra.Spec.PlatformSpec.AWS.ServiceEndpoints, endpoint)
infra.Status.PlatformStatus.AWS.ServiceEndpoints = append(infra.Status.PlatformStatus.AWS.ServiceEndpoints, endpoint)
}
}