github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/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 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestConsulFingerprint(t *testing.T) { 16 fp := NewConsulFingerprint(testLogger()) 17 node := &structs.Node{ 18 Attributes: make(map[string]string), 19 } 20 21 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 22 w.Header().Set("Content-Type", "application/json") 23 fmt.Fprintln(w, mockConsulResponse) 24 })) 25 defer ts.Close() 26 27 config := config.DefaultConfig() 28 config.ConsulConfig.Addr = strings.TrimPrefix(ts.URL, "http://") 29 30 ok, err := fp.Fingerprint(config, node) 31 if err != nil { 32 t.Fatalf("Failed to fingerprint: %s", err) 33 } 34 if !ok { 35 t.Fatalf("Failed to apply node attributes") 36 } 37 38 assertNodeAttributeContains(t, node, "consul.server") 39 assertNodeAttributeContains(t, node, "consul.version") 40 assertNodeAttributeContains(t, node, "consul.revision") 41 assertNodeAttributeContains(t, node, "unique.consul.name") 42 assertNodeAttributeContains(t, node, "consul.datacenter") 43 44 if _, ok := node.Links["consul"]; !ok { 45 t.Errorf("Expected a link to consul, none found") 46 } 47 } 48 49 // Taken from tryconsul using consul release 0.5.2 50 const mockConsulResponse = ` 51 { 52 "Config": { 53 "Bootstrap": false, 54 "BootstrapExpect": 3, 55 "Server": true, 56 "Datacenter": "vagrant", 57 "DataDir": "/var/lib/consul", 58 "DNSRecursor": "", 59 "DNSRecursors": [], 60 "DNSConfig": { 61 "NodeTTL": 0, 62 "ServiceTTL": null, 63 "AllowStale": false, 64 "EnableTruncate": false, 65 "MaxStale": 5000000000, 66 "OnlyPassing": false 67 }, 68 "Domain": "consul.", 69 "LogLevel": "INFO", 70 "NodeName": "consul2", 71 "ClientAddr": "0.0.0.0", 72 "BindAddr": "0.0.0.0", 73 "AdvertiseAddr": "172.16.59.133", 74 "AdvertiseAddrWan": "172.16.59.133", 75 "Ports": { 76 "DNS": 8600, 77 "HTTP": 8500, 78 "HTTPS": -1, 79 "RPC": 8400, 80 "SerfLan": 8301, 81 "SerfWan": 8302, 82 "Server": 8300 83 }, 84 "Addresses": { 85 "DNS": "", 86 "HTTP": "", 87 "HTTPS": "", 88 "RPC": "" 89 }, 90 "LeaveOnTerm": false, 91 "SkipLeaveOnInt": false, 92 "StatsiteAddr": "", 93 "StatsitePrefix": "consul", 94 "StatsdAddr": "", 95 "Protocol": 2, 96 "EnableDebug": false, 97 "VerifyIncoming": false, 98 "VerifyOutgoing": false, 99 "VerifyServerHostname": false, 100 "CAFile": "", 101 "CertFile": "", 102 "KeyFile": "", 103 "ServerName": "", 104 "StartJoin": [], 105 "StartJoinWan": [], 106 "RetryJoin": [], 107 "RetryMaxAttempts": 0, 108 "RetryIntervalRaw": "", 109 "RetryJoinWan": [], 110 "RetryMaxAttemptsWan": 0, 111 "RetryIntervalWanRaw": "", 112 "UiDir": "/opt/consul-ui", 113 "PidFile": "", 114 "EnableSyslog": true, 115 "SyslogFacility": "LOCAL0", 116 "RejoinAfterLeave": false, 117 "CheckUpdateInterval": 300000000000, 118 "ACLDatacenter": "", 119 "ACLTTL": 30000000000, 120 "ACLTTLRaw": "", 121 "ACLDefaultPolicy": "allow", 122 "ACLDownPolicy": "extend-cache", 123 "Watches": null, 124 "DisableRemoteExec": false, 125 "DisableUpdateCheck": false, 126 "DisableAnonymousSignature": false, 127 "HTTPAPIResponseHeaders": null, 128 "AtlasInfrastructure": "", 129 "AtlasJoin": false, 130 "Revision": "9a9cc9341bb487651a0399e3fc5e1e8a42e62dd9+CHANGES", 131 "Version": "0.5.2", 132 "VersionPrerelease": "", 133 "UnixSockets": { 134 "Usr": "", 135 "Grp": "", 136 "Perms": "" 137 }, 138 "SessionTTLMin": 0, 139 "SessionTTLMinRaw": "" 140 }, 141 "Member": { 142 "Name": "consul2", 143 "Addr": "172.16.59.133", 144 "Port": 8301, 145 "Tags": { 146 "build": "0.5.2:9a9cc934", 147 "dc": "vagrant", 148 "expect": "3", 149 "port": "8300", 150 "role": "consul", 151 "vsn": "2" 152 }, 153 "Status": 1, 154 "ProtocolMin": 1, 155 "ProtocolMax": 2, 156 "ProtocolCur": 2, 157 "DelegateMin": 2, 158 "DelegateMax": 4, 159 "DelegateCur": 4 160 } 161 } 162 ` 163 164 // TestConsulFingerprint_UnexpectedResponse asserts that the Consul 165 // fingerprinter does not panic when it encounters an unexpected response. 166 // See https://github.com/hashicorp/nomad/issues/3326 167 func TestConsulFingerprint_UnexpectedResponse(t *testing.T) { 168 assert := assert.New(t) 169 fp := NewConsulFingerprint(testLogger()) 170 node := &structs.Node{ 171 Attributes: make(map[string]string), 172 } 173 174 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 175 w.Header().Set("Content-Type", "application/json") 176 fmt.Fprintln(w, "{}") 177 })) 178 defer ts.Close() 179 180 config := config.DefaultConfig() 181 config.ConsulConfig.Addr = strings.TrimPrefix(ts.URL, "http://") 182 183 ok, err := fp.Fingerprint(config, node) 184 assert.Nil(err) 185 assert.True(ok) 186 187 attrs := []string{ 188 "consul.server", 189 "consul.version", 190 "consul.revision", 191 "unique.consul.name", 192 "consul.datacenter", 193 } 194 for _, attr := range attrs { 195 if v, ok := node.Attributes[attr]; ok { 196 t.Errorf("unexpected node attribute %q with vlaue %q", attr, v) 197 } 198 } 199 200 if v, ok := node.Links["consul"]; ok { 201 t.Errorf("Unexpected link to consul: %v", v) 202 } 203 }