open-nomad/client/consul_test.go

203 lines
4.4 KiB
Go
Raw Normal View History

2015-11-18 22:19:58 +00:00
package client
import (
"github.com/hashicorp/nomad/nomad/structs"
"log"
"os"
"testing"
"time"
)
2015-11-24 20:34:26 +00:00
func newConsulService() *ConsulService {
logger := log.New(os.Stdout, "logger: ", log.Lshortfile)
c, _ := NewConsulService(logger, "", "", "", false, false)
return c
}
func newTask() *structs.Task {
var services []*structs.Service
return &structs.Task{
Name: "redis",
Services: services,
Resources: &structs.Resources{
Networks: []*structs.NetworkResource{
{
IP: "10.10.0.1",
DynamicPorts: []structs.Port{{"db", 20413}},
},
},
},
}
}
2015-11-24 20:34:26 +00:00
func TestConsul_MakeChecks(t *testing.T) {
2015-11-18 22:19:58 +00:00
service := &structs.Service{
Id: "Foo",
Name: "Bar",
Checks: []structs.ServiceCheck{
{
Type: "http",
Path: "/foo/bar",
Interval: 10 * time.Second,
Timeout: 2 * time.Second,
},
{
Type: "http",
Protocol: "https",
Path: "/foo/bar",
Interval: 10 * time.Second,
Timeout: 2 * time.Second,
},
2015-11-18 22:19:58 +00:00
{
Type: "tcp",
Interval: 10 * time.Second,
Timeout: 2 * time.Second,
},
},
}
2015-11-24 20:34:26 +00:00
c := newConsulService()
2015-11-18 22:19:58 +00:00
checks := c.makeChecks(service, "10.10.0.1", 8090)
if checks[0].HTTP != "http://10.10.0.1:8090/foo/bar" {
t.Fatalf("Invalid http url for check: %v", checks[0].HTTP)
}
if checks[1].HTTP != "https://10.10.0.1:8090/foo/bar" {
t.Fatalf("Invalid http url for check: %v", checks[0].HTTP)
}
if checks[2].TCP != "10.10.0.1:8090" {
2015-11-18 22:19:58 +00:00
t.Fatalf("Invalid tcp check: %v", checks[0].TCP)
}
}
2015-11-24 20:34:26 +00:00
func TestConsul_InvalidPortLabelForService(t *testing.T) {
task := &structs.Task{
Name: "foo",
Driver: "docker",
Resources: &structs.Resources{
CPU: 500,
MemoryMB: 1024,
DiskMB: 1024,
IOPS: 10,
Networks: []*structs.NetworkResource{
{
Device: "eth0",
CIDR: "255.255.0.0/16",
MBits: 10,
ReservedPorts: []structs.Port{
{
Label: "http",
Value: 8080,
},
{
Label: "ssh",
Value: 2026,
},
},
},
},
},
}
service := &structs.Service{
Id: "service-id",
Name: "foo",
Tags: []string{"a", "b"},
PortLabel: "https",
Checks: make([]structs.ServiceCheck, 0),
}
2015-11-24 20:34:26 +00:00
c := newConsulService()
if err := c.registerService(service, task, "allocid"); err == nil {
t.Fatalf("Service should be invalid")
}
}
2015-11-24 20:34:26 +00:00
2015-11-24 22:37:14 +00:00
func TestConsul_Services_Deleted_From_Task(t *testing.T) {
c := newConsulService()
task := structs.Task{
2015-11-25 01:33:04 +00:00
Name: "redis",
Services: []*structs.Service{
&structs.Service{
Name: "example-cache-redis",
Tags: []string{"global"},
PortLabel: "db",
},
},
Resources: &structs.Resources{
Networks: []*structs.NetworkResource{
{
IP: "10.10.0.1",
DynamicPorts: []structs.Port{{"db", 20413}},
},
},
},
2015-11-24 22:37:14 +00:00
}
2015-11-25 01:33:04 +00:00
c.Register(&task, "1")
2015-11-25 20:06:14 +00:00
if len(c.trackedServices) != 1 {
t.Fatalf("Expected tracked services: %v, Actual: %v", 1, len(c.trackedServices))
}
2015-11-25 01:33:04 +00:00
task.Services = []*structs.Service{}
2015-11-24 20:34:26 +00:00
2015-11-24 22:37:14 +00:00
c.performSync(c.client.Agent())
if len(c.trackedServices) != 0 {
2015-11-25 20:06:14 +00:00
t.Fatalf("Expected tracked services: %v, Actual: %v", 0, len(c.trackedServices))
2015-11-24 22:37:14 +00:00
}
}
func TestConsul_Service_Should_Be_Re_Reregistered_On_Change(t *testing.T) {
c := newConsulService()
2015-11-25 20:06:14 +00:00
task := newTask()
2015-11-24 22:37:14 +00:00
s1 := structs.Service{
Id: "1-example-cache-redis",
Name: "example-cache-redis",
Tags: []string{"global"},
PortLabel: "db",
}
task.Services = append(task.Services, &s1)
2015-11-25 20:06:14 +00:00
c.Register(task, "1")
2015-11-24 22:37:14 +00:00
s1.Tags = []string{"frontcache"}
c.performSync(c.client.Agent())
if len(c.trackedServices) != 1 {
t.Fatal("We should be tracking one service")
}
if c.trackedServices[s1.Id].service.Tags[0] != "frontcache" {
t.Fatalf("Tag is %v, expected %v", c.trackedServices[s1.Id].service.Tags[0], "frontcache")
}
2015-11-24 20:34:26 +00:00
}
func TestConsul_AddCheck_To_Service(t *testing.T) {
c := newConsulService()
task := newTask()
var checks []structs.ServiceCheck
s1 := structs.Service{
Id: "1-example-cache-redis",
Name: "example-cache-redis",
Tags: []string{"global"},
PortLabel: "db",
Checks: checks,
}
task.Services = append(task.Services, &s1)
c.Register(task, "1")
check1 := structs.ServiceCheck{
Name: "alive",
Type: "tcp",
Interval: 10 * time.Second,
Timeout: 5 * time.Second,
}
s1.Checks = append(s1.Checks, check1)
c.performSync(c.client.Agent())
if len(c.trackedChecks) != 1 {
t.Fatalf("Expected tracked checks: %v, actual: %v", 1, len(c.trackedChecks))
}
}