Fixed tests

This commit is contained in:
Diptanu Choudhury 2015-12-14 15:47:01 -08:00
parent 3c4e15264c
commit 6b6d74e2eb
4 changed files with 19 additions and 23 deletions

View File

@ -1,7 +1,9 @@
package client
import (
"fmt"
consul "github.com/hashicorp/consul/api"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
"log"
"os"
@ -70,7 +72,6 @@ func newTask() *structs.Task {
func TestConsul_MakeChecks(t *testing.T) {
service := &structs.Service{
Id: "Foo",
Name: "Bar",
Checks: []*structs.ServiceCheck{
{
@ -95,10 +96,11 @@ func TestConsul_MakeChecks(t *testing.T) {
}
c := newConsulService()
serviceId := fmt.Sprintf("%s-1234", structs.NomadConsulPrefix)
check1 := c.makeCheck(service, service.Checks[0], "10.10.0.1", 8090)
check2 := c.makeCheck(service, service.Checks[1], "10.10.0.1", 8090)
check3 := c.makeCheck(service, service.Checks[2], "10.10.0.1", 8090)
check1 := c.makeCheck(serviceId, service.Checks[0], "10.10.0.1", 8090)
check2 := c.makeCheck(serviceId, service.Checks[1], "10.10.0.1", 8090)
check3 := c.makeCheck(serviceId, service.Checks[2], "10.10.0.1", 8090)
if check1.HTTP != "http://10.10.0.1:8090/foo/bar" {
t.Fatalf("Invalid http url for check: %v", check1.HTTP)
@ -142,7 +144,6 @@ func TestConsul_InvalidPortLabelForService(t *testing.T) {
},
}
service := &structs.Service{
Id: "service-id",
Name: "foo",
Tags: []string{"a", "b"},
PortLabel: "https",
@ -150,7 +151,7 @@ func TestConsul_InvalidPortLabelForService(t *testing.T) {
}
c := newConsulService()
if err := c.registerService(service, task, "allocid"); err == nil {
if err := c.registerService(service, task, mock.Alloc()); err == nil {
t.Fatalf("Service should be invalid")
}
}
@ -175,7 +176,7 @@ func TestConsul_Services_Deleted_From_Task(t *testing.T) {
},
},
}
c.Register(&task, "1")
c.Register(&task, mock.Alloc())
if len(c.serviceStates) != 1 {
t.Fatalf("Expected tracked services: %v, Actual: %v", 1, len(c.serviceStates))
}
@ -191,13 +192,14 @@ func TestConsul_Service_Should_Be_Re_Reregistered_On_Change(t *testing.T) {
c := newConsulService()
task := newTask()
s1 := structs.Service{
Id: "1-example-cache-redis",
Name: "example-cache-redis",
Tags: []string{"global"},
PortLabel: "db",
}
task.Services = append(task.Services, &s1)
c.Register(task, "1")
alloc := mock.Alloc()
serviceID := alloc.Services[s1.Name]
c.Register(task, alloc)
s1.Tags = []string{"frontcache"}
@ -207,8 +209,8 @@ func TestConsul_Service_Should_Be_Re_Reregistered_On_Change(t *testing.T) {
t.Fatal("We should be tracking one service")
}
if c.serviceStates[s1.Id] != s1.Hash() {
t.Fatalf("Hash is %v, expected %v", c.serviceStates[s1.Id], s1.Hash())
if c.serviceStates[serviceID] != s1.Hash() {
t.Fatalf("Hash is %v, expected %v", c.serviceStates[serviceID], s1.Hash())
}
}
@ -219,14 +221,13 @@ func TestConsul_AddCheck_To_Service(t *testing.T) {
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")
c.Register(task, mock.Alloc())
check1 := structs.ServiceCheck{
Name: "alive",
@ -250,14 +251,13 @@ func TestConsul_ModifyCheck(t *testing.T) {
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")
c.Register(task, mock.Alloc())
check1 := structs.ServiceCheck{
Name: "alive",

View File

@ -48,7 +48,7 @@ func testTaskRunner(restarts bool) (*MockTaskStateUpdater, *TaskRunner) {
}
state := alloc.TaskStates[task.Name]
tr := NewTaskRunner(logger, conf, upd.Update, ctx, alloc.ID, task, state, restartTracker, consulClient)
tr := NewTaskRunner(logger, conf, upd.Update, ctx, mock.Alloc(), task, state, restartTracker, consulClient)
return upd, tr
}
@ -166,7 +166,7 @@ func TestTaskRunner_SaveRestoreState(t *testing.T) {
// Create a new task runner
consulClient, _ := NewConsulService(&consulServiceConfig{tr.logger, "127.0.0.1:8500", "", "", false, false, &structs.Node{}})
tr2 := NewTaskRunner(tr.logger, tr.config, upd.Update,
tr.ctx, tr.allocID, &structs.Task{Name: tr.task.Name}, tr.state, tr.restartTracker,
tr.ctx, tr.alloc, &structs.Task{Name: tr.task.Name}, tr.state, tr.restartTracker,
consulClient)
if err := tr2.RestoreState(); err != nil {
t.Fatalf("err: %v", err)

View File

@ -226,6 +226,7 @@ func Alloc() *structs.Allocation {
},
},
},
Services: map[string]string{"web-frontend": "nomad-registered-task-1234"},
TaskStates: map[string]*structs.TaskState{
"web": &structs.TaskState{
State: structs.TaskStatePending,

View File

@ -391,13 +391,11 @@ func TestEncodeDecode(t *testing.T) {
func TestInvalidServiceCheck(t *testing.T) {
s := Service{
Id: "service-id",
Name: "service-name",
PortLabel: "bar",
Checks: []*ServiceCheck{
{
Id: "check-id",
Name: "check-name",
Type: "lol",
},
@ -442,7 +440,7 @@ func TestDistinctCheckId(t *testing.T) {
}
func TestService_InitFiels(t *testing.T) {
func TestService_InitFields(t *testing.T) {
job := "example"
taskGroup := "cache"
task := "redis"
@ -455,9 +453,6 @@ func TestService_InitFiels(t *testing.T) {
if s.Name != "redis-db" {
t.Fatalf("Expected name: %v, Actual: %v", "redis-db", s.Name)
}
if s.Id == "" {
t.Fatalf("Expected a GUID for Service ID, Actual: %v", s.Id)
}
s.Name = "db"
s.InitFields(job, taskGroup, task)