diff options
author | Lvbo163 <lv.bo163@zte.com.cn> | 2018-03-07 15:55:31 +0800 |
---|---|---|
committer | Lvbo163 <lv.bo163@zte.com.cn> | 2018-03-07 15:55:31 +0800 |
commit | b2b8cf15b00fff79137e69d3c52be1dc148f0c30 (patch) | |
tree | 82443d3fd08119c9e9998a62459084f38f1c63cd | |
parent | 1b8a72ebc9e0e86022b91cf459fbed0442cb6d3a (diff) |
add ut for msb register
Issue-ID: MSB-171
Change-Id: I694b5bbbe6e920d989a1d56ca4466ada61336ca7
Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
-rw-r--r-- | src/kube2msb/msb_client_test.go | 49 |
1 files changed, 49 insertions, 0 deletions
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) +} |