github.com/maier/nomad@v0.4.1-0.20161110003312-a9e3d0b8549d/command/agent/config_parse_test.go (about)

     1  package agent
     2  
     3  import (
     4  	"path/filepath"
     5  	"reflect"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/hashicorp/nomad/nomad/structs/config"
    10  )
    11  
    12  func TestConfig_Parse(t *testing.T) {
    13  	cases := []struct {
    14  		File   string
    15  		Result *Config
    16  		Err    bool
    17  	}{
    18  		{
    19  			"basic.hcl",
    20  			&Config{
    21  				Region:      "foobar",
    22  				Datacenter:  "dc2",
    23  				NodeName:    "my-web",
    24  				DataDir:     "/tmp/nomad",
    25  				LogLevel:    "ERR",
    26  				BindAddr:    "192.168.0.1",
    27  				EnableDebug: true,
    28  				Ports: &Ports{
    29  					HTTP: 1234,
    30  					RPC:  2345,
    31  					Serf: 3456,
    32  				},
    33  				Addresses: &Addresses{
    34  					HTTP: "127.0.0.1",
    35  					RPC:  "127.0.0.2",
    36  					Serf: "127.0.0.3",
    37  				},
    38  				AdvertiseAddrs: &AdvertiseAddrs{
    39  					RPC:  "127.0.0.3",
    40  					Serf: "127.0.0.4",
    41  				},
    42  				Client: &ClientConfig{
    43  					Enabled:   true,
    44  					StateDir:  "/tmp/client-state",
    45  					AllocDir:  "/tmp/alloc",
    46  					Servers:   []string{"a.b.c:80", "127.0.0.1:1234"},
    47  					NodeClass: "linux-medium-64bit",
    48  					Meta: map[string]string{
    49  						"foo": "bar",
    50  						"baz": "zip",
    51  					},
    52  					Options: map[string]string{
    53  						"foo": "bar",
    54  						"baz": "zip",
    55  					},
    56  					ChrootEnv: map[string]string{
    57  						"/opt/myapp/etc": "/etc",
    58  						"/opt/myapp/bin": "/bin",
    59  					},
    60  					NetworkInterface: "eth0",
    61  					NetworkSpeed:     100,
    62  					MaxKillTimeout:   "10s",
    63  					ClientMinPort:    1000,
    64  					ClientMaxPort:    2000,
    65  					Reserved: &Resources{
    66  						CPU:                 10,
    67  						MemoryMB:            10,
    68  						DiskMB:              10,
    69  						IOPS:                10,
    70  						ReservedPorts:       "1,100,10-12",
    71  						ParsedReservedPorts: []int{1, 10, 11, 12, 100},
    72  					},
    73  				},
    74  				Server: &ServerConfig{
    75  					Enabled:           true,
    76  					BootstrapExpect:   5,
    77  					DataDir:           "/tmp/data",
    78  					ProtocolVersion:   3,
    79  					NumSchedulers:     2,
    80  					EnabledSchedulers: []string{"test"},
    81  					NodeGCThreshold:   "12h",
    82  					HeartbeatGrace:    "30s",
    83  					RetryJoin:         []string{"1.1.1.1", "2.2.2.2"},
    84  					StartJoin:         []string{"1.1.1.1", "2.2.2.2"},
    85  					RetryInterval:     "15s",
    86  					RejoinAfterLeave:  true,
    87  					RetryMaxAttempts:  3,
    88  					EncryptKey:        "abc",
    89  				},
    90  				Telemetry: &Telemetry{
    91  					StatsiteAddr:             "127.0.0.1:1234",
    92  					StatsdAddr:               "127.0.0.1:2345",
    93  					DisableHostname:          true,
    94  					CollectionInterval:       "3s",
    95  					collectionInterval:       3 * time.Second,
    96  					PublishAllocationMetrics: true,
    97  					PublishNodeMetrics:       true,
    98  				},
    99  				LeaveOnInt:                true,
   100  				LeaveOnTerm:               true,
   101  				EnableSyslog:              true,
   102  				SyslogFacility:            "LOCAL1",
   103  				DisableUpdateCheck:        true,
   104  				DisableAnonymousSignature: true,
   105  				Atlas: &AtlasConfig{
   106  					Infrastructure: "armon/test",
   107  					Token:          "abcd",
   108  					Join:           true,
   109  					Endpoint:       "127.0.0.1:1234",
   110  				},
   111  				Consul: &config.ConsulConfig{
   112  					ServerServiceName:  "nomad",
   113  					ClientServiceName:  "nomad-client",
   114  					Addr:               "127.0.0.1:9500",
   115  					Token:              "token1",
   116  					Auth:               "username:pass",
   117  					EnableSSL:          true,
   118  					VerifySSL:          true,
   119  					CAFile:             "/path/to/ca/file",
   120  					CertFile:           "/path/to/cert/file",
   121  					KeyFile:            "/path/to/key/file",
   122  					ServerAutoJoin:     true,
   123  					ClientAutoJoin:     true,
   124  					AutoAdvertise:      true,
   125  					ChecksUseAdvertise: true,
   126  				},
   127  				Vault: &config.VaultConfig{
   128  					Addr:                 "127.0.0.1:9500",
   129  					AllowUnauthenticated: &trueValue,
   130  					Enabled:              &falseValue,
   131  					TLSCaFile:            "/path/to/ca/file",
   132  					TLSCaPath:            "/path/to/ca",
   133  					TLSCertFile:          "/path/to/cert/file",
   134  					TLSKeyFile:           "/path/to/key/file",
   135  					TLSServerName:        "foobar",
   136  					TLSSkipVerify:        &trueValue,
   137  					TaskTokenTTL:         "1s",
   138  					Token:                "12345",
   139  				},
   140  				TLSConfig: &config.TLSConfig{
   141  					EnableHTTP:           true,
   142  					EnableRPC:            true,
   143  					VerifyServerHostname: true,
   144  					CAFile:               "foo",
   145  					CertFile:             "bar",
   146  					KeyFile:              "pipe",
   147  				},
   148  				HTTPAPIResponseHeaders: map[string]string{
   149  					"Access-Control-Allow-Origin": "*",
   150  				},
   151  			},
   152  			false,
   153  		},
   154  	}
   155  
   156  	for _, tc := range cases {
   157  		t.Logf("Testing parse: %s", tc.File)
   158  
   159  		path, err := filepath.Abs(filepath.Join("./config-test-fixtures", tc.File))
   160  		if err != nil {
   161  			t.Fatalf("file: %s\n\n%s", tc.File, err)
   162  			continue
   163  		}
   164  
   165  		actual, err := ParseConfigFile(path)
   166  		if (err != nil) != tc.Err {
   167  			t.Fatalf("file: %s\n\n%s", tc.File, err)
   168  			continue
   169  		}
   170  
   171  		if !reflect.DeepEqual(actual, tc.Result) {
   172  			t.Fatalf("file: %s\n\n%#v\n\n%#v", tc.File, actual, tc.Result)
   173  		}
   174  	}
   175  }