github.com/jmitchell/nomad@v0.1.3-0.20151007230021-7ab84c2862d8/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 f := NewEnvAWSFingerprint(testLogger()) 17 node := &structs.Node{ 18 Attributes: make(map[string]string), 19 } 20 21 ok, err := f.Fingerprint(&config.Config{}, node) 22 if err != nil { 23 t.Fatalf("err: %v", err) 24 } 25 26 if ok { 27 t.Fatalf("Should be false without test server") 28 } 29 } 30 31 func TestEnvAWSFingerprint_aws(t *testing.T) { 32 f := NewEnvAWSFingerprint(testLogger()) 33 node := &structs.Node{ 34 Attributes: make(map[string]string), 35 } 36 37 // configure mock server with fixture routes, data 38 routes := routes{} 39 if err := json.Unmarshal([]byte(aws_routes), &routes); err != nil { 40 t.Fatalf("Failed to unmarshal JSON in AWS ENV test: %s", err) 41 } 42 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 43 for _, e := range routes.Endpoints { 44 if r.RequestURI == e.Uri { 45 w.Header().Set("Content-Type", e.ContentType) 46 fmt.Fprintln(w, e.Body) 47 } 48 } 49 })) 50 defer ts.Close() 51 os.Setenv("AWS_ENV_URL", ts.URL+"/latest/meta-data/") 52 53 ok, err := f.Fingerprint(&config.Config{}, node) 54 if err != nil { 55 t.Fatalf("err: %v", err) 56 } 57 58 if !ok { 59 t.Fatalf("Expected AWS attributes and Links") 60 } 61 62 keys := []string{ 63 "platform.aws.ami-id", 64 "platform.aws.hostname", 65 "platform.aws.instance-id", 66 "platform.aws.instance-type", 67 "platform.aws.local-hostname", 68 "platform.aws.local-ipv4", 69 "platform.aws.public-hostname", 70 "platform.aws.public-ipv4", 71 "platform.aws.placement.availability-zone", 72 "network.ip-address", 73 } 74 75 for _, k := range keys { 76 assertNodeAttributeContains(t, node, k) 77 } 78 79 if len(node.Links) == 0 { 80 t.Fatalf("Empty links for Node in AWS Fingerprint test") 81 } 82 83 // confirm we have at least instance-id and ami-id 84 for _, k := range []string{"aws.ec2"} { 85 assertNodeLinksContains(t, node, k) 86 } 87 } 88 89 type routes struct { 90 Endpoints []*endpoint `json:"endpoints"` 91 } 92 type endpoint struct { 93 Uri string `json:"uri"` 94 ContentType string `json:"content-type"` 95 Body string `json:"body"` 96 } 97 98 const aws_routes = ` 99 { 100 "endpoints": [ 101 { 102 "uri": "/latest/meta-data/ami-id", 103 "content-type": "text/plain", 104 "body": "ami-1234" 105 }, 106 { 107 "uri": "/latest/meta-data/hostname", 108 "content-type": "text/plain", 109 "body": "ip-10-0-0-207.us-west-2.compute.internal" 110 }, 111 { 112 "uri": "/latest/meta-data/placement/availability-zone", 113 "content-type": "text/plain", 114 "body": "us-west-2a" 115 }, 116 { 117 "uri": "/latest/meta-data/instance-id", 118 "content-type": "text/plain", 119 "body": "i-b3ba3875" 120 }, 121 { 122 "uri": "/latest/meta-data/instance-type", 123 "content-type": "text/plain", 124 "body": "m3.large" 125 }, 126 { 127 "uri": "/latest/meta-data/local-hostname", 128 "content-type": "text/plain", 129 "body": "ip-10-0-0-207.us-west-2.compute.internal" 130 }, 131 { 132 "uri": "/latest/meta-data/local-ipv4", 133 "content-type": "text/plain", 134 "body": "10.0.0.207" 135 }, 136 { 137 "uri": "/latest/meta-data/public-hostname", 138 "content-type": "text/plain", 139 "body": "ec2-54-191-117-175.us-west-2.compute.amazonaws.com" 140 }, 141 { 142 "uri": "/latest/meta-data/public-ipv4", 143 "content-type": "text/plain", 144 "body": "54.191.117.175" 145 } 146 ] 147 } 148 ` 149 150 func TestNetworkFingerprint_AWS(t *testing.T) { 151 // configure mock server with fixture routes, data 152 // TODO: Refator with the AWS ENV test 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, "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_notAWS(t *testing.T) { 202 f := NewEnvAWSFingerprint(testLogger()) 203 node := &structs.Node{ 204 Attributes: make(map[string]string), 205 } 206 207 ok, err := f.Fingerprint(&config.Config{}, node) 208 if err != nil { 209 t.Fatalf("err: %v", err) 210 } 211 if ok { 212 t.Fatalf("Should not apply") 213 } 214 }