2016-03-23 18:31:04 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2016-03-23 23:25:53 +00:00
|
|
|
"fmt"
|
2016-03-23 18:31:04 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-03-23 23:25:53 +00:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2016-03-23 18:31:04 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2016-05-24 04:28:12 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs/config"
|
2016-03-23 18:31:04 +00:00
|
|
|
)
|
|
|
|
|
2016-04-12 05:55:19 +00:00
|
|
|
const (
|
2016-06-10 04:19:06 +00:00
|
|
|
allocID = "12"
|
|
|
|
serviceRegPrefix = "test"
|
|
|
|
serviceGroupName = "executor"
|
2016-04-12 05:55:19 +00:00
|
|
|
)
|
|
|
|
|
2016-03-24 01:39:17 +00:00
|
|
|
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,
|
|
|
|
}
|
2016-06-12 23:36:49 +00:00
|
|
|
service1 = structs.Service{
|
2016-03-24 01:39:17 +00:00
|
|
|
Name: "foo-1",
|
|
|
|
Tags: []string{"tag1", "tag2"},
|
|
|
|
PortLabel: "port1",
|
|
|
|
Checks: []*structs.ServiceCheck{
|
|
|
|
&check1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-06-12 23:36:49 +00:00
|
|
|
service2 = structs.Service{
|
2016-03-24 01:39:17 +00:00
|
|
|
Name: "foo-2",
|
|
|
|
Tags: []string{"tag1", "tag2"},
|
|
|
|
PortLabel: "port2",
|
|
|
|
}
|
|
|
|
)
|
2016-03-23 18:31:04 +00:00
|
|
|
|
|
|
|
func TestConsulServiceRegisterServices(t *testing.T) {
|
2016-06-14 02:52:17 +00:00
|
|
|
t.Skip()
|
|
|
|
|
2016-06-02 16:15:30 +00:00
|
|
|
shutdownCh := make(chan struct{})
|
2016-06-17 04:21:39 +00:00
|
|
|
cs, err := NewSyncer(config.DefaultConsulConfig(), shutdownCh, logger)
|
2016-03-23 18:31:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
2016-03-23 22:36:46 +00:00
|
|
|
// Skipping the test if consul isn't present
|
|
|
|
if !cs.consulPresent() {
|
|
|
|
return
|
|
|
|
}
|
2016-03-24 01:39:17 +00:00
|
|
|
task := mockTask()
|
2016-06-14 02:52:17 +00:00
|
|
|
//cs.SetServiceRegPrefix(serviceRegPrefix)
|
2016-05-11 20:29:56 +00:00
|
|
|
cs.SetAddrFinder(task.FindHostAndPortFor)
|
2016-06-07 16:03:43 +00:00
|
|
|
if err := cs.SyncServices(); err != nil {
|
2016-03-23 18:31:04 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-03-23 23:25:53 +00:00
|
|
|
defer cs.Shutdown()
|
2016-03-24 01:39:17 +00:00
|
|
|
|
2016-06-14 02:52:17 +00:00
|
|
|
// service1 := &structs.Service{Name: task.Name}
|
|
|
|
// service2 := &structs.Service{Name: task.Name}
|
|
|
|
//services := []*structs.Service{service1, service2}
|
|
|
|
//service1.ServiceID = fmt.Sprintf("%s-%s:%s/%s", cs.GenerateServiceID(serviceGroupName, service1), task.Name, allocID)
|
|
|
|
//service2.ServiceID = fmt.Sprintf("%s-%s:%s/%s", cs.GenerateServiceID(serviceGroupName, service2), task.Name, allocID)
|
2016-06-10 04:19:06 +00:00
|
|
|
|
2016-06-14 02:52:17 +00:00
|
|
|
//cs.SetServices(serviceGroupName, services)
|
|
|
|
// if err := servicesPresent(t, services, cs); err != nil {
|
|
|
|
// t.Fatalf("err : %v", err)
|
|
|
|
// }
|
2016-06-10 04:19:06 +00:00
|
|
|
// FIXME(sean@)
|
|
|
|
// if err := checksPresent(t, []string{check1.Hash(service1ID)}, cs); err != nil {
|
|
|
|
// t.Fatalf("err : %v", err)
|
|
|
|
// }
|
2016-03-23 23:25:53 +00:00
|
|
|
}
|
2016-03-23 18:31:04 +00:00
|
|
|
|
2016-03-23 23:25:53 +00:00
|
|
|
func TestConsulServiceUpdateService(t *testing.T) {
|
2016-06-14 02:52:17 +00:00
|
|
|
t.Skip()
|
|
|
|
|
2016-06-02 16:15:30 +00:00
|
|
|
shutdownCh := make(chan struct{})
|
2016-06-17 04:21:39 +00:00
|
|
|
cs, err := NewSyncer(config.DefaultConsulConfig(), shutdownCh, logger)
|
2016-03-23 23:25:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
// Skipping the test if consul isn't present
|
|
|
|
if !cs.consulPresent() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-24 01:39:17 +00:00
|
|
|
task := mockTask()
|
2016-06-14 02:52:17 +00:00
|
|
|
//cs.SetServiceRegPrefix(serviceRegPrefix)
|
2016-05-11 20:29:56 +00:00
|
|
|
cs.SetAddrFinder(task.FindHostAndPortFor)
|
2016-06-07 16:03:43 +00:00
|
|
|
if err := cs.SyncServices(); err != nil {
|
2016-03-23 23:25:53 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer cs.Shutdown()
|
|
|
|
|
|
|
|
//Update Service defn 1
|
|
|
|
newTags := []string{"tag3"}
|
2016-06-12 23:36:49 +00:00
|
|
|
task.Services[0].Tags = newTags
|
2016-06-07 16:03:43 +00:00
|
|
|
if err := cs.SyncServices(); err != nil {
|
2016-03-23 18:31:04 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-03-23 23:25:53 +00:00
|
|
|
// Make sure all the services and checks are still present
|
2016-06-14 02:52:17 +00:00
|
|
|
// service1 := &structs.Service{Name: task.Name}
|
|
|
|
// service2 := &structs.Service{Name: task.Name}
|
|
|
|
//services := []*structs.Service{service1, service2}
|
|
|
|
//service1.ServiceID = fmt.Sprintf("%s-%s:%s/%s", cs.GenerateServiceID(serviceGroupName, service1), task.Name, allocID)
|
|
|
|
//service2.ServiceID = fmt.Sprintf("%s-%s:%s/%s", cs.GenerateServiceID(serviceGroupName, service2), task.Name, allocID)
|
|
|
|
// if err := servicesPresent(t, services, cs); err != nil {
|
|
|
|
// t.Fatalf("err : %v", err)
|
|
|
|
// }
|
2016-06-10 04:19:06 +00:00
|
|
|
// FIXME(sean@)
|
|
|
|
// if err := checksPresent(t, []string{check1.Hash(service1ID)}, cs); err != nil {
|
|
|
|
// t.Fatalf("err : %v", err)
|
|
|
|
// }
|
2016-03-23 23:25:53 +00:00
|
|
|
|
|
|
|
// check if service defn 1 has been updated
|
2016-06-14 02:52:17 +00:00
|
|
|
// consulServices, err := cs.client.Agent().Services()
|
|
|
|
// if err != nil {
|
|
|
|
// t.Fatalf("errL: %v", err)
|
|
|
|
// }
|
|
|
|
// srv, _ := consulServices[service1.ServiceID]
|
|
|
|
// if !reflect.DeepEqual(srv.Tags, newTags) {
|
|
|
|
// t.Fatalf("expected tags: %v, actual: %v", newTags, srv.Tags)
|
|
|
|
// }
|
2016-03-23 23:25:53 +00:00
|
|
|
}
|
|
|
|
|
2016-06-14 02:52:17 +00:00
|
|
|
// func servicesPresent(t *testing.T, configuredServices []*structs.Service, syncer *Syncer) error {
|
|
|
|
// var mErr multierror.Error
|
|
|
|
// // services, err := syncer.client.Agent().Services()
|
|
|
|
// // if err != nil {
|
|
|
|
// // t.Fatalf("err: %v", err)
|
|
|
|
// // }
|
|
|
|
|
|
|
|
// // for _, configuredService := range configuredServices {
|
|
|
|
// // if _, ok := services[configuredService.ServiceID]; !ok {
|
|
|
|
// // mErr.Errors = append(mErr.Errors, fmt.Errorf("service ID %q not synced", configuredService.ServiceID))
|
|
|
|
// // }
|
|
|
|
// // }
|
|
|
|
// return mErr.ErrorOrNil()
|
|
|
|
// }
|
2016-03-23 23:25:53 +00:00
|
|
|
|
2016-05-22 15:24:54 +00:00
|
|
|
func checksPresent(t *testing.T, checkIDs []string, syncer *Syncer) error {
|
2016-03-23 23:25:53 +00:00
|
|
|
var mErr multierror.Error
|
2016-05-22 15:24:54 +00:00
|
|
|
checks, err := syncer.client.Agent().Checks()
|
2016-03-23 23:25:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2016-03-23 18:31:04 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 23:25:53 +00:00
|
|
|
for _, checkID := range checkIDs {
|
|
|
|
if _, ok := checks[checkID]; !ok {
|
|
|
|
mErr.Errors = append(mErr.Errors, fmt.Errorf("check ID %q not synced", checkID))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mErr.ErrorOrNil()
|
2016-03-23 18:31:04 +00:00
|
|
|
}
|
2016-03-24 01:39:17 +00:00
|
|
|
|
|
|
|
func mockTask() *structs.Task {
|
|
|
|
task := structs.Task{
|
2016-06-12 23:36:49 +00:00
|
|
|
Name: "foo",
|
|
|
|
Services: []*structs.Service{&service1, &service2},
|
2016-03-24 01:39:17 +00:00
|
|
|
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
|
|
|
|
}
|