forked from vladimirvivien/gomes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
masterclient_test.go
176 lines (149 loc) · 4.76 KB
/
masterclient_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
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
package gomes
import (
"code.google.com/p/goprotobuf/proto"
mesos "github.com/vladimirvivien/gomes/mesosproto"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/url"
"regexp"
"testing"
)
func TestAddressType(t *testing.T) {
addr := address("127.0.0.1:5050")
if addr != "127.0.0.1:5050" {
t.Error("Address type value not translated to string")
}
u, err := addr.AsHttpURL()
if err != nil {
t.Error("address.AsURL() failed:", err)
}
if u.Host != "127.0.0.1:5050" {
t.Error("Address.AsURL() host not converted")
}
if u.Scheme != HTTP_SCHEME {
t.Error("Address.AsURL() Scheme not converted: ", u.Scheme)
}
}
func TestRegisterFramework_BadAddr(t *testing.T) {
master := newMasterClient("localhost:1010")
framework := &mesos.FrameworkInfo{
User: proto.String("test-user"),
Name: proto.String("test-name"),
Id: &mesos.FrameworkID{Value: proto.String("test-framework-1")},
}
err := master.RegisterFramework(newSchedProcID(":7000"), framework)
if err == nil {
t.Fatal("Expecting 'Connection Refused' error, but test did not fail.")
}
}
func TestRegisterFramework(t *testing.T) {
idreg := regexp.MustCompile(`^[a-z]+\(\d+\).*$`)
// Server-side Validation
server := makeMockServer(func(rsp http.ResponseWriter, req *http.Request) {
if req.Header.Get("Connection") != "Keep-Alive" {
t.Fatalf("Expected Connection Header not found")
}
cmdPath := buildReqPath(REGISTER_FRAMEWORK_CALL)
if req.URL.Path != cmdPath {
t.Fatalf("Expected URL path not found.")
}
proc := req.Header.Get("Libprocess-From")
if !idreg.MatchString(proc) {
t.Fatalf("Libprocess-From value malformed. Got [%s]", proc)
}
data, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Fatalf("Unable to get FrameworkInfo data")
}
defer req.Body.Close()
regMsg := new(mesos.RegisterFrameworkMessage)
err = proto.Unmarshal(data, regMsg)
if err != nil {
t.Fatal("Problem unmarshaling expected RegisterFrameworkMessage")
}
info := regMsg.Framework
if info.GetUser() != "test-user" ||
info.GetName() != "test-name" ||
info.Id.GetValue() != "test-framework-1" {
t.Fatalf("Got bad FrameworkInfo values %s, %s, %s", info.User, info.Name, info.Id.Value)
}
rsp.WriteHeader(http.StatusAccepted)
//fmt.Print (rsp)
})
defer server.Close()
url, _ := url.Parse(server.URL)
// Test Data
master := newMasterClient(url.Host)
framework := &mesos.FrameworkInfo{
User: proto.String("test-user"),
Name: proto.String("test-name"),
Id: &mesos.FrameworkID{Value: proto.String("test-framework-1")},
}
master.RegisterFramework(newSchedProcID(":7000"), framework)
}
func TestUnregisterFramework(t *testing.T) {
server := makeMockServer(func(rsp http.ResponseWriter, req *http.Request) {
cmdPath := buildReqPath(UNREGISTER_FRAMEWORK_CALL)
if req.URL.Path != cmdPath {
t.Fatalf("Expected URL path not found.")
}
data, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Fatalf("Unable to get FrameworkID data")
}
defer req.Body.Close()
msg := new(mesos.UnregisterFrameworkMessage)
err = proto.Unmarshal(data, msg)
if err != nil {
t.Fatal("Problem unmarshaling UnregisterFrameworkMessage")
}
if msg.GetFrameworkId().GetValue() != "test-framework-1" {
t.Fatal("Got bad FrameworkID.")
}
})
defer server.Close()
url, _ := url.Parse(server.URL)
master := newMasterClient(url.Host)
frameworkId := &mesos.FrameworkID{Value: proto.String("test-framework-1")}
master.UnregisterFramework(newSchedProcID(":7000"), frameworkId)
}
func TestDeactivateFramework(t *testing.T) {
server := makeMockServer(func(rsp http.ResponseWriter, req *http.Request) {
cmdPath := buildReqPath(DEACTIVATE_FRAMEWORK_CALL)
if req.URL.Path != cmdPath {
t.Fatalf("Expected URL path not found.")
}
data, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Fatalf("Unable to get FrameworkID data")
}
defer req.Body.Close()
msg := new(mesos.DeactivateFrameworkMessage)
err = proto.Unmarshal(data, msg)
if err != nil {
t.Fatal("Problem unmarshaling DeactivateFrameworkMessage")
}
if msg.GetFrameworkId().GetValue() != "test-framework-1" {
t.Fatal("Got bad FrameworkID.")
}
})
defer server.Close()
url, _ := url.Parse(server.URL)
master := newMasterClient(url.Host)
frameworkId := &mesos.FrameworkID{Value: proto.String("test-framework-1")}
master.DeactivateFramework(newSchedProcID(":7000"), frameworkId)
}
func makeMockServer(handler func(rsp http.ResponseWriter, req *http.Request)) *httptest.Server {
server := httptest.NewServer(http.HandlerFunc(handler))
log.Println("Created server " + server.URL)
return server
}
func makeMockFrameworkInfo() *mesos.FrameworkInfo {
return &mesos.FrameworkInfo{
User: proto.String("test-user"),
Name: proto.String("test-name"),
Id: &mesos.FrameworkID{Value: proto.String("test-framework-1")},
}
}