github.com/hspak/nomad@v0.7.2-0.20180309000617-bc4ae22a39a5/client/fingerprint_manager_test.go (about) 1 package client 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/nomad/client/config" 8 "github.com/hashicorp/nomad/client/driver" 9 "github.com/hashicorp/nomad/nomad/structs" 10 "github.com/hashicorp/nomad/testutil" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestFingerprintManager_Run_MockDriver(t *testing.T) { 15 driver.CheckForMockDriver(t) 16 t.Parallel() 17 require := require.New(t) 18 19 node := &structs.Node{ 20 Attributes: make(map[string]string, 0), 21 Links: make(map[string]string, 0), 22 Resources: &structs.Resources{}, 23 } 24 testConfig := config.Config{Node: node} 25 testClient := &Client{config: &testConfig} 26 conf := config.DefaultConfig() 27 28 getConfig := func() *config.Config { 29 return conf 30 } 31 32 fm := NewFingerprintManager( 33 getConfig, 34 node, 35 make(chan struct{}), 36 testClient.updateNodeFromFingerprint, 37 testLogger(), 38 ) 39 40 err := fm.Run() 41 require.Nil(err) 42 require.NotEqual("", node.Attributes["driver.mock_driver"]) 43 } 44 45 func TestFingerprintManager_Run_ResourcesFingerprint(t *testing.T) { 46 driver.CheckForMockDriver(t) 47 t.Parallel() 48 require := require.New(t) 49 50 node := &structs.Node{ 51 Attributes: make(map[string]string, 0), 52 Links: make(map[string]string, 0), 53 Resources: &structs.Resources{}, 54 } 55 testConfig := config.Config{Node: node} 56 testClient := &Client{config: &testConfig} 57 58 conf := config.DefaultConfig() 59 getConfig := func() *config.Config { 60 return conf 61 } 62 63 fm := NewFingerprintManager( 64 getConfig, 65 node, 66 make(chan struct{}), 67 testClient.updateNodeFromFingerprint, 68 testLogger(), 69 ) 70 71 err := fm.Run() 72 require.Nil(err) 73 require.NotEqual(0, node.Resources.CPU) 74 require.NotEqual(0, node.Resources.MemoryMB) 75 require.NotZero(node.Resources.DiskMB) 76 } 77 78 func TestFingerprintManager_Fingerprint_Run(t *testing.T) { 79 t.Parallel() 80 require := require.New(t) 81 82 node := &structs.Node{ 83 Attributes: make(map[string]string, 0), 84 Links: make(map[string]string, 0), 85 Resources: &structs.Resources{}, 86 } 87 testConfig := config.Config{Node: node} 88 testClient := &Client{config: &testConfig} 89 90 conf := config.DefaultConfig() 91 conf.Options = map[string]string{"driver.raw_exec.enable": "true"} 92 getConfig := func() *config.Config { 93 return conf 94 } 95 96 fm := NewFingerprintManager( 97 getConfig, 98 node, 99 make(chan struct{}), 100 testClient.updateNodeFromFingerprint, 101 testLogger(), 102 ) 103 104 err := fm.Run() 105 require.Nil(err) 106 107 require.NotEqual("", node.Attributes["driver.raw_exec"]) 108 } 109 110 func TestFingerprintManager_Fingerprint_Periodic(t *testing.T) { 111 t.Parallel() 112 require := require.New(t) 113 114 node := &structs.Node{ 115 Attributes: make(map[string]string, 0), 116 Links: make(map[string]string, 0), 117 Resources: &structs.Resources{}, 118 } 119 testConfig := config.Config{Node: node} 120 testClient := &Client{config: &testConfig} 121 122 conf := config.DefaultConfig() 123 conf.Options = map[string]string{ 124 "test.shutdown_periodic_after": "true", 125 "test.shutdown_periodic_duration": "3", 126 } 127 getConfig := func() *config.Config { 128 return conf 129 } 130 131 shutdownCh := make(chan struct{}) 132 defer (func() { 133 close(shutdownCh) 134 })() 135 136 fm := NewFingerprintManager( 137 getConfig, 138 node, 139 shutdownCh, 140 testClient.updateNodeFromFingerprint, 141 testLogger(), 142 ) 143 144 err := fm.Run() 145 require.Nil(err) 146 147 // Ensure the mock driver is registered on the client 148 testutil.WaitForResult(func() (bool, error) { 149 mockDriverStatus := node.Attributes["driver.mock_driver"] 150 if mockDriverStatus == "" { 151 return false, fmt.Errorf("mock driver attribute should be set on the client") 152 } 153 return true, nil 154 }, func(err error) { 155 t.Fatalf("err: %v", err) 156 }) 157 158 // Ensure that the client fingerprinter eventually removes this attribute 159 testutil.WaitForResult(func() (bool, error) { 160 mockDriverStatus := node.Attributes["driver.mock_driver"] 161 if mockDriverStatus != "" { 162 return false, fmt.Errorf("mock driver attribute should not be set on the client") 163 } 164 return true, nil 165 }, func(err error) { 166 t.Fatalf("err: %v", err) 167 }) 168 } 169 170 func TestFimgerprintManager_Run_InWhitelist(t *testing.T) { 171 t.Parallel() 172 require := require.New(t) 173 174 node := &structs.Node{ 175 Attributes: make(map[string]string, 0), 176 Links: make(map[string]string, 0), 177 Resources: &structs.Resources{}, 178 } 179 testConfig := config.Config{Node: node} 180 testClient := &Client{config: &testConfig} 181 182 conf := config.DefaultConfig() 183 conf.Options = map[string]string{"fingerprint.whitelist": " arch,cpu,memory,network,storage,foo,bar "} 184 getConfig := func() *config.Config { 185 return conf 186 } 187 188 shutdownCh := make(chan struct{}) 189 defer (func() { 190 close(shutdownCh) 191 })() 192 193 fm := NewFingerprintManager( 194 getConfig, 195 node, 196 shutdownCh, 197 testClient.updateNodeFromFingerprint, 198 testLogger(), 199 ) 200 201 err := fm.Run() 202 require.Nil(err) 203 require.NotEqual(node.Attributes["cpu.frequency"], "") 204 } 205 206 func TestFimgerprintManager_Run_InBlacklist(t *testing.T) { 207 t.Parallel() 208 require := require.New(t) 209 210 node := &structs.Node{ 211 Attributes: make(map[string]string, 0), 212 Links: make(map[string]string, 0), 213 Resources: &structs.Resources{}, 214 } 215 testConfig := config.Config{Node: node} 216 testClient := &Client{config: &testConfig} 217 218 conf := config.DefaultConfig() 219 conf.Options = map[string]string{"fingerprint.whitelist": " arch,memory,foo,bar "} 220 conf.Options = map[string]string{"fingerprint.blacklist": " cpu "} 221 getConfig := func() *config.Config { 222 return conf 223 } 224 225 shutdownCh := make(chan struct{}) 226 defer (func() { 227 close(shutdownCh) 228 })() 229 230 fm := NewFingerprintManager( 231 getConfig, 232 node, 233 shutdownCh, 234 testClient.updateNodeFromFingerprint, 235 testLogger(), 236 ) 237 238 err := fm.Run() 239 require.Nil(err) 240 require.Equal(node.Attributes["cpu.frequency"], "") 241 require.NotEqual(node.Attributes["memory.totalbytes"], "") 242 } 243 244 func TestFimgerprintManager_Run_Combination(t *testing.T) { 245 t.Parallel() 246 require := require.New(t) 247 248 node := &structs.Node{ 249 Attributes: make(map[string]string, 0), 250 Links: make(map[string]string, 0), 251 Resources: &structs.Resources{}, 252 } 253 testConfig := config.Config{Node: node} 254 testClient := &Client{config: &testConfig} 255 256 conf := config.DefaultConfig() 257 conf.Options = map[string]string{"fingerprint.whitelist": " arch,cpu,memory,foo,bar "} 258 conf.Options = map[string]string{"fingerprint.blacklist": " memory,nomad "} 259 getConfig := func() *config.Config { 260 return conf 261 } 262 263 shutdownCh := make(chan struct{}) 264 defer (func() { 265 close(shutdownCh) 266 })() 267 268 fm := NewFingerprintManager( 269 getConfig, 270 node, 271 shutdownCh, 272 testClient.updateNodeFromFingerprint, 273 testLogger(), 274 ) 275 276 err := fm.Run() 277 require.Nil(err) 278 require.NotEqual(node.Attributes["cpu.frequency"], "") 279 require.NotEqual(node.Attributes["cpu.arch"], "") 280 require.Equal(node.Attributes["memory.totalbytes"], "") 281 require.Equal(node.Attributes["nomad.version"], "") 282 } 283 284 func TestFimgerprintManager_Run_WhitelistDrivers(t *testing.T) { 285 t.Parallel() 286 require := require.New(t) 287 288 node := &structs.Node{ 289 Attributes: make(map[string]string, 0), 290 Links: make(map[string]string, 0), 291 Resources: &structs.Resources{}, 292 } 293 testConfig := config.Config{Node: node} 294 testClient := &Client{config: &testConfig} 295 296 conf := config.DefaultConfig() 297 conf.Options = map[string]string{ 298 "driver.raw_exec.enable": "1", 299 "driver.whitelist": " raw_exec , foo ", 300 } 301 getConfig := func() *config.Config { 302 return conf 303 } 304 305 shutdownCh := make(chan struct{}) 306 defer (func() { 307 close(shutdownCh) 308 })() 309 310 fm := NewFingerprintManager( 311 getConfig, 312 node, 313 shutdownCh, 314 testClient.updateNodeFromFingerprint, 315 testLogger(), 316 ) 317 318 err := fm.Run() 319 require.Nil(err) 320 require.NotEqual(node.Attributes["driver.raw_exec"], "") 321 } 322 323 func TestFimgerprintManager_Run_AllDriversBlacklisted(t *testing.T) { 324 t.Parallel() 325 require := require.New(t) 326 327 node := &structs.Node{ 328 Attributes: make(map[string]string, 0), 329 Links: make(map[string]string, 0), 330 Resources: &structs.Resources{}, 331 } 332 testConfig := config.Config{Node: node} 333 testClient := &Client{config: &testConfig} 334 335 conf := config.DefaultConfig() 336 conf.Options = map[string]string{ 337 "driver.whitelist": " foo,bar,baz ", 338 } 339 getConfig := func() *config.Config { 340 return conf 341 } 342 343 shutdownCh := make(chan struct{}) 344 defer (func() { 345 close(shutdownCh) 346 })() 347 348 fm := NewFingerprintManager( 349 getConfig, 350 node, 351 shutdownCh, 352 testClient.updateNodeFromFingerprint, 353 testLogger(), 354 ) 355 356 err := fm.Run() 357 require.Nil(err) 358 require.Equal(node.Attributes["driver.raw_exec"], "") 359 require.Equal(node.Attributes["driver.exec"], "") 360 require.Equal(node.Attributes["driver.docker"], "") 361 } 362 363 func TestFimgerprintManager_Run_DriversWhiteListBlacklistCombination(t *testing.T) { 364 t.Parallel() 365 require := require.New(t) 366 367 node := &structs.Node{ 368 Attributes: make(map[string]string, 0), 369 Links: make(map[string]string, 0), 370 Resources: &structs.Resources{}, 371 } 372 testConfig := config.Config{Node: node} 373 testClient := &Client{config: &testConfig} 374 375 conf := config.DefaultConfig() 376 conf.Options = map[string]string{ 377 "driver.raw_exec.enable": "1", 378 "driver.whitelist": " raw_exec,exec,foo,bar,baz ", 379 "driver.blacklist": " exec,foo,bar,baz ", 380 } 381 getConfig := func() *config.Config { 382 return conf 383 } 384 385 shutdownCh := make(chan struct{}) 386 defer (func() { 387 close(shutdownCh) 388 })() 389 390 fm := NewFingerprintManager( 391 getConfig, 392 node, 393 shutdownCh, 394 testClient.updateNodeFromFingerprint, 395 testLogger(), 396 ) 397 398 err := fm.Run() 399 require.Nil(err) 400 require.NotEqual(node.Attributes["driver.raw_exec"], "") 401 require.Equal(node.Attributes["driver.exec"], "") 402 require.Equal(node.Attributes["foo"], "") 403 require.Equal(node.Attributes["bar"], "") 404 require.Equal(node.Attributes["baz"], "") 405 } 406 407 func TestFimgerprintManager_Run_DriversInBlacklist(t *testing.T) { 408 t.Parallel() 409 require := require.New(t) 410 411 node := &structs.Node{ 412 Attributes: make(map[string]string, 0), 413 Links: make(map[string]string, 0), 414 Resources: &structs.Resources{}, 415 } 416 conf := config.DefaultConfig() 417 conf.Options = map[string]string{ 418 "driver.raw_exec.enable": "1", 419 "driver.whitelist": " raw_exec,foo,bar,baz ", 420 "driver.blacklist": " exec,foo,bar,baz ", 421 } 422 conf.Node = node 423 424 testClient := &Client{config: conf} 425 426 shutdownCh := make(chan struct{}) 427 defer (func() { 428 close(shutdownCh) 429 })() 430 431 fm := NewFingerprintManager( 432 testClient.GetConfig, 433 node, 434 shutdownCh, 435 testClient.updateNodeFromFingerprint, 436 testLogger(), 437 ) 438 439 err := fm.Run() 440 require.Nil(err) 441 require.NotEqual(node.Attributes["driver.raw_exec"], "") 442 require.Equal(node.Attributes["driver.exec"], "") 443 }