github.com/hspak/nomad@v0.7.2-0.20180309000617-bc4ae22a39a5/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  					AuthoritativeRegion:    "foobar",
    88  					BootstrapExpect:        5,
    89  					DataDir:                "/tmp/data",
    90  					ProtocolVersion:        3,
    91  					RaftProtocol:           3,
    92  					NumSchedulers:          2,
    93  					EnabledSchedulers:      []string{"test"},
    94  					NodeGCThreshold:        "12h",
    95  					EvalGCThreshold:        "12h",
    96  					JobGCThreshold:         "12h",
    97  					DeploymentGCThreshold:  "12h",
    98  					HeartbeatGrace:         30 * time.Second,
    99  					MinHeartbeatTTL:        33 * time.Second,
   100  					MaxHeartbeatsPerSecond: 11.0,
   101  					RetryJoin:              []string{"1.1.1.1", "2.2.2.2"},
   102  					StartJoin:              []string{"1.1.1.1", "2.2.2.2"},
   103  					RetryInterval:          "15s",
   104  					RejoinAfterLeave:       true,
   105  					RetryMaxAttempts:       3,
   106  					NonVotingServer:        true,
   107  					RedundancyZone:         "foo",
   108  					UpgradeVersion:         "0.8.0",
   109  					EncryptKey:             "abc",
   110  				},
   111  				ACL: &ACLConfig{
   112  					Enabled:          true,
   113  					TokenTTL:         60 * time.Second,
   114  					PolicyTTL:        60 * time.Second,
   115  					ReplicationToken: "foobar",
   116  				},
   117  				Telemetry: &Telemetry{
   118  					StatsiteAddr:               "127.0.0.1:1234",
   119  					StatsdAddr:                 "127.0.0.1:2345",
   120  					PrometheusMetrics:          true,
   121  					DisableHostname:            true,
   122  					UseNodeName:                false,
   123  					CollectionInterval:         "3s",
   124  					collectionInterval:         3 * time.Second,
   125  					PublishAllocationMetrics:   true,
   126  					PublishNodeMetrics:         true,
   127  					DisableTaggedMetrics:       true,
   128  					BackwardsCompatibleMetrics: true,
   129  				},
   130  				LeaveOnInt:                true,
   131  				LeaveOnTerm:               true,
   132  				EnableSyslog:              true,
   133  				SyslogFacility:            "LOCAL1",
   134  				DisableUpdateCheck:        true,
   135  				DisableAnonymousSignature: true,
   136  				Consul: &config.ConsulConfig{
   137  					ServerServiceName:  "nomad",
   138  					ClientServiceName:  "nomad-client",
   139  					Addr:               "127.0.0.1:9500",
   140  					Token:              "token1",
   141  					Auth:               "username:pass",
   142  					EnableSSL:          &trueValue,
   143  					VerifySSL:          &trueValue,
   144  					CAFile:             "/path/to/ca/file",
   145  					CertFile:           "/path/to/cert/file",
   146  					KeyFile:            "/path/to/key/file",
   147  					ServerAutoJoin:     &trueValue,
   148  					ClientAutoJoin:     &trueValue,
   149  					AutoAdvertise:      &trueValue,
   150  					ChecksUseAdvertise: &trueValue,
   151  				},
   152  				Vault: &config.VaultConfig{
   153  					Addr:                 "127.0.0.1:9500",
   154  					AllowUnauthenticated: &trueValue,
   155  					Enabled:              &falseValue,
   156  					Role:                 "test_role",
   157  					TLSCaFile:            "/path/to/ca/file",
   158  					TLSCaPath:            "/path/to/ca",
   159  					TLSCertFile:          "/path/to/cert/file",
   160  					TLSKeyFile:           "/path/to/key/file",
   161  					TLSServerName:        "foobar",
   162  					TLSSkipVerify:        &trueValue,
   163  					TaskTokenTTL:         "1s",
   164  					Token:                "12345",
   165  				},
   166  				TLSConfig: &config.TLSConfig{
   167  					EnableHTTP:           true,
   168  					EnableRPC:            true,
   169  					VerifyServerHostname: true,
   170  					CAFile:               "foo",
   171  					CertFile:             "bar",
   172  					KeyFile:              "pipe",
   173  					RPCUpgradeMode:       true,
   174  					VerifyHTTPSClient:    true,
   175  				},
   176  				HTTPAPIResponseHeaders: map[string]string{
   177  					"Access-Control-Allow-Origin": "*",
   178  				},
   179  				Sentinel: &config.SentinelConfig{
   180  					Imports: []*config.SentinelImport{
   181  						{
   182  							Name: "foo",
   183  							Path: "foo",
   184  							Args: []string{"a", "b", "c"},
   185  						},
   186  						{
   187  							Name: "bar",
   188  							Path: "bar",
   189  							Args: []string{"x", "y", "z"},
   190  						},
   191  					},
   192  				},
   193  				Autopilot: &config.AutopilotConfig{
   194  					CleanupDeadServers:      &trueValue,
   195  					ServerStabilizationTime: 23057 * time.Second,
   196  					LastContactThreshold:    12705 * time.Second,
   197  					MaxTrailingLogs:         17849,
   198  					EnableRedundancyZones:   &trueValue,
   199  					DisableUpgradeMigration: &trueValue,
   200  					EnableCustomUpgrades:    &trueValue,
   201  				},
   202  			},
   203  			false,
   204  		},
   205  	}
   206  
   207  	for _, tc := range cases {
   208  		t.Run(tc.File, func(t *testing.T) {
   209  			path, err := filepath.Abs(filepath.Join("./config-test-fixtures", tc.File))
   210  			if err != nil {
   211  				t.Fatalf("file: %s\n\n%s", tc.File, err)
   212  			}
   213  
   214  			actual, err := ParseConfigFile(path)
   215  			if (err != nil) != tc.Err {
   216  				t.Fatalf("file: %s\n\n%s", tc.File, err)
   217  			}
   218  
   219  			if !reflect.DeepEqual(actual, tc.Result) {
   220  				t.Errorf("file: %s  diff: (actual vs expected)\n\n%s", tc.File, strings.Join(pretty.Diff(actual, tc.Result), "\n"))
   221  			}
   222  		})
   223  	}
   224  }