2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-09-28 14:13:10 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-11-08 10:26:28 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2020-10-08 18:27:52 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2022-11-08 10:26:28 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2020-10-06 15:08:12 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2020-09-28 14:13:10 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testEvent struct {
|
|
|
|
ID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventStream(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-09-28 14:13:10 +00:00
|
|
|
|
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2023-07-26 15:38:39 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/v1/event/stream", nil)
|
2020-09-28 14:13:10 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
|
|
|
|
respErrCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
_, err = s.Server.EventStream(resp, req)
|
|
|
|
respErrCh <- err
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
2020-10-08 18:27:52 +00:00
|
|
|
pub, err := s.Agent.server.State().EventBroker()
|
2020-09-28 14:13:10 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-06 20:21:58 +00:00
|
|
|
pub.Publish(&structs.Events{Index: 100, Events: []structs.Event{{Payload: testEvent{ID: "123"}}}})
|
2020-09-28 14:13:10 +00:00
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
got := resp.Body.String()
|
|
|
|
want := `{"ID":"123"}`
|
|
|
|
if strings.Contains(got, want) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, fmt.Errorf("missing expected json, got: %v, want: %v", got, want)
|
|
|
|
}, func(err error) {
|
|
|
|
cancel()
|
|
|
|
require.Fail(t, err.Error())
|
|
|
|
})
|
|
|
|
|
|
|
|
// wait for response to close to prevent race between subscription
|
2020-10-08 15:57:21 +00:00
|
|
|
// shutdown and server shutdown returning subscription closed by server err
|
|
|
|
cancel()
|
|
|
|
select {
|
|
|
|
case err := <-respErrCh:
|
|
|
|
require.Nil(t, err)
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
require.Fail(t, "waiting for request cancellation")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventStream_NamespaceQuery(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-10-08 15:57:21 +00:00
|
|
|
|
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2020-10-08 18:27:52 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2023-07-26 15:38:39 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/v1/event/stream?namespace=foo", nil)
|
2020-10-08 15:57:21 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
|
|
|
|
respErrCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
_, err = s.Server.EventStream(resp, req)
|
|
|
|
respErrCh <- err
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
2020-10-08 18:27:52 +00:00
|
|
|
pub, err := s.Agent.server.State().EventBroker()
|
2020-10-08 15:57:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-08 18:27:52 +00:00
|
|
|
badID := uuid.Generate()
|
|
|
|
pub.Publish(&structs.Events{Index: 100, Events: []structs.Event{{Namespace: "bar", Payload: testEvent{ID: badID}}}})
|
2020-10-08 15:57:21 +00:00
|
|
|
pub.Publish(&structs.Events{Index: 101, Events: []structs.Event{{Namespace: "foo", Payload: testEvent{ID: "456"}}}})
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
got := resp.Body.String()
|
|
|
|
want := `"Namespace":"foo"`
|
2020-10-08 18:27:52 +00:00
|
|
|
if strings.Contains(got, badID) {
|
2020-10-08 15:57:21 +00:00
|
|
|
return false, fmt.Errorf("expected non matching namespace to be filtered, got:%v", got)
|
|
|
|
}
|
|
|
|
if strings.Contains(got, want) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, fmt.Errorf("missing expected json, got: %v, want: %v", got, want)
|
|
|
|
}, func(err error) {
|
|
|
|
require.Fail(t, err.Error())
|
|
|
|
})
|
|
|
|
|
|
|
|
// wait for response to close to prevent race between subscription
|
2020-09-28 14:13:10 +00:00
|
|
|
// shutdown and server shutdown returning subscription closed by server err
|
|
|
|
cancel()
|
|
|
|
select {
|
|
|
|
case err := <-respErrCh:
|
|
|
|
require.Nil(t, err)
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
require.Fail(t, "waiting for request cancellation")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventStream_QueryParse(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-09-28 14:13:10 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
query string
|
2020-10-04 19:12:35 +00:00
|
|
|
want map[structs.Topic][]string
|
2020-09-28 14:13:10 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "all topics and keys specified",
|
|
|
|
query: "?topic=*:*",
|
2020-10-04 19:12:35 +00:00
|
|
|
want: map[structs.Topic][]string{
|
2020-10-01 18:43:28 +00:00
|
|
|
"*": {"*"},
|
2020-09-28 14:13:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "all topics and keys inferred",
|
|
|
|
query: "",
|
2020-10-04 19:12:35 +00:00
|
|
|
want: map[structs.Topic][]string{
|
2020-10-01 18:43:28 +00:00
|
|
|
"*": {"*"},
|
2020-09-28 14:13:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "invalid key value formatting",
|
|
|
|
query: "?topic=NodeDrain:*:*",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
2020-10-06 15:08:12 +00:00
|
|
|
desc: "Infer wildcard if absent",
|
2020-09-28 14:13:10 +00:00
|
|
|
query: "?topic=NodeDrain",
|
2020-10-06 15:08:12 +00:00
|
|
|
wantErr: false,
|
|
|
|
want: map[structs.Topic][]string{
|
|
|
|
"NodeDrain": {"*"},
|
|
|
|
},
|
2020-09-28 14:13:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "single topic and key",
|
|
|
|
query: "?topic=NodeDrain:*",
|
2020-10-04 19:12:35 +00:00
|
|
|
want: map[structs.Topic][]string{
|
2020-10-01 18:43:28 +00:00
|
|
|
"NodeDrain": {"*"},
|
2020-09-28 14:13:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "single topic multiple keys",
|
|
|
|
query: "?topic=NodeDrain:*&topic=NodeDrain:3caace09-f1f4-4d23-b37a-9ab5eb75069d",
|
2020-10-04 19:12:35 +00:00
|
|
|
want: map[structs.Topic][]string{
|
2020-10-01 18:43:28 +00:00
|
|
|
"NodeDrain": {
|
2020-09-28 14:13:10 +00:00
|
|
|
"*",
|
|
|
|
"3caace09-f1f4-4d23-b37a-9ab5eb75069d",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "multiple topics",
|
|
|
|
query: "?topic=NodeRegister:*&topic=NodeDrain:3caace09-f1f4-4d23-b37a-9ab5eb75069d",
|
2020-10-04 19:12:35 +00:00
|
|
|
want: map[structs.Topic][]string{
|
2020-10-01 18:43:28 +00:00
|
|
|
"NodeDrain": {
|
2020-09-28 14:13:10 +00:00
|
|
|
"3caace09-f1f4-4d23-b37a-9ab5eb75069d",
|
|
|
|
},
|
2020-10-01 18:43:28 +00:00
|
|
|
"NodeRegister": {
|
2020-09-28 14:13:10 +00:00
|
|
|
"*",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
|
|
raw := fmt.Sprintf("http://localhost:80/v1/events%s", tc.query)
|
|
|
|
req, err := url.Parse(raw)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
got, err := parseEventTopics(req.Query())
|
|
|
|
if tc.wantErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-11-08 10:26:28 +00:00
|
|
|
|
|
|
|
func TestHTTP_Alloc_Port_Response(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
httpTest(t, nil, func(srv *TestAgent) {
|
|
|
|
client := srv.Client()
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, srv.Agent.RPC)
|
|
|
|
testutil.WaitForClient(t, srv.Agent.Client().RPC, srv.Agent.Client().NodeID(), srv.Agent.Client().Region())
|
|
|
|
|
|
|
|
job := MockRunnableJob()
|
|
|
|
|
|
|
|
resp, _, err := client.Jobs().Register(job, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, resp.EvalID)
|
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.Job = ApiJobToStructJob(job)
|
|
|
|
alloc.JobID = *job.ID
|
|
|
|
alloc.NodeID = srv.client.NodeID()
|
|
|
|
|
|
|
|
require.Nil(t, srv.server.State().UpsertJobSummary(101, mock.JobSummary(alloc.JobID)))
|
|
|
|
require.Nil(t, srv.server.State().UpsertAllocs(structs.MsgTypeTestSetup, 102, []*structs.Allocation{alloc}))
|
|
|
|
|
|
|
|
running := false
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
upsertResult, stateErr := srv.server.State().AllocByID(nil, alloc.ID)
|
|
|
|
if stateErr != nil {
|
|
|
|
return false, stateErr
|
|
|
|
}
|
|
|
|
if upsertResult.ClientStatus == structs.AllocClientStatusRunning {
|
|
|
|
running = true
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}, func(err error) {
|
|
|
|
require.NoError(t, err, "allocation query failed")
|
|
|
|
})
|
|
|
|
|
|
|
|
require.True(t, running)
|
|
|
|
|
|
|
|
topics := map[api.Topic][]string{
|
|
|
|
api.TopicAllocation: {*job.ID},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
events := client.EventStream()
|
|
|
|
streamCh, err := events.Stream(ctx, topics, 1, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var allocEvents []api.Event
|
|
|
|
// gather job alloc events
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event, ok := <-streamCh:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if event.IsHeartbeat() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
allocEvents = append(allocEvents, event.Events...)
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
require.Fail(t, "failed waiting for event stream event")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var networkResource *api.NetworkResource
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
for _, e := range allocEvents {
|
|
|
|
if e.Type == structs.TypeAllocationUpdated {
|
|
|
|
eventAlloc, err := e.Allocation()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if len(eventAlloc.AllocatedResources.Tasks["web"].Networks) == 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
networkResource = eventAlloc.AllocatedResources.Tasks["web"].Networks[0]
|
|
|
|
if networkResource.ReservedPorts[0].Value == 5000 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}, func(e error) {
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NotNil(t, networkResource)
|
|
|
|
require.Equal(t, 5000, networkResource.ReservedPorts[0].Value)
|
|
|
|
require.NotEqual(t, 0, networkResource.DynamicPorts[0].Value)
|
|
|
|
})
|
|
|
|
}
|