You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
387 lines
8.5 KiB
387 lines
8.5 KiB
2 years ago
|
package polaris
|
||
3 years ago
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
2 years ago
|
"io"
|
||
3 years ago
|
"net/http"
|
||
|
"reflect"
|
||
|
"strings"
|
||
|
"testing"
|
||
2 years ago
|
"time"
|
||
|
|
||
|
"github.com/go-kratos/kratos/v2/config"
|
||
3 years ago
|
|
||
|
"github.com/polarismesh/polaris-go"
|
||
|
)
|
||
|
|
||
|
var (
|
||
2 years ago
|
testNamespace = "default"
|
||
|
testFileGroup = "test"
|
||
|
testOriginContent = `server:
|
||
3 years ago
|
port: 8080`
|
||
2 years ago
|
testUpdatedContent = `server:
|
||
3 years ago
|
port: 8090`
|
||
2 years ago
|
testCenterURL = "http://127.0.0.1:8090"
|
||
3 years ago
|
)
|
||
|
|
||
|
func makeJSONRequest(uri string, data string, method string, headers map[string]string) ([]byte, error) {
|
||
|
client := http.Client{}
|
||
|
req, err := http.NewRequest(method, uri, strings.NewReader(data))
|
||
2 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
3 years ago
|
req.Header.Add("Content-Type", "application/json")
|
||
|
for k, v := range headers {
|
||
|
req.Header.Add(k, v)
|
||
|
}
|
||
|
res, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer res.Body.Close()
|
||
2 years ago
|
return io.ReadAll(res.Body)
|
||
3 years ago
|
}
|
||
|
|
||
|
type commonRes struct {
|
||
|
Code int32 `json:"code"`
|
||
|
}
|
||
|
|
||
|
type LoginRes struct {
|
||
|
Code int32 `json:"code"`
|
||
|
LoginResponse struct {
|
||
|
Token string `json:"token"`
|
||
|
} `json:"loginResponse"`
|
||
|
}
|
||
|
|
||
|
type configClient struct {
|
||
|
token string
|
||
|
}
|
||
|
|
||
|
func newConfigClient() (*configClient, error) {
|
||
2 years ago
|
token, err := getToken(testCenterURL)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return &configClient{
|
||
|
token: token,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
2 years ago
|
func getToken(testCenterURL string) (string, error) {
|
||
3 years ago
|
data, err := json.Marshal(map[string]string{
|
||
|
"name": "polaris",
|
||
|
"password": "polaris",
|
||
|
})
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
// login use default user
|
||
2 years ago
|
res, err := makeJSONRequest(fmt.Sprintf("%s/core/v1/user/login", testCenterURL), string(data), http.MethodPost, map[string]string{})
|
||
3 years ago
|
if err != nil {
|
||
|
return "", nil
|
||
|
}
|
||
|
var loginRes LoginRes
|
||
|
if err = json.Unmarshal(res, &loginRes); err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
return loginRes.LoginResponse.Token, nil
|
||
|
}
|
||
|
|
||
|
func (client *configClient) createConfigFile(name string) error {
|
||
|
data, err := json.Marshal(map[string]string{
|
||
|
"name": name,
|
||
2 years ago
|
"namespace": testNamespace,
|
||
|
"group": testFileGroup,
|
||
|
"content": testOriginContent,
|
||
3 years ago
|
"modifyBy": "polaris",
|
||
|
"format": "yaml",
|
||
|
})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
2 years ago
|
res, err := makeJSONRequest(fmt.Sprintf("%s/config/v1/configfiles", testCenterURL), string(data), http.MethodPost, map[string]string{
|
||
3 years ago
|
"X-Polaris-Token": client.token,
|
||
|
})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
var resJSON commonRes
|
||
|
err = json.Unmarshal(res, &resJSON)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if resJSON.Code != 200000 {
|
||
2 years ago
|
return fmt.Errorf("create error, res: %s", string(res))
|
||
3 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (client *configClient) updateConfigFile(name string) error {
|
||
|
data, err := json.Marshal(map[string]string{
|
||
|
"name": name,
|
||
2 years ago
|
"namespace": testNamespace,
|
||
|
"group": testFileGroup,
|
||
|
"content": testUpdatedContent,
|
||
3 years ago
|
"modifyBy": "polaris",
|
||
|
"format": "yaml",
|
||
|
})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
2 years ago
|
res, err := makeJSONRequest(fmt.Sprintf("%s/config/v1/configfiles", testCenterURL), string(data), http.MethodPut, map[string]string{
|
||
3 years ago
|
"X-Polaris-Token": client.token,
|
||
|
})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
var resJSON commonRes
|
||
|
err = json.Unmarshal(res, &resJSON)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if resJSON.Code != 200000 {
|
||
2 years ago
|
return fmt.Errorf("update error, res: %s", string(res))
|
||
3 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (client *configClient) deleteConfigFile(name string) error {
|
||
|
data, err := json.Marshal(map[string]string{})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
2 years ago
|
url := fmt.Sprintf("%s/config/v1/configfiles?namespace=%s&group=%s&name=%s", testCenterURL, testNamespace, testFileGroup, name)
|
||
3 years ago
|
res, err := makeJSONRequest(url, string(data), http.MethodDelete, map[string]string{
|
||
|
"X-Polaris-Token": client.token,
|
||
|
})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
var resJSON commonRes
|
||
|
err = json.Unmarshal(res, &resJSON)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if resJSON.Code != 200000 {
|
||
2 years ago
|
return fmt.Errorf("delete error, res: %s", string(res))
|
||
3 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (client *configClient) publishConfigFile(name string) error {
|
||
|
data, err := json.Marshal(map[string]string{
|
||
2 years ago
|
"namespace": testNamespace,
|
||
|
"group": testFileGroup,
|
||
3 years ago
|
"fileName": name,
|
||
|
"name": name,
|
||
|
})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
2 years ago
|
res, err := makeJSONRequest(fmt.Sprintf("%s/config/v1/configfiles/release", testCenterURL), string(data), http.MethodPost, map[string]string{
|
||
3 years ago
|
"X-Polaris-Token": client.token,
|
||
|
})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
var resJSON commonRes
|
||
|
err = json.Unmarshal(res, &resJSON)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if resJSON.Code != 200000 {
|
||
2 years ago
|
return fmt.Errorf("publish error, res: %s", string(res))
|
||
3 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func TestConfig(t *testing.T) {
|
||
2 years ago
|
name := "kratos-polaris-test.yaml"
|
||
3 years ago
|
client, err := newConfigClient()
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
2 years ago
|
_ = client.deleteConfigFile(name)
|
||
3 years ago
|
if err = client.createConfigFile(name); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
2 years ago
|
time.Sleep(5 * time.Second)
|
||
3 years ago
|
if err = client.publishConfigFile(name); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
2 years ago
|
time.Sleep(5 * time.Second)
|
||
|
|
||
3 years ago
|
// Always remember clear test resource
|
||
2 years ago
|
sdk, err := polaris.NewSDKContextByAddress("127.0.0.1:8091")
|
||
3 years ago
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
2 years ago
|
p := New(sdk)
|
||
|
config, err := p.Config(WithConfigFile(File{Name: name, Group: testFileGroup}))
|
||
3 years ago
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
kv, err := config.Load()
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
2 years ago
|
for _, value := range kv {
|
||
|
t.Logf("key: %s, value: %s", value.Key, value.Value)
|
||
|
}
|
||
|
if len(kv) != 1 || kv[0].Key != name || string(kv[0].Value) != testOriginContent {
|
||
3 years ago
|
t.Fatal("config error")
|
||
|
}
|
||
|
|
||
|
w, err := config.Watch()
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
2 years ago
|
|
||
|
t.Cleanup(func() {
|
||
3 years ago
|
err = client.deleteConfigFile(name)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
2 years ago
|
})
|
||
3 years ago
|
|
||
|
if err = client.updateConfigFile(name); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
if err = client.publishConfigFile(name); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
if kv, err = w.Next(); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
2 years ago
|
for _, value := range kv {
|
||
|
t.Log(value.Key, string(value.Value))
|
||
|
}
|
||
|
|
||
|
if len(kv) != 1 || kv[0].Key != name || string(kv[0].Value) != testUpdatedContent {
|
||
3 years ago
|
t.Fatal("config error")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestExtToFormat(t *testing.T) {
|
||
2 years ago
|
name := "kratos-polaris-ext.yaml"
|
||
3 years ago
|
client, err := newConfigClient()
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
2 years ago
|
_ = client.deleteConfigFile(name)
|
||
3 years ago
|
if err = client.createConfigFile(name); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
if err = client.publishConfigFile(name); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
// Always remember clear test resource
|
||
2 years ago
|
t.Cleanup(func() {
|
||
3 years ago
|
if err = client.deleteConfigFile(name); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
2 years ago
|
})
|
||
3 years ago
|
|
||
2 years ago
|
sdk, err := polaris.NewSDKContextByAddress("127.0.0.1:8091")
|
||
3 years ago
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
2 years ago
|
p := New(sdk)
|
||
3 years ago
|
|
||
2 years ago
|
cfg, err := p.Config(WithConfigFile(File{Name: name, Group: testFileGroup}))
|
||
3 years ago
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
2 years ago
|
|
||
|
kv, err := cfg.Load()
|
||
3 years ago
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
if !reflect.DeepEqual(len(kv), 1) {
|
||
|
t.Errorf("len(kvs) = %d", len(kv))
|
||
|
}
|
||
|
if !reflect.DeepEqual(name, kv[0].Key) {
|
||
|
t.Errorf("kvs[0].Key is %s", kv[0].Key)
|
||
|
}
|
||
2 years ago
|
if !reflect.DeepEqual(testOriginContent, string(kv[0].Value)) {
|
||
3 years ago
|
t.Errorf("kvs[0].Value is %s", kv[0].Value)
|
||
|
}
|
||
|
if !reflect.DeepEqual("yaml", kv[0].Format) {
|
||
|
t.Errorf("kvs[0].Format is %s", kv[0].Format)
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
|
func TestGetMultipleConfig(t *testing.T) {
|
||
|
client, err := newConfigClient()
|
||
|
files := make([]File, 0, 3)
|
||
|
for i := 0; i < 3; i++ {
|
||
|
name := fmt.Sprintf("kratos-polaris-test-%d.yaml", i)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
_ = client.deleteConfigFile(name)
|
||
|
if err = client.createConfigFile(name); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
if err = client.publishConfigFile(name); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
files = append(files, File{Name: name, Group: testFileGroup})
|
||
|
}
|
||
|
|
||
|
sdk, err := polaris.NewSDKContextByAddress("127.0.0.1:8091")
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
p := New(sdk, WithNamespace("default"))
|
||
|
|
||
|
cfg, err := p.Config(WithConfigFile(files...))
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
kvs, err := cfg.Load()
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
for _, kv := range kvs {
|
||
|
t.Logf("key: %s, value: %s", kv.Key, kv.Value)
|
||
|
}
|
||
|
|
||
|
w, err := cfg.Watch()
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
for _, file := range files {
|
||
|
if err = client.publishConfigFile(file.Name); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
kvs, err := w.Next()
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
m := make(map[string]*config.KeyValue)
|
||
|
for _, kv := range kvs {
|
||
|
m[kv.Key] = kv
|
||
|
}
|
||
|
if !reflect.DeepEqual(file.Name, m[file.Name].Key) {
|
||
|
t.Errorf("m[file.Name].Key is %s", m[file.Name].Key)
|
||
|
}
|
||
|
if !reflect.DeepEqual(testOriginContent, string(m[file.Name].Value)) {
|
||
|
t.Errorf("m[file.Name].Value is %s", m[file.Name].Value)
|
||
|
}
|
||
|
if !reflect.DeepEqual("yaml", m[file.Name].Format) {
|
||
|
t.Errorf("m[file.Name].Format is %s", m[file.Name].Format)
|
||
|
}
|
||
|
}
|
||
|
}
|