traefik/provider/mesos/mesos_helper_test.go

146 lines
3.1 KiB
Go
Raw Normal View History

package mesos
import (
2018-01-10 03:05:33 +00:00
"testing"
"github.com/mesosphere/mesos-dns/records/state"
2018-01-10 03:05:33 +00:00
"github.com/stretchr/testify/assert"
)
// 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-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)
}
2018-01-10 03:05:33 +00:00
return *ts
}
2018-01-10 03:05:33 +00:00
func withIP(ip string) func(*state.Task) {
return func(task *state.Task) {
task.SlaveIP = ip
}
}
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
}
}
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)
}
2018-01-10 03:05:33 +00:00
info.Ports = state.Ports{
DiscoveryPorts: ports,
}
}
}
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
}
}
2018-01-10 03:05:33 +00:00
func withPortTCP(port int, name string) func(port *state.DiscoveryPort) {
return withPort("TCP", port, name)
}
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)
}
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)
}
}
2018-01-10 03:05:33 +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)
}
2018-01-10 03:05:33 +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
}
}
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)
}
}
2018-01-10 03:05:33 +00:00
func Bool(v bool) *bool {
return &v
}