0e926ef3fd
Add a new hostname string parameter to the network block which allows operators to specify the hostname of the network namespace. Changing this causes a destructive update to the allocation and it is omitted if empty from API responses. This parameter also supports interpolation. In order to have a hostname passed as a configuration param when creating an allocation network, the CreateNetwork func of the DriverNetworkManager interface needs to be updated. In order to minimize the disruption of future changes, rather than add another string func arg, the function now accepts a request struct along with the allocID param. The struct has the hostname as a field. The in-tree implementations of DriverNetworkManager.CreateNetwork have been modified to account for the function signature change. In updating for the change, the enhancement of adding hostnames to network namespaces has also been added to the Docker driver, whilst the default Linux manager does not current implement it.
104 lines
3 KiB
Go
104 lines
3 KiB
Go
package allocrunner
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
|
"github.com/hashicorp/nomad/client/taskenv"
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
|
"github.com/hashicorp/nomad/plugins/drivers/testutils"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// statically assert network hook implements the expected interfaces
|
|
var _ interfaces.RunnerPrerunHook = (*networkHook)(nil)
|
|
var _ interfaces.RunnerPostrunHook = (*networkHook)(nil)
|
|
|
|
type mockNetworkIsolationSetter struct {
|
|
t *testing.T
|
|
expectedSpec *drivers.NetworkIsolationSpec
|
|
called bool
|
|
}
|
|
|
|
func (m *mockNetworkIsolationSetter) SetNetworkIsolation(spec *drivers.NetworkIsolationSpec) {
|
|
m.called = true
|
|
require.Exactly(m.t, m.expectedSpec, spec)
|
|
}
|
|
|
|
type mockNetworkStatusSetter struct {
|
|
t *testing.T
|
|
expectedStatus *structs.AllocNetworkStatus
|
|
called bool
|
|
}
|
|
|
|
func (m *mockNetworkStatusSetter) SetNetworkStatus(status *structs.AllocNetworkStatus) {
|
|
m.called = true
|
|
require.Exactly(m.t, m.expectedStatus, status)
|
|
}
|
|
|
|
// Test that the prerun and postrun hooks call the setter with the expected spec when
|
|
// the network mode is not host
|
|
func TestNetworkHook_Prerun_Postrun(t *testing.T) {
|
|
alloc := mock.Alloc()
|
|
alloc.Job.TaskGroups[0].Networks = []*structs.NetworkResource{
|
|
{
|
|
Mode: "bridge",
|
|
},
|
|
}
|
|
spec := &drivers.NetworkIsolationSpec{
|
|
Mode: drivers.NetIsolationModeGroup,
|
|
Path: "test",
|
|
Labels: map[string]string{"abc": "123"},
|
|
}
|
|
|
|
destroyCalled := false
|
|
nm := &testutils.MockDriver{
|
|
MockNetworkManager: testutils.MockNetworkManager{
|
|
CreateNetworkF: func(allocID string, req *drivers.NetworkCreateRequest) (*drivers.NetworkIsolationSpec, bool, error) {
|
|
require.Equal(t, alloc.ID, allocID)
|
|
return spec, false, nil
|
|
},
|
|
|
|
DestroyNetworkF: func(allocID string, netSpec *drivers.NetworkIsolationSpec) error {
|
|
destroyCalled = true
|
|
require.Equal(t, alloc.ID, allocID)
|
|
require.Exactly(t, spec, netSpec)
|
|
return nil
|
|
},
|
|
},
|
|
}
|
|
setter := &mockNetworkIsolationSetter{
|
|
t: t,
|
|
expectedSpec: spec,
|
|
}
|
|
statusSetter := &mockNetworkStatusSetter{
|
|
t: t,
|
|
expectedStatus: nil,
|
|
}
|
|
require := require.New(t)
|
|
|
|
envBuilder := taskenv.NewBuilder(mock.Node(), alloc, nil, alloc.Job.Region)
|
|
|
|
logger := testlog.HCLogger(t)
|
|
hook := newNetworkHook(logger, setter, alloc, nm, &hostNetworkConfigurator{}, statusSetter, envBuilder.Build())
|
|
require.NoError(hook.Prerun())
|
|
require.True(setter.called)
|
|
require.False(destroyCalled)
|
|
require.NoError(hook.Postrun())
|
|
require.True(destroyCalled)
|
|
|
|
// reset and use host network mode
|
|
setter.called = false
|
|
destroyCalled = false
|
|
alloc.Job.TaskGroups[0].Networks[0].Mode = "host"
|
|
hook = newNetworkHook(logger, setter, alloc, nm, &hostNetworkConfigurator{}, statusSetter, envBuilder.Build())
|
|
require.NoError(hook.Prerun())
|
|
require.False(setter.called)
|
|
require.False(destroyCalled)
|
|
require.NoError(hook.Postrun())
|
|
require.False(destroyCalled)
|
|
}
|