Fixed the tests

This commit is contained in:
Diptanu Choudhury 2016-03-23 18:39:17 -07:00
parent 31baa6ce4b
commit f6354ba114
2 changed files with 68 additions and 92 deletions

View file

@ -89,6 +89,7 @@ func NewConsulService(config *ConsulConfig, logger *log.Logger, allocID string)
}
consulService := ConsulService{
client: c,
allocID: allocID,
logger: logger,
trackedServices: make(map[string]*consul.AgentService),
trackedChecks: make(map[string]*structs.ServiceCheck),

View file

@ -12,10 +12,33 @@ import (
"github.com/hashicorp/nomad/nomad/structs"
)
var logger = log.New(os.Stdout, "", log.LstdFlags)
var (
logger = log.New(os.Stdout, "", log.LstdFlags)
check1 = structs.ServiceCheck{
Name: "check-foo-1",
Type: structs.ServiceCheckTCP,
Interval: 30 * time.Second,
Timeout: 5 * time.Second,
}
service1 = structs.Service{
Name: "foo-1",
Tags: []string{"tag1", "tag2"},
PortLabel: "port1",
Checks: []*structs.ServiceCheck{
&check1,
},
}
service2 = structs.Service{
Name: "foo-2",
Tags: []string{"tag1", "tag2"},
PortLabel: "port2",
}
)
func TestConsulServiceRegisterServices(t *testing.T) {
cs, err := NewConsulService(&ConsulConfig{}, logger, "12")
allocID := "12"
cs, err := NewConsulService(&ConsulConfig{}, logger, allocID)
if err != nil {
t.Fatalf("Err: %v", err)
}
@ -23,61 +46,25 @@ func TestConsulServiceRegisterServices(t *testing.T) {
if !cs.consulPresent() {
return
}
task := structs.Task{
Name: "foo",
Services: []*structs.Service{
&structs.Service{
Name: "foo-1",
Tags: []string{"tag1", "tag2"},
PortLabel: "port1",
Checks: []*structs.ServiceCheck{
&structs.ServiceCheck{
Name: "check-foo-1",
Type: structs.ServiceCheckTCP,
Interval: 30 * time.Second,
Timeout: 5 * time.Second,
},
},
},
&structs.Service{
Name: "foo-2",
Tags: []string{"tag1", "tag2"},
PortLabel: "port2",
},
},
Resources: &structs.Resources{
Networks: []*structs.NetworkResource{
&structs.NetworkResource{
IP: "10.10.11.5",
DynamicPorts: []structs.Port{
structs.Port{
Label: "port1",
Value: 20002,
},
structs.Port{
Label: "port2",
Value: 20003,
},
},
},
},
},
}
if err := cs.SyncTask(&task); err != nil {
task := mockTask()
if err := cs.SyncTask(task); err != nil {
t.Fatalf("err: %v", err)
}
defer cs.Shutdown()
if err := servicesPresent(t, []string{"1", "2"}, cs); err != nil {
service1ID := service1.ID(allocID, task.Name)
service2ID := service2.ID(allocID, task.Name)
if err := servicesPresent(t, []string{service1ID, service2ID}, cs); err != nil {
t.Fatalf("err : %v", err)
}
if err := checksPresent(t, []string{"100"}, cs); err != nil {
if err := checksPresent(t, []string{check1.Hash(service1ID)}, cs); err != nil {
t.Fatalf("err : %v", err)
}
}
func TestConsulServiceUpdateService(t *testing.T) {
cs, err := NewConsulService(&ConsulConfig{}, logger, "12")
allocID := "12"
cs, err := NewConsulService(&ConsulConfig{}, logger, allocID)
if err != nil {
t.Fatalf("Err: %v", err)
}
@ -85,48 +72,9 @@ func TestConsulServiceUpdateService(t *testing.T) {
if !cs.consulPresent() {
return
}
task := structs.Task{
Name: "foo",
Services: []*structs.Service{
&structs.Service{
Name: "foo-1",
Tags: []string{"tag1", "tag2"},
PortLabel: "port1",
Checks: []*structs.ServiceCheck{
&structs.ServiceCheck{
Name: "check-foo-1",
Type: structs.ServiceCheckTCP,
Interval: 30 * time.Second,
Timeout: 5 * time.Second,
},
},
},
&structs.Service{
Name: "foo-2",
Tags: []string{"tag1", "tag2"},
PortLabel: "port2",
},
},
Resources: &structs.Resources{
Networks: []*structs.NetworkResource{
&structs.NetworkResource{
IP: "10.10.11.5",
DynamicPorts: []structs.Port{
structs.Port{
Label: "port1",
Value: 20002,
},
structs.Port{
Label: "port2",
Value: 20003,
},
},
},
},
},
}
if err := cs.SyncTask(&task); err != nil {
task := mockTask()
if err := cs.SyncTask(task); err != nil {
t.Fatalf("err: %v", err)
}
defer cs.Shutdown()
@ -134,14 +82,16 @@ func TestConsulServiceUpdateService(t *testing.T) {
//Update Service defn 1
newTags := []string{"tag3"}
task.Services[0].Tags = newTags
if err := cs.SyncTask(&task); err != nil {
if err := cs.SyncTask(task); err != nil {
t.Fatalf("err: %v", err)
}
// Make sure all the services and checks are still present
if err := servicesPresent(t, []string{"1", "2"}, cs); err != nil {
service1ID := service1.ID(allocID, task.Name)
service2ID := service2.ID(allocID, task.Name)
if err := servicesPresent(t, []string{service1ID, service2ID}, cs); err != nil {
t.Fatalf("err : %v", err)
}
if err := checksPresent(t, []string{"100"}, cs); err != nil {
if err := checksPresent(t, []string{check1.Hash(service1ID)}, cs); err != nil {
t.Fatalf("err : %v", err)
}
@ -150,7 +100,7 @@ func TestConsulServiceUpdateService(t *testing.T) {
if err != nil {
t.Fatalf("errL: %v", err)
}
srv, _ := services["1"]
srv, _ := services[service1ID]
if !reflect.DeepEqual(srv.Tags, newTags) {
t.Fatalf("expected tags: %v, actual: %v", newTags, srv.Tags)
}
@ -185,3 +135,28 @@ func checksPresent(t *testing.T, checkIDs []string, consulService *ConsulService
}
return mErr.ErrorOrNil()
}
func mockTask() *structs.Task {
task := structs.Task{
Name: "foo",
Services: []*structs.Service{&service1, &service2},
Resources: &structs.Resources{
Networks: []*structs.NetworkResource{
&structs.NetworkResource{
IP: "10.10.11.5",
DynamicPorts: []structs.Port{
structs.Port{
Label: "port1",
Value: 20002,
},
structs.Port{
Label: "port2",
Value: 20003,
},
},
},
},
},
}
return &task
}