config_test.go 6.8 KB
Newer Older
H
hongming 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
Copyright 2020 The KubeSphere Authors.

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.
*/

17 18 19 20
package config

import (
	"fmt"
21
	"github.com/google/go-cmp/cmp"
22 23
	"gopkg.in/yaml.v2"
	"io/ioutil"
24 25
	"kubesphere.io/kubesphere/pkg/apiserver/authentication/oauth"
	authoptions "kubesphere.io/kubesphere/pkg/apiserver/authentication/options"
H
hongming 已提交
26
	authorizationoptions "kubesphere.io/kubesphere/pkg/apiserver/authorization/options"
J
Jeff 已提交
27
	"kubesphere.io/kubesphere/pkg/simple/client/alerting"
R
root 已提交
28
	auditingclient "kubesphere.io/kubesphere/pkg/simple/client/auditing/elasticsearch"
Z
zryfish 已提交
29
	"kubesphere.io/kubesphere/pkg/simple/client/cache"
R
runzexia 已提交
30
	"kubesphere.io/kubesphere/pkg/simple/client/devops/jenkins"
J
junotx 已提交
31
	eventsclient "kubesphere.io/kubesphere/pkg/simple/client/events/elasticsearch"
32 33
	"kubesphere.io/kubesphere/pkg/simple/client/k8s"
	"kubesphere.io/kubesphere/pkg/simple/client/ldap"
G
Guangzhe Huang 已提交
34
	"kubesphere.io/kubesphere/pkg/simple/client/logging/elasticsearch"
G
Guangzhe Huang 已提交
35
	"kubesphere.io/kubesphere/pkg/simple/client/monitoring/prometheus"
Z
zryfish 已提交
36
	"kubesphere.io/kubesphere/pkg/simple/client/multicluster"
37
	"kubesphere.io/kubesphere/pkg/simple/client/network"
J
Jeff 已提交
38
	"kubesphere.io/kubesphere/pkg/simple/client/notification"
39
	"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
Z
zryfish 已提交
40
	"kubesphere.io/kubesphere/pkg/simple/client/s3"
41 42 43 44 45 46 47
	"kubesphere.io/kubesphere/pkg/simple/client/servicemesh"
	"kubesphere.io/kubesphere/pkg/simple/client/sonarqube"
	"os"
	"testing"
	"time"
)

