config_test.go 6.2 KB
Newer Older
1 2 3 4
package config

import (
	"fmt"
5
	"github.com/google/go-cmp/cmp"
6 7
	"gopkg.in/yaml.v2"
	"io/ioutil"
8 9
	"kubesphere.io/kubesphere/pkg/apiserver/authentication/oauth"
	authoptions "kubesphere.io/kubesphere/pkg/apiserver/authentication/options"
J
Jeff 已提交
10
	"kubesphere.io/kubesphere/pkg/simple/client/alerting"
Z
zryfish 已提交
11
	"kubesphere.io/kubesphere/pkg/simple/client/cache"
R
runzexia 已提交
12
	"kubesphere.io/kubesphere/pkg/simple/client/devops/jenkins"
13 14
	"kubesphere.io/kubesphere/pkg/simple/client/k8s"
	"kubesphere.io/kubesphere/pkg/simple/client/ldap"
G
Guangzhe Huang 已提交
15
	"kubesphere.io/kubesphere/pkg/simple/client/logging/elasticsearch"
G
Guangzhe Huang 已提交
16
	"kubesphere.io/kubesphere/pkg/simple/client/monitoring/prometheus"
17
	"kubesphere.io/kubesphere/pkg/simple/client/mysql"
J
Jeff 已提交
18
	"kubesphere.io/kubesphere/pkg/simple/client/notification"
19
	"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
Z
zryfish 已提交
20
	"kubesphere.io/kubesphere/pkg/simple/client/s3"
21 22
	"kubesphere.io/kubesphere/pkg/simple/client/servicemesh"
	"kubesphere.io/kubesphere/pkg/simple/client/sonarqube"
J
Jeff 已提交
23
	"kubesphere.io/kubesphere/pkg/utils/reflectutils"
24 25 26 27 28 29
	"os"
	"testing"
	"time"
)

func newTestConfig() *Config {
30
	var conf = &Config{
Z
zryfish 已提交
31
		MySQLOptions: &mysql.Options{
32 33 34 35 36 37 38
			Host:                  "10.68.96.5:3306",
			Username:              "root",
			Password:              "admin",
			MaxIdleConnections:    10,
			MaxOpenConnections:    20,
			MaxConnectionLifeTime: time.Duration(10) * time.Second,
		},
R
runzexia 已提交
39
		DevopsOptions: &jenkins.Options{
40 41 42 43 44
			Host:           "http://ks-devops.kubesphere-devops-system.svc",
			Username:       "jenkins",
			Password:       "kubesphere",
			MaxConnections: 10,
		},
Z
zryfish 已提交
45
		SonarQubeOptions: &sonarqube.Options{
46 47 48 49 50 51 52 53 54
			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 已提交
55
		ServiceMeshOptions: &servicemesh.Options{
56 57 58 59
			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 已提交
60
		LdapOptions: &ldap.Options{
61 62 63 64 65 66
			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 已提交
67
		RedisOptions: &cache.Options{
H
update  
hongming 已提交
68
			Host:     "localhost",
Z
zryfish 已提交
69 70 71
			Port:     6379,
			Password: "P@88w0rd",
			DB:       0,
72
		},
Z
zryfish 已提交
73
		S3Options: &s3.Options{
74
			Endpoint:        "http://minio.openpitrix-system.svc",
J
Jeff 已提交
75 76
			Region:          "",
			DisableSSL:      false,
77 78 79 80 81 82
			ForcePathStyle:  false,
			AccessKeyID:     "ABCDEFGHIJKLMN",
			SecretAccessKey: "OPQRSTUVWXYZ",
			SessionToken:    "abcdefghijklmn",
			Bucket:          "ssss",
		},
Z
zryfish 已提交
83
		OpenPitrixOptions: &openpitrix.Options{
H
hongming 已提交
84 85 86 87 88 89
			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",
90
		},
Z
zryfish 已提交
91
		MonitoringOptions: &prometheus.Options{
92 93 94
			Endpoint:          "http://prometheus.kubesphere-monitoring-system.svc",
			SecondaryEndpoint: "http://prometheus.kubesphere-monitoring-system.svc",
		},
G
Guangzhe Huang 已提交
95
		LoggingOptions: &elasticsearch.Options{
H
huanggze 已提交
96 97 98
			Host:        "http://elasticsearch-logging.kubesphere-logging-system.svc:9200",
			IndexPrefix: "elk",
			Version:     "6",
J
Jeff 已提交
99
		},
Z
zryfish 已提交
100
		AlertingOptions: &alerting.Options{
J
Jeff 已提交
101 102
			Endpoint: "http://alerting.kubesphere-alerting-system.svc:9200",
		},
Z
zryfish 已提交
103
		NotificationOptions: &notification.Options{
J
Jeff 已提交
104 105
			Endpoint: "http://notification.kubesphere-alerting-system.svc:9200",
		},
106
		AuthenticationOptions: &authoptions.AuthenticationOptions{
107 108 109
			AuthenticateRateLimiterMaxTries: 5,
			AuthenticateRateLimiterDuration: 30 * time.Minute,
			MaxAuthenticateRetries:          6,
110
			JwtSecret:                       "xxxxxx",
111
			MultipleLogin:                   false,
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
			OAuthOptions: &oauth.Options{
				IdentityProviders: []oauth.IdentityProvider{{
					Name:          "github",
					MappingMethod: "auto",
					LoginRedirect: true,
					Type:          oauth.IdentityProviderTypeGithub,
					Github: &oauth.Github{
						ClientID:     "de6ff7bed0304e487b6e",
						ClientSecret: "xxxxxx-xxxxx-xxxxx",
						Endpoint: oauth.Endpoint{
							AuthURL:  "https://github.com/login/oauth/authorize",
							TokenURL: "https://github.com/login/oauth/token",
						},
						RedirectURL: "https://ks-console.kubesphere-system.svc/oauth/callbak/github",
						Scopes:      []string{"user"},
					},
				}},
				Clients: []oauth.Client{{
					Name:                                "kubesphere-console-client",
					Secret:                              "xxxxxx-xxxxxx-xxxxxx",
					RespondWithChallenges:               true,
					RedirectURIs:                        []string{"http://ks-console.kubesphere-system.svc/oauth/token/implicit"},
					GrantMethod:                         oauth.GrantHandlerAuto,
					AccessTokenInactivityTimeoutSeconds: nil,
				}},
				AccessTokenMaxAgeSeconds:            86400,
				AccessTokenInactivityTimeoutSeconds: 0,
			},
140
		},
141 142 143 144 145 146 147 148 149 150
	}
	return conf
}

func saveTestConfig(t *testing.T, conf *Config) {
	content, err := yaml.Marshal(conf)
	if err != nil {
		t.Fatalf("error marshal config. %v", err)
	}

151
	err = ioutil.WriteFile(fmt.Sprintf("%s.yaml", defaultConfigurationName), content, 0640)
152 153 154 155 156 157
	if err != nil {
		t.Fatalf("error write configuration file, %v", err)
	}
}

func cleanTestConfig(t *testing.T) {
158
	file := fmt.Sprintf("%s.yaml", defaultConfigurationName)
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	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) {
	conf := newTestConfig()
	saveTestConfig(t, conf)
J
Jeff 已提交
174
	defer cleanTestConfig(t)
175

176
	conf2, err := TryLoadFromDisk()
177 178 179 180
	if err != nil {
		t.Fatal(err)
	}

181
	cmp.Diff(conf, conf2)
J
Jeff 已提交
182 183
	if diff := reflectutils.Equal(conf, conf2); diff != nil {
		t.Fatal(diff)
184
	}
J
Jeff 已提交
185
}