local state: refactor TestAgentAntiEntropy_EnableTagOverride

Make intent clearer by being more explicit and adding some comments.
Use verify.Values to compare service entries.
This commit is contained in:
Frank Schroeder 2017-08-28 14:17:19 +02:00 committed by Frank Schröder
parent b288c1eb9b
commit cfc8bd092f
1 changed files with 62 additions and 52 deletions

View File

@ -237,7 +237,7 @@ func TestAgentAntiEntropy_EnableTagOverride(t *testing.T) {
} }
var out struct{} var out struct{}
// EnableTagOverride = true // register a local service with tag override enabled
srv1 := &structs.NodeService{ srv1 := &structs.NodeService{
ID: "svc_id1", ID: "svc_id1",
Service: "svc1", Service: "svc1",
@ -246,19 +246,8 @@ func TestAgentAntiEntropy_EnableTagOverride(t *testing.T) {
EnableTagOverride: true, EnableTagOverride: true,
} }
a.State.AddService(srv1, "") a.State.AddService(srv1, "")
if err := a.State.SyncChanges(); err != nil {
t.Fatalf("err: %v", err)
}
srv1_mod := new(structs.NodeService)
*srv1_mod = *srv1
srv1_mod.Port = 7100
srv1_mod.Tags = []string{"tag1_mod"}
args.Service = srv1_mod
if err := a.RPC("Catalog.Register", args, &out); err != nil {
t.Fatalf("err: %v", err)
}
// EnableTagOverride = false // register a local service with tag override disabled
srv2 := &structs.NodeService{ srv2 := &structs.NodeService{
ID: "svc_id2", ID: "svc_id2",
Service: "svc2", Service: "svc2",
@ -267,15 +256,38 @@ func TestAgentAntiEntropy_EnableTagOverride(t *testing.T) {
EnableTagOverride: false, EnableTagOverride: false,
} }
a.State.AddService(srv2, "") a.State.AddService(srv2, "")
srv2_mod := new(structs.NodeService)
*srv2_mod = *srv2 // make sure they are both in the catalog
srv2_mod.Port = 7200 if err := a.State.SyncChanges(); err != nil {
srv2_mod.Tags = []string{"tag2_mod"} t.Fatalf("err: %v", err)
args.Service = srv2_mod }
// update the services in the catalog and change the tags and port.
// Only tag changes should be propagated for services where tag
// override is enabled.
args.Service = &structs.NodeService{
ID: srv1.ID,
Service: srv1.Service,
Tags: []string{"tag1_mod"},
Port: 7100,
EnableTagOverride: true,
}
if err := a.RPC("Catalog.Register", args, &out); err != nil { if err := a.RPC("Catalog.Register", args, &out); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
args.Service = &structs.NodeService{
ID: srv2.ID,
Service: srv2.Service,
Tags: []string{"tag2_mod"},
Port: 7200,
EnableTagOverride: false,
}
if err := a.RPC("Catalog.Register", args, &out); err != nil {
t.Fatalf("err: %v", err)
}
// sync catalog and local state
if err := a.State.SyncFull(); err != nil { if err := a.State.SyncFull(); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -286,44 +298,42 @@ func TestAgentAntiEntropy_EnableTagOverride(t *testing.T) {
} }
var services structs.IndexedNodeServices var services structs.IndexedNodeServices
retry.Run(t, func(r *retry.R) { if err := a.RPC("Catalog.NodeServices", &req, &services); err != nil {
// runtime.Gosched() t.Fatalf("err: %v", err)
if err := a.RPC("Catalog.NodeServices", &req, &services); err != nil { }
r.Fatalf("err: %v", err)
}
a.State.RLock() // All the services should match
defer a.State.RUnlock() for id, serv := range services.NodeServices.Services {
serv.CreateIndex, serv.ModifyIndex = 0, 0
// All the services should match switch id {
for id, serv := range services.NodeServices.Services { case "svc_id1":
serv.CreateIndex, serv.ModifyIndex = 0, 0 // tags should be modified but not the port
switch id { got := serv
case "svc_id1": want := &structs.NodeService{
if serv.ID != "svc_id1" || ID: "svc_id1",
serv.Service != "svc1" || Service: "svc1",
serv.Port != 6100 || Tags: []string{"tag1_mod"},
!reflect.DeepEqual(serv.Tags, []string{"tag1_mod"}) { Port: 6100,
r.Fatalf("bad: %v %v", serv, srv1) EnableTagOverride: true,
}
case "svc_id2":
if serv.ID != "svc_id2" ||
serv.Service != "svc2" ||
serv.Port != 6200 ||
!reflect.DeepEqual(serv.Tags, []string{"tag2"}) {
r.Fatalf("bad: %v %v", serv, srv2)
}
case structs.ConsulServiceID:
// ignore
default:
r.Fatalf("unexpected service: %v", id)
} }
if !verify.Values(t, "", got, want) {
t.FailNow()
}
case "svc_id2":
got, want := serv, srv2
if !verify.Values(t, "", got, want) {
t.FailNow()
}
case structs.ConsulServiceID:
// ignore
default:
t.Fatalf("unexpected service: %v", id)
} }
}
if err := servicesInSync(a.State, 2); err != nil { if err := servicesInSync(a.State, 2); err != nil {
r.Fatal(err) t.Fatal(err)
} }
})
} }
func TestAgentAntiEntropy_Services_WithChecks(t *testing.T) { func TestAgentAntiEntropy_Services_WithChecks(t *testing.T) {