-
Notifications
You must be signed in to change notification settings - Fork 1
test: assert e2e resources through cli reads #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "fmt" | ||
| ) | ||
|
|
||
| // UpstreamNodes accepts both APISIX upstream node forms: | ||
| // {"host:port": weight} and [{"host":"...","port":...,"weight":...}]. | ||
| type UpstreamNodes map[string]interface{} | ||
|
|
||
| func (n *UpstreamNodes) UnmarshalJSON(data []byte) error { | ||
| trimmed := bytes.TrimSpace(data) | ||
| if string(trimmed) == "null" { | ||
| *n = nil | ||
| return nil | ||
| } | ||
|
|
||
| var keyed map[string]interface{} | ||
| if err := json.Unmarshal(trimmed, &keyed); err == nil { | ||
| *n = keyed | ||
| return nil | ||
| } | ||
|
|
||
| var listed []struct { | ||
| Host string `json:"host"` | ||
| Port int `json:"port"` | ||
| Weight json.RawMessage `json:"weight"` | ||
| } | ||
| if err := json.Unmarshal(trimmed, &listed); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| nodes := make(UpstreamNodes, len(listed)) | ||
| for _, node := range listed { | ||
| key := node.Host | ||
| if node.Port != 0 { | ||
| key = fmt.Sprintf("%s:%d", node.Host, node.Port) | ||
| } | ||
|
|
||
| var weight interface{} = float64(1) | ||
| weightData := bytes.TrimSpace(node.Weight) | ||
| if len(weightData) > 0 && string(weightData) != "null" { | ||
| if err := json.Unmarshal(weightData, &weight); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| nodes[key] = weight | ||
| } | ||
|
|
||
| *n = nodes | ||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestUpstreamNodesUnmarshalObject(t *testing.T) { | ||
| var nodes UpstreamNodes | ||
| if err := json.Unmarshal([]byte(`{"127.0.0.1:9080": 1}`), &nodes); err != nil { | ||
| t.Fatalf("unmarshal object nodes: %v", err) | ||
| } | ||
|
|
||
| if got := nodes["127.0.0.1:9080"]; got != float64(1) { | ||
| t.Fatalf("unexpected node weight: %#v", got) | ||
| } | ||
| } | ||
|
|
||
| func TestUpstreamNodesUnmarshalArray(t *testing.T) { | ||
| var nodes UpstreamNodes | ||
| if err := json.Unmarshal([]byte(`[{"host":"127.0.0.1","port":9080,"weight":2}]`), &nodes); err != nil { | ||
| t.Fatalf("unmarshal array nodes: %v", err) | ||
| } | ||
|
|
||
| if got := nodes["127.0.0.1:9080"]; got != float64(2) { | ||
| t.Fatalf("unexpected node weight: %#v", got) | ||
| } | ||
| } | ||
|
Comment on lines
+19
to
+28
|
||
|
|
||
| func TestUpstreamNodesUnmarshalNull(t *testing.T) { | ||
| nodes := UpstreamNodes{"127.0.0.1:9080": float64(1)} | ||
| if err := json.Unmarshal([]byte(` null `), &nodes); err != nil { | ||
| t.Fatalf("unmarshal null nodes: %v", err) | ||
| } | ||
|
|
||
| if nodes != nil { | ||
| t.Fatalf("expected nil nodes, got %#v", nodes) | ||
| } | ||
| } | ||
|
|
||
| func TestUpstreamNodesUnmarshalArrayDefaultWeight(t *testing.T) { | ||
| var nodes UpstreamNodes | ||
| if err := json.Unmarshal([]byte(`[{"host":"127.0.0.1","port":9080}]`), &nodes); err != nil { | ||
| t.Fatalf("unmarshal array nodes with default weight: %v", err) | ||
| } | ||
|
|
||
| if got := nodes["127.0.0.1:9080"]; got != float64(1) { | ||
| t.Fatalf("unexpected default node weight: %#v", got) | ||
| } | ||
| } | ||
|
|
||
| func TestRouteWithArrayUpstreamNodesUnmarshals(t *testing.T) { | ||
| var route Route | ||
| err := json.Unmarshal([]byte(`{ | ||
| "id": "route-with-array-nodes", | ||
| "uri": "/get", | ||
| "upstream": { | ||
| "type": "roundrobin", | ||
| "nodes": [{"host":"127.0.0.1","port":9080,"weight":1}] | ||
| } | ||
| }`), &route) | ||
| if err != nil { | ||
| t.Fatalf("unmarshal route: %v", err) | ||
| } | ||
|
|
||
| if route.Upstream == nil { | ||
| t.Fatal("expected upstream") | ||
| } | ||
| if got := route.Upstream.Nodes["127.0.0.1:9080"]; got != float64(1) { | ||
| t.Fatalf("unexpected route node weight: %#v", got) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UnmarshalJSONcomparesstring(data) == "null"without trimming whitespace. JSON values passed toUnmarshalJSONmay include leading/trailing spaces/newlines, so inputs like" null "will currently fall through and error. Considerbytes.TrimSpace(data)(and similarly trimmingnode.Weight) before comparing againstnull/"null"to make decoding robust.