github.com/akamai/AkamaiOPEN-edgegrid-golang@v1.2.2/reportsgtm-v1/property_test.go (about)

     1  package reportsgtm
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"gopkg.in/h2non/gock.v1"
     8  )
     9  
    10  //
    11  // Important note: The test cases enclosed piggyback on the objects created in the configgtm test cases
    12  //
    13  // TODO: Add tests for Opt args
    14  //
    15  
    16  //
    17  var GtmTestProperty = "testproperty"
    18  
    19  // Verify GetIpStatusPerProperty. Property and domain names hardcoded. Should pass, e.g. no API errors and property returned
    20  // Depends on CreateProperty
    21  func TestGetIpStatusProperty(t *testing.T) {
    22  
    23  	defer gock.Off()
    24  
    25  	mock := gock.New("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net/gtm-api/v1/reports/ip-availability/domains/gtmtest.akadns.net/properties/" + GtmTestProperty)
    26  	mock.
    27  		Get("/gtm-api/v1/reports/ip-availability/domains/gtmtest.akadns.net/properties/"+GtmTestProperty).
    28  		HeaderPresent("Authorization").
    29  		Reply(200).
    30  		SetHeader("Content-Type", "application/json").
    31  		BodyString(`{
    32                          "metadata": {
    33                                  "domain": "gtmtest.akadns.net",
    34                                  "property": "testproperty",
    35                                  "start" : "2017-02-23T21:00:00Z",
    36                                  "end" : "2017-03-23T22:00:00Z",
    37                                  "uri": "https://akaa-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx.luna.akamaiapis.net/gtm-api/v1/reports/ip-availability/domains/gtmtest.akadns.net/properties/testproperty"
    38                          },
    39                          "dataRows": [
    40                                  {
    41                                          "timestamp": "2017-02-23T21:42:35Z",
    42                                          "cutOff": 112.5,
    43                                          "datacenters": [
    44                                                  {
    45                                                          "datacenterId": 3132,
    46                                                          "nickname": "Winterfell",
    47                                                          "trafficTargetName": "Winterfell - 1.2.3.4",
    48                                                          "IPs": [
    49                                                                   {
    50                                                                           "ip": "1.2.3.4",
    51                                                                           "score": 75.0,
    52                                                                           "handedOut": true,
    53                                                                           "alive": true
    54                                                                   } ]
    55                                                  },
    56                                                  {
    57                                                          "datacenterId": 3133,
    58                                                          "nickname": "Braavos",
    59                                                          "trafficTargetName": "Braavos - 1.2.3.5",
    60                                                          "IPs": [
    61                                                                   {
    62                                                                           "ip": "1.2.3.5",
    63                                                                           "score": 85.0,
    64                                                                           "handedOut": true,
    65                                                                           "alive": true
    66                                                                   } ]
    67                                                  } ]
    68                                  },
    69                                  {
    70                                          "timestamp": "2017-03-23T21:42:35Z",
    71                                          "cutOff": 112.5,
    72                                          "datacenters": [
    73                                                  {
    74                                                          "datacenterId": 3132,
    75                                                          "nickname": "Winterfell",
    76                                                          "trafficTargetName": "Winterfell - 1.2.3.4",
    77                                                          "IPs": [
    78                                                                   {
    79                                                                           "ip": "1.2.3.4",
    80                                                                           "score": 115.0,
    81                                                                           "handedOut": false,
    82                                                                           "alive": false
    83                                                                   } ]
    84                                                  },               
    85                                                  {
    86                                                          "datacenterId": 3133,
    87                                                          "nickname": "Braavos",
    88                                                          "trafficTargetName": "Braavos - 1.2.3.5",
    89                                                          "IPs": [
    90                                                                   {
    91                                                                           "ip": "1.2.3.5",
    92                                                                           "score": 75.0,
    93                                                                           "handedOut": true,
    94                                                                           "alive": true
    95                                                                   } ]
    96                                                  } ]              
    97                                  } ],    
    98                          "links": [
    99                                  {
   100                                          "rel": "self",
   101                                          "href": "https://akaa-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx.luna.akamaiapis.net/gtm-api/v1/reports/ip-availability/domains/gtmtest.akadns.net/properties/testproperty"
   102                                  } ]
   103                   }`)
   104  
   105  	Init(config)
   106  
   107  	optArgs := make(map[string]string)
   108  	optArgs["start"] = "2017-02-23T21:00:00Z"
   109  	optArgs["end"] = "2017-03-23T22:00:00Z"
   110  	testPropertyIpStatus, err := GetIpStatusPerProperty(gtmTestDomain, GtmTestProperty, optArgs)
   111  
   112  	assert.NoError(t, err)
   113  	assert.Equal(t, testPropertyIpStatus.DataRows[0].Datacenters[0].DatacenterId, 3132)
   114  	assert.Equal(t, testPropertyIpStatus.Metadata.Domain, gtmTestDomain)
   115  	assert.Equal(t, testPropertyIpStatus.Metadata.Property, GtmTestProperty)
   116  
   117  }
   118  
   119  /*
   120  // TestGetIpStatus with mostRecent flag
   121  func TestGetIpStatusPropertyRecent(t *testing.T) {
   122  
   123         config, _ := edgegrid.InitEdgeRc("", "default")
   124         Init(config)
   125  
   126         // add mock ...
   127  
   128         optArgs := make(map[string]string)
   129         optArgs["mostRecent"] = "true"
   130  
   131         domDCs, err := configgtm.ListDatacenters(GtmObjectTestDomain)
   132         assert.NoError(t, err, "Failure retrieving DCs")
   133         if len(domDCs) > 0 {
   134                 optArgs["datacenterId"] = strconv.Itoa(domDCs[0].DatacenterId)
   135                 fmt.Println("dcid: "+optArgs["datacenterId"])
   136         }
   137  
   138         testPropertyIpStatus, err := GetIpStatusPerProperty(GtmObjectTestDomain, GtmTestProperty, optArgs)
   139  
   140         assert.NoError(t, err)
   141  
   142         json, err := json.MarshalIndent(testPropertyIpStatus, "", "    ")
   143         if err == nil {
   144                 fmt.Println(string(json))
   145         } else {
   146                 t.Fatal("PropertyIP Status retrival failed. " + err.Error())
   147         }
   148  }
   149  */
   150  
   151  // Verify GetTrafficPerProperty. Domain name and property name hardcoded.
   152  
   153  func TestGetTrafficPerProperty(t *testing.T) {
   154  
   155  	defer gock.Off()
   156  
   157  	mock := gock.New("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net/gtm-api/v1/reports/traffic/domains/gtmtest.akadns.net/properties/" + GtmTestProperty)
   158  	mock.
   159  		Get("/gtm-api/v1/reports/traffic/domains/gtmtest.akadns.net/properties/"+GtmTestProperty).
   160  		HeaderPresent("Authorization").
   161  		Reply(200).
   162  		SetHeader("Content-Type", "application/json").
   163  		BodyString(`{
   164                          "metadata": {
   165                                  "domain": "gtmtest.akadns.net",
   166                                  "property": "testproperty",
   167                                  "start": "2016-11-24T01:40:00Z",
   168                                  "end": "2016-11-24T01:50:00Z",
   169                                  "interval": "FIVE_MINUTE",
   170                                  "uri": "https://akaa-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx.luna.akamaiapis.net/gtm-api/v1/reports/traffic/domains/gtmtest.akadns.net/properties/testproperty?start=2016-11-23T00:00:00Z&2016-11-24T01:50:00Z"
   171                          },
   172                          "dataRows": [
   173                                  {
   174                                          "timestamp": "2016-11-24T01:40:00Z",
   175                                          "datacenters": [
   176                                                  {
   177                                                          "datacenterId": 3130,
   178                                                          "nickname": "Winterfell",
   179                                                          "trafficTargetName": "Winterfell - 1.2.3.4",
   180                                                          "requests": 34,
   181                                                          "status": "1"
   182                                                  } ]
   183                                  },
   184                                  {
   185                                          "timestamp": "2016-11-24T01:45:00Z",
   186                                          "datacenters": [
   187                                                  {
   188                                                          "datacenterId": 3130,
   189                                                          "nickname": "Winterfell",
   190                                                          "trafficTargetName": "Winterfell - 1.2.3.4",
   191                                                          "requests": 45,
   192                                                          "status": "1"
   193                                                  } ]
   194                                  } ],
   195                          "links": [
   196                                  {
   197                                          "rel": "self",
   198                                          "href": "https://akaa-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx.luna.akamaiapis.net/gtm-api/v1/reports/traffic/domains/gtmtest.akadns.net/properties/testproperty?start=2016-11-23T00:00:00Z&2016-11-24T01:50:00Z"
   199                                  } ]
   200                   }`)
   201  
   202  	Init(config)
   203  
   204  	optArgs := make(map[string]string)
   205  	optArgs["start"] = "2016-11-24T01:40:00Z"
   206  	optArgs["end"] = "2016-11-24T01:50:00Z"
   207  
   208  	testPropertyTraffic, err := GetTrafficPerProperty(gtmTestDomain, GtmTestProperty, optArgs)
   209  
   210  	assert.NoError(t, err)
   211  	assert.Equal(t, testPropertyTraffic.DataRows[0].Datacenters[0].DatacenterId, 3130)
   212  	assert.Equal(t, testPropertyTraffic.Metadata.Domain, gtmTestDomain)
   213  	assert.Equal(t, testPropertyTraffic.Metadata.Property, GtmTestProperty)
   214  
   215  }
   216  
   217  // Verify failed case for GetProperty. Should pass, e.g. no API errors and domain not found
   218  func TestGetBadTrafficPerProperty(t *testing.T) {
   219  
   220  	defer gock.Off()
   221  
   222  	mock := gock.New("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net/gtm-api/v1/reports/traffic/domains/gtmtest.akadns.net/properties/badproperty")
   223  	mock.
   224  		Get("/gtm-api/v1/reports/traffic/domains/gtmtest.akadns.net/properties/badproperty").
   225  		HeaderPresent("Authorization").
   226  		Reply(404).
   227  		SetHeader("Content-Type", "application/json").
   228  		BodyString(`{
   229                  }`)
   230  
   231  	Init(config)
   232  
   233  	optArgs := make(map[string]string)
   234  	_, err := GetTrafficPerProperty(gtmTestDomain, "badproperty", optArgs)
   235  
   236  	assert.Error(t, err)
   237  
   238  }