-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmistifyagent_test.go
More file actions
228 lines (207 loc) · 5.73 KB
/
Copy pathmistifyagent_test.go
File metadata and controls
228 lines (207 loc) · 5.73 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package lochness_test
import (
"encoding/json"
"fmt"
"net"
"net/http"
"net/http/httptest"
"net/url"
"regexp"
"strconv"
"testing"
"github.com/mistifyio/lochness"
"github.com/mistifyio/lochness/internal/tests/common"
magent "github.com/mistifyio/mistify-agent"
mnet "github.com/mistifyio/util/net"
"github.com/pborman/uuid"
"github.com/stretchr/testify/suite"
)
func TestMistifyAgent(t *testing.T) {
suite.Run(t, new(MistifyAgentSuite))
}
type MistifyAgentSuite struct {
common.Suite
agent *lochness.MistifyAgent
api *httptest.Server
guest *lochness.Guest
hypervisor *lochness.Hypervisor
}
func (s *MistifyAgentSuite) SetupSuite() {
s.Suite.SetupSuite()
s.api = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := r.URL.String()
actionRegexp := regexp.MustCompile(fmt.Sprintf("/guests/%s/\\w+", s.guest.ID))
jobRegexp := regexp.MustCompile("/jobs/\\w+")
switch {
case path == fmt.Sprintf("/guests/%s", s.guest.ID):
guestBytes, _ := json.Marshal(s.guest)
_, _ = w.Write(guestBytes)
case path == "/guests", actionRegexp.MatchString(path), path == "/images":
w.Header().Set("X-Guest-Job-ID", uuid.New())
w.WriteHeader(http.StatusAccepted)
case jobRegexp.MatchString(path):
job := &magent.Job{
Status: magent.Complete,
}
jobBytes, _ := json.Marshal(job)
_, _ = w.Write(jobBytes)
}
}))
}
func (s *MistifyAgentSuite) SetupTest() {
s.Suite.SetupTest()
u, _ := url.Parse(s.api.URL)
host, sPort, _ := mnet.SplitHostPort(u.Host)
port, _ := strconv.Atoi(sPort)
s.agent = s.Context.NewMistifyAgent(port)
s.hypervisor, s.guest = s.NewHypervisorWithGuest()
s.hypervisor.IP = net.ParseIP(host)
_ = s.hypervisor.Save()
}
func (s *MistifyAgentSuite) TearDownSuite() {
s.api.Close()
s.Suite.TearDownSuite()
}
func (s *MistifyAgentSuite) TestNewMistifyAgent() {
s.NotNil(s.agent)
}
func (s *MistifyAgentSuite) TestGetGuest() {
tests := []struct {
description string
id string
expectedErr bool
}{
{"missing id", "", true},
{"nonuuid id", "asdf", true},
{"nonexistent id", uuid.New(), true},
{"existing id", s.guest.ID, false},
}
for _, test := range tests {
msg := s.Messager(test.description)
guest, err := s.agent.GetGuest(test.id)
if test.expectedErr {
s.Error(err, msg("should fail"))
s.Nil(guest, msg("fail should not return guest"))
} else {
s.NoError(err, msg("should succeed"))
s.Equal(s.guest.ID, guest.ID, msg("should return guest"))
}
}
}
func (s *MistifyAgentSuite) TestCreateGuest() {
tests := []struct {
description string
id string
expectedErr bool
}{
{"missing id", "", true},
{"nonuuid id", "asdf", true},
{"nonexistent id", uuid.New(), true},
{"existing id", s.guest.ID, false},
}
for _, test := range tests {
msg := s.Messager(test.description)
jobID, err := s.agent.CreateGuest(test.id)
if test.expectedErr {
s.Error(err, msg("should fail"))
s.Nil(uuid.Parse(jobID), msg("fail should not return jobID"))
} else {
s.NoError(err, msg("should succeed"))
s.NotNil(uuid.Parse(jobID), msg("should return jobID"))
}
}
}
func (s *MistifyAgentSuite) TestDeleteGuest() {
tests := []struct {
description string
id string
expectedErr bool
}{
{"missing id", "", true},
{"nonuuid id", "asdf", true},
{"nonexistent id", uuid.New(), true},
{"existing id", s.guest.ID, false},
}
for _, test := range tests {
msg := s.Messager(test.description)
jobID, err := s.agent.DeleteGuest(test.id)
if test.expectedErr {
s.Error(err, msg("should fail"))
s.Nil(uuid.Parse(jobID), msg("fail should not return jobID"))
} else {
s.NoError(err, msg("should succeed"))
s.NotNil(uuid.Parse(jobID), msg("should return jobID"))
}
}
}
func (s *MistifyAgentSuite) TestGuestAction() {
tests := []struct {
description string
id string
expectedErr bool
}{
{"missing id", "", true},
{"nonuuid id", "asdf", true},
{"nonexistent id", uuid.New(), true},
{"existing id", s.guest.ID, false},
}
for _, test := range tests {
msg := s.Messager(test.description)
jobID, err := s.agent.GuestAction(test.id, "restart")
if test.expectedErr {
s.Error(err, msg("should fail"))
s.Nil(uuid.Parse(jobID), msg("fail should not return jobID"))
} else {
s.NoError(err, msg("should succeed"))
s.NotNil(uuid.Parse(jobID), msg("should return jobID"))
}
}
}
func (s *MistifyAgentSuite) TestCheckJobStatus() {
tests := []struct {
description string
guestID string
jobID string
expectedErr bool
}{
{"missing guest id", "", uuid.New(), true},
{"nonuuid guest id", "asdf", uuid.New(), true},
{"nonexistent guest id", uuid.New(), uuid.New(), true},
{"existing guest id, missing job id", s.guest.ID, "", true},
{"existing guest id, uuid job id", s.guest.ID, uuid.New(), false},
}
for _, test := range tests {
msg := s.Messager(test.description)
done, err := s.agent.CheckJobStatus(test.guestID, test.jobID)
if test.expectedErr {
s.Error(err, msg("should fail"))
s.False(done, msg("fail should not return done"))
} else {
s.NoError(err, msg("should succeed"))
s.True(done, msg("should return done"))
}
}
}
func (s *MistifyAgentSuite) TestFetchImage() {
tests := []struct {
description string
id string
expectedErr bool
}{
{"missing id", "", true},
{"nonuuid id", "asdf", true},
{"nonexistent id", uuid.New(), true},
{"existing id", s.guest.ID, false},
}
for _, test := range tests {
msg := s.Messager(test.description)
jobID, err := s.agent.FetchImage(test.id)
if test.expectedErr {
s.Error(err, msg("should fail"))
s.Nil(uuid.Parse(jobID), msg("fail should not return jobID"))
} else {
s.NoError(err, msg("should succeed"))
s.NotNil(uuid.Parse(jobID), msg("should return jobID"))
}
}
}