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

View File

@ -48,7 +48,7 @@ func testTaskRunner(restarts bool) (*MockTaskStateUpdater, *TaskRunner) {
} }
state := alloc.TaskStates[task.Name] 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 return upd, tr
} }
@ -166,7 +166,7 @@ func TestTaskRunner_SaveRestoreState(t *testing.T) {
// Create a new task runner // Create a new task runner
consulClient, _ := NewConsulService(&consulServiceConfig{tr.logger, "127.0.0.1:8500", "", "", false, false, &structs.Node{}}) consulClient, _ := NewConsulService(&consulServiceConfig{tr.logger, "127.0.0.1:8500", "", "", false, false, &structs.Node{}})
tr2 := NewTaskRunner(tr.logger, tr.config, upd.Update, 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) consulClient)
if err := tr2.RestoreState(); err != nil { if err := tr2.RestoreState(); err != nil {
t.Fatalf("err: %v", err) 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{ TaskStates: map[string]*structs.TaskState{
"web": &structs.TaskState{ "web": &structs.TaskState{
State: structs.TaskStatePending, State: structs.TaskStatePending,

View File

@ -391,13 +391,11 @@ func TestEncodeDecode(t *testing.T) {
func TestInvalidServiceCheck(t *testing.T) { func TestInvalidServiceCheck(t *testing.T) {
s := Service{ s := Service{
Id: "service-id",
Name: "service-name", Name: "service-name",
PortLabel: "bar", PortLabel: "bar",
Checks: []*ServiceCheck{ Checks: []*ServiceCheck{
{ {
Id: "check-id",
Name: "check-name", Name: "check-name",
Type: "lol", 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" job := "example"
taskGroup := "cache" taskGroup := "cache"
task := "redis" task := "redis"
@ -455,9 +453,6 @@ func TestService_InitFiels(t *testing.T) {
if s.Name != "redis-db" { if s.Name != "redis-db" {
t.Fatalf("Expected name: %v, Actual: %v", "redis-db", s.Name) 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.Name = "db"
s.InitFields(job, taskGroup, task) s.InitFields(job, taskGroup, task)