github.com/smintz/nomad@v0.8.3/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  					Meta: map[string]string{
    51  						"foo": "bar",
    52  						"baz": "zip",
    53  					},
    54  					Options: map[string]string{
    55  						"foo": "bar",
    56  						"baz": "zip",
    57  					},
    58  					ChrootEnv: map[string]string{
    59  						"/opt/myapp/etc": "/etc",
    60  						"/opt/myapp/bin": "/bin",
    61  					},
    62  					NetworkInterface: "eth0",
    63  					NetworkSpeed:     100,
    64  					CpuCompute:       4444,
    65  					MemoryMB:         0,
    66  					MaxKillTimeout:   "10s",
    67  					ClientMinPort:    1000,
    68  					ClientMaxPort:    2000,
    69  					Reserved: &Resources{
    70  						CPU:                 10,
    71  						MemoryMB:            10,
    72  						DiskMB:              10,
    73  						IOPS:                10,
    74  						ReservedPorts:       "1,100,10-12",
    75  						ParsedReservedPorts: []int{1, 10, 11, 12, 100},
    76  					},
    77  					GCInterval:            6 * time.Second,
    78  					GCParallelDestroys:    6,
    79  					GCDiskUsageThreshold:  82,
    80  					GCInodeUsageThreshold: 91,
    81  					GCMaxAllocs:           50,
    82  					NoHostUUID:            helper.BoolToPtr(false),
    83  				},
    84  				Server: &ServerConfig{
    85  					Enabled:                true,
    86  					AuthoritativeRegion:    "foobar",
    87  					BootstrapExpect:        5,
    88  					DataDir:                "/tmp/data",
    89  					ProtocolVersion:        3,
    90  					RaftProtocol:           3,
    91  					NumSchedulers:          2,
    92  					EnabledSchedulers:      []string{"test"},
    93  					NodeGCThreshold:        "12h",
    94  					EvalGCThreshold:        "12h",
    95  					JobGCThreshold:         "12h",
    96  					DeploymentGCThreshold:  "12h",
    97  					HeartbeatGrace:         30 * time.Second,
    98  					MinHeartbeatTTL:        33 * time.Second,
    99  					MaxHeartbeatsPerSecond: 11.0,
   100  					RetryJoin:              []string{"1.1.1.1", "2.2.2.2"},
   101  					StartJoin:              []string{"1.1.1.1", "2.2.2.2"},
   102  					RetryInterval:          "15s",
   103  					RejoinAfterLeave:       true,
   104  					RetryMaxAttempts:       3,
   105  					NonVotingServer:        true,
   106  					RedundancyZone:         "foo",
   107  					UpgradeVersion:         "0.8.0",
   108  					EncryptKey:             "abc",
   109  				},
   110  				ACL: &ACLConfig{
   111  					Enabled:          true,
   112  					TokenTTL:         60 * time.Second,
   113  					PolicyTTL:        60 * time.Second,
   114  					ReplicationToken: "foobar",
   115  				},
   116  				Telemetry: &Telemetry{
   117  					StatsiteAddr:               "127.0.0.1:1234",
   118  					StatsdAddr:                 "127.0.0.1:2345",
   119  					PrometheusMetrics:          true,
   120  					DisableHostname:            true,
   121  					UseNodeName:                false,
   122  					CollectionInterval:         "3s",
   123  					collectionInterval:         3 * time.Second,
   124  					PublishAllocationMetrics:   true,
   125  					PublishNodeMetrics:         true,
   126  					DisableTaggedMetrics:       true,
   127  					BackwardsCompatibleMetrics: true,
   128  				},
   129  				LeaveOnInt:                true,
   130  				LeaveOnTerm:               true,
   131  				EnableSyslog:              true,
   132  				SyslogFacility:            "LOCAL1",
   133  				DisableUpdateCheck:        helper.BoolToPtr(true),
   134  				DisableAnonymousSignature: true,
   135  				Consul: &config.ConsulConfig{
   136  					ServerServiceName:   "nomad",
   137  					ServerHTTPCheckName: "nomad-server-http-health-check",
   138  					ServerSerfCheckName: "nomad-server-serf-health-check",
   139  					ServerRPCCheckName:  "nomad-server-rpc-health-check",
   140  					ClientServiceName:   "nomad-client",
   141  					ClientHTTPCheckName: "nomad-client-http-health-check",
   142  					Addr:                "127.0.0.1:9500",
   143  					Token:               "token1",
   144  					Auth:                "username:pass",
   145  					EnableSSL:           &trueValue,
   146  					VerifySSL:           &trueValue,
   147  					CAFile:              "/path/to/ca/file",
   148  					CertFile:            "/path/to/cert/file",
   149  					KeyFile:             "/path/to/key/file",
   150  					ServerAutoJoin:      &trueValue,
   151  					ClientAutoJoin:      &trueValue,
   152  					AutoAdvertise:       &trueValue,
   153  					ChecksUseAdvertise:  &trueValue,
   154  				},
   155  				Vault: &config.VaultConfig{
   156  					Addr:                 "127.0.0.1:9500",
   157  					AllowUnauthenticated: &trueValue,
   158  					Enabled:              &falseValue,
   159  					Role:                 "test_role",
   160  					TLSCaFile:            "/path/to/ca/file",
   161  					TLSCaPath:            "/path/to/ca",
   162  					TLSCertFile:          "/path/to/cert/file",
   163  					TLSKeyFile:           "/path/to/key/file",
   164  					TLSServerName:        "foobar",
   165  					TLSSkipVerify:        &trueValue,
   166  					TaskTokenTTL:         "1s",
   167  					Token:                "12345",
   168  				},
   169  				TLSConfig: &config.TLSConfig{
   170  					EnableHTTP:           true,
   171  					EnableRPC:            true,
   172  					VerifyServerHostname: true,
   173  					CAFile:               "foo",
   174  					CertFile:             "bar",
   175  					KeyFile:              "pipe",
   176  					RPCUpgradeMode:       true,
   177  					VerifyHTTPSClient:    true,
   178  				},
   179  				HTTPAPIResponseHeaders: map[string]string{
   180  					"Access-Control-Allow-Origin": "*",
   181  				},
   182  				Sentinel: &config.SentinelConfig{
   183  					Imports: []*config.SentinelImport{
   184  						{
   185  							Name: "foo",
   186  							Path: "foo",
   187  							Args: []string{"a", "b", "c"},
   188  						},
   189  						{
   190  							Name: "bar",
   191  							Path: "bar",
   192  							Args: []string{"x", "y", "z"},
   193  						},
   194  					},
   195  				},
   196  				Autopilot: &config.AutopilotConfig{
   197  					CleanupDeadServers:      &trueValue,
   198  					ServerStabilizationTime: 23057 * time.Second,
   199  					LastContactThreshold:    12705 * time.Second,
   200  					MaxTrailingLogs:         17849,
   201  					EnableRedundancyZones:   &trueValue,
   202  					DisableUpgradeMigration: &trueValue,
   203  					EnableCustomUpgrades:    &trueValue,
   204  				},
   205  			},
   206  			false,
   207  		},
   208  		{
   209  			"non-optional.hcl",
   210  			&Config{
   211  				Region:         "",
   212  				Datacenter:     "",
   213  				NodeName:       "",
   214  				DataDir:        "",
   215  				LogLevel:       "",
   216  				BindAddr:       "",
   217  				EnableDebug:    false,
   218  				Ports:          nil,
   219  				Addresses:      nil,
   220  				AdvertiseAddrs: nil,
   221  				Client: &ClientConfig{
   222  					Enabled:               false,
   223  					StateDir:              "",
   224  					AllocDir:              "",
   225  					Servers:               nil,
   226  					NodeClass:             "",
   227  					Meta:                  nil,
   228  					Options:               nil,
   229  					ChrootEnv:             nil,
   230  					NetworkInterface:      "",
   231  					NetworkSpeed:          0,
   232  					CpuCompute:            0,
   233  					MemoryMB:              5555,
   234  					MaxKillTimeout:        "",
   235  					ClientMinPort:         0,
   236  					ClientMaxPort:         0,
   237  					Reserved:              nil,
   238  					GCInterval:            0,
   239  					GCParallelDestroys:    0,
   240  					GCDiskUsageThreshold:  0,
   241  					GCInodeUsageThreshold: 0,
   242  					GCMaxAllocs:           0,
   243  					NoHostUUID:            nil,
   244  				},
   245  				Server:                    nil,
   246  				ACL:                       nil,
   247  				Telemetry:                 nil,
   248  				LeaveOnInt:                false,
   249  				LeaveOnTerm:               false,
   250  				EnableSyslog:              false,
   251  				SyslogFacility:            "",
   252  				DisableUpdateCheck:        nil,
   253  				DisableAnonymousSignature: false,
   254  				Consul:                 nil,
   255  				Vault:                  nil,
   256  				TLSConfig:              nil,
   257  				HTTPAPIResponseHeaders: nil,
   258  				Sentinel:               nil,
   259  			},
   260  			false,
   261  		},
   262  	}
   263  
   264  	for _, tc := range cases {
   265  		require := require.New(t)
   266  		t.Run(tc.File, func(t *testing.T) {
   267  			path, err := filepath.Abs(filepath.Join("./config-test-fixtures", tc.File))
   268  			if err != nil {
   269  				t.Fatalf("file: %s\n\n%s", tc.File, err)
   270  			}
   271  
   272  			actual, err := ParseConfigFile(path)
   273  			if (err != nil) != tc.Err {
   274  				t.Fatalf("file: %s\n\n%s", tc.File, err)
   275  			}
   276  			require.EqualValues(actual, tc.Result)
   277  		})
   278  	}
   279  }