1 Star 2 Fork 0

baetyl/baetyl-state

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
kv_test.go 13.07 KB
一键复制 编辑 原始数据 按行查看 历史
chensheng 提交于 2020-04-28 13:27 . close server when exit (#7)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/baetyl/baetyl-go/http"
"io/ioutil"
"os"
"path"
"testing"
"time"
"github.com/baetyl/baetyl-go/utils"
"github.com/baetyl/baetyl-state/database"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
)
func TestKV(t *testing.T) {
dir, err := ioutil.TempDir("", t.Name())
assert.NoError(t, err)
defer os.RemoveAll(dir)
_conf := Config{}
_, err = NewServer(_conf)
assert.Error(t, err)
assert.Equal(t, err.Error(), "no such kind database")
//ctx := context.Background()
conf := struct {
serverConf Config
cliConf mockClientConfig
}{
serverConf: Config{
Database: database.Conf{
Driver: "boltdb",
Source: path.Join(dir, "kv3.db"),
},
Server: http.ServerConfig{
Address: "127.0.0.1:50030",
},
},
cliConf: mockClientConfig{
Address: "http://127.0.0.1:50030",
},
}
server, err := NewServer(conf.serverConf)
assert.NoError(t, err)
assert.NotEmpty(t, server)
defer server.Close()
time.Sleep(time.Second)
kv1 := database.KV{
Key: "key1",
Value: []byte("value1"),
}
data, err := json.Marshal(kv1)
assert.NoError(t, err)
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
url := conf.cliConf.Address
req.SetRequestURI(url)
req.Header.SetMethod("POST")
req.SetBody(data)
client := &fasthttp.Client{}
err = client.Do(req, resp)
assert.NoError(t, err)
assert.Equal(t, resp.StatusCode(), 200)
req2 := fasthttp.AcquireRequest()
resp2 := fasthttp.AcquireResponse()
url2 := fmt.Sprintf("%s/%s", conf.cliConf.Address, "key1")
req2.SetRequestURI(url2)
req2.Header.SetMethod("GET")
err = client.Do(req2, resp2)
assert.NoError(t, err)
assert.Equal(t, resp2.StatusCode(), 200)
_kv2 := new(database.KV)
err = json.Unmarshal(resp2.Body(), _kv2)
assert.NoError(t, err)
assert.Equal(t, _kv2.Key, "key1")
assert.Equal(t, _kv2.Value, []byte("value1"))
req3 := fasthttp.AcquireRequest()
resp3 := fasthttp.AcquireResponse()
url3 := fmt.Sprintf("%s/%s", conf.cliConf.Address, "key1")
req3.SetRequestURI(url3)
req3.Header.SetMethod("DELETE")
err = client.Do(req3, resp3)
assert.NoError(t, err)
assert.Equal(t, resp3.StatusCode(), 200)
req4 := fasthttp.AcquireRequest()
resp4 := fasthttp.AcquireResponse()
url4 := fmt.Sprintf("%s/%s", conf.cliConf.Address, "key1")
req4.SetRequestURI(url4)
req4.Header.SetMethod("GET")
err = client.Do(req4, resp4)
assert.NoError(t, err)
assert.Equal(t, resp4.StatusCode(), 200)
_kv4 := new(database.KV)
err = json.Unmarshal(resp4.Body(), _kv4)
assert.NoError(t, err)
assert.Equal(t, _kv4.Key, "key1")
kv2 := database.KV{
Key: "key2",
Value: []byte("value2"),
}
kv3 := database.KV{
Key: "xey3",
Value: []byte("value3"),
}
data2, err := json.Marshal(kv2)
assert.NoError(t, err)
data3, err := json.Marshal(kv3)
assert.NoError(t, err)
req5 := fasthttp.AcquireRequest()
resp5 := fasthttp.AcquireResponse()
url5 := conf.cliConf.Address
req5.SetRequestURI(url5)
req5.Header.SetMethod("POST")
req5.SetBody(data2)
err = client.Do(req5, resp5)
assert.NoError(t, err)
assert.Equal(t, resp5.StatusCode(), 200)
req6 := fasthttp.AcquireRequest()
resp6 := fasthttp.AcquireResponse()
url6 := conf.cliConf.Address
req6.SetRequestURI(url6)
req6.Header.SetMethod("POST")
req6.SetBody(data3)
err = client.Do(req6, resp6)
assert.NoError(t, err)
assert.Equal(t, resp6.StatusCode(), 200)
req8 := fasthttp.AcquireRequest()
resp8 := fasthttp.AcquireResponse()
url8 := conf.cliConf.Address
req8.SetRequestURI(url8)
req8.URI().SetQueryString("prefix=")
req8.Header.SetMethod("GET")
err = client.Do(req8, resp8)
assert.NoError(t, err)
assert.Equal(t, resp8.StatusCode(), 200)
var _kvs8 []database.KV
err = json.Unmarshal(resp8.Body(), &_kvs8)
assert.NoError(t, err)
assert.Equal(t, 2, len(_kvs8))
req9 := fasthttp.AcquireRequest()
resp9 := fasthttp.AcquireResponse()
url9 := conf.cliConf.Address
req9.SetRequestURI(url9)
req9.URI().SetQueryString("prefix=xey")
req9.Header.SetMethod("GET")
err = client.Do(req9, resp9)
assert.NoError(t, err)
assert.Equal(t, resp9.StatusCode(), 200)
var _kvs9 []database.KV
err = json.Unmarshal(resp9.Body(), &_kvs9)
assert.NoError(t, err)
assert.Equal(t, 1, len(_kvs9))
}
func TestHttps(t *testing.T) {
dir, err := ioutil.TempDir("", t.Name())
assert.NoError(t, err)
defer os.RemoveAll(dir)
conf := struct {
serverConf Config
cliConf mockClientConfig
}{
serverConf: Config{
Database: database.Conf{
Driver: "boltdb",
Source: path.Join(dir, "kv3.db"),
},
Server: http.ServerConfig{
Address: "127.0.0.1:50060",
Certificate: utils.Certificate{
Cert: "example/var/lib/baetyl/testcert/server.pem",
Key: "example/var/lib/baetyl/testcert/server.key",
},
},
},
cliConf: mockClientConfig{
Address: "https://127.0.0.1:50060",
Certificate: utils.Certificate{
Key: "example/var/lib/baetyl/testcert/client.key",
Cert: "example/var/lib/baetyl/testcert/client.pem",
InsecureSkipVerify: true,
},
},
}
server, err := NewServer(conf.serverConf)
assert.NoError(t, err)
assert.NotEmpty(t, server)
defer server.Close()
time.Sleep(time.Second)
kv1 := database.KV{
Key: "key1",
Value: []byte("value1"),
}
data, err := json.Marshal(kv1)
assert.NoError(t, err)
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
url := conf.cliConf.Address
req.SetRequestURI(url)
req.Header.SetMethod("POST")
req.SetBody(data)
_tls, err := utils.NewTLSConfigClient(conf.cliConf.Certificate)
assert.NoError(t, err)
client := &fasthttp.Client{
TLSConfig: _tls,
}
err = client.Do(req, resp)
assert.NoError(t, err)
assert.Equal(t, resp.StatusCode(), 200)
req2 := fasthttp.AcquireRequest()
resp2 := fasthttp.AcquireResponse()
url2 := fmt.Sprintf("%s/%s", conf.cliConf.Address, "key1")
req2.SetRequestURI(url2)
req2.Header.SetMethod("GET")
err = client.Do(req2, resp2)
assert.NoError(t, err)
assert.Equal(t, resp2.StatusCode(), 200)
_kv2 := new(database.KV)
err = json.Unmarshal(resp2.Body(), _kv2)
assert.NoError(t, err)
assert.Equal(t, _kv2.Key, "key1")
assert.Equal(t, _kv2.Value, []byte("value1"))
req3 := fasthttp.AcquireRequest()
resp3 := fasthttp.AcquireResponse()
url3 := fmt.Sprintf("%s/%s", conf.cliConf.Address, "key1")
req3.SetRequestURI(url3)
req3.Header.SetMethod("DELETE")
err = client.Do(req3, resp3)
assert.NoError(t, err)
assert.Equal(t, resp3.StatusCode(), 200)
req4 := fasthttp.AcquireRequest()
resp4 := fasthttp.AcquireResponse()
url4 := fmt.Sprintf("%s/%s", conf.cliConf.Address, "key1")
req4.SetRequestURI(url4)
req4.Header.SetMethod("GET")
err = client.Do(req4, resp4)
assert.NoError(t, err)
assert.Equal(t, resp4.StatusCode(), 200)
_kv4 := new(database.KV)
err = json.Unmarshal(resp4.Body(), _kv4)
assert.NoError(t, err)
assert.Equal(t, _kv4.Key, "key1")
kv2 := database.KV{
Key: "key2",
Value: []byte("value2"),
}
kv3 := database.KV{
Key: "xey3",
Value: []byte("value3"),
}
data2, err := json.Marshal(kv2)
assert.NoError(t, err)
data3, err := json.Marshal(kv3)
assert.NoError(t, err)
req5 := fasthttp.AcquireRequest()
resp5 := fasthttp.AcquireResponse()
url5 := conf.cliConf.Address
req5.SetRequestURI(url5)
req5.Header.SetMethod("POST")
req5.SetBody(data2)
err = client.Do(req5, resp5)
assert.NoError(t, err)
assert.Equal(t, resp5.StatusCode(), 200)
req6 := fasthttp.AcquireRequest()
resp6 := fasthttp.AcquireResponse()
url6 := conf.cliConf.Address
req6.SetRequestURI(url6)
req6.Header.SetMethod("POST")
req6.SetBody(data3)
err = client.Do(req6, resp6)
assert.NoError(t, err)
assert.Equal(t, resp6.StatusCode(), 200)
req8 := fasthttp.AcquireRequest()
resp8 := fasthttp.AcquireResponse()
url8 := conf.cliConf.Address
req8.SetRequestURI(url8)
req8.URI().SetQueryString("prefix=")
req8.Header.SetMethod("GET")
err = client.Do(req8, resp8)
assert.NoError(t, err)
assert.Equal(t, resp8.StatusCode(), 200)
var _kvs8 []database.KV
err = json.Unmarshal(resp8.Body(), &_kvs8)
assert.NoError(t, err)
assert.Equal(t, 2, len(_kvs8))
req9 := fasthttp.AcquireRequest()
resp9 := fasthttp.AcquireResponse()
url9 := conf.cliConf.Address
req9.SetRequestURI(url9)
req9.URI().SetQueryString("prefix=xey")
req9.Header.SetMethod("GET")
err = client.Do(req9, resp9)
assert.NoError(t, err)
assert.Equal(t, resp9.StatusCode(), 200)
var _kvs9 []database.KV
err = json.Unmarshal(resp9.Body(), &_kvs9)
assert.NoError(t, err)
assert.Equal(t, 1, len(_kvs9))
}
func TestHttpKVFail(t *testing.T) {
dir, err := ioutil.TempDir("", t.Name())
assert.NoError(t, err)
defer os.RemoveAll(dir)
registerMockDB()
conf := struct {
serverConf Config
cliConf mockClientConfig
}{
serverConf: Config{
Database: database.Conf{
Driver: "errdb",
Source: path.Join(dir, "kv3.db"),
},
Server: http.ServerConfig{
Address: "127.0.0.1:50040",
},
},
cliConf: mockClientConfig{
Address: "http://127.0.0.1:50040",
},
}
server, err := NewServer(conf.serverConf)
assert.NoError(t, err)
assert.NotEmpty(t, server)
defer server.Close()
time.Sleep(time.Second)
kv1 := database.KV{
Key: "key1",
Value: []byte("value1"),
}
data, err := json.Marshal(kv1)
assert.NoError(t, err)
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
url := conf.cliConf.Address
req.SetRequestURI(url)
req.Header.SetMethod("POST")
req.SetBody(data)
client := &fasthttp.Client{}
err = client.Do(req, resp)
assert.NoError(t, err)
assert.Equal(t, resp.StatusCode(), 500)
req2 := fasthttp.AcquireRequest()
resp2 := fasthttp.AcquireResponse()
url2 := fmt.Sprintf("%s/%s", conf.cliConf.Address, "key1")
req2.SetRequestURI(url2)
req2.Header.SetMethod("GET")
err = client.Do(req2, resp2)
assert.NoError(t, err)
assert.Equal(t, resp2.StatusCode(), 500)
req3 := fasthttp.AcquireRequest()
resp3 := fasthttp.AcquireResponse()
url3 := fmt.Sprintf("%s/%s", conf.cliConf.Address, "key1")
req3.SetRequestURI(url3)
req3.Header.SetMethod("DELETE")
err = client.Do(req3, resp3)
assert.NoError(t, err)
assert.Equal(t, resp3.StatusCode(), 500)
req4 := fasthttp.AcquireRequest()
resp4 := fasthttp.AcquireResponse()
url4 := conf.cliConf.Address
req4.SetRequestURI(url4)
req4.URI().SetQueryString("prefix=")
req4.Header.SetMethod("GET")
err = client.Do(req4, resp4)
assert.NoError(t, err)
assert.Equal(t, resp4.StatusCode(), 500)
}
func TestAddress(t *testing.T) {
dir, err := ioutil.TempDir("", t.Name())
assert.NoError(t, err)
defer os.RemoveAll(dir)
conf := struct {
serverConf Config
cliConf mockClientConfig
}{
serverConf: Config{
Database: database.Conf{
Driver: "boltdb",
Source: path.Join(dir, "kv3.db"),
},
Server: http.ServerConfig{
Address: "http://127.0.0.1:50070",
},
},
cliConf: mockClientConfig{
Address: "http://127.0.0.1:50070",
},
}
server, err := NewServer(conf.serverConf)
assert.NoError(t, err)
assert.NotEmpty(t, server)
defer server.Close()
time.Sleep(time.Second)
kv1 := database.KV{
Key: "key1",
Value: []byte("value1"),
}
data, err := json.Marshal(kv1)
assert.NoError(t, err)
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
url := conf.cliConf.Address
req.SetRequestURI(url)
req.Header.SetMethod("POST")
req.SetBody(data)
client := &fasthttp.Client{}
err = client.Do(req, resp)
assert.Error(t, err)
conf2 := struct {
serverConf Config
cliConf mockClientConfig
}{
serverConf: Config{
Database: database.Conf{
Driver: "boltdb",
Source: path.Join(dir, "kv4.db"),
},
Server: http.ServerConfig{
Address: "127.0.0.1:50080",
},
},
cliConf: mockClientConfig{
Address: "http://127.0.0.1:50080",
},
}
server2, err := NewServer(conf2.serverConf)
assert.NoError(t, err)
assert.NotEmpty(t, server2)
defer server2.Close()
time.Sleep(time.Second)
kv2 := database.KV{
Key: "key2",
Value: []byte("value2"),
}
data2, err := json.Marshal(kv2)
assert.NoError(t, err)
req2 := fasthttp.AcquireRequest()
resp2 := fasthttp.AcquireResponse()
url2 := conf2.cliConf.Address
req2.SetRequestURI(url2)
req2.Header.SetMethod("POST")
req2.SetBody(data2)
client2 := &fasthttp.Client{}
err = client2.Do(req2, resp2)
assert.NoError(t, err)
}
type mockDB struct{}
func registerMockDB() {
database.Factories["errdb"] = newMockDB
}
// New creates a new sql database
func newMockDB(conf database.Conf) (database.DB, error) {
return new(mockDB), nil
}
func (d *mockDB) Conf() database.Conf {
return database.Conf{}
}
// Set put key and value into SQL DB
func (d *mockDB) Set(kv *database.KV) error {
return errors.New("custom error")
}
// Get gets value by key from SQL DB
func (d *mockDB) Get(key string) (*database.KV, error) {
return nil, errors.New("custom error")
}
// Del deletes key and value from SQL DB
func (d *mockDB) Del(key string) error {
return errors.New("custom error")
}
// List list kvs with the prefix
func (d *mockDB) List(prefix string) ([]database.KV, error) {
return nil, errors.New("custom error")
}
func (d *mockDB) Close() error {
return nil
}
// ClientConfig api client config
type mockClientConfig struct {
Address string `yaml:"address" json:"address"`
utils.Certificate `yaml:",inline" json:",inline"`
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/baetyl/baetyl-state.git
git@gitee.com:baetyl/baetyl-state.git
baetyl
baetyl-state
baetyl-state
master

搜索帮助