github.com/openshift/installer@v1.4.17/pkg/asset/agent/manifests/agentpullsecret_test.go (about) 1 package manifests 2 3 import ( 4 "context" 5 "os" 6 "testing" 7 8 "github.com/golang/mock/gomock" 9 "github.com/pkg/errors" 10 "github.com/stretchr/testify/assert" 11 corev1 "k8s.io/api/core/v1" 12 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 13 "k8s.io/apimachinery/pkg/util/yaml" 14 15 "github.com/openshift/installer/pkg/asset" 16 "github.com/openshift/installer/pkg/asset/agent" 17 "github.com/openshift/installer/pkg/asset/agent/joiner" 18 "github.com/openshift/installer/pkg/asset/agent/workflow" 19 "github.com/openshift/installer/pkg/asset/mock" 20 ) 21 22 func TestAgentPullSecret_Generate(t *testing.T) { 23 cases := []struct { 24 name string 25 dependencies []asset.Asset 26 expectedError string 27 expectedConfig *corev1.Secret 28 }{ 29 { 30 name: "retrieved from cluster", 31 dependencies: []asset.Asset{ 32 &agent.OptionalInstallConfig{}, 33 &workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeAddNodes}, 34 &joiner.ClusterInfo{ 35 ClusterName: "ostest", 36 Namespace: "cluster0", 37 PullSecret: "{\n \"auths\": {\n \"cloud.openshift.com\": {\n \"auth\": \"b3BlUTA=\",\n \"email\": \"test@redhat.com\"\n }\n }\n}", 38 }, 39 }, 40 expectedConfig: &corev1.Secret{ 41 TypeMeta: v1.TypeMeta{ 42 Kind: "Secret", 43 APIVersion: "v1", 44 }, 45 ObjectMeta: v1.ObjectMeta{ 46 Name: "ostest-pull-secret", 47 Namespace: "cluster0", 48 }, 49 StringData: map[string]string{ 50 ".dockerconfigjson": "{\n \"auths\": {\n \"cloud.openshift.com\": {\n \"auth\": \"b3BlUTA=\",\n \"email\": \"test@redhat.com\"\n }\n }\n}", 51 }, 52 }, 53 }, 54 { 55 name: "missing install config", 56 dependencies: []asset.Asset{ 57 &agent.OptionalInstallConfig{}, &workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall}, &joiner.ClusterInfo{}, 58 }, 59 expectedError: "missing configuration or manifest file", 60 }, 61 { 62 name: "valid configuration", 63 dependencies: []asset.Asset{ 64 getValidOptionalInstallConfig(), &workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall}, &joiner.ClusterInfo{}, 65 }, 66 expectedConfig: &corev1.Secret{ 67 TypeMeta: v1.TypeMeta{ 68 Kind: "Secret", 69 APIVersion: "v1", 70 }, 71 ObjectMeta: v1.ObjectMeta{ 72 Name: getPullSecretName(getValidOptionalInstallConfig().ClusterName()), 73 Namespace: getValidOptionalInstallConfig().ClusterNamespace(), 74 }, 75 StringData: map[string]string{ 76 ".dockerconfigjson": "{\n \"auths\": {\n \"cloud.openshift.com\": {\n \"auth\": \"b3BlUTA=\",\n \"email\": \"test@redhat.com\"\n }\n }\n}", 77 }, 78 }, 79 }, 80 } 81 for _, tc := range cases { 82 t.Run(tc.name, func(t *testing.T) { 83 84 parents := asset.Parents{} 85 parents.Add(tc.dependencies...) 86 87 asset := &AgentPullSecret{} 88 err := asset.Generate(context.Background(), parents) 89 90 if tc.expectedError != "" { 91 assert.Equal(t, tc.expectedError, err.Error()) 92 } else { 93 assert.NoError(t, err) 94 assert.Equal(t, tc.expectedConfig, asset.Config) 95 assert.NotEmpty(t, asset.Files()) 96 97 configFile := asset.Files()[0] 98 assert.Equal(t, "cluster-manifests/pull-secret.yaml", configFile.Filename) 99 100 var actualConfig corev1.Secret 101 err = yaml.Unmarshal(configFile.Data, &actualConfig) 102 assert.NoError(t, err) 103 assert.Equal(t, *tc.expectedConfig, actualConfig) 104 } 105 }) 106 } 107 } 108 109 func TestAgentPullSecret_LoadedFromDisk(t *testing.T) { 110 111 cases := []struct { 112 name string 113 data string 114 fetchError error 115 expectedFound bool 116 expectedError string 117 expectedConfig *corev1.Secret 118 }{ 119 { 120 name: "valid-config-file", 121 data: ` 122 apiVersion: v1 123 kind: Secret 124 metadata: 125 name: pull-secret 126 namespace: cluster-0 127 stringData: 128 .dockerconfigjson: '{"auths":{"cloud.test":{"auth":"c3VwZXItc2VjcmV0Cg=="}}}'`, 129 expectedFound: true, 130 expectedConfig: &corev1.Secret{ 131 TypeMeta: v1.TypeMeta{ 132 Kind: "Secret", 133 APIVersion: "v1", 134 }, 135 ObjectMeta: v1.ObjectMeta{ 136 Name: "pull-secret", 137 Namespace: "cluster-0", 138 }, 139 StringData: map[string]string{ 140 ".dockerconfigjson": "{\n \"auths\": {\n \"cloud.test\": {\n \"auth\": \"c3VwZXItc2VjcmV0Cg==\"\n }\n }\n}", 141 }, 142 }, 143 }, 144 { 145 name: "not-yaml", 146 data: `This is not a yaml file`, 147 expectedError: "failed to unmarshal cluster-manifests/pull-secret.yaml: error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type v1.Secret", 148 }, 149 { 150 name: "empty", 151 data: "", 152 expectedError: "invalid PullSecret configuration: StringData: Required value: the pull secret is empty", 153 }, 154 { 155 name: "missing-string-data", 156 data: ` 157 apiVersion: v1 158 kind: Secret 159 metadata: 160 name: pull-secret 161 namespace: cluster-0`, 162 expectedError: "invalid PullSecret configuration: StringData: Required value: the pull secret is empty", 163 }, 164 { 165 name: "missing-secret-key", 166 data: ` 167 apiVersion: v1 168 kind: Secret 169 metadata: 170 name: pull-secret 171 namespace: cluster-0 172 stringData: 173 .dockerconfigjson:`, 174 expectedError: "invalid PullSecret configuration: StringData: Required value: the pull secret does not contain any data", 175 }, 176 { 177 name: "bad-secret-format", 178 data: ` 179 apiVersion: v1 180 kind: Secret 181 metadata: 182 name: pull-secret 183 namespace: cluster-0 184 stringData: 185 .dockerconfigjson: 'foo'`, 186 expectedError: "invalid PullSecret configuration: StringData: Invalid value: \"foo\": invalid character 'o' in literal false (expecting 'a')", 187 }, 188 { 189 name: "file-not-found", 190 fetchError: &os.PathError{Err: os.ErrNotExist}, 191 }, 192 { 193 name: "error-fetching-file", 194 fetchError: errors.New("fetch failed"), 195 expectedError: "failed to load cluster-manifests/pull-secret.yaml file: fetch failed", 196 }, 197 { 198 name: "unknown-field", 199 data: ` 200 metadata: 201 name: pull-secret 202 namespace: cluster0 203 spec: 204 wrongField: wrongValue`, 205 expectedError: "failed to unmarshal cluster-manifests/pull-secret.yaml: error converting YAML to JSON: yaml: line 2: found character that cannot start any token", 206 }, 207 } 208 for _, tc := range cases { 209 t.Run(tc.name, func(t *testing.T) { 210 211 mockCtrl := gomock.NewController(t) 212 defer mockCtrl.Finish() 213 214 fileFetcher := mock.NewMockFileFetcher(mockCtrl) 215 fileFetcher.EXPECT().FetchByName(agentPullSecretFilename). 216 Return( 217 &asset.File{ 218 Filename: agentPullSecretFilename, 219 Data: []byte(tc.data)}, 220 tc.fetchError, 221 ) 222 223 asset := &AgentPullSecret{} 224 found, err := asset.Load(fileFetcher) 225 assert.Equal(t, tc.expectedFound, found, "unexpected found value returned from Load") 226 if tc.expectedError != "" { 227 assert.Equal(t, tc.expectedError, err.Error()) 228 } else { 229 assert.NoError(t, err) 230 } 231 if tc.expectedFound { 232 assert.Equal(t, tc.expectedConfig, asset.Config, "unexpected Config in AgentPullSecret") 233 } 234 }) 235 } 236 237 }