github.com/zhizhiboom/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/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 BindAddr: "192.168.0.1", 30 EnableDebug: true, 31 Ports: &Ports{ 32 HTTP: 1234, 33 RPC: 2345, 34 Serf: 3456, 35 }, 36 Addresses: &Addresses{ 37 HTTP: "127.0.0.1", 38 RPC: "127.0.0.2", 39 Serf: "127.0.0.3", 40 }, 41 AdvertiseAddrs: &AdvertiseAddrs{ 42 RPC: "127.0.0.3", 43 Serf: "127.0.0.4", 44 }, 45 Client: &ClientConfig{ 46 Enabled: true, 47 StateDir: "/tmp/client-state", 48 AllocDir: "/tmp/alloc", 49 Servers: []string{"a.b.c:80", "127.0.0.1:1234"}, 50 NodeClass: "linux-medium-64bit", 51 ServerJoin: &ServerJoin{ 52 RetryJoin: []string{"1.1.1.1", "2.2.2.2"}, 53 RetryInterval: time.Duration(15) * time.Second, 54 RetryMaxAttempts: 3, 55 }, 56 Meta: map[string]string{ 57 "foo": "bar", 58 "baz": "zip", 59 }, 60 Options: map[string]string{ 61 "foo": "bar", 62 "baz": "zip", 63 }, 64 ChrootEnv: map[string]string{ 65 "/opt/myapp/etc": "/etc", 66 "/opt/myapp/bin": "/bin", 67 }, 68 NetworkInterface: "eth0", 69 NetworkSpeed: 100, 70 CpuCompute: 4444, 71 MemoryMB: 0, 72 MaxKillTimeout: "10s", 73 ClientMinPort: 1000, 74 ClientMaxPort: 2000, 75 Reserved: &Resources{ 76 CPU: 10, 77 MemoryMB: 10, 78 DiskMB: 10, 79 IOPS: 10, 80 ReservedPorts: "1,100,10-12", 81 ParsedReservedPorts: []int{1, 10, 11, 12, 100}, 82 }, 83 GCInterval: 6 * time.Second, 84 GCParallelDestroys: 6, 85 GCDiskUsageThreshold: 82, 86 GCInodeUsageThreshold: 91, 87 GCMaxAllocs: 50, 88 NoHostUUID: helper.BoolToPtr(false), 89 }, 90 Server: &ServerConfig{ 91 Enabled: true, 92 AuthoritativeRegion: "foobar", 93 BootstrapExpect: 5, 94 DataDir: "/tmp/data", 95 ProtocolVersion: 3, 96 RaftProtocol: 3, 97 NumSchedulers: helper.IntToPtr(2), 98 EnabledSchedulers: []string{"test"}, 99 NodeGCThreshold: "12h", 100 EvalGCThreshold: "12h", 101 JobGCThreshold: "12h", 102 DeploymentGCThreshold: "12h", 103 HeartbeatGrace: 30 * time.Second, 104 MinHeartbeatTTL: 33 * time.Second, 105 MaxHeartbeatsPerSecond: 11.0, 106 RetryJoin: []string{"1.1.1.1", "2.2.2.2"}, 107 StartJoin: []string{"1.1.1.1", "2.2.2.2"}, 108 RetryInterval: 15 * time.Second, 109 RejoinAfterLeave: true, 110 RetryMaxAttempts: 3, 111 NonVotingServer: true, 112 RedundancyZone: "foo", 113 UpgradeVersion: "0.8.0", 114 EncryptKey: "abc", 115 ServerJoin: &ServerJoin{ 116 RetryJoin: []string{"1.1.1.1", "2.2.2.2"}, 117 RetryInterval: time.Duration(15) * time.Second, 118 RetryMaxAttempts: 3, 119 }, 120 }, 121 ACL: &ACLConfig{ 122 Enabled: true, 123 TokenTTL: 60 * time.Second, 124 PolicyTTL: 60 * time.Second, 125 ReplicationToken: "foobar", 126 }, 127 Telemetry: &Telemetry{ 128 StatsiteAddr: "127.0.0.1:1234", 129 StatsdAddr: "127.0.0.1:2345", 130 PrometheusMetrics: true, 131 DisableHostname: true, 132 UseNodeName: false, 133 CollectionInterval: "3s", 134 collectionInterval: 3 * time.Second, 135 PublishAllocationMetrics: true, 136 PublishNodeMetrics: true, 137 DisableTaggedMetrics: true, 138 BackwardsCompatibleMetrics: true, 139 }, 140 LeaveOnInt: true, 141 LeaveOnTerm: true, 142 EnableSyslog: true, 143 SyslogFacility: "LOCAL1", 144 DisableUpdateCheck: helper.BoolToPtr(true), 145 DisableAnonymousSignature: true, 146 Consul: &config.ConsulConfig{ 147 ServerServiceName: "nomad", 148 ServerHTTPCheckName: "nomad-server-http-health-check", 149 ServerSerfCheckName: "nomad-server-serf-health-check", 150 ServerRPCCheckName: "nomad-server-rpc-health-check", 151 ClientServiceName: "nomad-client", 152 ClientHTTPCheckName: "nomad-client-http-health-check", 153 Addr: "127.0.0.1:9500", 154 Token: "token1", 155 Auth: "username:pass", 156 EnableSSL: &trueValue, 157 VerifySSL: &trueValue, 158 CAFile: "/path/to/ca/file", 159 CertFile: "/path/to/cert/file", 160 KeyFile: "/path/to/key/file", 161 ServerAutoJoin: &trueValue, 162 ClientAutoJoin: &trueValue, 163 AutoAdvertise: &trueValue, 164 ChecksUseAdvertise: &trueValue, 165 }, 166 Vault: &config.VaultConfig{ 167 Addr: "127.0.0.1:9500", 168 AllowUnauthenticated: &trueValue, 169 Enabled: &falseValue, 170 Role: "test_role", 171 TLSCaFile: "/path/to/ca/file", 172 TLSCaPath: "/path/to/ca", 173 TLSCertFile: "/path/to/cert/file", 174 TLSKeyFile: "/path/to/key/file", 175 TLSServerName: "foobar", 176 TLSSkipVerify: &trueValue, 177 TaskTokenTTL: "1s", 178 Token: "12345", 179 }, 180 TLSConfig: &config.TLSConfig{ 181 EnableHTTP: true, 182 EnableRPC: true, 183 VerifyServerHostname: true, 184 CAFile: "foo", 185 CertFile: "bar", 186 KeyFile: "pipe", 187 RPCUpgradeMode: true, 188 VerifyHTTPSClient: true, 189 TLSPreferServerCipherSuites: true, 190 TLSCipherSuites: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", 191 TLSMinVersion: "tls12", 192 }, 193 HTTPAPIResponseHeaders: map[string]string{ 194 "Access-Control-Allow-Origin": "*", 195 }, 196 Sentinel: &config.SentinelConfig{ 197 Imports: []*config.SentinelImport{ 198 { 199 Name: "foo", 200 Path: "foo", 201 Args: []string{"a", "b", "c"}, 202 }, 203 { 204 Name: "bar", 205 Path: "bar", 206 Args: []string{"x", "y", "z"}, 207 }, 208 }, 209 }, 210 Autopilot: &config.AutopilotConfig{ 211 CleanupDeadServers: &trueValue, 212 ServerStabilizationTime: 23057 * time.Second, 213 LastContactThreshold: 12705 * time.Second, 214 MaxTrailingLogs: 17849, 215 EnableRedundancyZones: &trueValue, 216 DisableUpgradeMigration: &trueValue, 217 EnableCustomUpgrades: &trueValue, 218 }, 219 Plugins: []*config.PluginConfig{ 220 { 221 Name: "docker", 222 Args: []string{"foo", "bar"}, 223 Config: map[string]interface{}{ 224 "foo": "bar", 225 "nested": []map[string]interface{}{ 226 { 227 "bam": 2, 228 }, 229 }, 230 }, 231 }, 232 { 233 Name: "exec", 234 Config: map[string]interface{}{ 235 "foo": true, 236 }, 237 }, 238 }, 239 }, 240 false, 241 }, 242 { 243 "non-optional.hcl", 244 &Config{ 245 Region: "", 246 Datacenter: "", 247 NodeName: "", 248 DataDir: "", 249 PluginDir: "", 250 LogLevel: "", 251 BindAddr: "", 252 EnableDebug: false, 253 Ports: nil, 254 Addresses: nil, 255 AdvertiseAddrs: nil, 256 Client: &ClientConfig{ 257 Enabled: false, 258 StateDir: "", 259 AllocDir: "", 260 Servers: nil, 261 NodeClass: "", 262 Meta: nil, 263 Options: nil, 264 ChrootEnv: nil, 265 NetworkInterface: "", 266 NetworkSpeed: 0, 267 CpuCompute: 0, 268 MemoryMB: 5555, 269 MaxKillTimeout: "", 270 ClientMinPort: 0, 271 ClientMaxPort: 0, 272 Reserved: nil, 273 GCInterval: 0, 274 GCParallelDestroys: 0, 275 GCDiskUsageThreshold: 0, 276 GCInodeUsageThreshold: 0, 277 GCMaxAllocs: 0, 278 NoHostUUID: nil, 279 }, 280 Server: nil, 281 ACL: nil, 282 Telemetry: nil, 283 LeaveOnInt: false, 284 LeaveOnTerm: false, 285 EnableSyslog: false, 286 SyslogFacility: "", 287 DisableUpdateCheck: nil, 288 DisableAnonymousSignature: false, 289 Consul: nil, 290 Vault: nil, 291 TLSConfig: nil, 292 HTTPAPIResponseHeaders: nil, 293 Sentinel: nil, 294 }, 295 false, 296 }, 297 } 298 299 for _, tc := range cases { 300 t.Run(tc.File, func(t *testing.T) { 301 require := require.New(t) 302 path, err := filepath.Abs(filepath.Join("./config-test-fixtures", tc.File)) 303 if err != nil { 304 t.Fatalf("file: %s\n\n%s", tc.File, err) 305 } 306 307 actual, err := ParseConfigFile(path) 308 if (err != nil) != tc.Err { 309 t.Fatalf("file: %s\n\n%s", tc.File, err) 310 } 311 312 //panic(fmt.Sprintf("first: %+v \n second: %+v", actual.TLSConfig, tc.Result.TLSConfig)) 313 require.EqualValues(removeHelperAttributes(actual), tc.Result) 314 }) 315 } 316 } 317 318 // In order to compare the Config struct after parsing, and from generating what 319 // is expected in the test, we need to remove helper attributes that are 320 // instantiated in the process of parsing the configuration 321 func removeHelperAttributes(c *Config) *Config { 322 if c.TLSConfig != nil { 323 c.TLSConfig.KeyLoader = nil 324 } 325 return c 326 }