-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_test.go
130 lines (115 loc) · 3.92 KB
/
api_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestCheckIn(t *testing.T) {
expected := `{"packages":null,"system_updates":{"regular":0,"security":0},"operating_system":{"name":"","uid":"","version":"","interfaces":null,"hostname":"","cpu_information":{"name":"","count":0},"memory_information":{"total":""}},"sources":null,"installed_packages":null,"group":"","custom_hostname":"","hostname":"","reboot_required":false}`
Convey("Checking in via the API", t, func(ctx C) {
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
body, _ := ioutil.ReadAll(r.Body)
ctx.So(string(body), ShouldEqual, expected)
w.WriteHeader(200)
}
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
api = SyswardApi{httpClient: http.Client{}}
c := new(MockConfig)
c.On("agentCheckinUrl").Return(server.URL)
config = c
api.CheckIn(AgentData{})
})
}
func TestApiJobFailure(t *testing.T) {
Convey("Job failure should send the job data to the server", t, func() {
postedBody := ""
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
body, _ := ioutil.ReadAll(r.Body)
postedBody = string(body)
w.WriteHeader(200)
}
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
api = SyswardApi{httpClient: http.Client{}}
c := new(MockConfig)
c.On("fetchJobPostbackUrl").Return(server.URL)
config = c
api.JobFailure(Job{JobId: 1}, "failed")
c.Mock.AssertExpectations(t)
So(postedBody, ShouldEqual, `{"job_id":1,"status":"failure","error_message":"failed"}`)
})
}
func TestApiJobPostBack(t *testing.T) {
Convey("Accepting a job post back", t, func() {
postedBody := ""
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
body, _ := ioutil.ReadAll(r.Body)
postedBody = string(body)
w.WriteHeader(200)
}
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
api = SyswardApi{httpClient: http.Client{}}
c := new(MockConfig)
c.On("fetchJobPostbackUrl").Return(server.URL)
config = c
api.JobPostBack(Job{JobId: 1})
c.Mock.AssertExpectations(t)
So(postedBody, ShouldEqual, `{"job_id":1,"status":"success"}`)
server.Close()
})
}
func TestApiCheckIn(t *testing.T) {
Convey("Geting a succesful a list of jobs", t, func() {
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, "[]")
}
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
api = SyswardApi{httpClient: http.Client{}}
c := new(MockConfig)
c.On("fetchJobUrl", getSystemUID()).Return(server.URL)
config = c
So(api.GetJobs(), ShouldEqual, "[]")
c.Mock.AssertExpectations(t)
server.Close()
})
Convey("Getting a list of jobs errors out", t, func() {
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(500)
io.WriteString(w, "[]")
}
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
api = SyswardApi{httpClient: http.Client{}}
c := new(MockConfig)
c.On("fetchJobUrl", getSystemUID()).Return(server.URL)
config = c
So(api.GetJobs(), ShouldEqual, "")
c.Mock.AssertExpectations(t)
server.Close()
})
Convey("Getting a list of jobs gives invalid body", t, func() {
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
}
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
api = SyswardApi{httpClient: http.Client{}}
c := new(MockConfig)
c.On("fetchJobUrl", getSystemUID()).Return(server.URL)
config = c
So(api.GetJobs(), ShouldEqual, "")
c.Mock.AssertExpectations(t)
server.Close()
})
}