1 Star 0 Fork 0

zhuchance / kubernetes

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
fixtures.go 5.15 KB
一键复制 编辑 原始数据 按行查看 历史
Sreekanth Pothanis 提交于 2015-09-16 12:00 . updated gophercloud
package nodes
import (
"fmt"
"net/http"
"strconv"
"testing"
th "github.com/rackspace/gophercloud/testhelper"
fake "github.com/rackspace/gophercloud/testhelper/client"
)
func _rootURL(lbID int) string {
return "/loadbalancers/" + strconv.Itoa(lbID) + "/nodes"
}
func _nodeURL(lbID, nodeID int) string {
return _rootURL(lbID) + "/" + strconv.Itoa(nodeID)
}
func mockListResponse(t *testing.T, lbID int) {
th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"nodes": [
{
"id": 410,
"address": "10.1.1.1",
"port": 80,
"condition": "ENABLED",
"status": "ONLINE",
"weight": 3,
"type": "PRIMARY"
},
{
"id": 411,
"address": "10.1.1.2",
"port": 80,
"condition": "ENABLED",
"status": "ONLINE",
"weight": 8,
"type": "SECONDARY"
}
]
}
`)
})
}
func mockCreateResponse(t *testing.T, lbID int) {
th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestJSONRequest(t, r, `
{
"nodes": [
{
"address": "10.2.2.3",
"port": 80,
"condition": "ENABLED",
"type": "PRIMARY"
},
{
"address": "10.2.2.4",
"port": 81,
"condition": "ENABLED",
"type": "SECONDARY"
}
]
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusAccepted)
fmt.Fprintf(w, `
{
"nodes": [
{
"address": "10.2.2.3",
"id": 185,
"port": 80,
"status": "ONLINE",
"condition": "ENABLED",
"weight": 1,
"type": "PRIMARY"
},
{
"address": "10.2.2.4",
"id": 186,
"port": 81,
"status": "ONLINE",
"condition": "ENABLED",
"weight": 1,
"type": "SECONDARY"
}
]
}
`)
})
}
func mockCreateErrResponse(t *testing.T, lbID int) {
th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestJSONRequest(t, r, `
{
"nodes": [
{
"address": "10.2.2.3",
"port": 80,
"condition": "ENABLED",
"type": "PRIMARY"
},
{
"address": "10.2.2.4",
"port": 81,
"condition": "ENABLED",
"type": "SECONDARY"
}
]
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(422) // Unprocessable Entity
fmt.Fprintf(w, `
{
"code": 422,
"message": "Load Balancer '%d' has a status of 'PENDING_UPDATE' and is considered immutable."
}
`, lbID)
})
}
func mockBatchDeleteResponse(t *testing.T, lbID int, ids []int) {
th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
r.ParseForm()
for k, v := range ids {
fids := r.Form["id"]
th.AssertEquals(t, strconv.Itoa(v), fids[k])
}
w.WriteHeader(http.StatusAccepted)
})
}
func mockDeleteResponse(t *testing.T, lbID, nodeID int) {
th.Mux.HandleFunc(_nodeURL(lbID, nodeID), func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.WriteHeader(http.StatusAccepted)
})
}
func mockGetResponse(t *testing.T, lbID, nodeID int) {
th.Mux.HandleFunc(_nodeURL(lbID, nodeID), func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"node": {
"id": 410,
"address": "10.1.1.1",
"port": 80,
"condition": "ENABLED",
"status": "ONLINE",
"weight": 12,
"type": "PRIMARY"
}
}
`)
})
}
func mockUpdateResponse(t *testing.T, lbID, nodeID int) {
th.Mux.HandleFunc(_nodeURL(lbID, nodeID), func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestJSONRequest(t, r, `
{
"node": {
"condition": "DRAINING",
"weight": 10,
"type": "SECONDARY"
}
}
`)
w.WriteHeader(http.StatusAccepted)
})
}
func mockListEventsResponse(t *testing.T, lbID int) {
th.Mux.HandleFunc(_rootURL(lbID)+"/events", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"nodeServiceEvents": [
{
"detailedMessage": "Node is ok",
"nodeId": 373,
"id": 7,
"type": "UPDATE_NODE",
"description": "Node '373' status changed to 'ONLINE' for load balancer '323'",
"category": "UPDATE",
"severity": "INFO",
"relativeUri": "/406271/loadbalancers/323/nodes/373/events",
"accountId": 406271,
"loadbalancerId": 323,
"title": "Node Status Updated",
"author": "Rackspace Cloud",
"created": "10-30-2012 10:18:23"
}
]
}
`)
})
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/meoom/kubernetes.git
git@gitee.com:meoom/kubernetes.git
meoom
kubernetes
kubernetes
v1.2.3

搜索帮助

344bd9b3 5694891 D2dac590 5694891