config_test.go 5.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"
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/network"
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 23 24 25 26 27
	"kubesphere.io/kubesphere/pkg/simple/client/servicemesh"
	"kubesphere.io/kubesphere/pkg/simple/client/sonarqube"
	"os"
	"testing"
	"time"
)

H
update  
hongming 已提交
28 29
func newTestConfig() (*Config, error) {

30
	var conf = &Config{
R
runzexia 已提交
31
		DevopsOptions: &jenkins.Options{
32 33 34 35 36
			Host:           "http://ks-devops.kubesphere-devops-system.svc",
			Username:       "jenkins",
			Password:       "kubesphere",
			MaxConnections: 10,
		},
Z
zryfish 已提交
37
		SonarQubeOptions: &sonarqube.Options{
38 39 40 41 42 43 44 45 46
			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 已提交
47
		ServiceMeshOptions: &servicemesh.Options{
48 49 50 51
			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 已提交
52
		LdapOptions: &ldap.Options{
53 54 55 56 57 58
			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 已提交
59
		RedisOptions: &cache.Options{
H
update  
hongming 已提交
60
			Host:     "localhost",
Z
zryfish 已提交
61 62 63
			Port:     6379,
			Password: "P@88w0rd",
			DB:       0,
64
		},
Z
zryfish 已提交
65
		S3Options: &s3.Options{
66
			Endpoint:        "http://minio.openpitrix-system.svc",
J
Jeff 已提交
67 68
			Region:          "",
			DisableSSL:      false,
69 70 71 72 73 74
			ForcePathStyle:  false,
			AccessKeyID:     "ABCDEFGHIJKLMN",
			SecretAccessKey: "OPQRSTUVWXYZ",
			SessionToken:    "abcdefghijklmn",
			Bucket:          "ssss",
		},
Z
zryfish 已提交
75
		OpenPitrixOptions: &openpitrix.Options{
H
hongming 已提交
76 77 78 79 80 81
			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",
82
		},
83 84 85
		NetworkOptions: &network.Options{
			WeaveScopeHost: "weave-scope-app.weave.svc",
		},
Z
zryfish 已提交
86
		MonitoringOptions: &prometheus.Options{
87 88 89
			Endpoint:          "http://prometheus.kubesphere-monitoring-system.svc",
			SecondaryEndpoint: "http://prometheus.kubesphere-monitoring-system.svc",
		},
G
Guangzhe Huang 已提交
90
		LoggingOptions: &elasticsearch.Options{
H
huanggze 已提交
91 92 93
			Host:        "http://elasticsearch-logging.kubesphere-logging-system.svc:9200",
			IndexPrefix: "elk",
			Version:     "6",
J
Jeff 已提交
94
		},
Z
zryfish 已提交
95
		AlertingOptions: &alerting.Options{
J
Jeff 已提交
96 97
			Endpoint: "http://alerting.kubesphere-alerting-system.svc:9200",
		},
Z
zryfish 已提交
98
		NotificationOptions: &notification.Options{
J
Jeff 已提交
99 100
			Endpoint: "http://notification.kubesphere-alerting-system.svc:9200",
		},
101
		AuthenticationOptions: &authoptions.AuthenticationOptions{
102 103 104
			AuthenticateRateLimiterMaxTries: 5,
			AuthenticateRateLimiterDuration: 30 * time.Minute,
			MaxAuthenticateRetries:          6,
105
			JwtSecret:                       "xxxxxx",
106
			MultipleLogin:                   false,
107
			OAuthOptions: &oauth.Options{
H
hongming 已提交
108
				IdentityProviders: []oauth.IdentityProviderOptions{},
109
				Clients: []oauth.Client{{
H
update  
hongming 已提交
110 111 112 113 114 115
					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,
116
				}},
H
update  
hongming 已提交
117 118
				AccessTokenMaxAge:            time.Hour * 24,
				AccessTokenInactivityTimeout: 0,
119
			},
120
		},
121
	}
H
update  
hongming 已提交
122
	return conf, nil
123 124 125 126 127 128 129
}

func saveTestConfig(t *testing.T, conf *Config) {
	content, err := yaml.Marshal(conf)
	if err != nil {
		t.Fatalf("error marshal config. %v", err)
	}
130
	err = ioutil.WriteFile(fmt.Sprintf("%s.yaml", defaultConfigurationName), content, 0640)
131 132 133 134 135 136
	if err != nil {
		t.Fatalf("error write configuration file, %v", err)
	}
}

func cleanTestConfig(t *testing.T) {
137
	file := fmt.Sprintf("%s.yaml", defaultConfigurationName)
138 139 140 141 142 143 144 145 146 147 148 149 150
	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 已提交
151 152 153 154
	conf, err := newTestConfig()
	if err != nil {
		t.Fatal(err)
	}
155
	saveTestConfig(t, conf)
J
Jeff 已提交
156
	defer cleanTestConfig(t)
157

158
	conf2, err := TryLoadFromDisk()
159 160 161
	if err != nil {
		t.Fatal(err)
	}
H
update  
hongming 已提交
162
	if diff := cmp.Diff(conf, conf2); diff != "" {
J
Jeff 已提交
163
		t.Fatal(diff)
164
	}
J
Jeff 已提交
165
}