github.com/remilapeyre/nomad@v0.8.5/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 LogLevel: "ERR", 28 BindAddr: "192.168.0.1", 29 EnableDebug: true, 30 Ports: &Ports{ 31 HTTP: 1234, 32 RPC: 2345, 33 Serf: 3456, 34 }, 35 Addresses: &Addresses{ 36 HTTP: "127.0.0.1", 37 RPC: "127.0.0.2", 38 Serf: "127.0.0.3", 39 }, 40 AdvertiseAddrs: &AdvertiseAddrs{ 41 RPC: "127.0.0.3", 42 Serf: "127.0.0.4", 43 }, 44 Client: &ClientConfig{ 45 Enabled: true, 46 StateDir: "/tmp/client-state", 47 AllocDir: "/tmp/alloc", 48 Servers: []string{"a.b.c:80", "127.0.0.1:1234"}, 49 NodeClass: "linux-medium-64bit", 50 ServerJoin: &ServerJoin{ 51 RetryJoin: []string{"1.1.1.1", "2.2.2.2"}, 52 RetryInterval: time.Duration(15) * time.Second, 53 RetryMaxAttempts: 3, 54 }, 55 Meta: map[string]string{ 56 "foo": "bar", 57 "baz": "zip", 58 }, 59 Options: map[string]string{ 60 "foo": "bar", 61 "baz": "zip", 62 }, 63 ChrootEnv: map[string]string{ 64 "/opt/myapp/etc": "/etc", 65 "/opt/myapp/bin": "/bin", 66 }, 67 NetworkInterface: "eth0", 68 NetworkSpeed: 100, 69 CpuCompute: 4444, 70 MemoryMB: 0, 71 MaxKillTimeout: "10s", 72 ClientMinPort: 1000, 73 ClientMaxPort: 2000, 74 Reserved: &Resources{ 75 CPU: 10, 76 MemoryMB: 10, 77 DiskMB: 10, 78 IOPS: 10, 79 ReservedPorts: "1,100,10-12", 80 ParsedReservedPorts: []int{1, 10, 11, 12, 100}, 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 }, 219 false, 220 }, 221 { 222 "non-optional.hcl", 223 &Config{ 224 Region: "", 225 Datacenter: "", 226 NodeName: "", 227 DataDir: "", 228 LogLevel: "", 229 BindAddr: "", 230 EnableDebug: false, 231 Ports: nil, 232 Addresses: nil, 233 AdvertiseAddrs: nil, 234 Client: &ClientConfig{ 235 Enabled: false, 236 StateDir: "", 237 AllocDir: "", 238 Servers: nil, 239 NodeClass: "", 240 Meta: nil, 241 Options: nil, 242 ChrootEnv: nil, 243 NetworkInterface: "", 244 NetworkSpeed: 0, 245 CpuCompute: 0, 246 MemoryMB: 5555, 247 MaxKillTimeout: "", 248 ClientMinPort: 0, 249 ClientMaxPort: 0, 250 Reserved: nil, 251 GCInterval: 0, 252 GCParallelDestroys: 0, 253 GCDiskUsageThreshold: 0, 254 GCInodeUsageThreshold: 0, 255 GCMaxAllocs: 0, 256 NoHostUUID: nil, 257 }, 258 Server: nil, 259 ACL: nil, 260 Telemetry: nil, 261 LeaveOnInt: false, 262 LeaveOnTerm: false, 263 EnableSyslog: false, 264 SyslogFacility: "", 265 DisableUpdateCheck: nil, 266 DisableAnonymousSignature: false, 267 Consul: nil, 268 Vault: nil, 269 TLSConfig: nil, 270 HTTPAPIResponseHeaders: nil, 271 Sentinel: nil, 272 }, 273 false, 274 }, 275 } 276 277 for _, tc := range cases { 278 require := require.New(t) 279 t.Run(tc.File, func(t *testing.T) { 280 path, err := filepath.Abs(filepath.Join("./config-test-fixtures", tc.File)) 281 if err != nil { 282 t.Fatalf("file: %s\n\n%s", tc.File, err) 283 } 284 285 actual, err := ParseConfigFile(path) 286 if (err != nil) != tc.Err { 287 t.Fatalf("file: %s\n\n%s", tc.File, err) 288 } 289 require.EqualValues(actual, tc.Result) 290 }) 291 } 292 }