327 lines
9.0 KiB
Go
327 lines
9.0 KiB
Go
//
|
|
// Copyright (c) 2012-2019 Red Hat, Inc.
|
|
// This program and the accompanying materials are made
|
|
// available under the terms of the Eclipse Public License 2.0
|
|
// which is available at https://www.eclipse.org/legal/epl-2.0/
|
|
//
|
|
// SPDX-License-Identifier: EPL-2.0
|
|
//
|
|
// Contributors:
|
|
// Red Hat, Inc. - initial API and implementation
|
|
//
|
|
package server
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/eclipse/che-operator/pkg/deploy"
|
|
corev1 "k8s.io/api/core/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/client-go/kubernetes/scheme"
|
|
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
|
"sigs.k8s.io/controller-runtime/pkg/log/zap"
|
|
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
|
|
|
|
orgv1 "github.com/eclipse/che-operator/pkg/apis/org/v1"
|
|
"github.com/eclipse/che-operator/pkg/util"
|
|
)
|
|
|
|
func TestNewCheConfigMap(t *testing.T) {
|
|
type testCase struct {
|
|
name string
|
|
isOpenShift bool
|
|
isOpenShift4 bool
|
|
initObjects []runtime.Object
|
|
cheCluster *orgv1.CheCluster
|
|
expectedData map[string]string
|
|
}
|
|
|
|
testCases := []testCase{
|
|
{
|
|
name: "Test",
|
|
initObjects: []runtime.Object{},
|
|
isOpenShift: true,
|
|
isOpenShift4: true,
|
|
cheCluster: &orgv1.CheCluster{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: "eclipse-che",
|
|
},
|
|
Spec: orgv1.CheClusterSpec{
|
|
Server: orgv1.CheClusterSpecServer{
|
|
CheHost: "myhostname.com",
|
|
TlsSupport: true,
|
|
CustomCheProperties: map[string]string{
|
|
"CHE_WORKSPACE_NO_PROXY": "myproxy.myhostname.com",
|
|
},
|
|
},
|
|
Auth: orgv1.CheClusterSpecAuth{
|
|
OpenShiftoAuth: util.NewBoolPointer(true),
|
|
},
|
|
},
|
|
},
|
|
expectedData: map[string]string{
|
|
"CHE_INFRA_OPENSHIFT_OAUTH__IDENTITY__PROVIDER": "openshift-v4",
|
|
"CHE_API": "https://myhostname.com/api",
|
|
"CHE_WORKSPACE_NO_PROXY": "myproxy.myhostname.com",
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
logf.SetLogger(zap.LoggerTo(os.Stdout, true))
|
|
orgv1.SchemeBuilder.AddToScheme(scheme.Scheme)
|
|
testCase.initObjects = append(testCase.initObjects)
|
|
cli := fake.NewFakeClientWithScheme(scheme.Scheme, testCase.initObjects...)
|
|
nonCachedClient := fake.NewFakeClientWithScheme(scheme.Scheme, testCase.initObjects...)
|
|
|
|
deployContext := &deploy.DeployContext{
|
|
CheCluster: testCase.cheCluster,
|
|
ClusterAPI: deploy.ClusterAPI{
|
|
Client: cli,
|
|
NonCachedClient: nonCachedClient,
|
|
Scheme: scheme.Scheme,
|
|
},
|
|
Proxy: &deploy.Proxy{},
|
|
}
|
|
|
|
util.IsOpenShift = testCase.isOpenShift
|
|
util.IsOpenShift4 = testCase.isOpenShift4
|
|
|
|
actualData, err := GetCheConfigMapData(deployContext)
|
|
if err != nil {
|
|
t.Fatalf("Error creating ConfigMap data: %v", err)
|
|
}
|
|
|
|
util.ValidateContainData(actualData, testCase.expectedData, t)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConfigMap(t *testing.T) {
|
|
type testCase struct {
|
|
name string
|
|
isOpenShift bool
|
|
isOpenShift4 bool
|
|
initObjects []runtime.Object
|
|
cheCluster *orgv1.CheCluster
|
|
expectedData map[string]string
|
|
}
|
|
|
|
testCases := []testCase{
|
|
{
|
|
name: "Test k8s data, no tls secret",
|
|
isOpenShift: false,
|
|
isOpenShift4: false,
|
|
initObjects: []runtime.Object{},
|
|
cheCluster: &orgv1.CheCluster{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: "eclipse-che",
|
|
},
|
|
Spec: orgv1.CheClusterSpec{
|
|
Server: orgv1.CheClusterSpecServer{
|
|
WorkspaceNamespaceDefault: "<username>-che",
|
|
},
|
|
},
|
|
},
|
|
expectedData: map[string]string{
|
|
"CHE_INFRA_KUBERNETES_TLS__CERT": "",
|
|
"CHE_INFRA_KUBERNETES_TLS__KEY": "",
|
|
},
|
|
},
|
|
{
|
|
name: "Test k8s data, with tls secret",
|
|
isOpenShift: false,
|
|
isOpenShift4: false,
|
|
initObjects: []runtime.Object{
|
|
&corev1.Secret{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "che-tls",
|
|
Namespace: "eclipse-che",
|
|
},
|
|
Data: map[string][]byte{
|
|
"tls.crt": []byte("CRT"),
|
|
"tls.key": []byte("KEY"),
|
|
},
|
|
},
|
|
},
|
|
cheCluster: &orgv1.CheCluster{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: "eclipse-che",
|
|
},
|
|
Spec: orgv1.CheClusterSpec{
|
|
Server: orgv1.CheClusterSpecServer{
|
|
WorkspaceNamespaceDefault: "<username>-che",
|
|
},
|
|
K8s: orgv1.CheClusterSpecK8SOnly{
|
|
TlsSecretName: "che-tls",
|
|
},
|
|
},
|
|
},
|
|
expectedData: map[string]string{
|
|
"CHE_INFRA_KUBERNETES_TLS__CERT": "CRT",
|
|
"CHE_INFRA_KUBERNETES_TLS__KEY": "KEY",
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
logf.SetLogger(zap.LoggerTo(os.Stdout, true))
|
|
orgv1.SchemeBuilder.AddToScheme(scheme.Scheme)
|
|
testCase.initObjects = append(testCase.initObjects)
|
|
cli := fake.NewFakeClientWithScheme(scheme.Scheme, testCase.initObjects...)
|
|
nonCachedClient := fake.NewFakeClientWithScheme(scheme.Scheme, testCase.initObjects...)
|
|
|
|
deployContext := &deploy.DeployContext{
|
|
CheCluster: testCase.cheCluster,
|
|
ClusterAPI: deploy.ClusterAPI{
|
|
Client: cli,
|
|
NonCachedClient: nonCachedClient,
|
|
Scheme: scheme.Scheme,
|
|
},
|
|
Proxy: &deploy.Proxy{},
|
|
}
|
|
|
|
util.IsOpenShift = testCase.isOpenShift
|
|
util.IsOpenShift4 = testCase.isOpenShift4
|
|
|
|
actualData, err := GetCheConfigMapData(deployContext)
|
|
if err != nil {
|
|
t.Fatalf("Error creating ConfigMap data: %v", err)
|
|
}
|
|
|
|
util.ValidateContainData(actualData, testCase.expectedData, t)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUpdateBitBucketEndpoints(t *testing.T) {
|
|
type testCase struct {
|
|
name string
|
|
initObjects []runtime.Object
|
|
cheCluster *orgv1.CheCluster
|
|
expectedData map[string]string
|
|
}
|
|
|
|
testCases := []testCase{
|
|
{
|
|
name: "Test set BitBucket endpoints from secret",
|
|
initObjects: []runtime.Object{
|
|
&corev1.Secret{
|
|
TypeMeta: metav1.TypeMeta{
|
|
Kind: "Secret",
|
|
APIVersion: "v1",
|
|
},
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "github-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": "bitbucket_endpoint_2",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
cheCluster: &orgv1.CheCluster{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: "eclipse-che",
|
|
},
|
|
},
|
|
expectedData: map[string]string{
|
|
"CHE_INTEGRATION_BITBUCKET_SERVER__ENDPOINTS": "bitbucket_endpoint_2",
|
|
},
|
|
},
|
|
{
|
|
name: "Test update BitBucket endpoints",
|
|
initObjects: []runtime.Object{
|
|
&corev1.Secret{
|
|
TypeMeta: metav1.TypeMeta{
|
|
Kind: "Secret",
|
|
APIVersion: "v1",
|
|
},
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "github-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": "bitbucket_endpoint_2",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
cheCluster: &orgv1.CheCluster{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: "eclipse-che",
|
|
},
|
|
Spec: orgv1.CheClusterSpec{
|
|
Server: orgv1.CheClusterSpecServer{
|
|
CustomCheProperties: map[string]string{
|
|
"CHE_INTEGRATION_BITBUCKET_SERVER__ENDPOINTS": "bitbucket_endpoint_1",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expectedData: map[string]string{
|
|
"CHE_INTEGRATION_BITBUCKET_SERVER__ENDPOINTS": "bitbucket_endpoint_1,bitbucket_endpoint_2",
|
|
},
|
|
},
|
|
{
|
|
name: "Test don't update BitBucket endpoints",
|
|
initObjects: []runtime.Object{},
|
|
cheCluster: &orgv1.CheCluster{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: "eclipse-che",
|
|
},
|
|
Spec: orgv1.CheClusterSpec{
|
|
Server: orgv1.CheClusterSpecServer{
|
|
CustomCheProperties: map[string]string{
|
|
"CHE_INTEGRATION_BITBUCKET_SERVER__ENDPOINTS": "bitbucket_endpoint_1",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expectedData: map[string]string{
|
|
"CHE_INTEGRATION_BITBUCKET_SERVER__ENDPOINTS": "bitbucket_endpoint_1",
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
logf.SetLogger(zap.LoggerTo(os.Stdout, true))
|
|
orgv1.SchemeBuilder.AddToScheme(scheme.Scheme)
|
|
testCase.initObjects = append(testCase.initObjects)
|
|
cli := fake.NewFakeClientWithScheme(scheme.Scheme, testCase.initObjects...)
|
|
nonCachedClient := fake.NewFakeClientWithScheme(scheme.Scheme, testCase.initObjects...)
|
|
|
|
deployContext := &deploy.DeployContext{
|
|
CheCluster: testCase.cheCluster,
|
|
ClusterAPI: deploy.ClusterAPI{
|
|
Client: cli,
|
|
NonCachedClient: nonCachedClient,
|
|
Scheme: scheme.Scheme,
|
|
},
|
|
Proxy: &deploy.Proxy{},
|
|
}
|
|
|
|
actualData, err := GetCheConfigMapData(deployContext)
|
|
if err != nil {
|
|
t.Fatalf("Error creating ConfigMap data: %v", err)
|
|
}
|
|
|
|
util.ValidateContainData(actualData, testCase.expectedData, t)
|
|
})
|
|
}
|
|
}
|