2015-10-21 14:41:42 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-10-21 14:41:42 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package kubectl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2017-10-28 01:31:42 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2017-01-17 03:38:19 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2015-10-21 14:41:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSecretForDockerRegistryGenerate(t *testing.T) {
|
|
|
|
username, password, email, server := "test-user", "test-password", "test-user@example.org", "https://index.docker.io/v1/"
|
2017-12-20 17:05:36 +00:00
|
|
|
secretData, err := handleDockerCfgJsonContent(username, password, email, server)
|
2015-10-21 14:41:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
2017-12-20 17:05:36 +00:00
|
|
|
secretDataNoEmail, err := handleDockerCfgJsonContent(username, password, "", server)
|
2017-02-27 22:12:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
2015-10-21 14:41:42 +00:00
|
|
|
|
2018-05-12 09:24:28 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2015-10-21 14:41:42 +00:00
|
|
|
params map[string]interface{}
|
2017-10-28 01:31:42 +00:00
|
|
|
expected *v1.Secret
|
2015-10-21 14:41:42 +00:00
|
|
|
expectErr bool
|
|
|
|
}{
|
2018-05-12 09:24:28 +00:00
|
|
|
{
|
|
|
|
name: "test-valid-use",
|
2015-10-21 14:41:42 +00:00
|
|
|
params: map[string]interface{}{
|
|
|
|
"name": "foo",
|
|
|
|
"docker-server": server,
|
|
|
|
"docker-username": username,
|
|
|
|
"docker-password": password,
|
|
|
|
"docker-email": email,
|
|
|
|
},
|
2017-10-28 01:31:42 +00:00
|
|
|
expected: &v1.Secret{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-10-21 14:41:42 +00:00
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
Data: map[string][]byte{
|
2017-12-20 17:05:36 +00:00
|
|
|
v1.DockerConfigJsonKey: secretData,
|
2015-10-21 14:41:42 +00:00
|
|
|
},
|
2017-12-20 17:05:36 +00:00
|
|
|
Type: v1.SecretTypeDockerConfigJson,
|
2015-10-21 14:41:42 +00:00
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
},
|
2018-05-12 09:24:28 +00:00
|
|
|
{
|
|
|
|
name: "test-valid-use-append-hash",
|
2017-07-28 17:18:08 +00:00
|
|
|
params: map[string]interface{}{
|
|
|
|
"name": "foo",
|
|
|
|
"docker-server": server,
|
|
|
|
"docker-username": username,
|
|
|
|
"docker-password": password,
|
|
|
|
"docker-email": email,
|
|
|
|
"append-hash": true,
|
|
|
|
},
|
2017-10-28 01:31:42 +00:00
|
|
|
expected: &v1.Secret{
|
2017-07-28 17:18:08 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-12-20 17:05:36 +00:00
|
|
|
Name: "foo-548cm7fgdh",
|
2017-07-28 17:18:08 +00:00
|
|
|
},
|
|
|
|
Data: map[string][]byte{
|
2017-12-20 17:05:36 +00:00
|
|
|
v1.DockerConfigJsonKey: secretData,
|
2017-07-28 17:18:08 +00:00
|
|
|
},
|
2017-12-20 17:05:36 +00:00
|
|
|
Type: v1.SecretTypeDockerConfigJson,
|
2017-07-28 17:18:08 +00:00
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
},
|
2018-05-12 09:24:28 +00:00
|
|
|
{
|
|
|
|
name: "test-valid-use-no-email",
|
2017-02-27 22:12:16 +00:00
|
|
|
params: map[string]interface{}{
|
|
|
|
"name": "foo",
|
|
|
|
"docker-server": server,
|
|
|
|
"docker-username": username,
|
|
|
|
"docker-password": password,
|
|
|
|
},
|
2017-10-28 01:31:42 +00:00
|
|
|
expected: &v1.Secret{
|
2017-02-27 22:12:16 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
Data: map[string][]byte{
|
2017-12-20 17:05:36 +00:00
|
|
|
v1.DockerConfigJsonKey: secretDataNoEmail,
|
2017-02-27 22:12:16 +00:00
|
|
|
},
|
2017-12-20 17:05:36 +00:00
|
|
|
Type: v1.SecretTypeDockerConfigJson,
|
2017-02-27 22:12:16 +00:00
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
},
|
2018-05-12 09:24:28 +00:00
|
|
|
{
|
|
|
|
name: "test-missing-required-param",
|
2015-10-21 14:41:42 +00:00
|
|
|
params: map[string]interface{}{
|
|
|
|
"name": "foo",
|
|
|
|
"docker-server": server,
|
|
|
|
"docker-password": password,
|
|
|
|
"docker-email": email,
|
|
|
|
},
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
generator := SecretForDockerRegistryGeneratorV1{}
|
2018-05-12 09:24:28 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
obj, err := generator.Generate(tt.params)
|
|
|
|
if !tt.expectErr && err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if tt.expectErr && err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(obj.(*v1.Secret), tt.expected) {
|
|
|
|
t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", tt.expected, obj.(*v1.Secret))
|
|
|
|
}
|
|
|
|
})
|
2015-10-21 14:41:42 +00:00
|
|
|
}
|
|
|
|
}
|