github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/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  		"platform.aws.ami-id",
    65  		"platform.aws.hostname",
    66  		"platform.aws.instance-id",
    67  		"platform.aws.instance-type",
    68  		"platform.aws.local-hostname",
    69  		"platform.aws.local-ipv4",
    70  		"platform.aws.public-hostname",
    71  		"platform.aws.public-ipv4",
    72  		"platform.aws.placement.availability-zone",
    73  		"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.large"
   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  	// TODO: Refator with the AWS ENV test
   154  	routes := routes{}
   155  	if err := json.Unmarshal([]byte(aws_routes), &routes); err != nil {
   156  		t.Fatalf("Failed to unmarshal JSON in AWS ENV test: %s", err)
   157  	}
   158  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   159  		for _, e := range routes.Endpoints {
   160  			if r.RequestURI == e.Uri {
   161  				w.Header().Set("Content-Type", e.ContentType)
   162  				fmt.Fprintln(w, e.Body)
   163  			}
   164  		}
   165  	}))
   166  
   167  	defer ts.Close()
   168  	os.Setenv("AWS_ENV_URL", ts.URL+"/latest/meta-data/")
   169  
   170  	f := NewEnvAWSFingerprint(testLogger())
   171  	node := &structs.Node{
   172  		Attributes: make(map[string]string),
   173  	}
   174  
   175  	ok, err := f.Fingerprint(&config.Config{}, node)
   176  	if err != nil {
   177  		t.Fatalf("err: %v", err)
   178  	}
   179  	if !ok {
   180  		t.Fatalf("should apply")
   181  	}
   182  
   183  	assertNodeAttributeContains(t, node, "network.ip-address")
   184  
   185  	if node.Resources == nil || len(node.Resources.Networks) == 0 {
   186  		t.Fatal("Expected to find Network Resources")
   187  	}
   188  
   189  	// Test at least the first Network Resource
   190  	net := node.Resources.Networks[0]
   191  	if net.IP == "" {
   192  		t.Fatal("Expected Network Resource to have an IP")
   193  	}
   194  	if net.CIDR == "" {
   195  		t.Fatal("Expected Network Resource to have a CIDR")
   196  	}
   197  	if net.Device == "" {
   198  		t.Fatal("Expected Network Resource to have a Device Name")
   199  	}
   200  }
   201  
   202  func TestNetworkFingerprint_notAWS(t *testing.T) {
   203  	os.Setenv("AWS_ENV_URL", "http://127.0.0.1/latest/meta-data/")
   204  	f := NewEnvAWSFingerprint(testLogger())
   205  	node := &structs.Node{
   206  		Attributes: make(map[string]string),
   207  	}
   208  
   209  	ok, err := f.Fingerprint(&config.Config{}, node)
   210  	if err != nil {
   211  		t.Fatalf("err: %v", err)
   212  	}
   213  	if ok {
   214  		t.Fatalf("Should not apply")
   215  	}
   216  }