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

     1  package configgtm
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/akamai/AkamaiOPEN-edgegrid-golang/jsonhooks-v1"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"gopkg.in/h2non/gock.v1"
    10  
    11  	"fmt"
    12  )
    13  
    14  var GtmTestProperty = "testproperty"
    15  
    16  func instantiateProperty() *Property {
    17  
    18  	property := NewProperty(GtmTestProperty)
    19  	propertyData := []byte(`{
    20                          "backupCName" : null,
    21                          "backupIp" : null,
    22                          "balanceByDownloadScore" : false,
    23                          "cname" : null,
    24                          "comments" : null,
    25                          "dynamicTTL" : 300,
    26                          "failoverDelay" : null,
    27                          "failbackDelay" : null,
    28                          "ghostDemandReporting" : false,
    29                          "handoutMode" : "normal",
    30                          "handoutLimit" : 1,
    31                          "healthMax" : null,
    32                          "healthMultiplier" : null,
    33                          "healthThreshold" : null,
    34                          "lastModified" : "2019-06-14T19:46:17.818+00:00",
    35                          "livenessTests" : [ ],
    36                          "loadImbalancePercentage" : null,
    37                          "mapName" : null,
    38                          "maxUnreachablePenalty" : null,
    39                          "minLiveFraction" : null,
    40                          "mxRecords" : [ ],
    41                          "name" : "testproperty",
    42                          "scoreAggregationType" : "median",
    43                          "stickinessBonusConstant" : null,
    44                          "stickinessBonusPercentage" : null,
    45                          "staticTTL" : null,
    46                          "trafficTargets" : [ {
    47                                  "datacenterId" : 3131,
    48                                  "enabled" : true,
    49                                  "weight" : 100.0,
    50                                  "handoutCName" : null,
    51                                  "name" : null,
    52                                  "servers" : [ "1.2.3.4" ]
    53                          } ],
    54                          "type" : "performance",
    55                          "unreachableThreshold" : null,
    56                          "useComputedTargets" : false,
    57                          "weightedHashBitsForIPv4" : null,
    58                          "weightedHashBitsForIPv6" : null,
    59                          "ipv6" : false,
    60                          "links" : [ {
    61                                              "rel" : "self",
    62                                               "href" : "https://akaa-32qkzqewderdchot-d3uwbyqc4pqi2c5l.luna-dev.akamaiapis.net/config-gtm/v1/domains/gtmdomtest.akadns.net/properties/testproperty"
    63                          } ]
    64              }`)
    65  	jsonhooks.Unmarshal(propertyData, property)
    66  
    67  	return property
    68  
    69  }
    70  
    71  // Verify GetListProperties. Should pass, e.g. no API errors and non nil list.
    72  func TestListProperties(t *testing.T) {
    73  
    74  	defer gock.Off()
    75  	mock := gock.New("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net/config-gtm/v1/domains/" + gtmTestDomain + "/properties")
    76  	mock.
    77  		Get("/config-gtm/v1/domains/"+gtmTestDomain+"/properties").
    78  		HeaderPresent("Authorization").
    79  		Reply(200).
    80  		SetHeader("Content-Type", "application/vnd.config-gtm.v1.5+json;charset=UTF-8").
    81  		BodyString(`{
    82                      "items": [ {
    83                          "backupCName" : null,
    84                          "backupIp" : null,
    85                          "balanceByDownloadScore" : false,
    86                          "cname" : null,
    87                          "comments" : null,
    88                          "dynamicTTL" : 300,
    89                          "failoverDelay" : null,
    90                          "failbackDelay" : null,
    91                          "ghostDemandReporting" : false,
    92                          "handoutMode" : "normal",
    93                          "handoutLimit" : 1,
    94                          "healthMax" : null,
    95                          "healthMultiplier" : null,
    96                          "healthThreshold" : null,
    97                          "lastModified" : "2019-06-14T19:46:17.818+00:00",
    98                          "livenessTests" : [ ],
    99                          "loadImbalancePercentage" : null,
   100                          "mapName" : null,
   101                          "maxUnreachablePenalty" : null,
   102                          "minLiveFraction" : null,
   103                          "mxRecords" : [ ],
   104                          "name" : "testproperty",
   105                          "scoreAggregationType" : "median",
   106                          "stickinessBonusConstant" : null,
   107                          "stickinessBonusPercentage" : null,
   108                          "staticTTL" : null,
   109                          "trafficTargets" : [ {
   110                                  "datacenterId" : 3131,
   111                                  "enabled" : true,
   112                                  "weight" : 100.0,
   113                                  "handoutCName" : null,
   114                                  "name" : null,
   115                                  "servers" : [ "1.2.3.4" ]
   116                          } ],
   117                          "type" : "performance",
   118                          "unreachableThreshold" : null,
   119                          "useComputedTargets" : false,
   120                          "weightedHashBitsForIPv4" : null,
   121                          "weightedHashBitsForIPv6" : null,
   122                          "ipv6" : false,
   123                          "links" : [ {
   124                                              "rel" : "self",
   125                                              "href" : "https://akaa-32qkzqewderdchot-d3uwbyqc4pqi2c5l.luna-dev.akamaiapis.net/config-gtm/v1/domains/gtmdomtest.akadns.net/properties/testproperty"
   126                          } ] 
   127                      } ]
   128                  }`)
   129  
   130  	Init(config)
   131  
   132  	propertyList, err := ListProperties(gtmTestDomain)
   133  	assert.NoError(t, err)
   134  	assert.NotEqual(t, propertyList, nil)
   135  
   136  	if len(propertyList) > 0 {
   137  		firstProp := propertyList[0]
   138  		assert.Equal(t, firstProp.Name, GtmTestProperty)
   139  	} else {
   140  		assert.Equal(t, 0, 1, "ListProperties: empty list")
   141  		fmt.Println("ListProperties: empty list")
   142  	}
   143  
   144  }
   145  
   146  // Verify GetProperty. Name hardcoded. Should pass, e.g. no API errors and property returned
   147  // Depends on CreateProperty
   148  func TestGetProperty(t *testing.T) {
   149  
   150  	defer gock.Off()
   151  	mock := gock.New("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net/config-gtm/v1/domains/" + gtmTestDomain + "/properties/" + GtmTestProperty)
   152  	mock.
   153  		Get("/config-gtm/v1/domains/"+gtmTestDomain+"/properties/"+GtmTestProperty).
   154  		HeaderPresent("Authorization").
   155  		Reply(200).
   156  		SetHeader("Content-Type", "application/vnd.config-gtm.v1.5+json;charset=UTF-8").
   157  		BodyString(`{
   158                          "backupCName" : null,
   159                          "backupIp" : null,
   160                          "balanceByDownloadScore" : false,
   161                          "cname" : null,
   162                          "comments" : null,
   163                          "dynamicTTL" : 300,
   164                          "failoverDelay" : null,
   165                          "failbackDelay" : null,
   166                          "ghostDemandReporting" : false,
   167                          "handoutMode" : "normal",
   168                          "handoutLimit" : 1,
   169                          "healthMax" : null,
   170                          "healthMultiplier" : null,
   171                          "healthThreshold" : null,
   172                          "lastModified" : "2019-06-14T19:46:17.818+00:00",
   173                          "livenessTests" : [  {
   174                              "name": "liveness-1",
   175                              "peerCertificateVerification": false,
   176                              "testInterval": 30,
   177                              "testObject": "/healthz",
   178                              "httpError3xx": true,
   179                              "httpError4xx": true,
   180                              "httpError5xx": true,
   181                              "disabled": false,
   182                              "testObjectProtocol": "HTTP",
   183                              "testObjectPort": 80,
   184                              "httpHeaders": [ {
   185                                  "name": "Host",
   186                                  "value": "example.com"
   187                              } ],
   188                              "answersRequired": true,
   189                              "httpMethod": "POST"
   190                          }  ],
   191                          "loadImbalancePercentage" : null,
   192                          "mapName" : null,
   193                          "maxUnreachablePenalty" : null,
   194                          "minLiveFraction" : null,
   195                          "mxRecords" : [ ],
   196                          "name" : "testproperty",
   197                          "scoreAggregationType" : "median",
   198                          "stickinessBonusConstant" : null,
   199                          "stickinessBonusPercentage" : null,
   200                          "staticTTL" : null,
   201                          "trafficTargets" : [ {
   202                                  "datacenterId" : 3131,
   203                                  "enabled" : true,
   204                                  "weight" : 100.0,
   205                                  "handoutCName" : null,
   206                                  "name" : null,
   207                                  "servers" : [ "1.2.3.4" ]
   208                          } ],
   209                          "type" : "performance",
   210                          "unreachableThreshold" : null,
   211                          "useComputedTargets" : false,
   212                          "weightedHashBitsForIPv4" : null,
   213                          "weightedHashBitsForIPv6" : null,
   214                          "ipv6" : false,
   215                          "links" : [ {
   216                                              "rel" : "self",
   217                                              "href" : "https://akaa-32qkzqewderdchot-d3uwbyqc4pqi2c5l.luna-dev.akamaiapis.net/config-gtm/v1/domains/gtmdomtest.akadns.net/properties/testproperty"
   218                          } ]
   219                 }`)
   220  
   221  	Init(config)
   222  
   223  	testProperty, err := GetProperty(GtmTestProperty, gtmTestDomain)
   224  
   225  	assert.NoError(t, err)
   226  	assert.Equal(t, GtmTestProperty, testProperty.Name)
   227  
   228  }
   229  
   230  // Verify failed case for GetProperty. Should pass, e.g. no API errors and domain not found
   231  func TestGetBadProperty(t *testing.T) {
   232  
   233  	boguspropertyname := "boguspropertyname"
   234  	defer gock.Off()
   235  	mock := gock.New("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net/config-gtm/v1/domains/" + gtmTestDomain + "/properties/" + boguspropertyname)
   236  	mock.
   237  		Get("/config-gtm/v1/domains/"+gtmTestDomain+"/properties/"+boguspropertyname).
   238  		HeaderPresent("Authorization").
   239  		Reply(404).
   240  		SetHeader("Content-Type", "application/vnd.config-gtm.v1.5+json;charset=UTF-8").
   241  		BodyString(`{
   242                 }`)
   243  	Init(config)
   244  
   245  	_, err := GetProperty("boguspropertyname", gtmTestDomain)
   246  	assert.Error(t, err)
   247  
   248  }
   249  
   250  // Test Create property. Name is hardcoded so this will effectively be an update. What happens to existing?
   251  func TestCreateProperty(t *testing.T) {
   252  
   253  	defer gock.Off()
   254  	mock := gock.New("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net/config-gtm/v1/domains/" + gtmTestDomain + "/properties/" + GtmTestProperty)
   255  	mock.
   256  		Put("/config-gtm/v1/domains/"+gtmTestDomain+"/properties/"+GtmTestProperty).
   257  		HeaderPresent("Authorization").
   258  		Reply(200).
   259  		SetHeader("Content-Type", "application/vnd.config-gtm.v1.5+json;charset=UTF-8").
   260  		BodyString(`{
   261                      "resource" : {
   262                          "backupCName" : null,
   263                          "backupIp" : null,
   264                          "balanceByDownloadScore" : false,
   265                          "cname" : null,
   266                          "comments" : null,
   267                          "dynamicTTL" : 300,
   268                          "failoverDelay" : null,
   269                          "failbackDelay" : null,
   270                          "ghostDemandReporting" : false,
   271                          "handoutMode" : "normal",
   272                          "handoutLimit" : 1,
   273                          "healthMax" : null,
   274                          "healthMultiplier" : null,
   275                          "healthThreshold" : null,
   276                          "lastModified" : "2019-06-14T19:46:17.818+00:00",
   277                          "livenessTests" : [  {
   278                              "name": "liveness-1",
   279                              "peerCertificateVerification": false,
   280                              "testInterval": 30,
   281                              "testObject": "/healthz",
   282                              "httpError3xx": true,
   283                              "httpError4xx": true,
   284                              "httpError5xx": true,
   285                              "disabled": false,
   286                              "testObjectProtocol": "HTTP",
   287                              "testObjectPort": 80,
   288                              "httpHeaders": [ {
   289                                  "name": "Host",
   290                                  "value": "example.com"
   291                              } ],
   292                              "answersRequired": true,
   293                              "httpMethod": "POST"
   294                          }  ],
   295                          "loadImbalancePercentage" : null,
   296                          "mapName" : null,
   297                          "maxUnreachablePenalty" : null,
   298                          "minLiveFraction" : null,
   299                          "mxRecords" : [ ],
   300                          "name" : "testproperty",
   301                          "scoreAggregationType" : "median",
   302                          "stickinessBonusConstant" : null,
   303                          "stickinessBonusPercentage" : null,
   304                          "staticTTL" : null,
   305                          "trafficTargets" : [ {
   306                                  "datacenterId" : 3131,
   307                                  "enabled" : true,
   308                                  "weight" : 100.0,
   309                                  "handoutCName" : null,
   310                                  "name" : null, 
   311                                  "servers" : [ "1.2.3.4" ]
   312                          } ],    
   313                              "type" : "performance",
   314                          "unreachableThreshold" : null,
   315                          "useComputedTargets" : false,
   316                          "weightedHashBitsForIPv4" : null,
   317                          "weightedHashBitsForIPv6" : null,
   318                          "ipv6" : false,
   319                          "links" : [ {
   320                                              "rel" : "self",
   321                                               "href" : "https://akaa-32qkzqewderdchot-d3uwbyqc4pqi2c5l.luna-dev.akamaiapis.net/config-gtm/v1/domains/gtmdomtest.akadns.net/properties/testproperty"
   322                          } ]
   323                      },
   324                      "status" : {
   325                          "message" : "Change Pending",
   326                          "changeId" : "ca4ca7a9-2b87-4f7c-8ba6-0cb1571df325",
   327                          "propagationStatus" : "PENDING",
   328                          "propagationStatusDate" : "2019-06-14T19:46:18.461+00:00",
   329                          "passingValidation" : true,
   330                          "links" : [ {
   331                                              "rel" : "self",
   332                                              "href" : "https://akaa-32qkzqewderdchot-d3uwbyqc4pqi2c5l.luna-dev.akamaiapis.net/config-gtm/v1/domains/gtmdomtest.akadns.net/status/current"
   333                          } ]
   334                      }
   335         }`)
   336  
   337  	Init(config)
   338  
   339  	// initialize required fields
   340  	testProperty := NewProperty(GtmTestProperty)
   341  	testProperty.ScoreAggregationType = "median"
   342  	testProperty.Type = "performance"
   343  	testProperty.HandoutLimit = 1
   344  	testProperty.HandoutMode = "normal"
   345  	testProperty.FailoverDelay = 0
   346  	testProperty.FailbackDelay = 0
   347  	testProperty.TrafficTargets = []*TrafficTarget{{DatacenterId: 3131, Enabled: true, Servers: []string{"1.2.3.4"}, Weight: 100.0}}
   348  
   349  	statresp, err := testProperty.Create(gtmTestDomain)
   350  	assert.NoError(t, err)
   351  
   352  	assert.IsType(t, &Property{}, statresp.Resource)
   353  	assert.Equal(t, GtmTestProperty, statresp.Resource.Name)
   354  
   355  }
   356  
   357  func TestUpdateProperty(t *testing.T) {
   358  
   359  	defer gock.Off()
   360  	mock := gock.New("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net/config-gtm/v1/domains/" + gtmTestDomain + "/properties/" + GtmTestProperty)
   361  	mock.
   362  		Put("/config-gtm/v1/domains/"+gtmTestDomain+"/properties/"+GtmTestProperty).
   363  		HeaderPresent("Authorization").
   364  		Reply(200).
   365  		SetHeader("Content-Type", "application/vnd.config-gtm.v1.5+json;charset=UTF-8").
   366  		BodyString(`{
   367                      "resource" : {
   368                          "backupCName" : null,
   369                          "backupIp" : null,
   370                          "balanceByDownloadScore" : false,
   371                          "cname" : null,
   372                          "comments" : null,
   373                          "dynamicTTL" : 300,
   374                          "failoverDelay" : null,
   375                          "failbackDelay" : null,
   376                          "ghostDemandReporting" : false,
   377                          "handoutMode" : "normal",
   378                          "handoutLimit" : 999,
   379                          "healthMax" : null,
   380                          "healthMultiplier" : null,
   381                          "healthThreshold" : null,
   382                          "lastModified" : "2019-06-14T19:46:17.818+00:00",
   383                          "livenessTests" : [  {
   384                              "name": "liveness-1",
   385                              "peerCertificateVerification": false,
   386                              "testInterval": 30,
   387                              "testObject": "/healthz",
   388                              "httpError3xx": true,
   389                              "httpError4xx": true,
   390                              "httpError5xx": true,
   391                              "disabled": false,
   392                              "testObjectProtocol": "HTTP",
   393                              "testObjectPort": 80,
   394                              "httpHeaders": [ {
   395                                  "name": "Host",
   396                                  "value": "example.com"
   397                              } ],
   398                              "answersRequired": true,
   399                              "httpMethod": "POST"
   400                          }  ],
   401                          "loadImbalancePercentage" : null,
   402                          "mapName" : null,
   403                          "maxUnreachablePenalty" : null,
   404                          "minLiveFraction" : null,
   405                          "mxRecords" : [ ],
   406                          "name" : "testproperty",
   407                          "scoreAggregationType" : "median",
   408                          "stickinessBonusConstant" : null,
   409                          "stickinessBonusPercentage" : null,
   410                          "staticTTL" : null,
   411                          "trafficTargets" : [ {
   412                                  "datacenterId" : 3131,
   413                                  "enabled" : true,
   414                                  "weight" : 100.0,
   415                                  "handoutCName" : null,
   416                                  "name" : null,
   417                                  "servers" : [ "1.2.3.4" ]
   418                          } ],
   419                                "type" : "performance",
   420                          "unreachableThreshold" : null,
   421                          "useComputedTargets" : false,
   422                          "weightedHashBitsForIPv4" : null,
   423                          "weightedHashBitsForIPv6" : null,
   424                          "ipv6" : false,
   425                          "links" : [ {
   426                                              "rel" : "self",
   427                                               "href" : "https://akaa-32qkzqewderdchot-d3uwbyqc4pqi2c5l.luna-dev.akamaiapis.net/config-gtm/v1/domains/gtmdomtest.akadns.net/properties/testproperty"
   428                          } ]
   429                      },
   430                      "status" : {
   431                          "message" : "Change Pending",
   432                          "changeId" : "ca4ca7a9-2b87-4f7c-8ba6-0cb1571df325",
   433                          "propagationStatus" : "PENDING",
   434                          "propagationStatusDate" : "2019-06-14T19:46:18.461+00:00",
   435                          "passingValidation" : true,
   436                          "links" : [ {
   437                                              "rel" : "self",
   438                                              "href" : "https://akaa-32qkzqewderdchot-d3uwbyqc4pqi2c5l.luna-dev.akamaiapis.net/config-gtm/v1/domains/gtmdomtest.akadns.net/status/current"
   439                          } ]
   440                      } 
   441                  }`)
   442  
   443  	Init(config)
   444  
   445  	testProperty := instantiateProperty()
   446  
   447  	testProperty.HandoutLimit = 999
   448  	stat, err := testProperty.Update(gtmTestDomain)
   449  	assert.NoError(t, err)
   450  	assert.Equal(t, stat.ChangeId, "ca4ca7a9-2b87-4f7c-8ba6-0cb1571df325")
   451  
   452  }
   453  
   454  func TestDeleteProperty(t *testing.T) {
   455  
   456  	defer gock.Off()
   457  	mock := gock.New("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net/config-gtm/v1/domains/" + gtmTestDomain + "/properties/" + GtmTestProperty)
   458  	mock.
   459  		Delete("/config-gtm/v1/domains/"+gtmTestDomain+"/properties/"+GtmTestProperty).
   460  		HeaderPresent("Authorization").
   461  		Reply(200).
   462  		SetHeader("Content-Type", "application/vnd.config-gtm.v1.5+json;charset=UTF-8").
   463  		BodyString(`{
   464                 }`)
   465  	Init(config)
   466  
   467  	getProperty := instantiateProperty()
   468  
   469  	_, err := getProperty.Delete(gtmTestDomain)
   470  	assert.NoError(t, err)
   471  
   472  }