add ut test

This commit is contained in:
runzexia
2020-03-27 10:52:39 +08:00
parent af3b87ddca
commit f411bec10b
2 changed files with 137 additions and 5 deletions

View File

@@ -90,8 +90,8 @@ func newSecret(namespace, name string, data map[string][]byte, withFinalizers bo
if withFinalizers {
secret.Finalizers = append(secret.Finalizers, devops.CredentialFinalizerName)
}
if autoSync{
if secret.Annotations == nil{
if autoSync {
if secret.Annotations == nil {
secret.Annotations = map[string]string{}
}
secret.Annotations[devops.CredentialAutoSyncAnnoKey] = "true"
@@ -380,7 +380,7 @@ func TestUpdateCredential(t *testing.T) {
ns := newNamespace(nsName, projectName)
initSecret := newSecret(nsName, secretName, nil, true, true)
expectSecret := newSecret(nsName, secretName, map[string][]byte{"a":[]byte("aa")}, true, true)
expectSecret := newSecret(nsName, secretName, map[string][]byte{"a": []byte("aa")}, true, true)
f.secretLister = append(f.secretLister, expectSecret)
f.namespaceLister = append(f.namespaceLister, ns)
f.kubeobjects = append(f.kubeobjects, expectSecret)
@@ -398,7 +398,7 @@ func TestNotUpdateCredential(t *testing.T) {
ns := newNamespace(nsName, projectName)
initSecret := newSecret(nsName, secretName, nil, true, false)
expectSecret := newSecret(nsName, secretName, map[string][]byte{"a":[]byte("aa")}, true, false)
expectSecret := newSecret(nsName, secretName, map[string][]byte{"a": []byte("aa")}, true, false)
f.secretLister = append(f.secretLister, expectSecret)
f.namespaceLister = append(f.namespaceLister, ns)
f.kubeobjects = append(f.kubeobjects, expectSecret)
@@ -407,4 +407,3 @@ func TestNotUpdateCredential(t *testing.T) {
f.expectCredential = []*v1.Secret{initSecret}
f.run(getKey(expectSecret, t))
}

View File

@@ -0,0 +1,133 @@
package jenkins
import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"
)
func TestNewUsernamePasswordCredential(t *testing.T) {
username := "test-user"
password := "password"
name := "test-secret"
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: "test",
},
Data: map[string][]byte{
"username": []byte(username),
"password": []byte(password),
},
Type: "credential.devops.kubesphere.io/basic-auth",
}
credential := NewUsernamePasswordCredential(secret)
if credential.StaplerClass != "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl" {
t.Fatalf("credential's stapler class should be com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"+
"other than %s ", credential.StaplerClass)
}
if credential.Id != name {
t.Fatalf("credential's id should be %s "+
"other than %s ", name, credential.Id)
}
if credential.Username != username {
t.Fatalf("credential's username should be %s "+
"other than %s ", username, credential.Username)
}
if credential.Password != password {
t.Fatalf("credential's password should be %s "+
"other than %s ", password, credential.Password)
}
}
func TestNewSshCredential(t *testing.T) {
username := "test-user"
passphrase := "passphrase"
privatekey := "pk"
name := "test-secret"
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: "test",
},
Data: map[string][]byte{
"username": []byte(username),
"passphrase": []byte(passphrase),
"privatekey": []byte(privatekey),
},
Type: "credential.devops.kubesphere.io/ssh-auth",
}
credential := NewSshCredential(secret)
if credential.StaplerClass != "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey" {
t.Fatalf("credential's stapler class should be com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey"+
"other than %s ", credential.StaplerClass)
}
if credential.Id != name {
t.Fatalf("credential's id should be %s "+
"other than %s ", name, credential.Id)
}
if credential.Username != username {
t.Fatalf("credential's username should be %s "+
"other than %s ", username, credential.Username)
}
if credential.Passphrase != passphrase {
t.Fatalf("credential's passphrase should be %s "+
"other than %s ", passphrase, credential.Passphrase)
}
if credential.KeySource.PrivateKey != privatekey {
t.Fatalf("credential's privatekey should be %s "+
"other than %s ", privatekey, credential.KeySource.PrivateKey)
}
}
func TestNewKubeconfigCredential(t *testing.T) {
content := []byte("test-content")
name := "test-secret"
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: "test",
},
Type: "credential.devops.kubesphere.io/kubeconfig",
Data: map[string][]byte{"secret": content},
}
credential := NewKubeconfigCredential(secret)
if credential.StaplerClass != "com.microsoft.jenkins.kubernetes.credentials.KubeconfigCredentials" {
t.Fatalf("credential's stapler class should be com.microsoft.jenkins.kubernetes.credentials.KubeconfigCredentials"+
"other than %s ", credential.StaplerClass)
}
if credential.Id != name {
t.Fatalf("credential's id should be %s "+
"other than %s ", name, credential.Id)
}
if credential.KubeconfigSource.Content != string(content) {
t.Fatalf("credential's content should be %s "+
"other than %s ", string(content), credential.KubeconfigSource.Content)
}
}
func TestNewSecretTextCredential(t *testing.T) {
content := []byte("test-content")
name := "test-secret"
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: "test",
},
Type: "credential.devops.kubesphere.io/secret-text",
Data: map[string][]byte{"secret": content},
}
credential := NewSecretTextCredential(secret)
if credential.StaplerClass != "org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl" {
t.Fatalf("credential's stapler class should be org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl"+
"other than %s ", credential.StaplerClass)
}
if credential.Id != name {
t.Fatalf("credential's id should be %s "+
"other than %s ", name, credential.Id)
}
if credential.Secret != string(content) {
t.Fatalf("credential's content should be %s "+
"other than %s ", string(content), credential.Secret)
}
}