H
update  
hongming 已提交
48 49
func newTestConfig() (*Config, error) {

50
	var conf = &Config{
R
runzexia 已提交
51
		DevopsOptions: &jenkins.Options{
52 53 54 55 56
			Host:           "http://ks-devops.kubesphere-devops-system.svc",
			Username:       "jenkins",
			Password:       "kubesphere",
			MaxConnections: 10,
		},
Z
zryfish 已提交
57
		SonarQubeOptions: &sonarqube.Options{
58 59 60 61 62 63 64 65 66
			Host:  "http://sonarqube.kubesphere-devops-system.svc",
			Token: "ABCDEFG",
		},
		KubernetesOptions: &k8s.KubernetesOptions{
			KubeConfig: "/Users/zry/.kube/config",
			Master:     "https://127.0.0.1:6443",
			QPS:        1e6,
			Burst:      1e6,
		},
Z
zryfish 已提交
67
		ServiceMeshOptions: &servicemesh.Options{
68 69 70 71
			IstioPilotHost:            "http://istio-pilot.istio-system.svc:9090",
			JaegerQueryHost:           "http://jaeger-query.istio-system.svc:80",
			ServicemeshPrometheusHost: "http://prometheus-k8s.kubesphere-monitoring-system.svc",
		},
Z
zryfish 已提交
72
		LdapOptions: &ldap.Options{
73 74 75 76 77 78
			Host:            "http://openldap.kubesphere-system.svc",
			ManagerDN:       "cn=admin,dc=example,dc=org",
			ManagerPassword: "P@88w0rd",
			UserSearchBase:  "ou=Users,dc=example,dc=org",
			GroupSearchBase: "ou=Groups,dc=example,dc=org",
		},
Z
zryfish 已提交
79
		RedisOptions: &cache.Options{
H
update  
hongming 已提交
80
			Host:     "localhost",
Z
zryfish 已提交
81 82 83
			Port:     6379,
			Password: "P@88w0rd",
			DB:       0,
84
		},
Z
zryfish 已提交
85
		S3Options: &s3.Options{
86
			Endpoint:        "http://minio.openpitrix-system.svc",
J
Jeff 已提交
87 88
			Region:          "",
			DisableSSL:      false,
89 90 91 92 93 94
			ForcePathStyle:  false,
			AccessKeyID:     "ABCDEFGHIJKLMN",
			SecretAccessKey: "OPQRSTUVWXYZ",
			SessionToken:    "abcdefghijklmn",
			Bucket:          "ssss",
		},
Z
zryfish 已提交
95
		OpenPitrixOptions: &openpitrix.Options{
H
hongming 已提交
96 97 98 99 100 101
			RuntimeManagerEndpoint:    "openpitrix-hyperpitrix.openpitrix-system.svc:9103",
			ClusterManagerEndpoint:    "openpitrix-hyperpitrix.openpitrix-system.svc:9104",
			RepoManagerEndpoint:       "openpitrix-hyperpitrix.openpitrix-system.svc:9101",
			AppManagerEndpoint:        "openpitrix-hyperpitrix.openpitrix-system.svc:9102",
			CategoryManagerEndpoint:   "openpitrix-hyperpitrix.openpitrix-system.svc:9113",
			AttachmentManagerEndpoint: "openpitrix-hyperpitrix.openpitrix-system.svc:9122",
102
		},
103 104 105
		NetworkOptions: &network.Options{
			WeaveScopeHost: "weave-scope-app.weave.svc",
		},
Z
zryfish 已提交
106
		MonitoringOptions: &prometheus.Options{
107
			Endpoint: "http://prometheus.kubesphere-monitoring-system.svc",
108
		},
G
Guangzhe Huang 已提交
109
		LoggingOptions: &elasticsearch.Options{
H
huanggze 已提交
110 111 112
			Host:        "http://elasticsearch-logging.kubesphere-logging-system.svc:9200",
			IndexPrefix: "elk",
			Version:     "6",
J
Jeff 已提交
113
		},
Z
zryfish 已提交
114
		AlertingOptions: &alerting.Options{
Z
zryfish 已提交
115
			Endpoint: "http://alerting-client-server.kubesphere-alerting-system.svc:9200/api",
J
Jeff 已提交
116
		},
Z
zryfish 已提交
117
		NotificationOptions: &notification.Options{
J
Jeff 已提交
118 119
			Endpoint: "http://notification.kubesphere-alerting-system.svc:9200",
		},
H
hongming 已提交
120
		AuthorizationOptions: authorizationoptions.NewAuthorizationOptions(),
121
		AuthenticationOptions: &authoptions.AuthenticationOptions{
122 123 124
			AuthenticateRateLimiterMaxTries: 5,
			AuthenticateRateLimiterDuration: 30 * time.Minute,
			MaxAuthenticateRetries:          6,
125
			JwtSecret:                       "xxxxxx",
126
			MultipleLogin:                   false,
127
			OAuthOptions: &oauth.Options{
H
hongming 已提交
128
				IdentityProviders: []oauth.IdentityProviderOptions{},
129
				Clients: []oauth.Client{{
H
update  
hongming 已提交
130 131 132 133 134 135
					Name:                         "kubesphere-console-client",
					Secret:                       "xxxxxx-xxxxxx-xxxxxx",
					RespondWithChallenges:        true,
					RedirectURIs:                 []string{"http://ks-console.kubesphere-system.svc/oauth/token/implicit"},
					GrantMethod:                  oauth.GrantHandlerAuto,
					AccessTokenInactivityTimeout: nil,
136
				}},
H
update  
hongming 已提交
137 138
				AccessTokenMaxAge:            time.Hour * 24,
				AccessTokenInactivityTimeout: 0,
139
			},
140
		},
Z
zryfish 已提交
141 142 143
		MultiClusterOptions: &multicluster.Options{
			Enable: false,
		},
J
junotx 已提交
144 145 146 147 148
		EventsOptions: &eventsclient.Options{
			Host:        "http://elasticsearch-logging-data.kubesphere-logging-system.svc:9200",
			IndexPrefix: "ks-logstash-events",
			Version:     "6",
		},
R
root 已提交
149 150 151 152 153
		AuditingOptions: &auditingclient.Options{
			Host:        "http://elasticsearch-logging-data.kubesphere-logging-system.svc:9200",
			IndexPrefix: "ks-logstash-auditing",
			Version:     "6",
		},
154
	}
H
update  
hongming 已提交
155
	return conf, nil
156 157 158 159 160 161 162
}

func saveTestConfig(t *testing.T, conf *Config) {
	content, err := yaml.Marshal(conf)
	if err != nil {
		t.Fatalf("error marshal config. %v", err)
	}
163
	err = ioutil.WriteFile(fmt.Sprintf("%s.yaml", defaultConfigurationName), content, 0640)
164 165 166 167 168 169
	if err != nil {
		t.Fatalf("error write configuration file, %v", err)
	}
}

func cleanTestConfig(t *testing.T) {
170
	file := fmt.Sprintf("%s.yaml", defaultConfigurationName)
171 172 173 174 175 176 177 178 179 180 181 182 183
	if _, err := os.Stat(file); os.IsNotExist(err) {
		t.Log("file not exists, skipping")
		return
	}

	err := os.Remove(file)
	if err != nil {
		t.Fatalf("remove %s file failed", file)
	}

}

func TestGet(t *testing.T) {
H
update  
hongming 已提交
184 185 186 187
	conf, err := newTestConfig()
	if err != nil {
		t.Fatal(err)
	}
188
	saveTestConfig(t, conf)
J
Jeff 已提交
189
	defer cleanTestConfig(t)
190

191
	conf2, err := TryLoadFromDisk()
192 193 194
	if err != nil {
		t.Fatal(err)
	}
H
update  
hongming 已提交
195
	if diff := cmp.Diff(conf, conf2); diff != "" {
J
Jeff 已提交
196
		t.Fatal(diff)
197
	}
J
Jeff 已提交
198
}