config_test.go 6.4 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"
H
update  
hongming 已提交
8 9
	"kubesphere.io/kubesphere/pkg/apiserver/authentication/identityprovider"
	"kubesphere.io/kubesphere/pkg/apiserver/authentication/identityprovider/github"
10 11
	"kubesphere.io/kubesphere/pkg/apiserver/authentication/oauth"
	authoptions "kubesphere.io/kubesphere/pkg/apiserver/authentication/options"
J
Jeff 已提交
12
	"kubesphere.io/kubesphere/pkg/simple/client/alerting"
Z
zryfish 已提交
13
	"kubesphere.io/kubesphere/pkg/simple/client/cache"
R
runzexia 已提交
14
	"kubesphere.io/kubesphere/pkg/simple/client/devops/jenkins"
15 16
	"kubesphere.io/kubesphere/pkg/simple/client/k8s"
	"kubesphere.io/kubesphere/pkg/simple/client/ldap"
G
Guangzhe Huang 已提交
17
	"kubesphere.io/kubesphere/pkg/simple/client/logging/elasticsearch"
G
Guangzhe Huang 已提交
18
	"kubesphere.io/kubesphere/pkg/simple/client/monitoring/prometheus"
19
	"kubesphere.io/kubesphere/pkg/simple/client/mysql"
J
Jeff 已提交
20
	"kubesphere.io/kubesphere/pkg/simple/client/notification"
21
	"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
Z
zryfish 已提交
22
	"kubesphere.io/kubesphere/pkg/simple/client/s3"
23 24 25 26 27 28 29
	"kubesphere.io/kubesphere/pkg/simple/client/servicemesh"
	"kubesphere.io/kubesphere/pkg/simple/client/sonarqube"
	"os"
	"testing"
	"time"
)

H
update  
hongming 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
func newTestConfig() (*Config, error) {

	githubOAuthProvider, err := identityprovider.ResolveOAuthOptions("github", &github.Github{
		ClientID:     "de6ff7bed0304e487b6e",
		ClientSecret: "xxxxxx-xxxxx-xxxxx",
		Endpoint: github.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"},
	})
	if err != nil {
		return nil, err
	}
45
	var conf = &Config{
Z
zryfish 已提交
46
		MySQLOptions: &mysql.Options{
47 48 49 50 51 52 53
			Host:                  "10.68.96.5:3306",
			Username:              "root",
			Password:              "admin",
			MaxIdleConnections:    10,
			MaxOpenConnections:    20,
			MaxConnectionLifeTime: time.Duration(10) * time.Second,
		},
R
runzexia 已提交
54
		DevopsOptions: &jenkins.Options{
55 56 57 58 59
			Host:           "http://ks-devops.kubesphere-devops-system.svc",
			Username:       "jenkins",
			Password:       "kubesphere",
			MaxConnections: 10,
		},
Z
zryfish 已提交
60
		SonarQubeOptions: &sonarqube.Options{
61 62 63 64 65 66 67 68 69
			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 已提交
70
		ServiceMeshOptions: &servicemesh.Options{
71 72 73 74
			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 已提交
75
		LdapOptions: &ldap.Options{
76 77 78 79 80 81
			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 已提交
82
		RedisOptions: &cache.Options{
H
update  
hongming 已提交
83
			Host:     "localhost",
Z
zryfish 已提交
84 85 86
			Port:     6379,
			Password: "P@88w0rd",
			DB:       0,
87
		},
Z
zryfish 已提交
88
		S3Options: &s3.Options{
89
			Endpoint:        "http://minio.openpitrix-system.svc",
J
Jeff 已提交
90 91
			Region:          "",
			DisableSSL:      false,
92 93 94 95 96 97
			ForcePathStyle:  false,
			AccessKeyID:     "ABCDEFGHIJKLMN",
			SecretAccessKey: "OPQRSTUVWXYZ",
			SessionToken:    "abcdefghijklmn",
			Bucket:          "ssss",
		},
Z
zryfish 已提交
98
		OpenPitrixOptions: &openpitrix.Options{
H
hongming 已提交
99 100 101 102 103 104
			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",
105
		},
Z
zryfish 已提交
106
		MonitoringOptions: &prometheus.Options{
107 108 109
			Endpoint:          "http://prometheus.kubesphere-monitoring-system.svc",
			SecondaryEndpoint: "http://prometheus.kubesphere-monitoring-system.svc",
		},
G
Guangzhe Huang 已提交
110
		LoggingOptions: &elasticsearch.Options{
H
huanggze 已提交
111 112 113
			Host:        "http://elasticsearch-logging.kubesphere-logging-system.svc:9200",
			IndexPrefix: "elk",
			Version:     "6",
J
Jeff 已提交
114
		},
Z
zryfish 已提交
115
		AlertingOptions: &alerting.Options{
J
Jeff 已提交
116 117
			Endpoint: "http://alerting.kubesphere-alerting-system.svc:9200",
		},
Z
zryfish 已提交
118
		NotificationOptions: &notification.Options{
J
Jeff 已提交
119 120
			Endpoint: "http://notification.kubesphere-alerting-system.svc:9200",
		},
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
update  
hongming 已提交
128
				IdentityProviders: []oauth.IdentityProviderOptions{{
129 130 131
					Name:          "github",
					MappingMethod: "auto",
					LoginRedirect: true,
H
update  
hongming 已提交
132 133
					Type:          "github",
					Provider:      githubOAuthProvider,
134 135
				}},
				Clients: []oauth.Client{{
H
update  
hongming 已提交
136 137 138 139 140 141
					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,
142
				}},
H
update  
hongming 已提交
143 144
				AccessTokenMaxAge:            time.Hour * 24,
				AccessTokenInactivityTimeout: 0,
145
			},
146
		},
147
	}
H
update  
hongming 已提交
148
	return conf, nil
149 150 151 152 153 154 155
}

func saveTestConfig(t *testing.T, conf *Config) {
	content, err := yaml.Marshal(conf)
	if err != nil {
		t.Fatalf("error marshal config. %v", err)
	}
156
	err = ioutil.WriteFile(fmt.Sprintf("%s.yaml", defaultConfigurationName), content, 0640)
157 158 159 160 161 162
	if err != nil {
		t.Fatalf("error write configuration file, %v", err)
	}
}

func cleanTestConfig(t *testing.T) {
163
	file := fmt.Sprintf("%s.yaml", defaultConfigurationName)
164 165 166 167 168 169 170 171 172 173 174 175 176
	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 已提交
177 178 179 180
	conf, err := newTestConfig()
	if err != nil {
		t.Fatal(err)
	}
181
	saveTestConfig(t, conf)
J
Jeff 已提交
182
	defer cleanTestConfig(t)
183

184
	conf2, err := TryLoadFromDisk()
185 186 187 188
	if err != nil {
		t.Fatal(err)
	}

H
update  
hongming 已提交
189
	if diff := cmp.Diff(conf, conf2); diff != "" {
J
Jeff 已提交
190
		t.Fatal(diff)
191
	}
J
Jeff 已提交
192
}