diff --git a/pkg/deploy/server/server_deployment.go b/pkg/deploy/server/server_deployment.go index 1438d5310..c9f795563 100644 --- a/pkg/deploy/server/server_deployment.go +++ b/pkg/deploy/server/server_deployment.go @@ -318,8 +318,12 @@ func MountBitBucketOAuthConfig(ctx *chetypes.DeployContext, deployment *appsv1.D mountEnv(deployment, "CHE_OAUTH1_BITBUCKET_CONSUMERKEYPATH", constants.BitBucketOAuthConfigMountPath+"/"+constants.BitBucketOAuthConfigConsumerKeyFileName) mountEnv(deployment, "CHE_OAUTH1_BITBUCKET_PRIVATEKEYPATH", constants.BitBucketOAuthConfigMountPath+"/"+constants.BitBucketOAuthConfigPrivateKeyFileName) - mountEnv(deployment, "CHE_OAUTH2_BITBUCKET_CLIENTID__FILEPATH", constants.BitBucketOAuthConfigMountPath+"/"+constants.BitBucketOAuthConfigClientIdFileName) - mountEnv(deployment, "CHE_OAUTH2_BITBUCKET_CLIENTSECRET__FILEPATH", constants.BitBucketOAuthConfigMountPath+"/"+constants.BitBucketOAuthConfigClientSecretFileName) + if secret.Data["id"] != nil { + mountEnv(deployment, "CHE_OAUTH2_BITBUCKET_CLIENTID__FILEPATH", constants.BitBucketOAuthConfigMountPath+"/"+constants.BitBucketOAuthConfigClientIdFileName) + } + if secret.Data["secret"] != nil { + mountEnv(deployment, "CHE_OAUTH2_BITBUCKET_CLIENTSECRET__FILEPATH", constants.BitBucketOAuthConfigMountPath+"/"+constants.BitBucketOAuthConfigClientSecretFileName) + } oauthEndpoint := secret.Annotations[constants.CheEclipseOrgScmServerEndpoint] if oauthEndpoint != "" { diff --git a/pkg/deploy/server/server_deployment_test.go b/pkg/deploy/server/server_deployment_test.go index 020cddc3e..58698642d 100644 --- a/pkg/deploy/server/server_deployment_test.go +++ b/pkg/deploy/server/server_deployment_test.go @@ -131,7 +131,7 @@ func TestDeployment(t *testing.T) { } } -func TestMountBitBucketOAuthEnvVar(t *testing.T) { +func TestMountBitBucketServerOAuthEnvVar(t *testing.T) { type testCase struct { name string initObjects []runtime.Object @@ -152,7 +152,7 @@ func TestMountBitBucketOAuthEnvVar(t *testing.T) { APIVersion: "v1", }, ObjectMeta: metav1.ObjectMeta{ - Name: "github-oauth-config", + Name: "bitbucket-oauth-config", Namespace: "eclipse-che", Labels: map[string]string{ "app.kubernetes.io/part-of": "che.eclipse.org", @@ -173,15 +173,15 @@ func TestMountBitBucketOAuthEnvVar(t *testing.T) { expectedPrivateKeyPath: "/che-conf/oauth/bitbucket/private.key", expectedOAuthEndpoint: "endpoint_1", expectedVolume: corev1.Volume{ - Name: "github-oauth-config", + Name: "bitbucket-oauth-config", VolumeSource: corev1.VolumeSource{ Secret: &corev1.SecretVolumeSource{ - SecretName: "github-oauth-config", + SecretName: "bitbucket-oauth-config", }, }, }, expectedVolumeMount: corev1.VolumeMount{ - Name: "github-oauth-config", + Name: "bitbucket-oauth-config", MountPath: "/che-conf/oauth/bitbucket", }, }, @@ -206,11 +206,93 @@ func TestMountBitBucketOAuthEnvVar(t *testing.T) { value = utils.GetEnvByName("CHE_OAUTH1_BITBUCKET_ENDPOINT", container.Env) assert.Equal(t, testCase.expectedOAuthEndpoint, value) - volume := test.FindVolume(deployment.Spec.Template.Spec.Volumes, "github-oauth-config") + volume := test.FindVolume(deployment.Spec.Template.Spec.Volumes, "bitbucket-oauth-config") assert.NotNil(t, volume) assert.Equal(t, testCase.expectedVolume, volume) - volumeMount := test.FindVolumeMount(container.VolumeMounts, "github-oauth-config") + volumeMount := test.FindVolumeMount(container.VolumeMounts, "bitbucket-oauth-config") + assert.NotNil(t, volumeMount) + assert.Equal(t, testCase.expectedVolumeMount, volumeMount) + }) + } +} + +func TestMountBitbucketOAuthEnvVar(t *testing.T) { + type testCase struct { + name string + initObjects []runtime.Object + expectedIdKeyPath string + expectedSecretKeyPath string + expectedOAuthEndpoint string + expectedVolume corev1.Volume + expectedVolumeMount corev1.VolumeMount + } + + testCases := []testCase{ + { + name: "Test", + initObjects: []runtime.Object{ + &corev1.Secret{ + TypeMeta: metav1.TypeMeta{ + Kind: "Secret", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "bitbucket-oauth-config", + Namespace: "eclipse-che", + Labels: map[string]string{ + "app.kubernetes.io/part-of": "che.eclipse.org", + "app.kubernetes.io/component": "oauth-scm-configuration", + }, + Annotations: map[string]string{ + "che.eclipse.org/oauth-scm-server": "bitbucket", + "che.eclipse.org/scm-server-endpoint": "endpoint_1", + }, + }, + Data: map[string][]byte{ + "id": []byte("some_id"), + "secret": []byte("some_secret"), + }, + }, + }, + expectedIdKeyPath: "/che-conf/oauth/bitbucket/id", + expectedSecretKeyPath: "/che-conf/oauth/bitbucket/secret", + expectedVolume: corev1.Volume{ + Name: "bitbucket-oauth-config", + VolumeSource: corev1.VolumeSource{ + Secret: &corev1.SecretVolumeSource{ + SecretName: "bitbucket-oauth-config", + }, + }, + }, + expectedVolumeMount: corev1.VolumeMount{ + Name: "bitbucket-oauth-config", + MountPath: "/che-conf/oauth/bitbucket", + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + ctx := test.GetDeployContext(nil, testCase.initObjects) + + server := NewCheServerReconciler() + deployment, err := server.getDeploymentSpec(ctx) + assert.Nil(t, err, "Unexpected error %v", err) + + container := &deployment.Spec.Template.Spec.Containers[0] + + value := utils.GetEnvByName("CHE_OAUTH2_BITBUCKET_CLIENTID__FILEPATH", container.Env) + assert.Equal(t, testCase.expectedIdKeyPath, value) + + value = utils.GetEnvByName("CHE_OAUTH2_BITBUCKET_CLIENTSECRET__FILEPATH", container.Env) + assert.Equal(t, testCase.expectedSecretKeyPath, value) + + volume := test.FindVolume(deployment.Spec.Template.Spec.Volumes, "bitbucket-oauth-config") + assert.NotNil(t, volume) + assert.Equal(t, testCase.expectedVolume, volume) + + volumeMount := test.FindVolumeMount(container.VolumeMounts, "bitbucket-oauth-config") assert.NotNil(t, volumeMount) assert.Equal(t, testCase.expectedVolumeMount, volumeMount) })