2015-08-17 10:49:10 -07:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-04-25 06:30:11 -07:00
|
|
|
package marathon
|
2015-07-16 05:02:07 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-08-20 08:18:46 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2016-02-18 08:26:27 -08:00
|
|
|
"golang.org/x/net/context"
|
2015-07-16 05:02:07 -07:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
var marathonValidLabel = map[string]string{"prometheus": "yes"}
|
|
|
|
|
2016-04-25 06:30:11 -07:00
|
|
|
func newTestDiscovery(client AppListClient) (chan []*config.TargetGroup, *Discovery) {
|
2016-02-18 08:26:27 -08:00
|
|
|
ch := make(chan []*config.TargetGroup)
|
2016-04-25 06:30:11 -07:00
|
|
|
md := &Discovery{
|
2015-07-16 05:02:07 -07:00
|
|
|
Servers: []string{"http://localhost:8080"},
|
2016-04-30 12:04:19 -07:00
|
|
|
Client: client,
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|
2015-07-16 05:02:07 -07:00
|
|
|
return ch, md
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonSDHandleError(t *testing.T) {
|
|
|
|
var errTesting = errors.New("testing failure")
|
2016-04-25 06:30:11 -07:00
|
|
|
ch, md := newTestDiscovery(func(url string) (*AppList, error) {
|
2015-07-16 05:02:07 -07:00
|
|
|
return nil, errTesting
|
|
|
|
})
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case tg := <-ch:
|
|
|
|
t.Fatalf("Got group: %s", tg)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}()
|
2016-04-25 07:32:04 -07:00
|
|
|
err := md.updateServices(context.Background(), ch)
|
2015-07-16 05:02:07 -07:00
|
|
|
if err != errTesting {
|
|
|
|
t.Fatalf("Expected error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonSDEmptyList(t *testing.T) {
|
2016-04-25 06:30:11 -07:00
|
|
|
ch, md := newTestDiscovery(func(url string) (*AppList, error) {
|
|
|
|
return &AppList{}, nil
|
2015-07-16 05:02:07 -07:00
|
|
|
})
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case tg := <-ch:
|
2016-02-18 08:26:27 -08:00
|
|
|
if len(tg) > 0 {
|
|
|
|
t.Fatalf("Got group: %v", tg)
|
|
|
|
}
|
2015-07-16 05:02:07 -07:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}()
|
2016-04-25 07:32:04 -07:00
|
|
|
err := md.updateServices(context.Background(), ch)
|
2015-07-16 05:02:07 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Got error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-25 06:30:11 -07:00
|
|
|
func marathonTestAppList(labels map[string]string, runningTasks int) *AppList {
|
|
|
|
task := Task{
|
2015-07-16 05:02:07 -07:00
|
|
|
ID: "test-task-1",
|
|
|
|
Host: "mesos-slave1",
|
|
|
|
Ports: []uint32{31000},
|
|
|
|
}
|
2016-04-25 06:30:11 -07:00
|
|
|
docker := DockerContainer{Image: "repo/image:tag"}
|
|
|
|
container := Container{Docker: docker}
|
|
|
|
app := App{
|
2015-07-16 05:02:07 -07:00
|
|
|
ID: "test-service",
|
2016-04-25 06:30:11 -07:00
|
|
|
Tasks: []Task{task},
|
2015-07-16 05:02:07 -07:00
|
|
|
RunningTasks: runningTasks,
|
|
|
|
Labels: labels,
|
|
|
|
Container: container,
|
|
|
|
}
|
2016-04-25 06:30:11 -07:00
|
|
|
return &AppList{
|
|
|
|
Apps: []App{app},
|
2015-07-16 05:02:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonSDSendGroup(t *testing.T) {
|
2016-04-25 06:30:11 -07:00
|
|
|
ch, md := newTestDiscovery(func(url string) (*AppList, error) {
|
2015-07-16 05:02:07 -07:00
|
|
|
return marathonTestAppList(marathonValidLabel, 1), nil
|
|
|
|
})
|
|
|
|
go func() {
|
|
|
|
select {
|
2016-02-18 08:26:27 -08:00
|
|
|
case tgs := <-ch:
|
|
|
|
tg := tgs[0]
|
|
|
|
|
2015-08-07 04:18:19 -07:00
|
|
|
if tg.Source != "test-service" {
|
2015-07-16 05:02:07 -07:00
|
|
|
t.Fatalf("Wrong target group name: %s", tg.Source)
|
|
|
|
}
|
|
|
|
if len(tg.Targets) != 1 {
|
|
|
|
t.Fatalf("Wrong number of targets: %v", tg.Targets)
|
|
|
|
}
|
|
|
|
tgt := tg.Targets[0]
|
2015-08-20 08:18:46 -07:00
|
|
|
if tgt[model.AddressLabel] != "mesos-slave1:31000" {
|
|
|
|
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
2015-07-16 05:02:07 -07:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatal("Did not get a target group.")
|
|
|
|
}
|
|
|
|
}()
|
2016-04-25 07:32:04 -07:00
|
|
|
err := md.updateServices(context.Background(), ch)
|
2015-07-16 05:02:07 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Got error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonSDRemoveApp(t *testing.T) {
|
2016-04-25 06:30:11 -07:00
|
|
|
ch, md := newTestDiscovery(func(url string) (*AppList, error) {
|
2015-07-16 05:02:07 -07:00
|
|
|
return marathonTestAppList(marathonValidLabel, 1), nil
|
|
|
|
})
|
2016-02-18 08:26:27 -08:00
|
|
|
|
2015-07-16 05:02:07 -07:00
|
|
|
go func() {
|
2016-02-18 08:26:27 -08:00
|
|
|
up1 := (<-ch)[0]
|
|
|
|
up2 := (<-ch)[0]
|
2015-07-16 05:02:07 -07:00
|
|
|
if up2.Source != up1.Source {
|
|
|
|
t.Fatalf("Source is different: %s", up2)
|
|
|
|
if len(up2.Targets) > 0 {
|
|
|
|
t.Fatalf("Got a non-empty target set: %s", up2.Targets)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2016-04-25 07:32:04 -07:00
|
|
|
err := md.updateServices(context.Background(), ch)
|
2015-07-16 05:02:07 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Got error on first update: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-04-25 06:30:11 -07:00
|
|
|
md.Client = func(url string) (*AppList, error) {
|
2015-07-16 05:02:07 -07:00
|
|
|
return marathonTestAppList(marathonValidLabel, 0), nil
|
|
|
|
}
|
2016-04-25 07:32:04 -07:00
|
|
|
err = md.updateServices(context.Background(), ch)
|
2015-07-16 05:02:07 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Got error on second update: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonSDRunAndStop(t *testing.T) {
|
2016-04-25 06:30:11 -07:00
|
|
|
ch, md := newTestDiscovery(func(url string) (*AppList, error) {
|
2015-07-16 05:02:07 -07:00
|
|
|
return marathonTestAppList(marathonValidLabel, 1), nil
|
|
|
|
})
|
2016-04-25 06:30:11 -07:00
|
|
|
md.RefreshInterval = time.Millisecond * 10
|
2016-02-18 08:26:27 -08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2015-07-16 05:02:07 -07:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-ch:
|
2016-02-18 08:26:27 -08:00
|
|
|
cancel()
|
2016-04-25 06:30:11 -07:00
|
|
|
case <-time.After(md.RefreshInterval * 3):
|
2016-02-18 08:26:27 -08:00
|
|
|
cancel()
|
2015-07-16 05:02:07 -07:00
|
|
|
t.Fatalf("Update took too long.")
|
|
|
|
}
|
|
|
|
}()
|
2016-02-18 08:26:27 -08:00
|
|
|
|
|
|
|
md.Run(ctx, ch)
|
|
|
|
|
2015-07-16 05:02:07 -07:00
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
default:
|
|
|
|
t.Fatalf("Channel not closed.")
|
|
|
|
}
|
|
|
|
}
|
2016-06-13 02:24:03 -07:00
|
|
|
|
|
|
|
func marathonTestZeroTaskPortAppList(labels map[string]string, runningTasks int) *AppList {
|
|
|
|
task := Task{
|
|
|
|
ID: "test-task-2",
|
|
|
|
Host: "mesos-slave-2",
|
|
|
|
Ports: []uint32{},
|
|
|
|
}
|
|
|
|
docker := DockerContainer{Image: "repo/image:tag"}
|
|
|
|
container := Container{Docker: docker}
|
|
|
|
app := App{
|
|
|
|
ID: "test-service-zero-ports",
|
|
|
|
Tasks: []Task{task},
|
|
|
|
RunningTasks: runningTasks,
|
|
|
|
Labels: labels,
|
|
|
|
Container: container,
|
|
|
|
}
|
|
|
|
return &AppList{
|
|
|
|
Apps: []App{app},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonZeroTaskPorts(t *testing.T) {
|
|
|
|
ch, md := newTestDiscovery(func(url string) (*AppList, error) {
|
|
|
|
return marathonTestZeroTaskPortAppList(marathonValidLabel, 1), nil
|
|
|
|
})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case tgs := <-ch:
|
|
|
|
tg := tgs[0]
|
|
|
|
|
|
|
|
if tg.Source != "test-service-zero-ports" {
|
|
|
|
t.Fatalf("Wrong target group name: %s", tg.Source)
|
|
|
|
}
|
|
|
|
if len(tg.Targets) != 0 {
|
|
|
|
t.Fatalf("Wrong number of targets: %v", tg.Targets)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatal("Did not get a target group.")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
err := md.updateServices(context.Background(), ch)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Got error: %s", err)
|
|
|
|
}
|
|
|
|
}
|