github.com/Tyktechnologies/tyk@v2.9.5+incompatible/gateway/service_discovery_test.go (about)

     1  package gateway
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  const consul = `
     8  [{
     9  	"Node": "foobar",
    10  	"Address": "10.1.10.12",
    11  	"ServiceID": "redis",
    12  	"ServiceName": "redis",
    13  	"ServicePort": 8000
    14  },
    15  {
    16  	"Node": "foobar2",
    17  	"Address": "10.1.10.13",
    18  	"ServiceID": "redis",
    19  	"ServiceName": "redis",
    20  	"ServicePort": 8000
    21  }]
    22  `
    23  
    24  const eureka_real = `{
    25  	"application": {
    26  		"name": "ROUTE",
    27  		"instance": [{
    28  			"hostName": "ip-172-31-57-136",
    29  			"app": "ROUTE",
    30  			"ipAddr": "172.31.57.136",
    31  			"status": "UP",
    32  			"overriddenstatus": "UNKNOWN",
    33  			"port": {
    34  				"@enabled": "true",
    35  				"$": "60565"
    36  			},
    37  			"securePort": {
    38  				"@enabled": "false",
    39  				"$": "443"
    40  			},
    41  			"countryId": 1,
    42  			"dataCenterInfo": {
    43  				"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
    44  				"name": "MyOwn"
    45  			},
    46  			"leaseInfo": {
    47  				"renewalIntervalInSecs": 10,
    48  				"durationInSecs": 10,
    49  				"registrationTimestamp": 1460471383902,
    50  				"lastRenewalTimestamp": 1460471403565,
    51  				"serviceUpTimestamp": 1460471383340
    52  			},
    53  			"metadata": {
    54  				"instanceId": "route:f673c15eebfc456a3c679a55d234a8ca",
    55  				"payment": "perCall",
    56  				"providerName": "MisterA"
    57  			},
    58  			"homePageUrl": "http:\/\/ip-172-31-57-136:60565\/",
    59  			"statusPageUrl": "http:\/\/ip-172-31-57-136:60565\/info",
    60  			"healthCheckUrl": "http:\/\/ip-172-31-57-136:60565\/health",
    61  			"vipAddress": "route",
    62  			"lastUpdatedTimestamp": 1460471383902,
    63  			"lastDirtyTimestamp": 1460471429751,
    64  			"actionType": "ADDED"
    65  		}, {
    66  			"hostName": "ip-172-31-13-37",
    67  			"app": "ROUTE",
    68  			"ipAddr": "172.31.13.37",
    69  			"status": "UP",
    70  			"overriddenstatus": "UNKNOWN",
    71  			"port": {
    72  				"@enabled": "true",
    73  				"$": "50045"
    74  			},
    75  			"securePort": {
    76  				"@enabled": "false",
    77  				"$": "443"
    78  			},
    79  			"countryId": 1,
    80  			"dataCenterInfo": {
    81  				"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
    82  				"name": "MyOwn"
    83  			},
    84  			"leaseInfo": {
    85  				"renewalIntervalInSecs": 10,
    86  				"durationInSecs": 10,
    87  				"registrationTimestamp": 1460471387114,
    88  				"lastRenewalTimestamp": 1460471407062,
    89  				"serviceUpTimestamp": 1460471386750
    90  			},
    91  			"metadata": {
    92  				"instanceId": "route:838ba7845f1fd63d94c10ca9efdf77a5",
    93  				"payment": "flat",
    94  				"providerName": "MissB"
    95  			},
    96  			"homePageUrl": "http:\/\/ip-172-31-13-37:50045\/",
    97  			"statusPageUrl": "http:\/\/ip-172-31-13-37:50045\/info",
    98  			"healthCheckUrl": "http:\/\/ip-172-31-13-37:50045\/health",
    99  			"vipAddress": "route",
   100  			"lastUpdatedTimestamp": 1460471387114,
   101  			"lastDirtyTimestamp": 1460471360189,
   102  			"actionType": "ADDED"
   103  		}]
   104  	}
   105  }`
   106  
   107  const nested_consul = `
   108  [{
   109  	"Name": "beep",
   110  	"Data": "{\"hostname\": \"httpbin1.org\", \"port\": \"80\"}"
   111  },
   112  {
   113  	"Name": "boop",
   114  	"Data": "{\"hostname\": \"httpbin2.org\", \"port\": \"80\"}"
   115  }]`
   116  
   117  const etcd = `{
   118  	"action": "get",
   119  	"node": {
   120  		"key": "/services/single",
   121  		"value": "httpbin.org:6000",
   122  		"modifiedIndex": 6,
   123  		"createdIndex": 6
   124  	}
   125  }`
   126  
   127  const nested = `{
   128  	"action": "get",
   129  	"node": {
   130  		"key": "/services/single",
   131  		"value": "{\"hostname\": \"httpbin.org\", \"port\": \"80\"}",
   132  		"modifiedIndex": 6,
   133  		"createdIndex": 6
   134  	}
   135  }`
   136  
   137  const nested_list = `{
   138  	"action": "get",
   139  	"node": {
   140  		"key": "/services/single",
   141  		"value": "[{\"hostname\": \"httpbin.org\", \"port\": \"80\"}, {\"hostname\": \"httpbin2.org\", \"port\": \"80\"}]",
   142  		"modifiedIndex": 6,
   143  		"createdIndex": 6
   144  	}
   145  }`
   146  
   147  const mesosphere = `{
   148  	"tasks": [{
   149  		"id": "myservice.7fc21d4c-eabb-11e5-b381-066c48d09c8f",
   150  		"host": "httpbin.org",
   151  		"ports": [80],
   152  		"startedAt": "2016-03-15T14:37:55.941Z",
   153  		"stagedAt": "2016-03-15T14:37:52.792Z",
   154  		"version": "2016-03-15T14:37:52.726Z",
   155  		"slaveId": "d70867df-fdb2-4889-abeb-0829c742fded-S2",
   156  		"appId": "/httpbin"
   157  	}]
   158  }`
   159  
   160  func configureService(name string, sd *ServiceDiscovery) string {
   161  	switch name {
   162  	case "consul":
   163  		sd.isTargetList = true
   164  		sd.endpointReturnsList = true
   165  		sd.portSeperate = true
   166  		sd.dataPath = "Address"
   167  		sd.portPath = "ServicePort"
   168  		return consul
   169  	case "etcd":
   170  		sd.dataPath = "node.value"
   171  		return etcd
   172  	case "nested":
   173  		sd.isNested = true
   174  		sd.portSeperate = true
   175  		sd.dataPath = "hostname"
   176  		sd.parentPath = "node.value"
   177  		sd.portPath = "port"
   178  		return nested
   179  	case "nested_list":
   180  		sd.isNested = true
   181  		sd.isTargetList = true
   182  		sd.portSeperate = true
   183  		sd.dataPath = "hostname"
   184  		sd.parentPath = "node.value"
   185  		sd.portPath = "port"
   186  		return nested_list
   187  	case "nested_consul":
   188  		sd.isNested = true
   189  		sd.isTargetList = true
   190  		sd.endpointReturnsList = true
   191  		sd.portSeperate = true
   192  		sd.dataPath = "hostname"
   193  		sd.parentPath = "Data"
   194  		sd.portPath = "port"
   195  		return nested_consul
   196  	case "mesosphere":
   197  		sd.isTargetList = true
   198  		sd.portSeperate = true
   199  		sd.dataPath = "host"
   200  		sd.parentPath = "tasks"
   201  		sd.portPath = "ports"
   202  		return mesosphere
   203  	case "eureka":
   204  		sd.isTargetList = true
   205  		sd.portSeperate = true
   206  		sd.dataPath = "hostName"
   207  		sd.parentPath = "application.instance"
   208  		sd.portPath = "port.$"
   209  		return eureka_real
   210  	}
   211  	return ""
   212  }
   213  
   214  func TestServiceDiscovery_EUREKA(t *testing.T) {
   215  	sd := ServiceDiscovery{}
   216  	rawData := configureService("eureka", &sd)
   217  	data, err := sd.ProcessRawData(rawData)
   218  
   219  	if err != nil {
   220  		t.Error(err)
   221  	}
   222  
   223  	arr := []string{"ip-172-31-57-136:60565", "ip-172-31-13-37:50045"}
   224  
   225  	if data.Len() != len(arr) {
   226  		t.Error("Result lists length do not match expected value")
   227  	}
   228  
   229  	for i, v := range data.All() {
   230  		if v != arr[i] {
   231  			err := "Value is wrong, should be: " + arr[i] + " have: " + v
   232  			t.Error(err)
   233  		}
   234  	}
   235  
   236  }
   237  
   238  func TestServiceDiscovery_CONSUL(t *testing.T) {
   239  	sd := ServiceDiscovery{}
   240  	rawData := configureService("consul", &sd)
   241  	data, err := sd.ProcessRawData(rawData)
   242  
   243  	if err != nil {
   244  		t.Error(err)
   245  	}
   246  
   247  	arr := []string{"10.1.10.12:8000", "10.1.10.13:8000"}
   248  
   249  	if data.Len() != len(arr) {
   250  		t.Error("Result lists length do not match expected value")
   251  	}
   252  
   253  	for i, v := range data.All() {
   254  		if v != arr[i] {
   255  			err := "Value is wrong, should be: " + arr[i] + " have: " + v
   256  			t.Error(err)
   257  		}
   258  	}
   259  
   260  }
   261  
   262  func TestServiceDiscovery_NESTED_CONSUL(t *testing.T) {
   263  	sd := ServiceDiscovery{}
   264  	rawData := configureService("nested_consul", &sd)
   265  	data, err := sd.ProcessRawData(rawData)
   266  
   267  	if err != nil {
   268  		t.Error(err)
   269  	}
   270  
   271  	arr := []string{"httpbin1.org:80", "httpbin2.org:80"}
   272  
   273  	if data.Len() != len(arr) {
   274  		t.Error("Result lists length do not match expected value")
   275  	}
   276  
   277  	for i, v := range data.All() {
   278  		if v != arr[i] {
   279  			err := "Value is wrong, should be: " + arr[i] + " have: " + v
   280  			t.Error(err)
   281  		}
   282  	}
   283  
   284  }
   285  
   286  func TestServiceDiscovery_ETCD_NESTED_LIST(t *testing.T) {
   287  	sd := ServiceDiscovery{}
   288  	rawData := configureService("nested_list", &sd)
   289  	data, err := sd.ProcessRawData(rawData)
   290  
   291  	if err != nil {
   292  		t.Error(err)
   293  	}
   294  
   295  	arr := []string{"httpbin.org:80", "httpbin2.org:80"}
   296  
   297  	if data.Len() != len(arr) {
   298  		t.Error("Result lists length do not match expected value")
   299  	}
   300  
   301  	for i, v := range data.All() {
   302  		if v != arr[i] {
   303  			err := "Value is wrong, should be: " + arr[i] + " have: " + v
   304  			t.Error(err)
   305  		}
   306  	}
   307  
   308  }
   309  
   310  func TestServiceDiscovery_ETCD_NESTED_NOLIST(t *testing.T) {
   311  	sd := ServiceDiscovery{}
   312  	rawData := configureService("nested", &sd)
   313  	data, err := sd.ProcessRawData(rawData)
   314  
   315  	if err != nil {
   316  		t.Error(err)
   317  	}
   318  
   319  	host, _ := data.GetIndex(0)
   320  
   321  	tVal := "httpbin.org:80"
   322  
   323  	if tVal != host {
   324  		err := "Value is wrong, should be: " + tVal + " have: " + host
   325  		t.Error(err)
   326  	}
   327  
   328  }
   329  
   330  func TestServiceDiscovery_ETCD_NOLIST(t *testing.T) {
   331  	sd := ServiceDiscovery{}
   332  	rawData := configureService("etcd", &sd)
   333  	data, err := sd.ProcessRawData(rawData)
   334  
   335  	if err != nil {
   336  		t.Error(err)
   337  	}
   338  
   339  	host, _ := data.GetIndex(0)
   340  
   341  	tVal := "httpbin.org:6000"
   342  
   343  	if tVal != host {
   344  		err := "Value is wrong, should be: " + tVal + " have: " + host
   345  		t.Error(err)
   346  	}
   347  
   348  }
   349  
   350  func TestServiceDiscovery_MESOSPHERE(t *testing.T) {
   351  	sd := ServiceDiscovery{}
   352  	rawData := configureService("mesosphere", &sd)
   353  	data, err := sd.ProcessRawData(rawData)
   354  
   355  	if err != nil {
   356  		t.Error(err)
   357  	}
   358  
   359  	arr := []string{"httpbin.org:80"}
   360  
   361  	if data.Len() != len(arr) {
   362  		t.Error("Result lists length do not match expected value")
   363  	}
   364  
   365  	for i, v := range data.All() {
   366  		if v != arr[i] {
   367  			err := "Value is wrong, should be: " + arr[i] + " have: " + v
   368  			t.Error(err)
   369  		}
   370  	}
   371  
   372  }