github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/client/fingerprint/env_aws_test.go (about) 1 package fingerprint 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 "net/http/httptest" 8 "os" 9 "testing" 10 11 "github.com/hashicorp/nomad/client/config" 12 "github.com/hashicorp/nomad/nomad/structs" 13 ) 14 15 func TestEnvAWSFingerprint_nonAws(t *testing.T) { 16 os.Setenv("AWS_ENV_URL", "http://127.0.0.1/latest/meta-data/") 17 f := NewEnvAWSFingerprint(testLogger()) 18 node := &structs.Node{ 19 Attributes: make(map[string]string), 20 } 21 22 ok, err := f.Fingerprint(&config.Config{}, node) 23 if err != nil { 24 t.Fatalf("err: %v", err) 25 } 26 27 if ok { 28 t.Fatalf("Should be false without test server") 29 } 30 } 31 32 func TestEnvAWSFingerprint_aws(t *testing.T) { 33 f := NewEnvAWSFingerprint(testLogger()) 34 node := &structs.Node{ 35 Attributes: make(map[string]string), 36 } 37 38 // configure mock server with fixture routes, data 39 routes := routes{} 40 if err := json.Unmarshal([]byte(aws_routes), &routes); err != nil { 41 t.Fatalf("Failed to unmarshal JSON in AWS ENV test: %s", err) 42 } 43 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 44 for _, e := range routes.Endpoints { 45 if r.RequestURI == e.Uri { 46 w.Header().Set("Content-Type", e.ContentType) 47 fmt.Fprintln(w, e.Body) 48 } 49 } 50 })) 51 defer ts.Close() 52 os.Setenv("AWS_ENV_URL", ts.URL+"/latest/meta-data/") 53 54 ok, err := f.Fingerprint(&config.Config{}, node) 55 if err != nil { 56 t.Fatalf("err: %v", err) 57 } 58 59 if !ok { 60 t.Fatalf("Expected AWS attributes and Links") 61 } 62 63 keys := []string{ 64 "unique.platform.aws.ami-id", 65 "unique.platform.aws.hostname", 66 "unique.platform.aws.instance-id", 67 "platform.aws.instance-type", 68 "unique.platform.aws.local-hostname", 69 "unique.platform.aws.local-ipv4", 70 "unique.platform.aws.public-hostname", 71 "unique.platform.aws.public-ipv4", 72 "platform.aws.placement.availability-zone", 73 "unique.network.ip-address", 74 } 75 76 for _, k := range keys { 77 assertNodeAttributeContains(t, node, k) 78 } 79 80 if len(node.Links) == 0 { 81 t.Fatalf("Empty links for Node in AWS Fingerprint test") 82 } 83 84 // confirm we have at least instance-id and ami-id 85 for _, k := range []string{"aws.ec2"} { 86 assertNodeLinksContains(t, node, k) 87 } 88 } 89 90 type routes struct { 91 Endpoints []*endpoint `json:"endpoints"` 92 } 93 type endpoint struct { 94 Uri string `json:"uri"` 95 ContentType string `json:"content-type"` 96 Body string `json:"body"` 97 } 98 99 const aws_routes = ` 100 { 101 "endpoints": [ 102 { 103 "uri": "/latest/meta-data/ami-id", 104 "content-type": "text/plain", 105 "body": "ami-1234" 106 }, 107 { 108 "uri": "/latest/meta-data/hostname", 109 "content-type": "text/plain", 110 "body": "ip-10-0-0-207.us-west-2.compute.internal" 111 }, 112 { 113 "uri": "/latest/meta-data/placement/availability-zone", 114 "content-type": "text/plain", 115 "body": "us-west-2a" 116 }, 117 { 118 "uri": "/latest/meta-data/instance-id", 119 "content-type": "text/plain", 120 "body": "i-b3ba3875" 121 }, 122 { 123 "uri": "/latest/meta-data/instance-type", 124 "content-type": "text/plain", 125 "body": "m3.2xlarge" 126 }, 127 { 128 "uri": "/latest/meta-data/local-hostname", 129 "content-type": "text/plain", 130 "body": "ip-10-0-0-207.us-west-2.compute.internal" 131 }, 132 { 133 "uri": "/latest/meta-data/local-ipv4", 134 "content-type": "text/plain", 135 "body": "10.0.0.207" 136 }, 137 { 138 "uri": "/latest/meta-data/public-hostname", 139 "content-type": "text/plain", 140 "body": "ec2-54-191-117-175.us-west-2.compute.amazonaws.com" 141 }, 142 { 143 "uri": "/latest/meta-data/public-ipv4", 144 "content-type": "text/plain", 145 "body": "54.191.117.175" 146 } 147 ] 148 } 149 ` 150 151 func TestNetworkFingerprint_AWS(t *testing.T) { 152 // configure mock server with fixture routes, data 153 routes := routes{} 154 if err := json.Unmarshal([]byte(aws_routes), &routes); err != nil { 155 t.Fatalf("Failed to unmarshal JSON in AWS ENV test: %s", err) 156 } 157 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 158 for _, e := range routes.Endpoints { 159 if r.RequestURI == e.Uri { 160 w.Header().Set("Content-Type", e.ContentType) 161 fmt.Fprintln(w, e.Body) 162 } 163 } 164 })) 165 166 defer ts.Close() 167 os.Setenv("AWS_ENV_URL", ts.URL+"/latest/meta-data/") 168 169 f := NewEnvAWSFingerprint(testLogger()) 170 node := &structs.Node{ 171 Attributes: make(map[string]string), 172 } 173 174 ok, err := f.Fingerprint(&config.Config{}, node) 175 if err != nil { 176 t.Fatalf("err: %v", err) 177 } 178 if !ok { 179 t.Fatalf("should apply") 180 } 181 182 assertNodeAttributeContains(t, node, "unique.network.ip-address") 183 184 if node.Resources == nil || len(node.Resources.Networks) == 0 { 185 t.Fatal("Expected to find Network Resources") 186 } 187 188 // Test at least the first Network Resource 189 net := node.Resources.Networks[0] 190 if net.IP == "" { 191 t.Fatal("Expected Network Resource to have an IP") 192 } 193 if net.CIDR == "" { 194 t.Fatal("Expected Network Resource to have a CIDR") 195 } 196 if net.Device == "" { 197 t.Fatal("Expected Network Resource to have a Device Name") 198 } 199 } 200 201 func TestNetworkFingerprint_AWS_network(t *testing.T) { 202 // configure mock server with fixture routes, data 203 routes := routes{} 204 if err := json.Unmarshal([]byte(aws_routes), &routes); err != nil { 205 t.Fatalf("Failed to unmarshal JSON in AWS ENV test: %s", err) 206 } 207 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 208 for _, e := range routes.Endpoints { 209 if r.RequestURI == e.Uri { 210 w.Header().Set("Content-Type", e.ContentType) 211 fmt.Fprintln(w, e.Body) 212 } 213 } 214 })) 215 216 defer ts.Close() 217 os.Setenv("AWS_ENV_URL", ts.URL+"/latest/meta-data/") 218 219 f := NewEnvAWSFingerprint(testLogger()) 220 node := &structs.Node{ 221 Attributes: make(map[string]string), 222 } 223 224 cfg := &config.Config{} 225 ok, err := f.Fingerprint(cfg, node) 226 if err != nil { 227 t.Fatalf("err: %v", err) 228 } 229 if !ok { 230 t.Fatalf("should apply") 231 } 232 233 assertNodeAttributeContains(t, node, "unique.network.ip-address") 234 235 if node.Resources == nil || len(node.Resources.Networks) == 0 { 236 t.Fatal("Expected to find Network Resources") 237 } 238 239 // Test at least the first Network Resource 240 net := node.Resources.Networks[0] 241 if net.IP == "" { 242 t.Fatal("Expected Network Resource to have an IP") 243 } 244 if net.CIDR == "" { 245 t.Fatal("Expected Network Resource to have a CIDR") 246 } 247 if net.Device == "" { 248 t.Fatal("Expected Network Resource to have a Device Name") 249 } 250 if net.MBits != 1000 { 251 t.Fatalf("Expected Network Resource to have speed %d; got %d", 1000, net.MBits) 252 } 253 254 // Try again this time setting a network speed in the config 255 node = &structs.Node{ 256 Attributes: make(map[string]string), 257 } 258 259 cfg.NetworkSpeed = 10 260 ok, err = f.Fingerprint(cfg, node) 261 if err != nil { 262 t.Fatalf("err: %v", err) 263 } 264 if !ok { 265 t.Fatalf("should apply") 266 } 267 268 assertNodeAttributeContains(t, node, "unique.network.ip-address") 269 270 if node.Resources == nil || len(node.Resources.Networks) == 0 { 271 t.Fatal("Expected to find Network Resources") 272 } 273 274 // Test at least the first Network Resource 275 net = node.Resources.Networks[0] 276 if net.IP == "" { 277 t.Fatal("Expected Network Resource to have an IP") 278 } 279 if net.CIDR == "" { 280 t.Fatal("Expected Network Resource to have a CIDR") 281 } 282 if net.Device == "" { 283 t.Fatal("Expected Network Resource to have a Device Name") 284 } 285 if net.MBits != 10 { 286 t.Fatalf("Expected Network Resource to have speed %d; got %d", 10, net.MBits) 287 } 288 } 289 290 func TestNetworkFingerprint_notAWS(t *testing.T) { 291 os.Setenv("AWS_ENV_URL", "http://127.0.0.1/latest/meta-data/") 292 f := NewEnvAWSFingerprint(testLogger()) 293 node := &structs.Node{ 294 Attributes: make(map[string]string), 295 } 296 297 ok, err := f.Fingerprint(&config.Config{}, node) 298 if err != nil { 299 t.Fatalf("err: %v", err) 300 } 301 if ok { 302 t.Fatalf("Should not apply") 303 } 304 }