Skip to content

Commit

Permalink
Refactored test and added some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
diptanu committed Dec 11, 2015
1 parent 57ff1a3 commit 6a1e615
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
27 changes: 25 additions & 2 deletions client/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/hashicorp/nomad/nomad/structs"
"log"
"os"
"reflect"
"testing"
"time"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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))
}
Expand Down
3 changes: 3 additions & 0 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 6a1e615

Please sign in to comment.