refactor(mesos) be testable.
This commit is contained in:
parent
e9d2124885
commit
17137ba3e7
4 changed files with 466 additions and 302 deletions
|
@ -1,113 +1,145 @@
|
|||
package mesos
|
||||
|
||||
import (
|
||||
"github.com/containous/traefik/log"
|
||||
"testing"
|
||||
|
||||
"github.com/mesosphere/mesos-dns/records/state"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// test helpers
|
||||
|
||||
type (
|
||||
taskOpt func(*state.Task)
|
||||
statusOpt func(*state.Status)
|
||||
)
|
||||
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")))
|
||||
|
||||
func task(opts ...taskOpt) state.Task {
|
||||
var t state.Task
|
||||
for _, opt := range opts {
|
||||
opt(&t)
|
||||
}
|
||||
return t
|
||||
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)
|
||||
}
|
||||
|
||||
func statuses(st ...state.Status) taskOpt {
|
||||
return func(t *state.Task) {
|
||||
t.Statuses = append(t.Statuses, st...)
|
||||
func aTask(id string, ops ...func(*state.Task)) state.Task {
|
||||
ts := &state.Task{ID: id}
|
||||
for _, op := range ops {
|
||||
op(ts)
|
||||
}
|
||||
return *ts
|
||||
}
|
||||
|
||||
func withIP(ip string) func(*state.Task) {
|
||||
return func(task *state.Task) {
|
||||
task.SlaveIP = ip
|
||||
}
|
||||
}
|
||||
|
||||
func discovery(dp state.DiscoveryInfo) taskOpt {
|
||||
return func(t *state.Task) {
|
||||
t.DiscoveryInfo = dp
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
func setLabels(kvs ...string) taskOpt {
|
||||
return func(t *state.Task) {
|
||||
if len(kvs)%2 != 0 {
|
||||
panic("odd number")
|
||||
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)
|
||||
}
|
||||
|
||||
for i := 0; i < len(kvs); i += 2 {
|
||||
var label = state.Label{Key: kvs[i], Value: kvs[i+1]}
|
||||
log.Debugf("Label1.1 : %v", label)
|
||||
t.Labels = append(t.Labels, label)
|
||||
log.Debugf("Label1.2 : %v", t.Labels)
|
||||
info.Ports = state.Ports{
|
||||
DiscoveryPorts: ports,
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func status(opts ...statusOpt) state.Status {
|
||||
var s state.Status
|
||||
for _, opt := range opts {
|
||||
opt(&s)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func setDiscoveryPort(proto string, port int, name string) state.DiscoveryInfo {
|
||||
|
||||
dp := state.DiscoveryPort{
|
||||
Protocol: proto,
|
||||
Number: port,
|
||||
Name: name,
|
||||
}
|
||||
|
||||
discoveryPorts := []state.DiscoveryPort{dp}
|
||||
|
||||
ports := state.Ports{
|
||||
DiscoveryPorts: discoveryPorts,
|
||||
}
|
||||
|
||||
return state.DiscoveryInfo{
|
||||
Ports: ports,
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
func setDiscoveryPorts(proto1 string, port1 int, name1 string, proto2 string, port2 int, name2 string) state.DiscoveryInfo {
|
||||
func withPortTCP(port int, name string) func(port *state.DiscoveryPort) {
|
||||
return withPort("TCP", port, name)
|
||||
}
|
||||
|
||||
dp1 := state.DiscoveryPort{
|
||||
Protocol: proto1,
|
||||
Number: port1,
|
||||
Name: name1,
|
||||
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)
|
||||
}
|
||||
|
||||
dp2 := state.DiscoveryPort{
|
||||
Protocol: proto2,
|
||||
Number: port2,
|
||||
Name: name2,
|
||||
}
|
||||
|
||||
discoveryPorts := []state.DiscoveryPort{dp1, dp2}
|
||||
|
||||
ports := state.Ports{
|
||||
DiscoveryPorts: discoveryPorts,
|
||||
}
|
||||
|
||||
return state.DiscoveryInfo{
|
||||
Ports: ports,
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func setState(st string) statusOpt {
|
||||
return func(s *state.Status) {
|
||||
s.State = st
|
||||
func withHealthy(st bool) func(*state.Status) {
|
||||
return func(status *state.Status) {
|
||||
status.Healthy = Bool(st)
|
||||
}
|
||||
}
|
||||
|
||||
func setHealthy(b bool) statusOpt {
|
||||
return func(s *state.Status) {
|
||||
s.Healthy = &b
|
||||
func withState(st string) func(*state.Status) {
|
||||
return func(status *state.Status) {
|
||||
status.State = st
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func Bool(v bool) *bool {
|
||||
return &v
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue