github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/client/fingerprint/consul_test.go (about)

     1  package fingerprint
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/nomad/client/config"
    10  	"github.com/hashicorp/nomad/nomad/structs"
    11  )
    12  
    13  func TestConsulFingerprint(t *testing.T) {
    14  	fp := NewConsulFingerprint(testLogger())
    15  	node := &structs.Node{
    16  		Attributes: make(map[string]string),
    17  	}
    18  
    19  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    20  		w.Header().Set("Content-Type", "application/json")
    21  		fmt.Fprintln(w, mockConsulResponse)
    22  	}))
    23  	defer ts.Close()
    24  
    25  	consulConfig := &config.Config{
    26  		Options: map[string]string{
    27  			// Split off "http://"
    28  			"consul.address": ts.URL[7:],
    29  		},
    30  	}
    31  
    32  	ok, err := fp.Fingerprint(consulConfig, node)
    33  	if err != nil {
    34  		t.Fatalf("Failed to fingerprint: %s", err)
    35  	}
    36  	if !ok {
    37  		t.Fatalf("Failed to apply node attributes")
    38  	}
    39  
    40  	assertNodeAttributeContains(t, node, "consul.server")
    41  	assertNodeAttributeContains(t, node, "consul.version")
    42  	assertNodeAttributeContains(t, node, "consul.revision")
    43  	assertNodeAttributeContains(t, node, "consul.name")
    44  	assertNodeAttributeContains(t, node, "consul.datacenter")
    45  
    46  	expectedLink := "vagrant.consul2"
    47  	if node.Links["consul"] != expectedLink {
    48  		t.Errorf("Expected consul link: %s\nFound links: %#v", expectedLink, node.Links)
    49  	}
    50  }
    51  
    52  // Taken from tryconsul using consul release 0.5.2
    53  const mockConsulResponse = `
    54  {
    55    "Config": {
    56      "Bootstrap": false,
    57      "BootstrapExpect": 3,
    58      "Server": true,
    59      "Datacenter": "vagrant",
    60      "DataDir": "/var/lib/consul",
    61      "DNSRecursor": "",
    62      "DNSRecursors": [],
    63      "DNSConfig": {
    64        "NodeTTL": 0,
    65        "ServiceTTL": null,
    66        "AllowStale": false,
    67        "EnableTruncate": false,
    68        "MaxStale": 5000000000,
    69        "OnlyPassing": false
    70      },
    71      "Domain": "consul.",
    72      "LogLevel": "INFO",
    73      "NodeName": "consul2",
    74      "ClientAddr": "0.0.0.0",
    75      "BindAddr": "0.0.0.0",
    76      "AdvertiseAddr": "172.16.59.133",
    77      "AdvertiseAddrWan": "172.16.59.133",
    78      "Ports": {
    79        "DNS": 8600,
    80        "HTTP": 8500,
    81        "HTTPS": -1,
    82        "RPC": 8400,
    83        "SerfLan": 8301,
    84        "SerfWan": 8302,
    85        "Server": 8300
    86      },
    87      "Addresses": {
    88        "DNS": "",
    89        "HTTP": "",
    90        "HTTPS": "",
    91        "RPC": ""
    92      },
    93      "LeaveOnTerm": false,
    94      "SkipLeaveOnInt": false,
    95      "StatsiteAddr": "",
    96      "StatsitePrefix": "consul",
    97      "StatsdAddr": "",
    98      "Protocol": 2,
    99      "EnableDebug": false,
   100      "VerifyIncoming": false,
   101      "VerifyOutgoing": false,
   102      "VerifyServerHostname": false,
   103      "CAFile": "",
   104      "CertFile": "",
   105      "KeyFile": "",
   106      "ServerName": "",
   107      "StartJoin": [],
   108      "StartJoinWan": [],
   109      "RetryJoin": [],
   110      "RetryMaxAttempts": 0,
   111      "RetryIntervalRaw": "",
   112      "RetryJoinWan": [],
   113      "RetryMaxAttemptsWan": 0,
   114      "RetryIntervalWanRaw": "",
   115      "UiDir": "/opt/consul-ui",
   116      "PidFile": "",
   117      "EnableSyslog": true,
   118      "SyslogFacility": "LOCAL0",
   119      "RejoinAfterLeave": false,
   120      "CheckUpdateInterval": 300000000000,
   121      "ACLDatacenter": "",
   122      "ACLTTL": 30000000000,
   123      "ACLTTLRaw": "",
   124      "ACLDefaultPolicy": "allow",
   125      "ACLDownPolicy": "extend-cache",
   126      "Watches": null,
   127      "DisableRemoteExec": false,
   128      "DisableUpdateCheck": false,
   129      "DisableAnonymousSignature": false,
   130      "HTTPAPIResponseHeaders": null,
   131      "AtlasInfrastructure": "",
   132      "AtlasJoin": false,
   133      "Revision": "9a9cc9341bb487651a0399e3fc5e1e8a42e62dd9+CHANGES",
   134      "Version": "0.5.2",
   135      "VersionPrerelease": "",
   136      "UnixSockets": {
   137        "Usr": "",
   138        "Grp": "",
   139        "Perms": ""
   140      },
   141      "SessionTTLMin": 0,
   142      "SessionTTLMinRaw": ""
   143    },
   144    "Member": {
   145      "Name": "consul2",
   146      "Addr": "172.16.59.133",
   147      "Port": 8301,
   148      "Tags": {
   149        "build": "0.5.2:9a9cc934",
   150        "dc": "vagrant",
   151        "expect": "3",
   152        "port": "8300",
   153        "role": "consul",
   154        "vsn": "2",
   155        "vsn_max": "2",
   156        "vsn_min": "1"
   157      },
   158      "Status": 1,
   159      "ProtocolMin": 1,
   160      "ProtocolMax": 2,
   161      "ProtocolCur": 2,
   162      "DelegateMin": 2,
   163      "DelegateMax": 4,
   164      "DelegateCur": 4
   165    }
   166  }
   167  `