github.com/jenkins-x/jx/v2@v2.1.155/pkg/kube/git_services_test.go (about) 1 // +build unit 2 3 package kube_test 4 5 import ( 6 "io/ioutil" 7 "path" 8 "testing" 9 10 v1fake "github.com/jenkins-x/jx-api/pkg/client/clientset/versioned/fake" 11 "github.com/jenkins-x/jx/v2/pkg/auth" 12 "github.com/jenkins-x/jx/v2/pkg/kube" 13 "github.com/jenkins-x/jx/v2/pkg/secreturl/fakevault" 14 "github.com/stretchr/testify/assert" 15 "github.com/stretchr/testify/require" 16 v1 "k8s.io/api/core/v1" 17 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 18 "k8s.io/client-go/kubernetes/fake" 19 "sigs.k8s.io/yaml" 20 ) 21 22 const serviceURL = "https://github.beescloud.com" 23 const secretName = kube.SecretJenkinsPipelineGitCredentials + "github-ghe" 24 const serviceKind = "github" 25 26 func createSecret(secretName string, labels map[string]string, annotations map[string]string) *v1.Secret { 27 return &v1.Secret{ 28 ObjectMeta: metav1.ObjectMeta{ 29 Name: secretName, 30 DeletionGracePeriodSeconds: nil, 31 Labels: labels, 32 Annotations: annotations, 33 }, 34 } 35 } 36 37 func TestGitServiceKindFromSecrets(t *testing.T) { 38 t.Parallel() 39 secret := createSecret(secretName, 40 map[string]string{ 41 "jenkins.io/service-kind": serviceKind, 42 }, 43 map[string]string{ 44 "jenkins.io/url": serviceURL, 45 }) 46 kubeClient := fake.NewSimpleClientset(secret) 47 48 foundServiceKind, err := kube.GetServiceKindFromSecrets(kubeClient, "", serviceURL) 49 50 assert.NoError(t, err, "should find a service kind without any error") 51 assert.Equal(t, serviceKind, foundServiceKind, "should find a service kind equal with '%s'", serviceKind) 52 } 53 54 func TestGitServiceKindFromSecretsWithMissingKind(t *testing.T) { 55 t.Parallel() 56 secret := createSecret("jx-pipeline-git", 57 map[string]string{ 58 "jenkins.io/kind": "git", 59 "jenkins.io/service-kind": "", 60 }, 61 map[string]string{ 62 "jenkins.io/url": serviceURL, 63 }) 64 ns := "jx" 65 secret.Namespace = ns 66 kubeClient := fake.NewSimpleClientset(secret) 67 jxClient := v1fake.NewSimpleClientset() 68 69 foundServiceKind, err := kube.GetGitServiceKind(jxClient, kubeClient, ns, nil, serviceURL) 70 71 t.Logf("found service kind %s for URL %s\n\n", foundServiceKind, serviceURL) 72 73 assert.NoError(t, err, "should find a service kind without any error") 74 assert.Equal(t, serviceKind, foundServiceKind, "should find a service kind equal with '%s'", serviceKind) 75 } 76 77 func TestGitServiceKindFromClusterAuthConfig(t *testing.T) { 78 t.Parallel() 79 80 ns := "jx" 81 expectedServiceKind := "bitbucketserver" 82 bbsServerURL := "https://bitbucket.example.com" 83 84 authFile := path.Join("test_data", "git_service_kind", "configmap_vault_auth.yaml") 85 data, err := ioutil.ReadFile(authFile) 86 require.NoError(t, err) 87 88 var expectedAuthConfig auth.AuthConfig 89 err = yaml.Unmarshal(data, &expectedAuthConfig) 90 require.NoError(t, err) 91 92 namespace := &v1.Namespace{ 93 ObjectMeta: metav1.ObjectMeta{ 94 Name: ns, 95 }, 96 } 97 config := &v1.ConfigMap{ 98 ObjectMeta: metav1.ObjectMeta{ 99 Name: "jx-auth", 100 Namespace: ns, 101 Labels: map[string]string{ 102 "jenkins.io/config-type": "auth", 103 }, 104 }, 105 Data: map[string]string{ 106 secretName: string(data), 107 }, 108 } 109 kubeClient := fake.NewSimpleClientset(namespace, config) 110 jxClient := v1fake.NewSimpleClientset() 111 configMapInterface := kubeClient.CoreV1().ConfigMaps(ns) 112 113 vaultClient := fakevault.NewFakeClient() 114 _, err = vaultClient.Write("test-cluster/pipelineUser", map[string]interface{}{"token": "test"}) 115 require.NoError(t, err) 116 expectedAuthConfig.Servers[0].Users[0].ApiToken = "test" 117 118 handler := auth.NewConfigMapVaultConfigHandler(secretName, configMapInterface, vaultClient) 119 authConfig, err := handler.LoadConfig() 120 121 assert.NoError(t, err, "auth config should be loaded") 122 assert.NotNil(t, authConfig, "auth config should be set") 123 assert.EqualValues(t, expectedAuthConfig, *authConfig) 124 125 foundServiceKind, err := kube.GetGitServiceKind(jxClient, kubeClient, ns, authConfig, bbsServerURL) 126 127 assert.NoError(t, err, "should find a service kind without any error") 128 assert.Equal(t, expectedServiceKind, foundServiceKind, "should find a service kind equal with '%s'", expectedServiceKind) 129 } 130 131 func TestGitServiceKindFromSecretsWithoutURL(t *testing.T) { 132 t.Parallel() 133 secret := createSecret(secretName, 134 map[string]string{ 135 "jenkins.io/service-kind": serviceKind, 136 }, 137 nil) 138 139 kubeClient := fake.NewSimpleClientset(secret) 140 foundServiceKind, err := kube.GetServiceKindFromSecrets(kubeClient, "", serviceURL) 141 142 assert.Error(t, err, "should not found a service kind") 143 assert.Equal(t, "", foundServiceKind, "should return no service kind") 144 } 145 146 func TestGitServiceKindFromSecretsWithoutKindLabel(t *testing.T) { 147 t.Parallel() 148 secret := createSecret(secretName, 149 map[string]string{ 150 "jenkins.io/service-kind": "test", 151 }, 152 map[string]string{ 153 "jenkins.io/url": "test", 154 }) 155 kubeClient := fake.NewSimpleClientset(secret) 156 157 foundServiceKind, err := kube.GetServiceKindFromSecrets(kubeClient, "", serviceURL) 158 159 assert.Error(t, err, "should not found a service kind") 160 assert.Equal(t, "", foundServiceKind, "should return no service kind") 161 }