Skip to content

Commit

Permalink
feat : testcode of monitors.go
Browse files Browse the repository at this point in the history
Signed-off-by: yamasaki <[email protected]>
  • Loading branch information
seipan committed Jun 20, 2023
1 parent b2e5f06 commit e31a2eb
Showing 1 changed file with 132 additions and 0 deletions.
132 changes: 132 additions & 0 deletions monitors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,138 @@ func TestGetMonitor(t *testing.T) {
t.Error("request sends json including memo but: ", m)
}
}
func TestCreateMonitor(t *testing.T) {
var (
monitorID = "2cSZzK3XfmG"
)

ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/api/v0/monitors" {
t.Errorf("request URL should be /api/v0/monitors but got: %s", req.URL.Path)
}
if req.Method != http.MethodPost {
t.Errorf("request method should be POST but got: %s", req.Method)
}

respJSON, _ := json.Marshal(map[string]interface{}{
"id": monitorID,
"type": "connectivity",
"memo": "connectivity monitor",
"scopes": []string{},
"excludeScopes": []string{},
})

res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()

client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
monitor, err := client.CreateMonitor(&MonitorConnectivity{
Type: "connectivity",
Memo: "connectivity monitor",
})
if err != nil {
t.Error("err should be nil but: ", err)
}

m, ok := monitor.(*MonitorConnectivity)
if !ok || m.Type != "connectivity" {
t.Error("request sends json including type but: ", m)
}
if m.Memo != "connectivity monitor" {
t.Error("request sends json including memo but: ", m)
}
}

func TestUpdateMonitor(t *testing.T) {
var (
monitorID = "2cSZzK3XfmG"
)

ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
u := fmt.Sprintf("/api/v0/monitors/%s", monitorID)
if req.URL.Path != u {
t.Errorf("request URL should be %v but %v:", u, req.URL.Path)
}

if req.Method != http.MethodPut {
t.Error("request method should be PUT but: ", req.Method)
}

respJSON, _ := json.Marshal(map[string]interface{}{
"id": monitorID,
"type": "connectivity",
"memo": "connectivity monitor",
"scopes": []string{},
"excludeScopes": []string{},
})

res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()

client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
monitor, err := client.UpdateMonitor(monitorID, &MonitorConnectivity{
Type: "connectivity",
Memo: "connectivity monitor",
})
if err != nil {
t.Error("err should be nil but: ", err)
}

m, ok := monitor.(*MonitorConnectivity)
if !ok || m.Type != "connectivity" {
t.Error("request sends json including type but: ", m)
}
if m.Memo != "connectivity monitor" {
t.Error("request sends json including memo but: ", m)
}
}

func TestDeleteMonitor(t *testing.T) {
var (
monitorID = "2cSZzK3XfmG"
)

ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
u := fmt.Sprintf("/api/v0/monitors/%s", monitorID)
if req.URL.Path != u {
t.Errorf("request URL should be %v but %v:", u, req.URL.Path)
}

if req.Method != http.MethodDelete {
t.Error("request method should be DELETE but: ", req.Method)
}

respJSON, _ := json.Marshal(map[string]interface{}{
"id": monitorID,
"type": "connectivity",
"memo": "connectivity monitor",
"scopes": []string{},
"excludeScopes": []string{},
})

res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()

client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
monitor, err := client.DeleteMonitor(monitorID)
if err != nil {
t.Error("err should be nil but: ", err)
}

m, ok := monitor.(*MonitorConnectivity)
if !ok || m.Type != "connectivity" {
t.Error("request sends json including type but: ", m)
}
if m.Memo != "connectivity monitor" {
t.Error("request sends json including memo but: ", m)
}
}

// ensure that it supports `"headers":[]` and headers must be nil by default.
func TestMonitorExternalHTTP_headers(t *testing.T) {
Expand Down

0 comments on commit e31a2eb

Please sign in to comment.