|
| 1 | +package joiner |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/openshift/assisted-service/api/hiveextension/v1beta1" |
| 7 | + fakeclientconfig "github.com/openshift/client-go/config/clientset/versioned/fake" |
| 8 | + "gopkg.in/yaml.v2" |
| 9 | + corev1 "k8s.io/api/core/v1" |
| 10 | + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/runtime" |
| 12 | + |
| 13 | + "k8s.io/client-go/kubernetes/fake" |
| 14 | + "k8s.io/client-go/rest" |
| 15 | + |
| 16 | + configv1 "github.com/openshift/api/config/v1" |
| 17 | + "github.com/openshift/installer/pkg/asset" |
| 18 | + "github.com/openshift/installer/pkg/asset/agent/workflow" |
| 19 | + "github.com/openshift/installer/pkg/types" |
| 20 | + "github.com/openshift/installer/pkg/types/baremetal" |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | +) |
| 23 | + |
| 24 | +func TestClusterInfo_Generate(t *testing.T) { |
| 25 | + cases := []struct { |
| 26 | + name string |
| 27 | + workflow workflow.AgentWorkflowType |
| 28 | + objects []runtime.Object |
| 29 | + openshiftOjects []runtime.Object |
| 30 | + configHost string |
| 31 | + expectedClusterInfo ClusterInfo |
| 32 | + }{ |
| 33 | + { |
| 34 | + name: "skip if not add-nodes workflow", |
| 35 | + workflow: workflow.AgentWorkflowTypeInstall, |
| 36 | + expectedClusterInfo: ClusterInfo{}, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "default", |
| 40 | + workflow: workflow.AgentWorkflowTypeAddNodes, |
| 41 | + configHost: "https://api.ostest.test.metalkube.org:6443", |
| 42 | + openshiftOjects: []runtime.Object{ |
| 43 | + &configv1.ClusterVersion{ |
| 44 | + ObjectMeta: v1.ObjectMeta{ |
| 45 | + Name: "version", |
| 46 | + }, |
| 47 | + Spec: configv1.ClusterVersionSpec{ |
| 48 | + ClusterID: "1b5ba46b-7e56-47b1-a326-a9eebddfb38c", |
| 49 | + }, |
| 50 | + Status: configv1.ClusterVersionStatus{ |
| 51 | + History: []configv1.UpdateHistory{ |
| 52 | + { |
| 53 | + Image: "registry.ci.openshift.org/ocp/release@sha256:65d9b652d0d23084bc45cb66001c22e796d43f5e9e005c2bc2702f94397d596e", |
| 54 | + Version: "4.15.0", |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + &configv1.Proxy{ |
| 60 | + ObjectMeta: v1.ObjectMeta{ |
| 61 | + Name: "cluster", |
| 62 | + }, |
| 63 | + Spec: configv1.ProxySpec{ |
| 64 | + HTTPProxy: "http://proxy", |
| 65 | + HTTPSProxy: "https://proxy", |
| 66 | + NoProxy: "localhost", |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + objects: []runtime.Object{ |
| 71 | + &corev1.Secret{ |
| 72 | + ObjectMeta: v1.ObjectMeta{ |
| 73 | + Name: "pull-secret", |
| 74 | + Namespace: "openshift-config", |
| 75 | + }, |
| 76 | + Data: map[string][]byte{ |
| 77 | + ".dockerconfigjson": []byte("c3VwZXJzZWNyZXQK"), |
| 78 | + }, |
| 79 | + }, |
| 80 | + &corev1.ConfigMap{ |
| 81 | + ObjectMeta: v1.ObjectMeta{ |
| 82 | + Name: "user-ca-bundle", |
| 83 | + Namespace: "openshift-config", |
| 84 | + }, |
| 85 | + Data: map[string]string{ |
| 86 | + "ca-bundle.crt": "--- bundle ---", |
| 87 | + }, |
| 88 | + }, |
| 89 | + &corev1.Node{ |
| 90 | + ObjectMeta: v1.ObjectMeta{ |
| 91 | + Labels: map[string]string{ |
| 92 | + "node-role.kubernetes.io/master": "", |
| 93 | + }, |
| 94 | + }, |
| 95 | + Status: corev1.NodeStatus{ |
| 96 | + NodeInfo: corev1.NodeSystemInfo{ |
| 97 | + Architecture: "amd64", |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + &corev1.ConfigMap{ |
| 102 | + ObjectMeta: v1.ObjectMeta{ |
| 103 | + Name: "cluster-config-v1", |
| 104 | + Namespace: "kube-system", |
| 105 | + }, |
| 106 | + Data: map[string]string{ |
| 107 | + "install-config": makeInstallConfig(t), |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + expectedClusterInfo: ClusterInfo{ |
| 112 | + ClusterID: "1b5ba46b-7e56-47b1-a326-a9eebddfb38c", |
| 113 | + ReleaseImage: "registry.ci.openshift.org/ocp/release@sha256:65d9b652d0d23084bc45cb66001c22e796d43f5e9e005c2bc2702f94397d596e", |
| 114 | + Version: "4.15.0", |
| 115 | + APIDNSName: "api.ostest.test.metalkube.org", |
| 116 | + Namespace: "cluster0", |
| 117 | + PullSecret: "c3VwZXJzZWNyZXQK", |
| 118 | + UserCaBundle: "--- bundle ---", |
| 119 | + Architecture: "amd64", |
| 120 | + Proxy: &types.Proxy{ |
| 121 | + HTTPProxy: "http://proxy", |
| 122 | + HTTPSProxy: "https://proxy", |
| 123 | + NoProxy: "localhost", |
| 124 | + }, |
| 125 | + ImageDigestSources: []types.ImageDigestSource{ |
| 126 | + { |
| 127 | + Source: "quay.io/openshift-release-dev/ocp-v4.0-art-dev", |
| 128 | + Mirrors: []string{ |
| 129 | + "registry.example.com:5000/ocp4/openshift4", |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + PlatformType: v1beta1.BareMetalPlatformType, |
| 134 | + SSHKey: "my-ssh-key", |
| 135 | + }, |
| 136 | + }, |
| 137 | + } |
| 138 | + for _, tc := range cases { |
| 139 | + t.Run(tc.name, func(t *testing.T) { |
| 140 | + agentWorkflow := &workflow.AgentWorkflow{Workflow: tc.workflow} |
| 141 | + addNodesConfig := &AddNodesConfig{} |
| 142 | + parents := asset.Parents{} |
| 143 | + parents.Add(agentWorkflow) |
| 144 | + parents.Add(addNodesConfig) |
| 145 | + |
| 146 | + fakeClient := fake.NewSimpleClientset(tc.objects...) |
| 147 | + fakeOCClient := fakeclientconfig.NewSimpleClientset(tc.openshiftOjects...) |
| 148 | + fakeConfig := &rest.Config{ |
| 149 | + Host: tc.configHost, |
| 150 | + } |
| 151 | + |
| 152 | + clusterInfo := &ClusterInfo{ |
| 153 | + Config: fakeConfig, |
| 154 | + Client: fakeClient, |
| 155 | + OpenshiftClient: fakeOCClient, |
| 156 | + } |
| 157 | + err := clusterInfo.Generate(parents) |
| 158 | + |
| 159 | + assert.NoError(t, err) |
| 160 | + assert.Equal(t, tc.expectedClusterInfo.ClusterID, clusterInfo.ClusterID) |
| 161 | + assert.Equal(t, tc.expectedClusterInfo.Version, clusterInfo.Version) |
| 162 | + assert.Equal(t, tc.expectedClusterInfo.ReleaseImage, clusterInfo.ReleaseImage) |
| 163 | + assert.Equal(t, tc.expectedClusterInfo.APIDNSName, clusterInfo.APIDNSName) |
| 164 | + assert.Equal(t, tc.expectedClusterInfo.PullSecret, clusterInfo.PullSecret) |
| 165 | + assert.Equal(t, tc.expectedClusterInfo.Namespace, clusterInfo.Namespace) |
| 166 | + assert.Equal(t, tc.expectedClusterInfo.UserCaBundle, clusterInfo.UserCaBundle) |
| 167 | + assert.Equal(t, tc.expectedClusterInfo.Proxy, clusterInfo.Proxy) |
| 168 | + assert.Equal(t, tc.expectedClusterInfo.Architecture, clusterInfo.Architecture) |
| 169 | + assert.Equal(t, tc.expectedClusterInfo.ImageDigestSources, clusterInfo.ImageDigestSources) |
| 170 | + assert.Equal(t, tc.expectedClusterInfo.DeprecatedImageContentSources, clusterInfo.DeprecatedImageContentSources) |
| 171 | + assert.Equal(t, tc.expectedClusterInfo.PlatformType, clusterInfo.PlatformType) |
| 172 | + assert.Equal(t, tc.expectedClusterInfo.SSHKey, clusterInfo.SSHKey) |
| 173 | + }) |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +func makeInstallConfig(t *testing.T) string { |
| 178 | + ic := &types.InstallConfig{ |
| 179 | + ImageDigestSources: []types.ImageDigestSource{ |
| 180 | + { |
| 181 | + Source: "quay.io/openshift-release-dev/ocp-v4.0-art-dev", |
| 182 | + Mirrors: []string{ |
| 183 | + "registry.example.com:5000/ocp4/openshift4", |
| 184 | + }, |
| 185 | + }, |
| 186 | + }, |
| 187 | + Platform: types.Platform{ |
| 188 | + BareMetal: &baremetal.Platform{}, |
| 189 | + }, |
| 190 | + SSHKey: "my-ssh-key", |
| 191 | + } |
| 192 | + data, err := yaml.Marshal(ic) |
| 193 | + if err != nil { |
| 194 | + t.Error(err) |
| 195 | + } |
| 196 | + return string(data) |
| 197 | +} |
0 commit comments