Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests for creating HTTPS, IMAP and custom port monitors #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/testdata/requestNewCustomPortMonitor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"api_key": "dummy",
"format": "json",
"friendly_name": "My custom port monitor",
"url": "example.com",
"type": 4,
"sub_type": 99,
"port": 8443,
"alert_contacts": "3_0_0-5_0_0-7_0_0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
"friendly_name": "My test monitor",
"url": "http://example.com",
"type": 1,
"port": 80,
"alert_contacts": "3_0_0-5_0_0-7_0_0"
}
8 changes: 8 additions & 0 deletions pkg/testdata/requestNewHttpsMonitor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"api_key": "dummy",
"format": "json",
"friendly_name": "My HTTPS test monitor",
"url": "https://example.com",
"type": 1,
"alert_contacts": "3_0_0-5_0_0-7_0_0"
}
9 changes: 9 additions & 0 deletions pkg/testdata/requestNewImapPortMonitor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"api_key": "dummy",
"format": "json",
"friendly_name": "My IMAP port monitor",
"url": "example.com",
"type": 4,
"sub_type": 6,
"alert_contacts": "3_0_0-5_0_0-7_0_0"
}
172 changes: 113 additions & 59 deletions pkg/uptimerobot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,66 +75,120 @@ func TestUnmarshalMonitor(t *testing.T) {

func TestCreate(t *testing.T) {
t.Parallel()
client := New("dummy")
// force test coverage of the client's dump functionality
client.Debug = ioutil.Discard
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("want POST request, got %q", r.Method)
}
wantURL := "/v2/newMonitor"
if r.URL.EscapedPath() != wantURL {
t.Errorf("want %q, got %q", wantURL, r.URL.EscapedPath())
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
r.Body.Close()
want, err := ioutil.ReadFile("testdata/requestNewMonitor.json")
if err != nil {
t.Fatal(err)
}
// Convert the received body and the expected body to maps, for
// ease of comparison
wantMap := map[string]interface{}{}
err = json.Unmarshal(want, &wantMap)
if err != nil {
t.Fatal(err)
}
bodyMap := map[string]interface{}{}
err = json.Unmarshal(body, &bodyMap)
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(wantMap, bodyMap) {
t.Error(cmp.Diff(wantMap, bodyMap))
}
w.WriteHeader(http.StatusOK)
data, err := os.Open("testdata/newMonitor.json")
if err != nil {
t.Fatal(err)
}
defer data.Close()
io.Copy(w, data)
}))
defer ts.Close()
client.HTTPClient = ts.Client()
client.URL = ts.URL
create := Monitor{
FriendlyName: "My test monitor",
URL: "http://example.com",
Type: TypeHTTP,
Port: 80,
AlertContacts: []string{"3", "5", "7"},
}
got, err := client.CreateMonitor(create)
if err != nil {
t.Error(err)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a house style tip; I don't use blank lines within functions in this project.

tcs := []struct {
name string
input Monitor
requestFile string
responseFile string
}{
{
name: "Simple HTTP",
input: Monitor{
FriendlyName: "My test monitor",
URL: "http://example.com",
Type: TypeHTTP,
AlertContacts: []string{"3", "5", "7"},
},
requestFile: "testdata/requestNewHttpMonitor.json",
responseFile: "testdata/newMonitor.json",
},
{
name: "Simple HTTPS",
input: Monitor{
FriendlyName: "My HTTPS test monitor",
URL: "https://example.com",
Type: TypeHTTP,
AlertContacts: []string{"3", "5", "7"},
},
requestFile: "testdata/requestNewHttpsMonitor.json",
responseFile: "testdata/newMonitor.json",
},
{
name: "IMAP port",
input: Monitor{
FriendlyName: "My IMAP port monitor",
URL: "example.com",
Type: TypePort,
SubType: SubTypeIMAP,
AlertContacts: []string{"3", "5", "7"},
},
requestFile: "testdata/requestNewImapPortMonitor.json",
responseFile: "testdata/newMonitor.json",
},
{
name: "Custom port",
input: Monitor{
FriendlyName: "My custom port monitor",
URL: "example.com",
Type: TypePort,
SubType: SubTypeCustomPort,
Port: 8443,
AlertContacts: []string{"3", "5", "7"},
},
requestFile: "testdata/requestNewCustomPortMonitor.json",
responseFile: "testdata/newMonitor.json",
},
}
var want int64 = 777810874
if !cmp.Equal(want, got) {
t.Error(cmp.Diff(want, got))

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Watch out, there's a subtle bug here: https://gist.github.com/posener/92a55c4cd441fc5e5e85f27bca008721

In fact, we just end up running the Custom port test four times, and none of the others.

client := New("dummy")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably move the client setup outside the t.Run() function, shouldn't we?

// force test coverage of the client's dump functionality
client.Debug = ioutil.Discard
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("want POST request, got %q", r.Method)
}
wantURL := "/v2/newMonitor"
if r.URL.EscapedPath() != wantURL {
t.Errorf("want %q, got %q", wantURL, r.URL.EscapedPath())
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
r.Body.Close()
want, err := ioutil.ReadFile(tc.requestFile)
if err != nil {
t.Fatal(err)
}
// Convert the received body and the expected body to maps, for
// ease of comparison
wantMap := map[string]interface{}{}
err = json.Unmarshal(want, &wantMap)
if err != nil {
t.Fatal(err)
}
bodyMap := map[string]interface{}{}
err = json.Unmarshal(body, &bodyMap)
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(wantMap, bodyMap) {
t.Error(cmp.Diff(wantMap, bodyMap))
}
w.WriteHeader(http.StatusOK)
data, err := os.Open(tc.responseFile)
if err != nil {
t.Fatal(err)
}
defer data.Close()
io.Copy(w, data)
}))
defer ts.Close()
client.HTTPClient = ts.Client()
client.URL = ts.URL
got, err := client.CreateMonitor(tc.input)
if err != nil {
t.Error(err)
}
var want int64 = 777810874
if !cmp.Equal(want, got) {
t.Error(cmp.Diff(want, got))
}
})
}
}

Expand Down