Skip to content

Commit

Permalink
tests: add api and e23 tests and changelog entry
Browse files Browse the repository at this point in the history
  • Loading branch information
DerekStrickland committed May 12, 2022
1 parent 1d6a104 commit df9fa0c
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/12916.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
event_stream: fixed a bug where dynamic port values would fail to serialize in the event stream
```
26 changes: 26 additions & 0 deletions command/agent/alloc_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1121,3 +1121,29 @@ func TestHTTP_ReadWsHandshake(t *testing.T) {
})
}
}

// TestHTTP_AllocPort_Parsing tests that removing the mapstructure tags from the
// Port struct has no adverse affects on serialization.
func TestHTTP_AllocPort_Parsing(t *testing.T) {
ci.Parallel(t)

httpTest(t, nil, func(s *TestAgent) {
state := s.Agent.server.State()
alloc := mock.Alloc()

require.NoError(t, state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{alloc}))

req, err := http.NewRequest("GET", "/v1/allocation/"+alloc.ID, nil)
require.NoError(t, err)
respW := httptest.NewRecorder()

obj, err := s.Server.AllocSpecificRequest(respW, req)
require.NoError(t, err)

a := obj.(*structs.Allocation)
networkResource := a.AllocatedResources.Tasks["web"].Networks[0]
require.Equal(t, a.ID, alloc.ID)
require.Equal(t, 5000, networkResource.ReservedPorts[0].Value)
require.Equal(t, 9876, networkResource.DynamicPorts[0].Value)
})
}
127 changes: 127 additions & 0 deletions e2e/events/events_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package events

import (
"context"
"fmt"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/e2e/e2eutil"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/testutil"
"github.com/stretchr/testify/require"
"testing"
)

// TestEventStream_Ports tests that removing the mapstructure tags from the Port
// struct results in Port values being serialized correctly to the event stream.
func TestEventStream_Ports(t *testing.T) {
nomadClient := e2eutil.NomadClient(t)
e2eutil.WaitForLeader(t, nomadClient)
e2eutil.WaitForNodesReady(t, nomadClient, 1)

testCases := []struct {
name string
networks []*structs.NetworkResource
}{
{
name: "static-ports",
networks: []*structs.NetworkResource{
{
ReservedPorts: []structs.Port{
{
Label: "http",
Value: 9000,
},
},
},
},
},
{
name: "dynamic-ports",
networks: []*structs.NetworkResource{
{
DynamicPorts: []structs.Port{
{
Label: "http",
},
},
},
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var jobIDs []string
t.Cleanup(e2eutil.CleanupJobsAndGC(t, &jobIDs))

jobID := "event-stream-" + tc.name + "-" + uuid.Short()

err := e2eutil.Register(jobID, "./input/"+tc.name+".nomad")
require.NoError(t, err)
jobIDs = append(jobIDs, jobID)

err = e2eutil.WaitForAllocStatusExpected(jobID, "",
[]string{"running"})
require.NoError(t, err, "job should be running")

err = e2eutil.WaitForLastDeploymentStatus(jobID, "", "successful", nil)
require.NoError(t, err, "success", "deployment did not complete")

topics := map[api.Topic][]string{
api.TopicAllocation: {jobID},
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

events := nomadClient.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 <-ctx.Done():
return
case event, ok := <-streamCh:
if !ok {
return
}
if event.IsHeartbeat() {
continue
}
allocEvents = append(allocEvents, event.Events...)
}
}
}()

var alloc *api.Allocation
testutil.WaitForResult(func() (bool, error) {
var got string
for _, e := range allocEvents {
if e.Type == "AllocationUpdated" {
alloc, err = e.Allocation()
return true, nil
}
got = e.Type
}
return false, fmt.Errorf("expected to receive allocation updated event, got: %#v", got)
}, func(e error) {
require.NoError(t, err)
})

require.NotNil(t, alloc)
require.Len(t, alloc.Resources.Networks, 1)
if len(tc.networks[0].ReservedPorts) == 1 {
require.Equal(t, tc.networks[0].ReservedPorts[0].Value, alloc.Resources.Networks[0].ReservedPorts[0].Value)
}

if len(tc.networks[0].DynamicPorts) == 1 {
require.NotEqual(t, 0, alloc.Resources.Networks[0].DynamicPorts[0].Value)
}
})
}
}
28 changes: 28 additions & 0 deletions e2e/events/input/dynamic-ports.nomad
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
job "static-ports" {
datacenters = ["dc1"]
type = "service"

constraint {
attribute = "${attr.kernel.name}"
value = "linux"
}

group "group" {
count = 1

network {
port "db" {}
}

task "dynamic-port" {
driver = "docker"

config {
image = "busybox:1"
command = "nc"
args = ["-ll", "-p", "1234", "-e", "/bin/cat"]
ports = ["db"]
}
}
}
}
30 changes: 30 additions & 0 deletions e2e/events/input/static-ports.nomad
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
job "static-ports" {
datacenters = ["dc1"]
type = "service"

constraint {
attribute = "${attr.kernel.name}"
value = "linux"
}

group "group" {
count = 1

network {
port "db" {
static = 9000
}
}

task "static-port" {
driver = "docker"

config {
image = "busybox:1"
command = "nc"
args = ["-ll", "-p", "1234", "-e", "/bin/cat"]
ports = ["db"]
}
}
}
}

0 comments on commit df9fa0c

Please sign in to comment.