config_test.go 6.9 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"
D
Duan Jiong 已提交
24
	networkv1alpha1 "kubesphere.io/kubesphere/pkg/apis/network/v1alpha1"
25 26
	"kubesphere.io/kubesphere/pkg/apiserver/authentication/oauth"
	authoptions "kubesphere.io/kubesphere/pkg/apiserver/authentication/options"
H
hongming 已提交
27
	authorizationoptions "kubesphere.io/kubesphere/pkg/apiserver/authorization/options"
J
Jeff 已提交
28
	"kubesphere.io/kubesphere/pkg/simple/client/alerting"
R
root 已提交
29
	auditingclient "kubesphere.io/kubesphere/pkg/simple/client/auditing/elasticsearch"
Z
zryfish 已提交
30
	"kubesphere.io/kubesphere/pkg/simple/client/cache"
R
runzexia 已提交
31
	"kubesphere.io/kubesphere/pkg/simple/client/devops/jenkins"
J
junotx 已提交
32
	eventsclient "kubesphere.io/kubesphere/pkg/simple/client/events/elasticsearch"
33 34
	"kubesphere.io/kubesphere/pkg/simple/client/k8s"
	"kubesphere.io/kubesphere/pkg/simple/client/ldap"
G
Guangzhe Huang 已提交
35
	"kubesphere.io/kubesphere/pkg/simple/client/logging/elasticsearch"
G
Guangzhe Huang 已提交
36
	"kubesphere.io/kubesphere/pkg/simple/client/monitoring/prometheus"
Z
zryfish 已提交
37
	"kubesphere.io/kubesphere/pkg/simple/client/multicluster"
38
	"kubesphere.io/kubesphere/pkg/simple/client/network"
J
Jeff 已提交
39
	"kubesphere.io/kubesphere/pkg/simple/client/notification"
40
	"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
Z
zryfish 已提交
41
	"kubesphere.io/kubesphere/pkg/simple/client/s3"
42 43 44 45 46 47 48
	"kubesphere.io/kubesphere/pkg/simple/client/servicemesh"
	"kubesphere.io/kubesphere/pkg/simple/client/sonarqube"
	"os"
	"testing"
	"time"
)

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

51
	var conf = &Config{
R
runzexia 已提交
52
		DevopsOptions: &jenkins.Options{
53 54 55 56 57
			Host:           "http://ks-devops.kubesphere-devops-system.svc",
			Username:       "jenkins",
			Password:       "kubesphere",
			MaxConnections: 10,
		},
Z
zryfish 已提交
58
		SonarQubeOptions: &sonarqube.Options{
59 60 61 62 63 64 65 66 67
			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 已提交
68
		ServiceMeshOptions: &servicemesh.Options{
69 70 71 72
			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 已提交
73
		LdapOptions: &ldap.Options{
74 75 76 77 78 79
			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 已提交
80
		RedisOptions: &cache.Options{
H
update  
hongming 已提交
81
			Host:     "localhost",
Z
zryfish 已提交
82 83 84
			Port:     6379,
			Password: "P@88w0rd",
			DB:       0,
85
		},
Z
zryfish 已提交
86
		S3Options: &s3.Options{
87
			Endpoint:        "http://minio.openpitrix-system.svc",
J
Jeff 已提交
88 89
			Region:          "",
			DisableSSL:      false,
90 91 92 93 94 95
			ForcePathStyle:  false,
			AccessKeyID:     "ABCDEFGHIJKLMN",
			SecretAccessKey: "OPQRSTUVWXYZ",
			SessionToken:    "abcdefghijklmn",
			Bucket:          "ssss",
		},
Z
zryfish 已提交
96
		OpenPitrixOptions: &openpitrix.Options{
H
hongming 已提交
97 98 99 100 101 102
			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",
103
		},
104
		NetworkOptions: &network.Options{
Z
Zhengyi Lai 已提交
105
			EnableNetworkPolicy: true,
106 107 108
			NSNPOptions: network.NSNPOptions{
				AllowedIngressNamespaces: []string{},
			},
Z
Zhengyi Lai 已提交
109
			WeaveScopeHost: "weave-scope-app.weave",
D
Duan Jiong 已提交
110
			IPPoolType:     networkv1alpha1.IPPoolTypeNone,
111
		},
Z
zryfish 已提交
112
		MonitoringOptions: &prometheus.Options{
113
			Endpoint: "http://prometheus.kubesphere-monitoring-system.svc",
114
		},
G
Guangzhe Huang 已提交
115
		LoggingOptions: &elasticsearch.Options{
H
huanggze 已提交
116 117 118
			Host:        "http://elasticsearch-logging.kubesphere-logging-system.svc:9200",
			IndexPrefix: "elk",
			Version:     "6",
J
Jeff 已提交
119
		},
Z
zryfish 已提交
120
		AlertingOptions: &alerting.Options{
Z
zryfish 已提交
121
			Endpoint: "http://alerting-client-server.kubesphere-alerting-system.svc:9200/api",
J
Jeff 已提交
122
		},
Z
zryfish 已提交
123
		NotificationOptions: &notification.Options{
J
Jeff 已提交
124 125
			Endpoint: "http://notification.kubesphere-alerting-system.svc:9200",
		},
H
hongming 已提交
126
		AuthorizationOptions: authorizationoptions.NewAuthorizationOptions(),
127
		AuthenticationOptions: &authoptions.AuthenticationOptions{
128 129
			AuthenticateRateLimiterMaxTries: 5,
			AuthenticateRateLimiterDuration: 30 * time.Minute,
130
			JwtSecret:                       "xxxxxx",
131
			MultipleLogin:                   false,
132
			OAuthOptions: &oauth.Options{
H
hongming 已提交
133
				IdentityProviders: []oauth.IdentityProviderOptions{},
134
				Clients: []oauth.Client{{
H
update  
hongming 已提交
135 136 137 138 139 140
					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,
141
				}},
H
update  
hongming 已提交
142 143
				AccessTokenMaxAge:            time.Hour * 24,
				AccessTokenInactivityTimeout: 0,
144
			},
145
		},
Z
zryfish 已提交
146 147 148
		MultiClusterOptions: &multicluster.Options{
			Enable: false,
		},
J
junotx 已提交
149 150 151 152 153
		EventsOptions: &eventsclient.Options{
			Host:        "http://elasticsearch-logging-data.kubesphere-logging-system.svc:9200",
			IndexPrefix: "ks-logstash-events",
			Version:     "6",
		},
R
root 已提交
154 155 156 157 158
		AuditingOptions: &auditingclient.Options{
			Host:        "http://elasticsearch-logging-data.kubesphere-logging-system.svc:9200",
			IndexPrefix: "ks-logstash-auditing",
			Version:     "6",
		},
159
	}
H
update  
hongming 已提交
160
	return conf, nil
161 162 163 164 165 166 167
}

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

func cleanTestConfig(t *testing.T) {
175
	file := fmt.Sprintf("%s.yaml", defaultConfigurationName)
176 177 178 179 180 181 182 183 184 185 186 187 188
	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 已提交
189 190 191 192
	conf, err := newTestConfig()
	if err != nil {
		t.Fatal(err)
	}
193
	saveTestConfig(t, conf)
J
Jeff 已提交
194
	defer cleanTestConfig(t)
195

196
	conf2, err := TryLoadFromDisk()
197 198 199
	if err != nil {
		t.Fatal(err)
	}
H
update  
hongming 已提交
200
	if diff := cmp.Diff(conf, conf2); diff != "" {
J
Jeff 已提交
201
		t.Fatal(diff)
202
	}
J
Jeff 已提交
203
}