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
parent 524981f367
commit 5c77c59501
No known key found for this signature in database
GPG Key ID: 4D65C6EAEC87DECD
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) {
// runtime.Gosched()
if err := a.RPC("Catalog.NodeServices", &req, &services); err != nil { if err := a.RPC("Catalog.NodeServices", &req, &services); err != nil {
r.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
a.State.RLock()
defer a.State.RUnlock()
// All the services should match // All the services should match
for id, serv := range services.NodeServices.Services { for id, serv := range services.NodeServices.Services {
serv.CreateIndex, serv.ModifyIndex = 0, 0 serv.CreateIndex, serv.ModifyIndex = 0, 0
switch id { switch id {
case "svc_id1": case "svc_id1":
if serv.ID != "svc_id1" || // tags should be modified but not the port
serv.Service != "svc1" || got := serv
serv.Port != 6100 || want := &structs.NodeService{
!reflect.DeepEqual(serv.Tags, []string{"tag1_mod"}) { ID: "svc_id1",
r.Fatalf("bad: %v %v", serv, srv1) Service: "svc1",
Tags: []string{"tag1_mod"},
Port: 6100,
EnableTagOverride: true,
}
if !verify.Values(t, "", got, want) {
t.FailNow()
} }
case "svc_id2": case "svc_id2":
if serv.ID != "svc_id2" || got, want := serv, srv2
serv.Service != "svc2" || if !verify.Values(t, "", got, want) {
serv.Port != 6200 || t.FailNow()
!reflect.DeepEqual(serv.Tags, []string{"tag2"}) {
r.Fatalf("bad: %v %v", serv, srv2)
} }
case structs.ConsulServiceID: case structs.ConsulServiceID:
// ignore // ignore
default: default:
r.Fatalf("unexpected service: %v", id) 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) {