github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/client/fingerprint/consul_test.go (about)

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