adding additional unit tests for getDataCenter() in consul (#6192)

* adding additional unit tests for getDataCenter() in consul

Signed-off-by: Jean-Baptiste Le Duigou <jb.leduigou@gmail.com>

* Consult Tests : update comments to start with uppercase and end with point

Signed-off-by: Jean-Baptiste Le Duigou <jb.leduigou@gmail.com>

* Consult Test : using table-driven tests

Signed-off-by: Jean-Baptiste Le Duigou <jb.leduigou@gmail.com>

* Consul Test : cleaner syntax

Signed-off-by: Jean-Baptiste Le Duigou <jb.leduigou@gmail.com>

* Consul Test : even cleaner syntax

Signed-off-by: Jean-Baptiste Le Duigou <jb.leduigou@gmail.com>

* Consul Test : update comments

Signed-off-by: Jean-Baptiste Le Duigou <jb.leduigou@gmail.com>

* Fixing naming convention by removing underscore in function name

Signed-off-by: Jean-Baptiste Le Duigou <jb.leduigou@gmail.com>

* Removing duplicated test case for getDatacenter()

Signed-off-by: Jean-Baptiste Le Duigou <jb.leduigou@gmail.com>
This commit is contained in:
Jean-Baptiste Le Duigou 2019-11-15 14:52:39 +01:00 committed by Simon Pasquier
parent 9f35c75c21
commit 5973227434

View file

@ -297,3 +297,47 @@ func TestAllOptions(t *testing.T) {
checkOneTarget(t, <-ch)
cancel()
}
func TestGetDatacenterShouldReturnError(t *testing.T) {
for _, tc := range []struct {
handler func(http.ResponseWriter, *http.Request)
errMessage string
}{
{
// Define a handler that will return status 500.
handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
},
errMessage: "Unexpected response code: 500 ()",
},
{
// Define a handler that will return incorrect response.
handler: func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"Config": {"Not-Datacenter": "test-dc"}}`))
},
errMessage: "invalid value '<nil>' for Config.Datacenter",
},
} {
stub := httptest.NewServer(http.HandlerFunc(tc.handler))
stuburl, err := url.Parse(stub.URL)
testutil.Ok(t, err)
config := &SDConfig{
Server: stuburl.Host,
Token: "fake-token",
RefreshInterval: model.Duration(1 * time.Second),
}
defer stub.Close()
d := newDiscovery(t, config)
// Should be empty if not initialized.
testutil.Equals(t, "", d.clientDatacenter)
err = d.getDatacenter()
// An error should be returned.
testutil.Equals(t, tc.errMessage, err.Error())
// Should still be empty.
testutil.Equals(t, "", d.clientDatacenter)
}
}