2017-07-17 11:42:48 +00:00
|
|
|
package marathon
|
|
|
|
|
2017-08-18 01:08:03 +00:00
|
|
|
import (
|
2017-08-21 08:46:03 +00:00
|
|
|
"strings"
|
2017-08-18 01:08:03 +00:00
|
|
|
"time"
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
"github.com/containous/traefik/old/provider/label"
|
2017-08-18 01:08:03 +00:00
|
|
|
"github.com/gambol99/go-marathon"
|
|
|
|
)
|
|
|
|
|
2017-12-02 18:27:47 +00:00
|
|
|
const testTaskName = "taskID"
|
2017-07-17 11:42:48 +00:00
|
|
|
|
2018-04-17 18:58:24 +00:00
|
|
|
func withAppData(app marathon.Application, segmentName string) appData {
|
|
|
|
segmentProperties := label.ExtractTraefikLabels(stringValueMap(app.Labels))
|
|
|
|
return appData{
|
|
|
|
Application: app,
|
|
|
|
SegmentLabels: segmentProperties[segmentName],
|
|
|
|
SegmentName: segmentName,
|
|
|
|
LinkedApps: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
// Functions related to building applications.
|
|
|
|
|
2018-03-26 13:32:04 +00:00
|
|
|
func withApplications(apps ...marathon.Application) *marathon.Applications {
|
|
|
|
return &marathon.Applications{Apps: apps}
|
|
|
|
}
|
|
|
|
|
2017-08-18 01:08:03 +00:00
|
|
|
func application(ops ...func(*marathon.Application)) marathon.Application {
|
2017-07-17 11:42:48 +00:00
|
|
|
app := marathon.Application{}
|
|
|
|
app.EmptyLabels()
|
2017-08-18 01:08:03 +00:00
|
|
|
app.Deployments = []map[string]string{}
|
|
|
|
app.ReadinessChecks = &[]marathon.ReadinessCheck{}
|
|
|
|
app.ReadinessCheckResults = &[]marathon.ReadinessCheckResult{}
|
2017-07-17 11:42:48 +00:00
|
|
|
|
|
|
|
for _, op := range ops {
|
|
|
|
op(&app)
|
|
|
|
}
|
|
|
|
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
func appID(name string) func(*marathon.Application) {
|
|
|
|
return func(app *marathon.Application) {
|
|
|
|
app.Name(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func appPorts(ports ...int) func(*marathon.Application) {
|
|
|
|
return func(app *marathon.Application) {
|
|
|
|
app.Ports = append(app.Ports, ports...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 18:27:47 +00:00
|
|
|
func withLabel(key, value string) func(*marathon.Application) {
|
2017-07-17 11:42:48 +00:00
|
|
|
return func(app *marathon.Application) {
|
|
|
|
app.AddLabel(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 09:48:04 +00:00
|
|
|
func constraint(value string) func(*marathon.Application) {
|
|
|
|
return func(app *marathon.Application) {
|
|
|
|
app.AddConstraint(strings.Split(value, ":")...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 10:28:03 +00:00
|
|
|
func withSegmentLabel(key, value string, segmentName string) func(*marathon.Application) {
|
|
|
|
if len(segmentName) == 0 {
|
|
|
|
panic("segmentName can not be empty")
|
2017-08-21 08:46:03 +00:00
|
|
|
}
|
|
|
|
|
2017-12-02 18:27:47 +00:00
|
|
|
property := strings.TrimPrefix(key, label.Prefix)
|
2017-08-21 08:46:03 +00:00
|
|
|
return func(app *marathon.Application) {
|
2018-04-04 10:28:03 +00:00
|
|
|
app.AddLabel(label.Prefix+segmentName+"."+property, value)
|
2017-08-21 08:46:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
func portDefinition(port int) func(*marathon.Application) {
|
|
|
|
return func(app *marathon.Application) {
|
|
|
|
app.AddPortDefinition(marathon.PortDefinition{
|
|
|
|
Port: &port,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 21:42:03 +00:00
|
|
|
func bridgeNetwork() func(*marathon.Application) {
|
|
|
|
return func(app *marathon.Application) {
|
|
|
|
app.SetNetwork("bridge", marathon.BridgeNetworkMode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func containerNetwork() func(*marathon.Application) {
|
|
|
|
return func(app *marathon.Application) {
|
|
|
|
app.SetNetwork("cni", marathon.ContainerNetworkMode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
func ipAddrPerTask(port int) func(*marathon.Application) {
|
|
|
|
return func(app *marathon.Application) {
|
|
|
|
p := marathon.Port{
|
|
|
|
Number: port,
|
|
|
|
Name: "port",
|
|
|
|
}
|
|
|
|
disc := marathon.Discovery{}
|
|
|
|
disc.AddPort(p)
|
|
|
|
ipAddr := marathon.IPAddressPerTask{}
|
|
|
|
ipAddr.SetDiscovery(disc)
|
|
|
|
app.SetIPAddressPerTask(ipAddr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 01:08:03 +00:00
|
|
|
func deployments(ids ...string) func(*marathon.Application) {
|
|
|
|
return func(app *marathon.Application) {
|
|
|
|
for _, id := range ids {
|
|
|
|
app.Deployments = append(app.Deployments, map[string]string{
|
|
|
|
"ID": id,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readinessCheck(timeout time.Duration) func(*marathon.Application) {
|
|
|
|
return func(app *marathon.Application) {
|
|
|
|
app.ReadinessChecks = &[]marathon.ReadinessCheck{
|
|
|
|
{
|
|
|
|
Path: "/ready",
|
|
|
|
TimeoutSeconds: int(timeout.Seconds()),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readinessCheckResult(taskID string, ready bool) func(*marathon.Application) {
|
|
|
|
return func(app *marathon.Application) {
|
|
|
|
*app.ReadinessCheckResults = append(*app.ReadinessCheckResults, marathon.ReadinessCheckResult{
|
|
|
|
TaskID: taskID,
|
|
|
|
Ready: ready,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 10:58:03 +00:00
|
|
|
func withTasks(tasks ...marathon.Task) func(*marathon.Application) {
|
|
|
|
return func(application *marathon.Application) {
|
|
|
|
for _, task := range tasks {
|
|
|
|
tu := task
|
|
|
|
application.Tasks = append(application.Tasks, &tu)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
// Functions related to building tasks.
|
|
|
|
|
2017-08-18 01:08:03 +00:00
|
|
|
func task(ops ...func(*marathon.Task)) marathon.Task {
|
2018-01-10 10:58:03 +00:00
|
|
|
t := &marathon.Task{
|
2017-08-18 01:08:03 +00:00
|
|
|
ID: testTaskName,
|
2017-07-17 11:42:48 +00:00
|
|
|
// The vast majority of tests expect the task state to be TASK_RUNNING.
|
|
|
|
State: string(taskStateRunning),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, op := range ops {
|
2018-01-10 10:58:03 +00:00
|
|
|
op(t)
|
2017-07-17 11:42:48 +00:00
|
|
|
}
|
|
|
|
|
2018-01-10 10:58:03 +00:00
|
|
|
return *t
|
|
|
|
}
|
|
|
|
|
|
|
|
func withTaskID(id string) func(*marathon.Task) {
|
|
|
|
return func(task *marathon.Task) {
|
|
|
|
task.ID = id
|
|
|
|
}
|
2017-07-17 11:42:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-18 01:08:03 +00:00
|
|
|
func localhostTask(ops ...func(*marathon.Task)) marathon.Task {
|
|
|
|
t := task(
|
2017-07-17 11:42:48 +00:00
|
|
|
host("localhost"),
|
|
|
|
ipAddresses("127.0.0.1"),
|
2018-04-04 10:28:03 +00:00
|
|
|
taskState(taskStateRunning),
|
2017-07-17 11:42:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for _, op := range ops {
|
|
|
|
op(&t)
|
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func taskPorts(ports ...int) func(*marathon.Task) {
|
|
|
|
return func(t *marathon.Task) {
|
|
|
|
t.Ports = append(t.Ports, ports...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 10:28:03 +00:00
|
|
|
func taskState(state TaskState) func(*marathon.Task) {
|
|
|
|
return func(t *marathon.Task) {
|
|
|
|
t.State = string(state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 11:42:48 +00:00
|
|
|
func host(h string) func(*marathon.Task) {
|
|
|
|
return func(t *marathon.Task) {
|
|
|
|
t.Host = h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ipAddresses(addresses ...string) func(*marathon.Task) {
|
|
|
|
return func(t *marathon.Task) {
|
|
|
|
for _, addr := range addresses {
|
|
|
|
t.IPAddresses = append(t.IPAddresses, &marathon.IPAddress{
|
|
|
|
IPAddress: addr,
|
|
|
|
Protocol: "tcp",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 01:08:03 +00:00
|
|
|
func startedAt(timestamp string) func(*marathon.Task) {
|
|
|
|
return func(t *marathon.Task) {
|
|
|
|
t.StartedAt = timestamp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startedAtFromNow(offset time.Duration) func(*marathon.Task) {
|
|
|
|
return func(t *marathon.Task) {
|
|
|
|
t.StartedAt = time.Now().Add(-offset).Format(time.RFC3339)
|
|
|
|
}
|
|
|
|
}
|