Skip to content

Commit

Permalink
Merge pull request #145 from seipan/feat-monitors-test
Browse files Browse the repository at this point in the history
Add test code for create,delete update in monitors.go
  • Loading branch information
ne-sachirou committed Jun 21, 2023
2 parents b2e5f06 + e31a2eb commit 83d5bef
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 83d5bef

Please sign in to comment.