Skip to content

Commit

Permalink
add a test case filtering by service name and status for check type
Browse files Browse the repository at this point in the history
  • Loading branch information
huikang committed Jun 22, 2023
1 parent f86466e commit d8694a6
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions api/watch/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,110 @@ func TestChecksWatch_Filter(t *testing.T) {
}
}

func TestChecksWatch_Filter_by_ServiceNameStatus(t *testing.T) {
t.Parallel()
c, s := makeClient(t)
defer s.Stop()

s.WaitForSerfCheck(t)

var (
wakeups [][]*api.HealthCheck
notifyCh = make(chan struct{})
)

plan := mustParse(t, `{"type":"checks", "filter":"ServiceName == bar and Status == critical"}`)
plan.Handler = func(idx uint64, raw interface{}) {
if raw == nil {
return // ignore
}
v, ok := raw.([]*api.HealthCheck)
if !ok {
return // ignore
}
wakeups = append(wakeups, v)
notifyCh <- struct{}{}
}

var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
if err := plan.Run(s.HTTPAddr); err != nil {
t.Errorf("err: %v", err)
}
}()
defer plan.Stop()

// Wait for first wakeup.
<-notifyCh
{
catalog := c.Catalog()

// we don't want to find this one
reg := &api.CatalogRegistration{
Node: "foo",
Address: "1.1.1.1",
Datacenter: "dc1",
Service: &api.AgentService{
ID: "foo",
Service: "foo",
Tags: []string{"a"},
},
Check: &api.AgentCheck{
Node: "foo",
CheckID: "foo",
Name: "foo",
Status: api.HealthPassing,
ServiceID: "foo",
},
}
if _, err := catalog.Register(reg, nil); err != nil {
t.Fatalf("err: %v", err)
}

// we want to find this one
reg = &api.CatalogRegistration{
Node: "bar",
Address: "2.2.2.2",
Datacenter: "dc1",
Service: &api.AgentService{
ID: "bar",
Service: "bar",
Tags: []string{"a", "b"},
},
Check: &api.AgentCheck{
Node: "bar",
CheckID: "bar",
Name: "bar",
Status: api.HealthCritical,
ServiceID: "bar",
},
}
if _, err := catalog.Register(reg, nil); err != nil {
t.Fatalf("err: %v", err)
}
}

// Wait for second wakeup.
<-notifyCh

plan.Stop()
wg.Wait()

require.Len(t, wakeups, 2)

{
v := wakeups[0]
require.Len(t, v, 0)
}
{
v := wakeups[1]
require.Len(t, v, 1)
require.Equal(t, "bar", v[0].CheckID)
}
}

func TestEventWatch(t *testing.T) {
t.Parallel()
c, s := makeClient(t)
Expand Down

0 comments on commit d8694a6

Please sign in to comment.