Skip to content

Commit

Permalink
Merge branch 'upstream' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason committed Dec 2, 2020
2 parents e31164b + 0e2bf1c commit 5622010
Show file tree
Hide file tree
Showing 6 changed files with 906 additions and 47 deletions.
33 changes: 20 additions & 13 deletions api/internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,42 @@ func init() {
}
salt = uint16(i)
}

ips, err := getLocalIPs()
if err != nil {
panic(err)
}
_sf = sonyflake.NewSonyflake(sonyflake.Settings{
MachineID: func() (u uint16, e error) {
return sumIP(GetOutboundIP()) + salt, nil
return sumIPs(ips) + salt, nil
},
})
if _sf == nil {
panic("sonyflake init failed")
}
}

func sumIP(ip net.IP) uint16 {
func sumIPs(ips []net.IP) uint16 {
total := 0
for i := range ip {
total += int(ip[i])
for _, ip := range ips {
for i := range ip {
total += int(ip[i])
}
}
return uint16(total)
}

// Get preferred outbound ip of this machine
func GetOutboundIP() net.IP {
conn, err := net.Dial("udp", "8.8.8.8:80")
func getLocalIPs() ([]net.IP, error) {
var ips []net.IP
addrs, err := net.InterfaceAddrs()
if err != nil {
panic(err)
return ips, err
}
defer conn.Close()

localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP
for _, a := range addrs {
if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() && ipNet.IP.To4() != nil {
ips = append(ips, ipNet.IP)
}
}
return ips, nil
}

func GetFlakeUid() uint64 {
Expand Down
14 changes: 13 additions & 1 deletion api/internal/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
package utils

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetFlakeUid(t *testing.T) {
Expand All @@ -29,4 +30,15 @@ func TestGetFlakeUid(t *testing.T) {
func TestGetFlakeUidStr(t *testing.T) {
id := GetFlakeUidStr()
assert.NotEqual(t, "", id)
assert.Equal(t, 18, len(id))
}

func TestGetLocalIPs(t *testing.T) {
_, err := getLocalIPs()
assert.Equal(t, nil, err)
}

func TestSumIPs_with_nil(t *testing.T) {
total := sumIPs(nil)
assert.Equal(t, uint16(0), total)
}
262 changes: 244 additions & 18 deletions api/test/e2e/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@
package e2e

import (
"fmt"
"io/ioutil"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/tidwall/gjson"
)

func TestConsumer_with_key_auth(t *testing.T) {
Expand All @@ -29,19 +35,19 @@ func TestConsumer_with_key_auth(t *testing.T) {
Method: http.MethodPut,
Path: "/apisix/admin/routes/r1",
Body: `{
"uri": "/hello",
"plugins": {
"key-auth": {}
},
"upstream": {
"type": "roundrobin",
"nodes": [{
"uri": "/hello",
"plugins": {
"key-auth": {}
},
"upstream": {
"type": "roundrobin",
"nodes": [{
"host": "172.16.238.20",
"port": 1980,
"weight": 1
}]
}
}`,
}
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
Expand All @@ -52,22 +58,22 @@ func TestConsumer_with_key_auth(t *testing.T) {
Path: "/hello",
ExpectStatus: http.StatusUnauthorized,
ExpectBody: "Missing API key found in request",
Sleep: sleepTime, //sleep x millisecond before verify route
Sleep: sleepTime * 2,
},
{
caseDesc: "create consumer",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/consumers",
Method: http.MethodPut,
Body: `{
"username": "jack",
"plugins": {
"key-auth": {
"key": "auth-one"
}
},
"desc": "test description"
}`,
"username": "jack",
"plugins": {
"key-auth": {
"key": "auth-one"
}
},
"desc": "test description"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
Expand Down Expand Up @@ -131,3 +137,223 @@ func TestConsumer_with_key_auth(t *testing.T) {
testCaseCheck(tc)
}
}

func TestConsumer_with_notexist_plugin(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create consumer with not exist plugin",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/consumers",
Method: http.MethodPut,
Body: `{
"username": "jack",
"plugins": {
"key-authaa": {
"key": "auth-one"
}
},
"desc": "test description"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusBadRequest,
ExpectBody: "schema validate failed: schema not found, path: plugins.key-authaa",
},
{
caseDesc: "verify the consumer",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/consumers/jack",
Method: http.MethodGet,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusNotFound,
Sleep: sleepTime,
},
}

for _, tc := range tests {
testCaseCheck(tc)
}
}

func TestConsumer_add_consumer_with_labels(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create the consumer",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/consumers",
Method: http.MethodPut,
Body: `{
"username": "jack",
"labels": {
"build":"16",
"env":"production",
"version":"v2"
},
"plugins": {
"key-auth": {
"key": "auth-two"
}
},
"desc": "test description"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "verify the consumer",
Object: ManagerApiExpect(t),
Method: http.MethodGet,
Path: "/apisix/admin/consumers/jack",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
ExpectBody: "\"username\":\"jack\",\"desc\":\"test description\",\"plugins\":{\"key-auth\":{\"key\":\"auth-two\"}},\"labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"}",
Sleep: sleepTime,
},
{
caseDesc: "create the route",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/routes/r1",
Body: `{
"uri": "/hello",
"plugins": {
"key-auth": {}
},
"upstream": {
"type": "roundrobin",
"nodes": [{
"host": "172.16.238.20",
"port": 1980,
"weight": 1
}]
}
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "hit the route with correct apikey",
Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/hello",
Headers: map[string]string{"apikey": "auth-two"},
ExpectStatus: http.StatusOK,
Sleep: sleepTime,
},
{
caseDesc: "delete the consumer",
Object: ManagerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/consumers/jack",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "delete the route",
Object: ManagerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/routes/r1",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
}

for _, tc := range tests {
testCaseCheck(tc)
}
}

func TestConsumer_with_createtime_updatetime(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create the consumer",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/consumers",
Method: http.MethodPut,
Body: `{
"username":"jack",
"desc": "new consumer"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
Sleep: sleepTime,
},
}
for _, tc := range tests {
testCaseCheck(tc)
}

basepath := "http://127.0.0.1:9000/apisix/admin/consumers"
time.Sleep(time.Duration(1) * time.Second)

// get the consumer, save createtime and updatetime
request, _ := http.NewRequest("GET", basepath+"/jack", nil)
request.Header.Add("Authorization", token)
resp, err := http.DefaultClient.Do(request)
if err != nil {
fmt.Printf("server not responding %s", err.Error())
}
defer resp.Body.Close()
respBody, _ := ioutil.ReadAll(resp.Body)
createtime := gjson.Get(string(respBody), "data.create_time")
updatetime := gjson.Get(string(respBody), "data.update_time")

// wait 1 second so the update_time should be different
time.Sleep(time.Duration(1) * time.Second)

tests = []HttpTestCase{
{
caseDesc: "update the consumer",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/consumers",
Method: http.MethodPut,
Body: `{
"username":"jack",
"desc": "updated consumer"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
Sleep: sleepTime,
},
}

for _, tc := range tests {
testCaseCheck(tc)
}

// get the consumer
request, _ = http.NewRequest("GET", basepath+"/jack", nil)
request.Header.Add("Authorization", token)
resp, _ = http.DefaultClient.Do(request)
respBody, _ = ioutil.ReadAll(resp.Body)
createtime2 := gjson.Get(string(respBody), "data.create_time")
updatetime2 := gjson.Get(string(respBody), "data.update_time")

// verify the consumer and compare result
assert.Equal(t, "updated consumer", gjson.Get(string(respBody), "data.desc").String())
assert.Equal(t, createtime.String(), createtime2.String())
assert.NotEqual(t, updatetime.String(), updatetime2.String())

tests = []HttpTestCase{
{
caseDesc: "delete the consumer",
Object: ManagerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/consumers/jack",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "after delete consumer verify it again",
Object: ManagerApiExpect(t),
Method: http.MethodGet,
Path: "/apisix/admin/consumers/jack",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusNotFound,
Sleep: sleepTime,
},
}

for _, tc := range tests {
testCaseCheck(tc)
}
}
Loading

0 comments on commit 5622010

Please sign in to comment.