Skip to content

Commit

Permalink
Merge pull request #6 from allegro/add_tests
Browse files Browse the repository at this point in the history
Add tests for marathon.go and refactor
  • Loading branch information
janisz committed Dec 8, 2015
2 parents a2e1317 + 9e0b2b1 commit 6c68a44
Show file tree
Hide file tree
Showing 12 changed files with 795 additions and 392 deletions.
19 changes: 19 additions & 0 deletions apps/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package apps

import (
"encoding/json"
"github.com/allegro/marathon-consul/tasks"
)

Expand All @@ -18,9 +19,27 @@ type AppWrapper struct {
App App `json:"app"`
}

type AppsResponse struct {
Apps []*App `json:"apps"`
}

type App struct {
Labels map[string]string `json:"labels"`
HealthChecks []HealthCheck `json:"healthChecks"`
ID string `json:"id"`
Tasks []tasks.Task `json:"tasks"`
}

func ParseApps(jsonBlob []byte) ([]*App, error) {
apps := &AppsResponse{}
err := json.Unmarshal(jsonBlob, apps)

return apps.Apps, err
}

func ParseApp(jsonBlob []byte) (*App, error) {
wrapper := &AppWrapper{}
err := json.Unmarshal(jsonBlob, wrapper)

return &wrapper.App, err
}
106 changes: 106 additions & 0 deletions apps/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"app": {
"id": "/myapp",
"cmd": "env && python -m SimpleHTTPServer $PORT0",
"args": null,
"user": null,
"env": {},
"instances": 2,
"cpus": 0.1,
"mem": 32.0,
"disk": 0.0,
"executor": "",
"constraints": [],
"uris": [],
"storeUrls": [],
"ports": [
10002,
1,
2,
3
],
"requirePorts": false,
"backoffSeconds": 1,
"backoffFactor": 1.15,
"maxLaunchDelaySeconds": 3600,
"container": null,
"healthChecks": [
{
"path": "/",
"protocol": "HTTP",
"portIndex": 0,
"gracePeriodSeconds": 10,
"intervalSeconds": 5,
"timeoutSeconds": 10,
"maxConsecutiveFailures": 3,
"ignoreHttp1xx": false
}
],
"dependencies": [],
"upgradeStrategy": {
"minimumHealthCapacity": 1.0,
"maximumOverCapacity": 1.0
},
"labels": {
"consul": "true",
"public": "tag"
},
"version": "2015-12-01T10:03:32.003Z",
"tasksStaged": 0,
"tasksRunning": 2,
"tasksHealthy": 2,
"tasksUnhealthy": 0,
"deployments": [],
"tasks": [
{
"id": "myapp.cc49ccc1-9812-11e5-a06e-56847afe9799",
"host": "10.141.141.10",
"ports": [
31678,
31679,
31680,
31681
],
"startedAt": "2015-12-01T10:03:40.966Z",
"stagedAt": "2015-12-01T10:03:40.890Z",
"version": "2015-12-01T10:03:32.003Z",
"appId": "/myapp",
"healthCheckResults": [
{
"alive": true,
"consecutiveFailures": 0,
"firstSuccess": "2015-12-01T10:03:42.324Z",
"lastFailure": null,
"lastSuccess": "2015-12-01T10:03:42.324Z",
"taskId": "myapp.cc49ccc1-9812-11e5-a06e-56847afe9799"
}
]
},
{
"id": "myapp.c8b449f0-9812-11e5-a06e-56847afe9799",
"host": "10.141.141.10",
"ports": [
31307,
31308,
31309,
31310
],
"startedAt": "2015-12-01T10:03:34.945Z",
"stagedAt": "2015-12-01T10:03:34.877Z",
"version": "2015-12-01T10:03:32.003Z",
"appId": "/myapp",
"healthCheckResults": [
{
"alive": true,
"consecutiveFailures": 0,
"firstSuccess": "2015-12-01T10:03:37.313Z",
"lastFailure": null,
"lastSuccess": "2015-12-01T10:03:42.337Z",
"taskId": "myapp.c8b449f0-9812-11e5-a06e-56847afe9799"
}
]
}
],
"lastTaskFailure": null
}
}
88 changes: 88 additions & 0 deletions apps/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package apps

import (
"github.com/allegro/marathon-consul/tasks"
"github.com/stretchr/testify/assert"
"io/ioutil"
"testing"
)

func TestParseApps(t *testing.T) {
t.Parallel()

appBlob, _ := ioutil.ReadFile("apps.json")

expected := []*App{
&App{
HealthChecks: []HealthCheck{
HealthCheck{
Path: "/",
PortIndex: 0,
Protocol: "HTTP",
GracePeriodSeconds: 5,
IntervalSeconds: 20,
TimeoutSeconds: 20,
MaxConsecutiveFailures: 3,
},
},
ID: "/bridged-webapp",
Tasks: []tasks.Task{
tasks.Task{
ID: "test.47de43bd-1a81-11e5-bdb6-e6cb6734eaf8",
AppID: "/test",
Host: "192.168.2.114",
Ports: []int{31315},
HealthCheckResults: []tasks.HealthCheckResult{tasks.HealthCheckResult{Alive: true}},
},
tasks.Task{
ID: "test.4453212c-1a81-11e5-bdb6-e6cb6734eaf8",
AppID: "/test",
Host: "192.168.2.114",
Ports: []int{31797},
},
},
},
}
apps, err := ParseApps(appBlob)
assert.NoError(t, err)
assert.Len(t, apps, 1)
assert.Equal(t, expected, apps)
}

