From b2b8cf15b00fff79137e69d3c52be1dc148f0c30 Mon Sep 17 00:00:00 2001 From: Lvbo163 Date: Wed, 7 Mar 2018 15:55:31 +0800 Subject: add ut for msb register Issue-ID: MSB-171 Change-Id: I694b5bbbe6e920d989a1d56ca4466ada61336ca7 Signed-off-by: Lvbo163 --- src/kube2msb/msb_client_test.go | 49 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/kube2msb/msb_client_test.go b/src/kube2msb/msb_client_test.go index bdc7278..72ff6fb 100644 --- a/src/kube2msb/msb_client_test.go +++ b/src/kube2msb/msb_client_test.go @@ -16,7 +16,9 @@ limitations under the License. package main import ( + "encoding/json" "fmt" + "io/ioutil" "net/http" "net/http/httptest" "testing" @@ -69,3 +71,50 @@ func TestServiceAnnotation2ServiceUnit(t *testing.T) { t.Errorf("ServiceAnnotation2ServiceUnit error") } } + +func TestRegister(t *testing.T) { + serviceInfo := `[{ + "ip":"192.168.1.10", + "port":"8080", + "serviceName":"resgisterTest", + "version":"v1", + "url":"/register/test", + "protocol":"http", + "lb_policy":"random", + "visualRange":"1", + "path":"rt", + "enable_ssl":true + }]` + + handler := func(res http.ResponseWriter, req *http.Request) { + if req.Method != "POST" { + t.Errorf("Register() request method should be 'Post' not %s", req.Method) + } else if urlPrefix != req.URL.String() { + t.Errorf("Register() url should be %s, not %s", urlPrefix, req.URL) + } else { + body, err := ioutil.ReadAll(req.Body) + if err != nil { + t.Errorf("Register() fail to read request body") + } + var su = ServiceUnit{} + parseError := json.Unmarshal([]byte(body), &su) + if parseError != nil { + t.Errorf("Register() request body can not parse to ServiceUnit, %s", body) + return + } else { + res.WriteHeader(200) + res.Header().Set("Content-Type", "application/xml") + fmt.Fprintln(res, "regist success") + } + } + + } + server := httptest.NewServer(http.HandlerFunc(handler)) + defer server.Close() + + client := MSBAgent{ + url: server.URL, + } + + client.Register(serviceInfo) +} -- cgit 1.2.3-korg