github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/command/agent/config_parse_test.go (about) 1 package agent 2 3 import ( 4 "path/filepath" 5 "reflect" 6 "strings" 7 "testing" 8 "time" 9 10 "github.com/hashicorp/nomad/helper" 11 "github.com/hashicorp/nomad/nomad/structs/config" 12 "github.com/kr/pretty" 13 ) 14 15 func TestConfig_Parse(t *testing.T) { 16 t.Parallel() 17 cases := []struct { 18 File string 19 Result *Config 20 Err bool 21 }{ 22 { 23 "basic.hcl", 24 &Config{ 25 Region: "foobar", 26 Datacenter: "dc2", 27 NodeName: "my-web", 28 DataDir: "/tmp/nomad", 29 LogLevel: "ERR", 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 Meta: map[string]string{ 53 "foo": "bar", 54 "baz": "zip", 55 }, 56 Options: map[string]string{ 57 "foo": "bar", 58 "baz": "zip", 59 }, 60 ChrootEnv: map[string]string{ 61 "/opt/myapp/etc": "/etc", 62 "/opt/myapp/bin": "/bin", 63 }, 64 NetworkInterface: "eth0", 65 NetworkSpeed: 100, 66 CpuCompute: 4444, 67 MaxKillTimeout: "10s", 68 ClientMinPort: 1000, 69 ClientMaxPort: 2000, 70 Reserved: &Resources{ 71 CPU: 10, 72 MemoryMB: 10, 73 DiskMB: 10, 74 IOPS: 10, 75 ReservedPorts: "1,100,10-12", 76 ParsedReservedPorts: []int{1, 10, 11, 12, 100}, 77 }, 78 GCInterval: 6 * time.Second, 79 GCParallelDestroys: 6, 80 GCDiskUsageThreshold: 82, 81 GCInodeUsageThreshold: 91, 82 GCMaxAllocs: 50, 83 NoHostUUID: helper.BoolToPtr(false), 84 }, 85 Server: &ServerConfig{ 86 Enabled: true, 87 BootstrapExpect: 5, 88 DataDir: "/tmp/data", 89 ProtocolVersion: 3, 90 NumSchedulers: 2, 91 EnabledSchedulers: []string{"test"}, 92 NodeGCThreshold: "12h", 93 EvalGCThreshold: "12h", 94 JobGCThreshold: "12h", 95 DeploymentGCThreshold: "12h", 96 HeartbeatGrace: 30 * time.Second, 97 MinHeartbeatTTL: 33 * time.Second, 98 MaxHeartbeatsPerSecond: 11.0, 99 RetryJoin: []string{"1.1.1.1", "2.2.2.2"}, 100 StartJoin: []string{"1.1.1.1", "2.2.2.2"}, 101 RetryInterval: "15s", 102 RejoinAfterLeave: true, 103 RetryMaxAttempts: 3, 104 EncryptKey: "abc", 105 }, 106 Telemetry: &Telemetry{ 107 StatsiteAddr: "127.0.0.1:1234", 108 StatsdAddr: "127.0.0.1:2345", 109 DisableHostname: true, 110 UseNodeName: false, 111 CollectionInterval: "3s", 112 collectionInterval: 3 * time.Second, 113 PublishAllocationMetrics: true, 114 PublishNodeMetrics: true, 115 }, 116 LeaveOnInt: true, 117 LeaveOnTerm: true, 118 EnableSyslog: true, 119 SyslogFacility: "LOCAL1", 120 DisableUpdateCheck: true, 121 DisableAnonymousSignature: true, 122 Atlas: &AtlasConfig{ 123 Infrastructure: "armon/test", 124 Token: "abcd", 125 Join: true, 126 Endpoint: "127.0.0.1:1234", 127 }, 128 Consul: &config.ConsulConfig{ 129 ServerServiceName: "nomad", 130 ClientServiceName: "nomad-client", 131 Addr: "127.0.0.1:9500", 132 Token: "token1", 133 Auth: "username:pass", 134 EnableSSL: &trueValue, 135 VerifySSL: &trueValue, 136 CAFile: "/path/to/ca/file", 137 CertFile: "/path/to/cert/file", 138 KeyFile: "/path/to/key/file", 139 ServerAutoJoin: &trueValue, 140 ClientAutoJoin: &trueValue, 141 AutoAdvertise: &trueValue, 142 ChecksUseAdvertise: &trueValue, 143 }, 144 Vault: &config.VaultConfig{ 145 Addr: "127.0.0.1:9500", 146 AllowUnauthenticated: &trueValue, 147 Enabled: &falseValue, 148 Role: "test_role", 149 TLSCaFile: "/path/to/ca/file", 150 TLSCaPath: "/path/to/ca", 151 TLSCertFile: "/path/to/cert/file", 152 TLSKeyFile: "/path/to/key/file", 153 TLSServerName: "foobar", 154 TLSSkipVerify: &trueValue, 155 TaskTokenTTL: "1s", 156 Token: "12345", 157 }, 158 TLSConfig: &config.TLSConfig{ 159 EnableHTTP: true, 160 EnableRPC: true, 161 VerifyServerHostname: true, 162 CAFile: "foo", 163 CertFile: "bar", 164 KeyFile: "pipe", 165 VerifyHTTPSClient: true, 166 }, 167 HTTPAPIResponseHeaders: map[string]string{ 168 "Access-Control-Allow-Origin": "*", 169 }, 170 }, 171 false, 172 }, 173 } 174 175 for _, tc := range cases { 176 t.Run(tc.File, func(t *testing.T) { 177 path, err := filepath.Abs(filepath.Join("./config-test-fixtures", tc.File)) 178 if err != nil { 179 t.Fatalf("file: %s\n\n%s", tc.File, err) 180 } 181 182 actual, err := ParseConfigFile(path) 183 if (err != nil) != tc.Err { 184 t.Fatalf("file: %s\n\n%s", tc.File, err) 185 } 186 187 if !reflect.DeepEqual(actual, tc.Result) { 188 t.Errorf("file: %s diff: (actual vs expected)\n\n%s", tc.File, strings.Join(pretty.Diff(actual, tc.Result), "\n")) 189 } 190 }) 191 } 192 }