func TestParseApp(t *testing.T) {
t.Parallel()

appBlob, _ := ioutil.ReadFile("app.json")

expected := &App{Labels: map[string]string{"consul": "true", "public": "tag"},
HealthChecks: []HealthCheck{HealthCheck{Path: "/",
PortIndex: 0,
Protocol: "HTTP",
GracePeriodSeconds: 10,
IntervalSeconds: 5,
TimeoutSeconds: 10,
MaxConsecutiveFailures: 3}},
ID: "/myapp",
Tasks: []tasks.Task{tasks.Task{
ID: "myapp.cc49ccc1-9812-11e5-a06e-56847afe9799",
AppID: "/myapp",
Host: "10.141.141.10",
Ports: []int{31678,
31679,
31680,
31681},
HealthCheckResults: []tasks.HealthCheckResult{tasks.HealthCheckResult{Alive: true}}},
tasks.Task{
ID: "myapp.c8b449f0-9812-11e5-a06e-56847afe9799",
AppID: "/myapp",
Host: "10.141.141.10",
Ports: []int{31307,
31308,
31309,
31310},
HealthCheckResults: []tasks.HealthCheckResult{tasks.HealthCheckResult{Alive: true}}}}}

app, err := ParseApp(appBlob)
assert.NoError(t, err)
assert.Equal(t, expected, app)
}
104 changes: 104 additions & 0 deletions apps/apps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"apps": [
{
"args": null,
"backoffFactor": 1.15,
"backoffSeconds": 1,
"maxLaunchDelaySeconds": 3600,
"cmd": "python3 -m http.server 8080",
"constraints": [],
"container": {
"docker": {
"image": "python:3",
"network": "BRIDGE",
"portMappings": [
{
"containerPort": 8080,
"hostPort": 0,
"servicePort": 9000,
"protocol": "tcp"
},
{
"containerPort": 161,
"hostPort": 0,
"protocol": "udp"
}
]
},
"type": "DOCKER",
"volumes": []
},
"cpus": 0.5,
"dependencies": [],
"deployments": [],
"disk": 0.0,
"env": {},
"executor": "",
"healthChecks": [
{
"command": null,
"gracePeriodSeconds": 5,
"intervalSeconds": 20,
"maxConsecutiveFailures": 3,
"path": "/",
"portIndex": 0,
"protocol": "HTTP",
"timeoutSeconds": 20
}
],
"id": "/bridged-webapp",
"instances": 2,
"mem": 64.0,
"ports": [
10000,
10001
],
"requirePorts": false,
"storeUrls": [],
"tasksRunning": 2,
"tasksHealthy": 2,
"tasksUnhealthy": 0,
"tasksStaged": 0,
"upgradeStrategy": {
"minimumHealthCapacity": 1.0
},
"uris": [],
"user": null,
"version": "2014-09-25T02:26:59.256Z",
"tasks": [
{
"appId": "/test",
"host": "192.168.2.114",
"id": "test.47de43bd-1a81-11e5-bdb6-e6cb6734eaf8",
"ports": [
31315
],
"stagedAt": "2015-06-24T14:57:06.353Z",
"startedAt": "2015-06-24T14:57:06.466Z",
"version": "2015-06-24T14:56:57.466Z",
"healthCheckResults": [
{
"alive": true,
"consecutiveFailures": 0,
"firstSuccess": "2015-11-28T18:21:11.957Z",
"lastFailure": null,
"lastSuccess": "2015-11-30T10:08:19.477Z",
"taskId": "bridged-webapp.a9b051fb-95fc-11e5-9571-02818b42970e"
}
]
},
{
"appId": "/test",
"host": "192.168.2.114",
"id": "test.4453212c-1a81-11e5-bdb6-e6cb6734eaf8",
"ports": [
31797
],
"stagedAt": "2015-06-24T14:57:00.474Z",
"startedAt": "2015-06-24T14:57:00.611Z",
"version": "2015-06-24T14:56:57.466Z"
}
]
}
]
}
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (config *Config) parseFlags() {
flag.StringVar(&config.Marathon.Protocol, "marathon-protocol", "http", "Marathon protocol (http or https)")
flag.StringVar(&config.Marathon.Username, "marathon-username", "", "Marathon username for basic auth")
flag.StringVar(&config.Marathon.Password, "marathon-password", "", "Marathon password for basic auth")
flag.BoolVar(&config.Marathon.VerifySsl, "marathon-ssl-verify", true, "Verify certificates when connecting via SSL")

// Metrics
flag.StringVar(&config.Metrics.Target, "metrics-target", "stdout", "Metrics destination stdout or graphite")
Expand Down
9 changes: 5 additions & 4 deletions marathon/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package marathon

type Config struct {
Location string
Protocol string
Username string
Password string
Location string
Protocol string
Username string
Password string
VerifySsl bool
}
Loading

0 comments on commit 6c68a44

Please sign in to comment.