2019-05-08 17:29:31 +00:00
|
|
|
package allocrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2019-05-08 17:29:31 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
2021-09-16 06:13:09 +00:00
|
|
|
"github.com/hashicorp/nomad/client/taskenv"
|
2019-05-08 17:29:31 +00:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
2020-10-12 17:43:04 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-05-08 17:29:31 +00:00
|
|
|
// 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) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2019-05-08 17:29:31 +00:00
|
|
|
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{
|
2021-09-16 06:13:09 +00:00
|
|
|
CreateNetworkF: func(allocID string, req *drivers.NetworkCreateRequest) (*drivers.NetworkIsolationSpec, bool, error) {
|
2019-05-08 17:29:31 +00:00
|
|
|
require.Equal(t, alloc.ID, allocID)
|
2019-09-18 20:34:57 +00:00
|
|
|
return spec, false, nil
|
2019-05-08 17:29:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
2020-10-12 17:43:04 +00:00
|
|
|
statusSetter := &mockNetworkStatusSetter{
|
|
|
|
t: t,
|
|
|
|
expectedStatus: nil,
|
|
|
|
}
|
2019-05-08 17:29:31 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
2021-09-16 06:13:09 +00:00
|
|
|
envBuilder := taskenv.NewBuilder(mock.Node(), alloc, nil, alloc.Job.Region)
|
|
|
|
|
2019-05-08 17:29:31 +00:00
|
|
|
logger := testlog.HCLogger(t)
|
2021-09-16 06:13:09 +00:00
|
|
|
hook := newNetworkHook(logger, setter, alloc, nm, &hostNetworkConfigurator{}, statusSetter, envBuilder.Build())
|
2019-05-08 17:29:31 +00:00
|
|
|
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"
|
2021-09-16 06:13:09 +00:00
|
|
|
hook = newNetworkHook(logger, setter, alloc, nm, &hostNetworkConfigurator{}, statusSetter, envBuilder.Build())
|
2019-05-08 17:29:31 +00:00
|
|
|
require.NoError(hook.Prerun())
|
|
|
|
require.False(setter.called)
|
|
|
|
require.False(destroyCalled)
|
|
|
|
require.NoError(hook.Postrun())
|
|
|
|
require.False(destroyCalled)
|
|
|
|
}
|