From 6a1e6159919097fcbd9cd8fa678684220297e137 Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Fri, 11 Dec 2015 14:14:04 -0800 Subject: [PATCH] Refactored test and added some comments --- client/consul_test.go | 27 +++++++++++++++++++++++++-- nomad/structs/structs.go | 3 +++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/client/consul_test.go b/client/consul_test.go index e75aee74ebb..e7bb8012ee4 100644 --- a/client/consul_test.go +++ b/client/consul_test.go @@ -5,6 +5,7 @@ import ( "github.com/hashicorp/nomad/nomad/structs" "log" "os" + "reflect" "testing" "time" ) @@ -298,9 +299,19 @@ func TestConsul_FilterNomadServicesAndChecks(t *testing.T) { }, } + expSrvcs := map[string]*consul.AgentService{ + "nomad-2121212": { + ID: "nomad-2121212", + Service: "identity-service", + Tags: []string{"global"}, + Port: 8080, + Address: "10.10.1.11", + }, + } + nomadServices := c.filterConsulServices(srvs) - if len(nomadServices) != 1 { - t.Fatalf("Expected number of services: %v, Actual: %v", 1, len(nomadServices)) + if !reflect.DeepEqual(expSrvcs, nomadServices) { + t.Fatalf("Expected: %v, Actual: %v", expSrvcs, nomadServices) } nomadServices = c.filterConsulServices(nil) @@ -321,7 +332,19 @@ func TestConsul_FilterNomadServicesAndChecks(t *testing.T) { }, } + expChks := map[string]*consul.AgentCheck{ + "212121212": { + CheckID: "212121212", + ServiceID: "nomad-2121212", + Name: "ping", + }, + } + nomadChecks := c.filterConsulChecks(chks) + if !reflect.DeepEqual(expChks, nomadChecks) { + t.Fatalf("Expected: %v, Actual: %v", expChks, nomadChecks) + } + if len(nomadChecks) != 1 { t.Fatalf("Expected number of checks: %v, Actual: %v", 1, len(nomadChecks)) } diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 2c91d5f936e..84a171e678f 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -1161,6 +1161,9 @@ type Service struct { // InitFields interpolates values of Job, Task Group and Task in the Service // Name. This also generates check names, service id and check ids. func (s *Service) InitFields(job string, taskGroup string, task string) { + // We add a prefix to the Service ID so that we can know that this service + // is managed by Consul since Consul can also have service which are not + // managed by Nomad s.Id = fmt.Sprintf("%s-%s", NomadConsulPrefix, GenerateUUID()) s.Name = args.ReplaceEnv(s.Name, map[string]string{ "JOB": job,