github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/allocrunner/network_manager_linux_test.go (about) 1 package allocrunner 2 3 import ( 4 "testing" 5 6 "github.com/hashicorp/nomad/ci" 7 "github.com/hashicorp/nomad/client/pluginmanager" 8 "github.com/hashicorp/nomad/nomad/structs" 9 "github.com/hashicorp/nomad/plugins/drivers" 10 "github.com/hashicorp/nomad/plugins/drivers/testutils" 11 "github.com/stretchr/testify/require" 12 ) 13 14 var mockDrivers = map[string]drivers.DriverPlugin{ 15 "hostonly": &testutils.MockDriver{ 16 CapabilitiesF: func() (*drivers.Capabilities, error) { 17 return &drivers.Capabilities{ 18 NetIsolationModes: []drivers.NetIsolationMode{drivers.NetIsolationModeHost}, 19 }, nil 20 }, 21 }, 22 "group1": &testutils.MockDriver{ 23 CapabilitiesF: func() (*drivers.Capabilities, error) { 24 return &drivers.Capabilities{ 25 NetIsolationModes: []drivers.NetIsolationMode{ 26 drivers.NetIsolationModeHost, drivers.NetIsolationModeGroup}, 27 }, nil 28 }, 29 }, 30 "group2": &testutils.MockDriver{ 31 CapabilitiesF: func() (*drivers.Capabilities, error) { 32 return &drivers.Capabilities{ 33 NetIsolationModes: []drivers.NetIsolationMode{ 34 drivers.NetIsolationModeHost, drivers.NetIsolationModeGroup}, 35 }, nil 36 }, 37 }, 38 "mustinit1": &testutils.MockDriver{ 39 CapabilitiesF: func() (*drivers.Capabilities, error) { 40 return &drivers.Capabilities{ 41 NetIsolationModes: []drivers.NetIsolationMode{ 42 drivers.NetIsolationModeHost, drivers.NetIsolationModeGroup}, 43 MustInitiateNetwork: true, 44 }, nil 45 }, 46 }, 47 "mustinit2": &testutils.MockDriver{ 48 CapabilitiesF: func() (*drivers.Capabilities, error) { 49 return &drivers.Capabilities{ 50 NetIsolationModes: []drivers.NetIsolationMode{ 51 drivers.NetIsolationModeHost, drivers.NetIsolationModeGroup}, 52 MustInitiateNetwork: true, 53 }, nil 54 }, 55 }, 56 } 57 58 type mockDriverManager struct { 59 pluginmanager.MockPluginManager 60 } 61 62 func (m *mockDriverManager) Dispense(driver string) (drivers.DriverPlugin, error) { 63 return mockDrivers[driver], nil 64 } 65 66 func TestNewNetworkManager(t *testing.T) { 67 ci.Parallel(t) 68 69 for _, tc := range []struct { 70 name string 71 alloc *structs.Allocation 72 err bool 73 mustInit bool 74 errContains string 75 }{ 76 { 77 name: "defaults/backwards compat", 78 alloc: &structs.Allocation{ 79 TaskGroup: "group", 80 Job: &structs.Job{ 81 TaskGroups: []*structs.TaskGroup{ 82 { 83 Name: "group", 84 Networks: []*structs.NetworkResource{}, 85 Tasks: []*structs.Task{ 86 { 87 Name: "task1", 88 Driver: "group1", 89 Resources: &structs.Resources{}, 90 }, 91 { 92 Name: "task2", 93 Driver: "group2", 94 Resources: &structs.Resources{}, 95 }, 96 { 97 Name: "task3", 98 Driver: "mustinit1", 99 Resources: &structs.Resources{}, 100 }, 101 }, 102 }, 103 }, 104 }, 105 }, 106 }, 107 { 108 name: "driver /w must init network", 109 alloc: &structs.Allocation{ 110 TaskGroup: "group", 111 Job: &structs.Job{ 112 TaskGroups: []*structs.TaskGroup{ 113 { 114 Name: "group", 115 Networks: []*structs.NetworkResource{ 116 { 117 Mode: "bridge", 118 }, 119 }, 120 Tasks: []*structs.Task{ 121 { 122 Name: "task1", 123 Driver: "group1", 124 Resources: &structs.Resources{}, 125 }, 126 { 127 Name: "task2", 128 Driver: "mustinit2", 129 Resources: &structs.Resources{}, 130 }, 131 }, 132 }, 133 }, 134 }, 135 }, 136 mustInit: true, 137 }, 138 { 139 name: "multiple mustinit", 140 alloc: &structs.Allocation{ 141 TaskGroup: "group", 142 Job: &structs.Job{ 143 TaskGroups: []*structs.TaskGroup{ 144 { 145 Name: "group", 146 Networks: []*structs.NetworkResource{ 147 { 148 Mode: "bridge", 149 }, 150 }, 151 Tasks: []*structs.Task{ 152 { 153 Name: "task1", 154 Driver: "mustinit1", 155 Resources: &structs.Resources{}, 156 }, 157 { 158 Name: "task2", 159 Driver: "mustinit2", 160 Resources: &structs.Resources{}, 161 }, 162 }, 163 }, 164 }, 165 }, 166 }, 167 err: true, 168 errContains: "want to initiate networking but only one", 169 }, 170 { 171 name: "hostname set in bridged mode", 172 alloc: &structs.Allocation{ 173 TaskGroup: "group", 174 Job: &structs.Job{ 175 TaskGroups: []*structs.TaskGroup{ 176 { 177 Name: "group", 178 Networks: []*structs.NetworkResource{ 179 { 180 Mode: "bridge", 181 Hostname: "foobar", 182 }, 183 }, 184 Tasks: []*structs.Task{ 185 { 186 Name: "task1", 187 Driver: "mustinit1", 188 Resources: &structs.Resources{}, 189 }, 190 }, 191 }, 192 }, 193 }, 194 }, 195 mustInit: true, 196 err: false, 197 }, 198 { 199 name: "hostname set in host mode", 200 alloc: &structs.Allocation{ 201 TaskGroup: "group", 202 Job: &structs.Job{ 203 TaskGroups: []*structs.TaskGroup{ 204 { 205 Name: "group", 206 Networks: []*structs.NetworkResource{ 207 { 208 Mode: "host", 209 Hostname: "foobar", 210 }, 211 }, 212 Tasks: []*structs.Task{ 213 { 214 Name: "task1", 215 Driver: "group1", 216 Resources: &structs.Resources{}, 217 }, 218 }, 219 }, 220 }, 221 }, 222 }, 223 mustInit: false, 224 err: true, 225 errContains: `hostname cannot be set on task group using "host" networking mode`, 226 }, 227 { 228 name: "hostname set using exec driver", 229 alloc: &structs.Allocation{ 230 TaskGroup: "group", 231 Job: &structs.Job{ 232 TaskGroups: []*structs.TaskGroup{ 233 { 234 Name: "group", 235 Networks: []*structs.NetworkResource{ 236 { 237 Mode: "bridge", 238 Hostname: "foobar", 239 }, 240 }, 241 Tasks: []*structs.Task{ 242 { 243 Name: "task1", 244 Driver: "group1", 245 Resources: &structs.Resources{}, 246 }, 247 }, 248 }, 249 }, 250 }, 251 }, 252 mustInit: false, 253 err: true, 254 errContains: "hostname is not currently supported on driver group1", 255 }, 256 } { 257 t.Run(tc.name, func(t *testing.T) { 258 require := require.New(t) 259 nm, err := newNetworkManager(tc.alloc, &mockDriverManager{}) 260 if tc.err { 261 require.Error(err) 262 require.Contains(err.Error(), tc.errContains) 263 } else { 264 require.NoError(err) 265 } 266 267 if tc.mustInit { 268 _, ok := nm.(*testutils.MockDriver) 269 require.True(ok) 270 } else if tc.err { 271 require.Nil(nm) 272 } else { 273 _, ok := nm.(*defaultNetworkManager) 274 require.True(ok) 275 } 276 }) 277 } 278 279 }