2017-12-02 18:29:34 +00:00
|
|
|
package mesos
|
|
|
|
|
|
|
|
import (
|
2018-01-10 03:05:33 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-12-02 18:29:34 +00:00
|
|
|
"github.com/mesosphere/mesos-dns/records/state"
|
2018-01-10 03:05:33 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-12-02 18:29:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// test helpers
|
|
|
|
|
2018-01-10 03:05:33 +00:00
|
|
|
func TestBuilder(t *testing.T) {
|
|
|
|
result := aTask("ID1",
|
|
|
|
withIP("10.10.10.10"),
|
|
|
|
withLabel("foo", "bar"),
|
|
|
|
withLabel("fii", "bar"),
|
|
|
|
withLabel("fuu", "bar"),
|
|
|
|
withInfo("name1",
|
|
|
|
withPorts(withPort("TCP", 80, "p"),
|
|
|
|
withPortTCP(81, "n"))),
|
|
|
|
withStatus(withHealthy(true), withState("a")))
|
|
|
|
|
|
|
|
expected := state.Task{
|
|
|
|
FrameworkID: "",
|
|
|
|
ID: "ID1",
|
|
|
|
SlaveIP: "10.10.10.10",
|
|
|
|
Name: "",
|
|
|
|
SlaveID: "",
|
|
|
|
State: "",
|
|
|
|
Statuses: []state.Status{{
|
|
|
|
State: "a",
|
|
|
|
Healthy: Bool(true),
|
|
|
|
ContainerStatus: state.ContainerStatus{},
|
|
|
|
}},
|
|
|
|
DiscoveryInfo: state.DiscoveryInfo{
|
|
|
|
Name: "name1",
|
|
|
|
Labels: struct {
|
|
|
|
Labels []state.Label "json:\"labels\""
|
|
|
|
}{},
|
|
|
|
Ports: state.Ports{DiscoveryPorts: []state.DiscoveryPort{
|
|
|
|
{Protocol: "TCP", Number: 80, Name: "p"},
|
|
|
|
{Protocol: "TCP", Number: 81, Name: "n"}}}},
|
|
|
|
Labels: []state.Label{
|
|
|
|
{Key: "foo", Value: "bar"},
|
|
|
|
{Key: "fii", Value: "bar"},
|
|
|
|
{Key: "fuu", Value: "bar"}}}
|
|
|
|
|
|
|
|
assert.Equal(t, expected, result)
|
|
|
|
}
|
2018-03-28 22:01:24 +00:00
|
|
|
|
|
|
|
func aTaskData(id string, ops ...func(*state.Task)) taskData {
|
|
|
|
ts := &state.Task{ID: id}
|
|
|
|
for _, op := range ops {
|
|
|
|
op(ts)
|
|
|
|
}
|
|
|
|
return taskData{Task: *ts, TraefikLabels: extractLabels(*ts)}
|
|
|
|
}
|
2017-12-02 18:29:34 +00:00
|
|
|
|
2018-01-10 03:05:33 +00:00
|
|
|
func aTask(id string, ops ...func(*state.Task)) state.Task {
|
|
|
|
ts := &state.Task{ID: id}
|
|
|
|
for _, op := range ops {
|
|
|
|
op(ts)
|
2017-12-02 18:29:34 +00:00
|
|
|
}
|
2018-01-10 03:05:33 +00:00
|
|
|
return *ts
|
2017-12-02 18:29:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-10 03:05:33 +00:00
|
|
|
func withIP(ip string) func(*state.Task) {
|
|
|
|
return func(task *state.Task) {
|
|
|
|
task.SlaveIP = ip
|
2017-12-02 18:29:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 03:05:33 +00:00
|
|
|
func withInfo(name string, ops ...func(*state.DiscoveryInfo)) func(*state.Task) {
|
|
|
|
return func(task *state.Task) {
|
|
|
|
info := &state.DiscoveryInfo{Name: name}
|
|
|
|
for _, op := range ops {
|
|
|
|
op(info)
|
|
|
|
}
|
|
|
|
task.DiscoveryInfo = *info
|
2017-12-02 18:29:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 03:05:33 +00:00
|
|
|
func withPorts(ops ...func(port *state.DiscoveryPort)) func(*state.DiscoveryInfo) {
|
|
|
|
return func(info *state.DiscoveryInfo) {
|
|
|
|
var ports []state.DiscoveryPort
|
|
|
|
for _, op := range ops {
|
|
|
|
pt := &state.DiscoveryPort{}
|
|
|
|
op(pt)
|
|
|
|
ports = append(ports, *pt)
|
2017-12-02 18:29:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-10 03:05:33 +00:00
|
|
|
info.Ports = state.Ports{
|
|
|
|
DiscoveryPorts: ports,
|
2017-12-02 18:29:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 03:05:33 +00:00
|
|
|
func withPort(proto string, port int, name string) func(port *state.DiscoveryPort) {
|
|
|
|
return func(p *state.DiscoveryPort) {
|
|
|
|
p.Protocol = proto
|
|
|
|
p.Number = port
|
|
|
|
p.Name = name
|
2017-12-02 18:29:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 03:05:33 +00:00
|
|
|
func withPortTCP(port int, name string) func(port *state.DiscoveryPort) {
|
|
|
|
return withPort("TCP", port, name)
|
2017-12-02 18:29:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-10 03:05:33 +00:00
|
|
|
func withStatus(ops ...func(*state.Status)) func(*state.Task) {
|
|
|
|
return func(task *state.Task) {
|
|
|
|
st := &state.Status{}
|
|
|
|
for _, op := range ops {
|
|
|
|
op(st)
|
|
|
|
}
|
|
|
|
task.Statuses = append(task.Statuses, *st)
|
2017-12-02 18:29:34 +00:00
|
|
|
}
|
2018-01-10 03:05:33 +00:00
|
|
|
}
|
|
|
|
func withDefaultStatus(ops ...func(*state.Status)) func(*state.Task) {
|
|
|
|
return func(task *state.Task) {
|
|
|
|
for _, op := range ops {
|
|
|
|
st := &state.Status{
|
|
|
|
State: "TASK_RUNNING",
|
|
|
|
Healthy: Bool(true),
|
|
|
|
}
|
|
|
|
op(st)
|
|
|
|
task.Statuses = append(task.Statuses, *st)
|
|
|
|
}
|
2017-12-02 18:29:34 +00:00
|
|
|
}
|
2018-01-10 03:05:33 +00:00
|
|
|
}
|
2017-12-02 18:29:34 +00:00
|
|
|
|
2018-01-10 03:05:33 +00:00
|
|
|
func withHealthy(st bool) func(*state.Status) {
|
|
|
|
return func(status *state.Status) {
|
|
|
|
status.Healthy = Bool(st)
|
2017-12-02 18:29:34 +00:00
|
|
|
}
|
2018-01-10 03:05:33 +00:00
|
|
|
}
|
2017-12-02 18:29:34 +00:00
|
|
|
|
2018-01-10 03:05:33 +00:00
|
|
|
func withState(st string) func(*state.Status) {
|
|
|
|
return func(status *state.Status) {
|
|
|
|
status.State = st
|
2017-12-02 18:29:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 03:05:33 +00:00
|
|
|
func withLabel(key, value string) func(*state.Task) {
|
|
|
|
return func(task *state.Task) {
|
|
|
|
lbl := state.Label{Key: key, Value: value}
|
|
|
|
task.Labels = append(task.Labels, lbl)
|
2017-12-02 18:29:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 03:05:33 +00:00
|
|
|
func Bool(v bool) *bool {
|
|
|
|
return &v
|
2017-12-02 18:29:34 +00:00
|
|
|
}
|