github.com/smithx10/nomad@v0.9.1-rc1/command/agent/config_parse_test.go (about) 1 package agent 2 3 import ( 4 "path/filepath" 5 "testing" 6 "time" 7 8 "github.com/hashicorp/nomad/helper" 9 "github.com/hashicorp/nomad/nomad/structs/config" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestConfig_Parse(t *testing.T) { 14 t.Parallel() 15 cases := []struct { 16 File string 17 Result *Config 18 Err bool 19 }{ 20 { 21 "basic.hcl", 22 &Config{ 23 Region: "foobar", 24 Datacenter: "dc2", 25 NodeName: "my-web", 26 DataDir: "/tmp/nomad", 27 PluginDir: "/tmp/nomad-plugins", 28 LogLevel: "ERR", 29 LogJson: true, 30 BindAddr: "192.168.0.1", 31 EnableDebug: true, 32 Ports: &Ports{ 33 HTTP: 1234, 34 RPC: 2345, 35 Serf: 3456, 36 }, 37 Addresses: &Addresses{ 38 HTTP: "127.0.0.1", 39 RPC: "127.0.0.2", 40 Serf: "127.0.0.3", 41 }, 42 AdvertiseAddrs: &AdvertiseAddrs{ 43 RPC: "127.0.0.3", 44 Serf: "127.0.0.4", 45 }, 46 Client: &ClientConfig{ 47 Enabled: true, 48 StateDir: "/tmp/client-state", 49 AllocDir: "/tmp/alloc", 50 Servers: []string{"a.b.c:80", "127.0.0.1:1234"}, 51 NodeClass: "linux-medium-64bit", 52 ServerJoin: &ServerJoin{ 53 RetryJoin: []string{"1.1.1.1", "2.2.2.2"}, 54 RetryInterval: time.Duration(15) * time.Second, 55 RetryMaxAttempts: 3, 56 }, 57 Meta: map[string]string{ 58 "foo": "bar", 59 "baz": "zip", 60 }, 61 Options: map[string]string{ 62 "foo": "bar", 63 "baz": "zip", 64 }, 65 ChrootEnv: map[string]string{ 66 "/opt/myapp/etc": "/etc", 67 "/opt/myapp/bin": "/bin", 68 }, 69 NetworkInterface: "eth0", 70 NetworkSpeed: 100, 71 CpuCompute: 4444, 72 MemoryMB: 0, 73 MaxKillTimeout: "10s", 74 ClientMinPort: 1000, 75 ClientMaxPort: 2000, 76 Reserved: &Resources{ 77 CPU: 10, 78 MemoryMB: 10, 79 DiskMB: 10, 80 ReservedPorts: "1,100,10-12", 81 }, 82 GCInterval: 6 * time.Second, 83 GCParallelDestroys: 6, 84 GCDiskUsageThreshold: 82, 85 GCInodeUsageThreshold: 91, 86 GCMaxAllocs: 50, 87 NoHostUUID: helper.BoolToPtr(false), 88 }, 89 Server: &ServerConfig{ 90 Enabled: true, 91 AuthoritativeRegion: "foobar", 92 BootstrapExpect: 5, 93 DataDir: "/tmp/data", 94 ProtocolVersion: 3, 95 RaftProtocol: 3, 96 NumSchedulers: helper.IntToPtr(2), 97 EnabledSchedulers: []string{"test"}, 98 NodeGCThreshold: "12h", 99 EvalGCThreshold: "12h", 100 JobGCThreshold: "12h", 101 DeploymentGCThreshold: "12h", 102 HeartbeatGrace: 30 * time.Second, 103 MinHeartbeatTTL: 33 * time.Second, 104 MaxHeartbeatsPerSecond: 11.0, 105 RetryJoin: []string{"1.1.1.1", "2.2.2.2"}, 106 StartJoin: []string{"1.1.1.1", "2.2.2.2"}, 107 RetryInterval: 15 * time.Second, 108 RejoinAfterLeave: true, 109 RetryMaxAttempts: 3, 110 NonVotingServer: true, 111 RedundancyZone: "foo", 112 UpgradeVersion: "0.8.0", 113 EncryptKey: "abc", 114 ServerJoin: &ServerJoin{ 115 RetryJoin: []string{"1.1.1.1", "2.2.2.2"}, 116 RetryInterval: time.Duration(15) * time.Second, 117 RetryMaxAttempts: 3, 118 }, 119 }, 120 ACL: &ACLConfig{ 121 Enabled: true, 122 TokenTTL: 60 * time.Second, 123 PolicyTTL: 60 * time.Second, 124 ReplicationToken: "foobar", 125 }, 126 Telemetry: &Telemetry{ 127 StatsiteAddr: "127.0.0.1:1234", 128 StatsdAddr: "127.0.0.1:2345", 129 PrometheusMetrics: true, 130 DisableHostname: true, 131 UseNodeName: false, 132 CollectionInterval: "3s", 133 collectionInterval: 3 * time.Second, 134 PublishAllocationMetrics: true, 135 PublishNodeMetrics: true, 136 DisableTaggedMetrics: true, 137 BackwardsCompatibleMetrics: true, 138 }, 139 LeaveOnInt: true, 140 LeaveOnTerm: true, 141 EnableSyslog: true, 142 SyslogFacility: "LOCAL1", 143 DisableUpdateCheck: helper.BoolToPtr(true), 144 DisableAnonymousSignature: true, 145 Consul: &config.ConsulConfig{ 146 ServerServiceName: "nomad", 147 ServerHTTPCheckName: "nomad-server-http-health-check", 148 ServerSerfCheckName: "nomad-server-serf-health-check", 149 ServerRPCCheckName: "nomad-server-rpc-health-check", 150 ClientServiceName: "nomad-client", 151 ClientHTTPCheckName: "nomad-client-http-health-check", 152 Addr: "127.0.0.1:9500", 153 Token: "token1", 154 Auth: "username:pass", 155 EnableSSL: &trueValue, 156 VerifySSL: &trueValue, 157 CAFile: "/path/to/ca/file", 158 CertFile: "/path/to/cert/file", 159 KeyFile: "/path/to/key/file", 160 ServerAutoJoin: &trueValue, 161 ClientAutoJoin: &trueValue, 162 AutoAdvertise: &trueValue, 163 ChecksUseAdvertise: &trueValue, 164 }, 165 Vault: &config.VaultConfig{ 166 Addr: "127.0.0.1:9500", 167 AllowUnauthenticated: &trueValue, 168 Enabled: &falseValue, 169 Role: "test_role", 170 TLSCaFile: "/path/to/ca/file", 171 TLSCaPath: "/path/to/ca", 172 TLSCertFile: "/path/to/cert/file", 173 TLSKeyFile: "/path/to/key/file", 174 TLSServerName: "foobar", 175 TLSSkipVerify: &trueValue, 176 TaskTokenTTL: "1s", 177 Token: "12345", 178 }, 179 TLSConfig: &config.TLSConfig{ 180 EnableHTTP: true, 181 EnableRPC: true, 182 VerifyServerHostname: true, 183 CAFile: "foo", 184 CertFile: "bar", 185 KeyFile: "pipe", 186 RPCUpgradeMode: true, 187 VerifyHTTPSClient: true, 188 TLSPreferServerCipherSuites: true, 189 TLSCipherSuites: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", 190 TLSMinVersion: "tls12", 191 }, 192 HTTPAPIResponseHeaders: map[string]string{ 193 "Access-Control-Allow-Origin": "*", 194 }, 195 Sentinel: &config.SentinelConfig{ 196 Imports: []*config.SentinelImport{ 197 { 198 Name: "foo", 199 Path: "foo", 200 Args: []string{"a", "b", "c"}, 201 }, 202 { 203 Name: "bar", 204 Path: "bar", 205 Args: []string{"x", "y", "z"}, 206 }, 207 }, 208 }, 209 Autopilot: &config.AutopilotConfig{ 210 CleanupDeadServers: &trueValue, 211 ServerStabilizationTime: 23057 * time.Second, 212 LastContactThreshold: 12705 * time.Second, 213 MaxTrailingLogs: 17849, 214 EnableRedundancyZones: &trueValue, 215 DisableUpgradeMigration: &trueValue, 216 EnableCustomUpgrades: &trueValue, 217 }, 218 Plugins: []*config.PluginConfig{ 219 { 220 Name: "docker", 221 Args: []string{"foo", "bar"}, 222 Config: map[string]interface{}{ 223 "foo": "bar", 224 "nested": []map[string]interface{}{ 225 { 226 "bam": 2, 227 }, 228 }, 229 }, 230 }, 231 { 232 Name: "exec", 233 Config: map[string]interface{}{ 234 "foo": true, 235 }, 236 }, 237 }, 238 }, 239 false, 240 }, 241 { 242 "basic.json", 243 &Config{ 244 Region: "foobar", 245 Datacenter: "dc2", 246 NodeName: "my-web", 247 DataDir: "/tmp/nomad", 248 PluginDir: "/tmp/nomad-plugins", 249 LogLevel: "ERR", 250 LogJson: true, 251 BindAddr: "192.168.0.1", 252 EnableDebug: true, 253 Ports: &Ports{ 254 HTTP: 1234, 255 RPC: 2345, 256 Serf: 3456, 257 }, 258 Addresses: &Addresses{ 259 HTTP: "127.0.0.1", 260 RPC: "127.0.0.2", 261 Serf: "127.0.0.3", 262 }, 263 AdvertiseAddrs: &AdvertiseAddrs{ 264 RPC: "127.0.0.3", 265 Serf: "127.0.0.4", 266 }, 267 Client: &ClientConfig{ 268 Enabled: true, 269 StateDir: "/tmp/client-state", 270 AllocDir: "/tmp/alloc", 271 Servers: []string{"a.b.c:80", "127.0.0.1:1234"}, 272 NodeClass: "linux-medium-64bit", 273 ServerJoin: &ServerJoin{ 274 RetryJoin: []string{"1.1.1.1", "2.2.2.2"}, 275 RetryInterval: time.Duration(15) * time.Second, 276 RetryMaxAttempts: 3, 277 }, 278 Meta: map[string]string{ 279 "foo": "bar", 280 "baz": "zip", 281 }, 282 Options: map[string]string{ 283 "foo": "bar", 284 "baz": "zip", 285 }, 286 ChrootEnv: map[string]string{ 287 "/opt/myapp/etc": "/etc", 288 "/opt/myapp/bin": "/bin", 289 }, 290 NetworkInterface: "eth0", 291 NetworkSpeed: 100, 292 CpuCompute: 4444, 293 MemoryMB: 0, 294 MaxKillTimeout: "10s", 295 ClientMinPort: 1000, 296 ClientMaxPort: 2000, 297 Reserved: &Resources{ 298 CPU: 10, 299 MemoryMB: 10, 300 DiskMB: 10, 301 ReservedPorts: "1,100,10-12", 302 }, 303 GCInterval: 6 * time.Second, 304 GCParallelDestroys: 6, 305 GCDiskUsageThreshold: 82, 306 GCInodeUsageThreshold: 91, 307 GCMaxAllocs: 50, 308 NoHostUUID: helper.BoolToPtr(false), 309 }, 310 Server: &ServerConfig{ 311 Enabled: true, 312 AuthoritativeRegion: "foobar", 313 BootstrapExpect: 5, 314 DataDir: "/tmp/data", 315 ProtocolVersion: 3, 316 RaftProtocol: 3, 317 NumSchedulers: helper.IntToPtr(2), 318 EnabledSchedulers: []string{"test"}, 319 NodeGCThreshold: "12h", 320 EvalGCThreshold: "12h", 321 JobGCThreshold: "12h", 322 DeploymentGCThreshold: "12h", 323 HeartbeatGrace: 30 * time.Second, 324 MinHeartbeatTTL: 33 * time.Second, 325 MaxHeartbeatsPerSecond: 11.0, 326 RetryJoin: []string{"1.1.1.1", "2.2.2.2"}, 327 StartJoin: []string{"1.1.1.1", "2.2.2.2"}, 328 RetryInterval: 15 * time.Second, 329 RejoinAfterLeave: true, 330 RetryMaxAttempts: 3, 331 NonVotingServer: true, 332 RedundancyZone: "foo", 333 UpgradeVersion: "0.8.0", 334 EncryptKey: "abc", 335 ServerJoin: &ServerJoin{ 336 RetryJoin: []string{"1.1.1.1", "2.2.2.2"}, 337 RetryInterval: time.Duration(15) * time.Second, 338 RetryMaxAttempts: 3, 339 }, 340 }, 341 ACL: &ACLConfig{ 342 Enabled: true, 343 TokenTTL: 60 * time.Second, 344 PolicyTTL: 60 * time.Second, 345 ReplicationToken: "foobar", 346 }, 347 Telemetry: &Telemetry{ 348 StatsiteAddr: "127.0.0.1:1234", 349 StatsdAddr: "127.0.0.1:2345", 350 PrometheusMetrics: true, 351 DisableHostname: true, 352 UseNodeName: false, 353 CollectionInterval: "3s", 354 collectionInterval: 3 * time.Second, 355 PublishAllocationMetrics: true, 356 PublishNodeMetrics: true, 357 DisableTaggedMetrics: true, 358 BackwardsCompatibleMetrics: true, 359 }, 360 LeaveOnInt: true, 361 LeaveOnTerm: true, 362 EnableSyslog: true, 363 SyslogFacility: "LOCAL1", 364 DisableUpdateCheck: helper.BoolToPtr(true), 365 DisableAnonymousSignature: true, 366 Consul: &config.ConsulConfig{ 367 ServerServiceName: "nomad", 368 ServerHTTPCheckName: "nomad-server-http-health-check", 369 ServerSerfCheckName: "nomad-server-serf-health-check", 370 ServerRPCCheckName: "nomad-server-rpc-health-check", 371 ClientServiceName: "nomad-client", 372 ClientHTTPCheckName: "nomad-client-http-health-check", 373 Addr: "127.0.0.1:9500", 374 Token: "token1", 375 Auth: "username:pass", 376 EnableSSL: &trueValue, 377 VerifySSL: &trueValue, 378 CAFile: "/path/to/ca/file", 379 CertFile: "/path/to/cert/file", 380 KeyFile: "/path/to/key/file", 381 ServerAutoJoin: &trueValue, 382 ClientAutoJoin: &trueValue, 383 AutoAdvertise: &trueValue, 384 ChecksUseAdvertise: &trueValue, 385 }, 386 Vault: &config.VaultConfig{ 387 Addr: "127.0.0.1:9500", 388 AllowUnauthenticated: &trueValue, 389 Enabled: &falseValue, 390 Role: "test_role", 391 TLSCaFile: "/path/to/ca/file", 392 TLSCaPath: "/path/to/ca", 393 TLSCertFile: "/path/to/cert/file", 394 TLSKeyFile: "/path/to/key/file", 395 TLSServerName: "foobar", 396 TLSSkipVerify: &trueValue, 397 TaskTokenTTL: "1s", 398 Token: "12345", 399 }, 400 TLSConfig: &config.TLSConfig{ 401 EnableHTTP: true, 402 EnableRPC: true, 403 VerifyServerHostname: true, 404 CAFile: "foo", 405 CertFile: "bar", 406 KeyFile: "pipe", 407 RPCUpgradeMode: true, 408 VerifyHTTPSClient: true, 409 TLSPreferServerCipherSuites: true, 410 TLSCipherSuites: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", 411 TLSMinVersion: "tls12", 412 }, 413 HTTPAPIResponseHeaders: map[string]string{ 414 "Access-Control-Allow-Origin": "*", 415 }, 416 Sentinel: &config.SentinelConfig{ 417 Imports: []*config.SentinelImport{ 418 { 419 Name: "foo", 420 Path: "foo", 421 Args: []string{"a", "b", "c"}, 422 }, 423 { 424 Name: "bar", 425 Path: "bar", 426 Args: []string{"x", "y", "z"}, 427 }, 428 }, 429 }, 430 Autopilot: &config.AutopilotConfig{ 431 CleanupDeadServers: &trueValue, 432 ServerStabilizationTime: 23057 * time.Second, 433 LastContactThreshold: 12705 * time.Second, 434 MaxTrailingLogs: 17849, 435 EnableRedundancyZones: &trueValue, 436 DisableUpgradeMigration: &trueValue, 437 EnableCustomUpgrades: &trueValue, 438 }, 439 Plugins: []*config.PluginConfig{ 440 { 441 Name: "docker", 442 Args: []string{"foo", "bar"}, 443 Config: map[string]interface{}{ 444 "foo": "bar", 445 "nested": []map[string]interface{}{ 446 { 447 "bam": 2, 448 }, 449 }, 450 }, 451 }, 452 { 453 Name: "exec", 454 Config: map[string]interface{}{ 455 "foo": true, 456 }, 457 }, 458 }, 459 }, 460 false, 461 }, 462 { 463 "plugin.hcl", 464 &Config{ 465 Region: "", 466 Datacenter: "", 467 NodeName: "", 468 DataDir: "", 469 PluginDir: "", 470 LogLevel: "", 471 BindAddr: "", 472 EnableDebug: false, 473 Ports: nil, 474 Addresses: nil, 475 AdvertiseAddrs: nil, 476 Client: &ClientConfig{ 477 Enabled: false, 478 StateDir: "", 479 AllocDir: "", 480 Servers: nil, 481 NodeClass: "", 482 Meta: nil, 483 Options: nil, 484 ChrootEnv: nil, 485 NetworkInterface: "", 486 NetworkSpeed: 0, 487 CpuCompute: 0, 488 MemoryMB: 5555, 489 MaxKillTimeout: "", 490 ClientMinPort: 0, 491 ClientMaxPort: 0, 492 Reserved: nil, 493 GCInterval: 0, 494 GCParallelDestroys: 0, 495 GCDiskUsageThreshold: 0, 496 GCInodeUsageThreshold: 0, 497 GCMaxAllocs: 0, 498 NoHostUUID: nil, 499 }, 500 Server: nil, 501 ACL: nil, 502 Telemetry: nil, 503 LeaveOnInt: false, 504 LeaveOnTerm: false, 505 EnableSyslog: false, 506 SyslogFacility: "", 507 DisableUpdateCheck: nil, 508 DisableAnonymousSignature: false, 509 Consul: nil, 510 Vault: nil, 511 TLSConfig: nil, 512 HTTPAPIResponseHeaders: nil, 513 Sentinel: nil, 514 Plugins: []*config.PluginConfig{ 515 { 516 Name: "docker", 517 Config: map[string]interface{}{ 518 "allow_privileged": true, 519 }, 520 }, 521 { 522 Name: "raw_exec", 523 Config: map[string]interface{}{ 524 "enabled": true, 525 }, 526 }, 527 }, 528 }, 529 false, 530 }, 531 { 532 "plugin.json", 533 &Config{ 534 Region: "", 535 Datacenter: "", 536 NodeName: "", 537 DataDir: "", 538 PluginDir: "", 539 LogLevel: "", 540 BindAddr: "", 541 EnableDebug: false, 542 Ports: nil, 543 Addresses: nil, 544 AdvertiseAddrs: nil, 545 Client: &ClientConfig{ 546 Enabled: false, 547 StateDir: "", 548 AllocDir: "", 549 Servers: nil, 550 NodeClass: "", 551 Meta: nil, 552 Options: nil, 553 ChrootEnv: nil, 554 NetworkInterface: "", 555 NetworkSpeed: 0, 556 CpuCompute: 0, 557 MemoryMB: 5555, 558 MaxKillTimeout: "", 559 ClientMinPort: 0, 560 ClientMaxPort: 0, 561 Reserved: nil, 562 GCInterval: 0, 563 GCParallelDestroys: 0, 564 GCDiskUsageThreshold: 0, 565 GCInodeUsageThreshold: 0, 566 GCMaxAllocs: 0, 567 NoHostUUID: nil, 568 }, 569 Server: nil, 570 ACL: nil, 571 Telemetry: nil, 572 LeaveOnInt: false, 573 LeaveOnTerm: false, 574 EnableSyslog: false, 575 SyslogFacility: "", 576 DisableUpdateCheck: nil, 577 DisableAnonymousSignature: false, 578 Consul: nil, 579 Vault: nil, 580 TLSConfig: nil, 581 HTTPAPIResponseHeaders: nil, 582 Sentinel: nil, 583 Plugins: []*config.PluginConfig{ 584 { 585 Name: "docker", 586 Config: map[string]interface{}{ 587 "allow_privileged": true, 588 }, 589 }, 590 { 591 Name: "raw_exec", 592 Config: map[string]interface{}{ 593 "enabled": true, 594 }, 595 }, 596 }, 597 }, 598 false, 599 }, 600 { 601 "non-optional.hcl", 602 &Config{ 603 Region: "", 604 Datacenter: "", 605 NodeName: "", 606 DataDir: "", 607 PluginDir: "", 608 LogLevel: "", 609 BindAddr: "", 610 EnableDebug: false, 611 Ports: nil, 612 Addresses: nil, 613 AdvertiseAddrs: nil, 614 Client: &ClientConfig{ 615 Enabled: false, 616 StateDir: "", 617 AllocDir: "", 618 Servers: nil, 619 NodeClass: "", 620 Meta: nil, 621 Options: nil, 622 ChrootEnv: nil, 623 NetworkInterface: "", 624 NetworkSpeed: 0, 625 CpuCompute: 0, 626 MemoryMB: 5555, 627 MaxKillTimeout: "", 628 ClientMinPort: 0, 629 ClientMaxPort: 0, 630 Reserved: nil, 631 GCInterval: 0, 632 GCParallelDestroys: 0, 633 GCDiskUsageThreshold: 0, 634 GCInodeUsageThreshold: 0, 635 GCMaxAllocs: 0, 636 NoHostUUID: nil, 637 }, 638 Server: nil, 639 ACL: nil, 640 Telemetry: nil, 641 LeaveOnInt: false, 642 LeaveOnTerm: false, 643 EnableSyslog: false, 644 SyslogFacility: "", 645 DisableUpdateCheck: nil, 646 DisableAnonymousSignature: false, 647 Consul: nil, 648 Vault: nil, 649 TLSConfig: nil, 650 HTTPAPIResponseHeaders: nil, 651 Sentinel: nil, 652 }, 653 false, 654 }, 655 } 656 657 for _, tc := range cases { 658 t.Run(tc.File, func(t *testing.T) { 659 require := require.New(t) 660 path, err := filepath.Abs(filepath.Join("./testdata", tc.File)) 661 if err != nil { 662 t.Fatalf("file: %s\n\n%s", tc.File, err) 663 } 664 665 actual, err := ParseConfigFile(path) 666 if (err != nil) != tc.Err { 667 t.Fatalf("file: %s\n\n%s", tc.File, err) 668 } 669 670 //panic(fmt.Sprintf("first: %+v \n second: %+v", actual.TLSConfig, tc.Result.TLSConfig)) 671 require.EqualValues(removeHelperAttributes(actual), tc.Result) 672 }) 673 } 674 } 675 676 // In order to compare the Config struct after parsing, and from generating what 677 // is expected in the test, we need to remove helper attributes that are 678 // instantiated in the process of parsing the configuration 679 func removeHelperAttributes(c *Config) *Config { 680 if c.TLSConfig != nil { 681 c.TLSConfig.KeyLoader = nil 682 } 683 return c 684 }