github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/cloudlets/loadbalancer_version_test.go (about)

     1  package cloudlets
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"errors"
     7  	"io/ioutil"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"testing"
    11  
    12  	"github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/tools"
    13  	validation "github.com/go-ozzo/ozzo-validation/v4"
    14  
    15  	"github.com/stretchr/testify/require"
    16  	"github.com/tj/assert"
    17  )
    18  
    19  func TestCreateLoadBalancerVersion(t *testing.T) {
    20  	tests := map[string]struct {
    21  		params           CreateLoadBalancerVersionRequest
    22  		responseStatus   int
    23  		responseBody     string
    24  		expectedPath     string
    25  		expectedResponse *LoadBalancerVersion
    26  		withError        func(*testing.T, error)
    27  	}{
    28  		"201 Created": {
    29  			params: CreateLoadBalancerVersionRequest{
    30  				LoadBalancerVersion: LoadBalancerVersion{
    31  					BalancingType: BalancingTypeWeighted,
    32  					DataCenters: []DataCenter{
    33  						{
    34  							CloudService: false,
    35  							Hostname:     "clorigin.example.com",
    36  							LivenessHosts: []string{
    37  								"clorigin3.www.example.com",
    38  							},
    39  							Latitude:  tools.Float64Ptr(102.78108),
    40  							Longitude: tools.Float64Ptr(-116.07064),
    41  							Continent: "NA",
    42  							Country:   "US",
    43  							OriginID:  "clorigin3",
    44  							Percent:   tools.Float64Ptr(100.0),
    45  						},
    46  					},
    47  					Deleted:     false,
    48  					Description: "Test load balancing configuration.",
    49  					Immutable:   false,
    50  					LivenessSettings: &LivenessSettings{
    51  						HostHeader: "clorigin3.www.example.com",
    52  						AdditionalHeaders: map[string]string{
    53  							"Host": "example.com",
    54  							"test": "test",
    55  						},
    56  						Interval:         25,
    57  						Path:             "/status",
    58  						Port:             443,
    59  						Protocol:         "HTTPS",
    60  						Status3xxFailure: false,
    61  						Status4xxFailure: true,
    62  						Status5xxFailure: false,
    63  						Timeout:          30,
    64  					},
    65  					OriginID: "clorigin3",
    66  					Version:  4,
    67  				},
    68  				OriginID: "clorigin3",
    69  			},
    70  			responseStatus: http.StatusCreated,
    71  			responseBody: `
    72  {
    73      "balancingType": "WEIGHTED",
    74      "createdBy": "jjones",
    75      "createdDate": "2015-10-08T11:42:18.690Z",
    76      "dataCenters": [
    77          {
    78              "cloudService": false,
    79  			"hostname": "clorigin.example.com",
    80              "livenessHosts": [
    81                  "clorigin3.www.example.com"
    82              ],
    83              "latitude": 102.78108,
    84              "longitude": -116.07064,
    85              "continent": "NA",
    86              "country": "US",
    87              "originId": "clorigin3",
    88              "percent": 100.0
    89          }
    90      ],
    91      "deleted": false,
    92      "description": "Test load balancing configuration.",
    93      "immutable": false,
    94      "lastModifiedBy": "ejnovak",
    95      "lastModifiedDate": "2016-05-02T00:40:02.237Z",
    96      "livenessSettings": {
    97          "hostHeader": "clorigin3.www.example.com",
    98  		"additionalHeaders": {
    99  			"Host": "example.com",
   100  			"test": "test"
   101  		},
   102          "interval": 25,
   103          "path": "/status",
   104          "port": 443,
   105          "protocol": "HTTPS",
   106          "status3xxFailure": false,
   107          "status4xxFailure": true,
   108          "status5xxFailure": false,
   109          "timeout": 30
   110      },
   111      "originId": "clorigin3",
   112      "version": 4
   113  }`,
   114  			expectedPath: "/cloudlets/api/v2/origins/clorigin3/versions",
   115  			expectedResponse: &LoadBalancerVersion{
   116  				BalancingType: BalancingTypeWeighted,
   117  				CreatedBy:     "jjones",
   118  				CreatedDate:   "2015-10-08T11:42:18.690Z",
   119  				DataCenters: []DataCenter{
   120  					{
   121  						CloudService: false,
   122  						Hostname:     "clorigin.example.com",
   123  						LivenessHosts: []string{
   124  							"clorigin3.www.example.com",
   125  						},
   126  						Latitude:  tools.Float64Ptr(102.78108),
   127  						Longitude: tools.Float64Ptr(-116.07064),
   128  						Continent: "NA",
   129  						Country:   "US",
   130  						OriginID:  "clorigin3",
   131  						Percent:   tools.Float64Ptr(100.0),
   132  					},
   133  				},
   134  				Deleted:          false,
   135  				Description:      "Test load balancing configuration.",
   136  				Immutable:        false,
   137  				LastModifiedBy:   "ejnovak",
   138  				LastModifiedDate: "2016-05-02T00:40:02.237Z",
   139  				LivenessSettings: &LivenessSettings{
   140  					HostHeader: "clorigin3.www.example.com",
   141  					AdditionalHeaders: map[string]string{
   142  						"Host": "example.com",
   143  						"test": "test",
   144  					},
   145  					Interval:         25,
   146  					Path:             "/status",
   147  					Port:             443,
   148  					Protocol:         "HTTPS",
   149  					Status3xxFailure: false,
   150  					Status4xxFailure: true,
   151  					Status5xxFailure: false,
   152  					Timeout:          30,
   153  				},
   154  				OriginID: "clorigin3",
   155  				Version:  4,
   156  			},
   157  		},
   158  		"201 OK empty": {
   159  			params: CreateLoadBalancerVersionRequest{
   160  				LoadBalancerVersion: LoadBalancerVersion{},
   161  				OriginID:            "clorigin3",
   162  			},
   163  			responseStatus: http.StatusCreated,
   164  			responseBody: `
   165  {
   166      "createdBy": "jjones",
   167      "createdDate": "2021-09-02T12:51:19.798Z",
   168      "deleted": false,
   169      "immutable": false,
   170      "lastModifiedBy": "jjones",
   171      "lastModifiedDate": "2021-09-02T12:51:19.798Z",
   172      "originId": "clorigin3",
   173      "version": 1,
   174      "warnings": [
   175          {
   176              "detail": "The Load Balancer type is not set",
   177              "title": "Validation Warning",
   178              "type": "/cloudlets/error-types/validation-warning",
   179              "jsonPointer": "/"
   180          },
   181          {
   182              "detail": "The Load Balancer must have at least one data center defined",
   183              "title": "Validation Warning",
   184              "type": "/cloudlets/error-types/validation-warning",
   185              "jsonPointer": "/"
   186          }
   187      ]
   188  }`,
   189  			expectedPath: "/cloudlets/api/v2/origins/clorigin3/versions",
   190  			expectedResponse: &LoadBalancerVersion{
   191  				CreatedBy:        "jjones",
   192  				CreatedDate:      "2021-09-02T12:51:19.798Z",
   193  				Deleted:          false,
   194  				Immutable:        false,
   195  				LastModifiedBy:   "jjones",
   196  				LastModifiedDate: "2021-09-02T12:51:19.798Z",
   197  				OriginID:         "clorigin3",
   198  				Version:          1,
   199  				Warnings: []Warning{
   200  					{
   201  						Detail:      "The Load Balancer type is not set",
   202  						Title:       "Validation Warning",
   203  						Type:        "/cloudlets/error-types/validation-warning",
   204  						JSONPointer: "/",
   205  					},
   206  					{
   207  						Detail:      "The Load Balancer must have at least one data center defined",
   208  						Title:       "Validation Warning",
   209  						Type:        "/cloudlets/error-types/validation-warning",
   210  						JSONPointer: "/",
   211  					},
   212  				},
   213  			},
   214  		},
   215  		"500 internal server error": {
   216  			params: CreateLoadBalancerVersionRequest{
   217  				OriginID:            "23",
   218  				LoadBalancerVersion: LoadBalancerVersion{},
   219  			},
   220  			responseStatus: http.StatusInternalServerError,
   221  			responseBody: `
   222  {
   223  	"type": "internal_error",
   224     "title": "Internal Server Error",
   225     "detail": "Error making request",
   226     "status": 500
   227  }`,
   228  			expectedPath: "/cloudlets/api/v2/origins/23/versions",
   229  			withError: func(t *testing.T, err error) {
   230  				want := &Error{
   231  					Type:       "internal_error",
   232  					Title:      "Internal Server Error",
   233  					Detail:     "Error making request",
   234  					StatusCode: http.StatusInternalServerError,
   235  				}
   236  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   237  			},
   238  		},
   239  	}
   240  
   241  	for name, test := range tests {
   242  		t.Run(name, func(t *testing.T) {
   243  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   244  				assert.Equal(t, test.expectedPath, r.URL.String())
   245  				assert.Equal(t, http.MethodPost, r.Method)
   246  
   247  				// Check if received body is valid json
   248  				b, err := ioutil.ReadAll(r.Body)
   249  				assert.NoError(t, err)
   250  				assert.True(t, json.Valid(b))
   251  
   252  				w.WriteHeader(test.responseStatus)
   253  				_, err = w.Write([]byte(test.responseBody))
   254  				assert.NoError(t, err)
   255  			}))
   256  			client := mockAPIClient(t, mockServer)
   257  			result, err := client.CreateLoadBalancerVersion(context.Background(), test.params)
   258  			if test.withError != nil {
   259  				test.withError(t, err)
   260  				return
   261  			}
   262  			require.NoError(t, err)
   263  			assert.Equal(t, test.expectedResponse, result)
   264  		})
   265  	}
   266  }
   267  
   268  func TestDataCenterValidate(t *testing.T) {
   269  	tests := map[string]struct {
   270  		DataCenter
   271  		withError error
   272  	}{
   273  		"valid data center": {
   274  			DataCenter: DataCenter{
   275  				CloudService: false,
   276  				Hostname:     "clorigin.example.com",
   277  				LivenessHosts: []string{
   278  					"clorigin3.www.example.com",
   279  				},
   280  				Latitude:  tools.Float64Ptr(102.78108),
   281  				Longitude: tools.Float64Ptr(-116.07064),
   282  				Continent: "NA",
   283  				Country:   "US",
   284  				OriginID:  "clorigin3",
   285  				Percent:   tools.Float64Ptr(100.0),
   286  			},
   287  		},
   288  		"valid data center, minimal set of params": {
   289  			DataCenter: DataCenter{
   290  				Latitude:  tools.Float64Ptr(102.78108),
   291  				Longitude: tools.Float64Ptr(-116.07064),
   292  				Continent: "NA",
   293  				Country:   "US",
   294  				OriginID:  "clorigin3",
   295  				Percent:   tools.Float64Ptr(100.0),
   296  			},
   297  		},
   298  		"longitude, latitude and percent can be 0": {
   299  			DataCenter: DataCenter{
   300  				CloudService: false,
   301  				Hostname:     "clorigin.example.com",
   302  				LivenessHosts: []string{
   303  					"clorigin3.www.example.com",
   304  				},
   305  				Latitude:  tools.Float64Ptr(0),
   306  				Longitude: tools.Float64Ptr(0),
   307  				Continent: "NA",
   308  				Country:   "US",
   309  				OriginID:  "clorigin3",
   310  				Percent:   tools.Float64Ptr(0),
   311  			},
   312  		},
   313  		"missing all required parameters error": {
   314  			DataCenter: DataCenter{},
   315  			withError: validation.Errors{
   316  				"Continent": validation.ErrRequired,
   317  				"Country":   validation.ErrRequired,
   318  				"Latitude":  validation.ErrNotNilRequired,
   319  				"Longitude": validation.ErrNotNilRequired,
   320  				"OriginID":  validation.ErrRequired,
   321  				"Percent":   validation.ErrNotNilRequired,
   322  			},
   323  		},
   324  	}
   325  
   326  	for name, test := range tests {
   327  		t.Run(name, func(t *testing.T) {
   328  			err := test.DataCenter.Validate()
   329  			if test.withError != nil {
   330  				require.Error(t, err)
   331  				assert.Equal(t, test.withError.Error(), err.Error(), "want: %s; got %s", test.withError, err)
   332  				return
   333  			}
   334  			require.NoError(t, err)
   335  		})
   336  	}
   337  }
   338  
   339  func TestLivenessSettingsValidate(t *testing.T) {
   340  	tests := map[string]struct {
   341  		LivenessSettings *LivenessSettings
   342  		withError        error
   343  	}{
   344  		"path is required error": {
   345  			LivenessSettings: &LivenessSettings{
   346  				Port:     80,
   347  				Protocol: "HTTP",
   348  			},
   349  			withError: validation.Errors{
   350  				"Path": validation.ErrRequired,
   351  			},
   352  		},
   353  		"RequestString is required error": {
   354  			LivenessSettings: &LivenessSettings{
   355  				Port:           1234,
   356  				Protocol:       "TCP",
   357  				ResponseString: "response",
   358  			},
   359  			withError: validation.Errors{
   360  				"RequestString": validation.ErrRequired,
   361  			},
   362  		},
   363  	}
   364  
   365  	for name, test := range tests {
   366  		t.Run(name, func(t *testing.T) {
   367  			err := test.LivenessSettings.Validate()
   368  			if test.withError != nil && err != nil {
   369  				assert.True(t, err.Error() == test.withError.Error(), "want: %s; got: %s", test.withError, err)
   370  				return
   371  			}
   372  			require.NoError(t, err)
   373  		})
   374  	}
   375  }
   376  
   377  func TestGetLoadBalancerVersion(t *testing.T) {
   378  	tests := map[string]struct {
   379  		params           GetLoadBalancerVersionRequest
   380  		responseStatus   int
   381  		responseBody     string
   382  		expectedPath     string
   383  		expectedResponse *LoadBalancerVersion
   384  		withError        error
   385  	}{
   386  		"200 OK": {
   387  			params: GetLoadBalancerVersionRequest{
   388  				OriginID: "clorigin3",
   389  				Version:  4,
   390  			},
   391  			responseStatus: http.StatusOK,
   392  			responseBody: `
   393  {
   394      "balancingType": "WEIGHTED",
   395      "createdBy": "jjones",
   396      "createdDate": "2015-10-08T11:42:18.690Z",
   397      "dataCenters": [
   398          {
   399              "cloudService": false,
   400              "livenessHosts": [
   401                  "clorigin3.www.example.com"
   402              ],
   403              "latitude": 102.78108,
   404              "longitude": -116.07064,
   405              "continent": "NA",
   406              "country": "US",
   407              "originId": "clorigin3",
   408              "percent": 100.0
   409          }
   410      ],
   411      "deleted": false,
   412      "description": "Test load balancing configuration.",
   413      "immutable": false,
   414      "lastModifiedBy": "ejnovak",
   415      "lastModifiedDate": "2016-05-02T00:40:02.237Z",
   416      "livenessSettings": {
   417          "hostHeader": "clorigin3.www.example.com",
   418          "interval": 25,
   419          "path": "/status",
   420          "port": 443,
   421          "protocol": "HTTPS",
   422          "status3xxFailure": false,
   423          "status4xxFailure": true,
   424          "status5xxFailure": false,
   425          "timeout": 30
   426      },
   427      "originId": "clorigin3",
   428      "version": 4
   429  }`,
   430  			expectedPath: "/cloudlets/api/v2/origins/clorigin3/versions/4",
   431  			expectedResponse: &LoadBalancerVersion{
   432  				BalancingType: BalancingTypeWeighted,
   433  				CreatedBy:     "jjones",
   434  				CreatedDate:   "2015-10-08T11:42:18.690Z",
   435  				DataCenters: []DataCenter{
   436  					{
   437  						CloudService: false,
   438  						LivenessHosts: []string{
   439  							"clorigin3.www.example.com",
   440  						},
   441  						Latitude:  tools.Float64Ptr(102.78108),
   442  						Longitude: tools.Float64Ptr(-116.07064),
   443  						Continent: "NA",
   444  						Country:   "US",
   445  						OriginID:  "clorigin3",
   446  						Percent:   tools.Float64Ptr(100.0),
   447  					},
   448  				},
   449  				Deleted:          false,
   450  				Description:      "Test load balancing configuration.",
   451  				Immutable:        false,
   452  				LastModifiedBy:   "ejnovak",
   453  				LastModifiedDate: "2016-05-02T00:40:02.237Z",
   454  				LivenessSettings: &LivenessSettings{
   455  					HostHeader:       "clorigin3.www.example.com",
   456  					Interval:         25,
   457  					Path:             "/status",
   458  					Port:             443,
   459  					Protocol:         "HTTPS",
   460  					Status3xxFailure: false,
   461  					Status4xxFailure: true,
   462  					Status5xxFailure: false,
   463  					Timeout:          30,
   464  				},
   465  				OriginID: "clorigin3",
   466  				Version:  4,
   467  			},
   468  		},
   469  		"500 internal server error": {
   470  			params: GetLoadBalancerVersionRequest{
   471  				OriginID: "oid1",
   472  				Version:  3,
   473  			},
   474  			responseStatus: http.StatusInternalServerError,
   475  			responseBody: `
   476  {
   477   "type": "internal_error",
   478   "title": "Internal Server Error",
   479   "detail": "Error creating enrollment",
   480   "status": 500
   481  }`,
   482  			expectedPath: "/cloudlets/api/v2/origins/oid1/versions/3",
   483  			withError: &Error{
   484  				Type:       "internal_error",
   485  				Title:      "Internal Server Error",
   486  				Detail:     "Error creating enrollment",
   487  				StatusCode: http.StatusInternalServerError,
   488  			},
   489  		},
   490  	}
   491  
   492  	for name, test := range tests {
   493  		t.Run(name, func(t *testing.T) {
   494  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   495  				assert.Equal(t, test.expectedPath, r.URL.String())
   496  				assert.Equal(t, http.MethodGet, r.Method)
   497  
   498  				w.WriteHeader(test.responseStatus)
   499  				_, err := w.Write([]byte(test.responseBody))
   500  				assert.NoError(t, err)
   501  			}))
   502  			client := mockAPIClient(t, mockServer)
   503  			result, err := client.GetLoadBalancerVersion(context.Background(), test.params)
   504  			if test.withError != nil {
   505  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   506  				return
   507  			}
   508  			require.NoError(t, err)
   509  			assert.Equal(t, test.expectedResponse, result)
   510  		})
   511  	}
   512  }
   513  
   514  func TestUpdateLoadBalancerVersion(t *testing.T) {
   515  	tests := map[string]struct {
   516  		params           UpdateLoadBalancerVersionRequest
   517  		responseStatus   int
   518  		responseBody     string
   519  		expectedPath     string
   520  		expectedResponse *LoadBalancerVersion
   521  		withError        error
   522  	}{
   523  		"200 OK": {
   524  			params: UpdateLoadBalancerVersionRequest{
   525  				OriginID: "clorigin3",
   526  				Version:  4,
   527  				LoadBalancerVersion: LoadBalancerVersion{
   528  					BalancingType: BalancingTypeWeighted,
   529  					DataCenters: []DataCenter{
   530  						{
   531  							CloudService: false,
   532  							Hostname:     "clorigin.example.com",
   533  							LivenessHosts: []string{
   534  								"clorigin3.www.example.com",
   535  							},
   536  							Latitude:  tools.Float64Ptr(102.78108),
   537  							Longitude: tools.Float64Ptr(-116.07064),
   538  							Continent: "NA",
   539  							Country:   "US",
   540  							OriginID:  "clorigin3",
   541  							Percent:   tools.Float64Ptr(100.0),
   542  						},
   543  					},
   544  					Deleted:     false,
   545  					Description: "Test load balancing configuration.",
   546  					Immutable:   false,
   547  					LivenessSettings: &LivenessSettings{
   548  						HostHeader:       "clorigin3.www.example.com",
   549  						Interval:         25,
   550  						Path:             "/status",
   551  						Port:             443,
   552  						Protocol:         "HTTPS",
   553  						Status3xxFailure: false,
   554  						Status4xxFailure: true,
   555  						Status5xxFailure: false,
   556  						Timeout:          30,
   557  					},
   558  					OriginID: "clorigin3",
   559  					Version:  4,
   560  				},
   561  			},
   562  			responseStatus: http.StatusOK,
   563  			responseBody: `
   564  {
   565      "balancingType": "WEIGHTED",
   566      "createdBy": "jjones",
   567      "createdDate": "2015-10-08T11:42:18.690Z",
   568      "dataCenters": [
   569          {
   570              "cloudService": false,
   571  			"hostname": "clorigin.example.com",
   572              "livenessHosts": [
   573                  "clorigin3.www.example.com"
   574              ],
   575              "latitude": 102.78108,
   576              "longitude": -116.07064,
   577              "continent": "NA",
   578              "country": "US",
   579              "originId": "clorigin3",
   580              "percent": 100.0
   581          }
   582      ],
   583      "deleted": false,
   584      "description": "Test load balancing configuration.",
   585      "immutable": false,
   586      "lastModifiedBy": "ejnovak",
   587      "lastModifiedDate": "2016-05-02T00:40:02.237Z",
   588      "livenessSettings": {
   589          "hostHeader": "clorigin3.www.example.com",
   590          "interval": 25,
   591          "path": "/status",
   592          "port": 443,
   593          "protocol": "HTTPS",
   594          "status3xxFailure": false,
   595          "status4xxFailure": true,
   596          "status5xxFailure": false,
   597          "timeout": 30
   598      },
   599      "originId": "clorigin3",
   600      "version": 4
   601  }`,
   602  			expectedPath: "/cloudlets/api/v2/origins/clorigin3/versions/4",
   603  			expectedResponse: &LoadBalancerVersion{
   604  				BalancingType: BalancingTypeWeighted,
   605  				CreatedBy:     "jjones",
   606  				CreatedDate:   "2015-10-08T11:42:18.690Z",
   607  				DataCenters: []DataCenter{
   608  					{
   609  						CloudService: false,
   610  						Hostname:     "clorigin.example.com",
   611  						LivenessHosts: []string{
   612  							"clorigin3.www.example.com",
   613  						},
   614  						Latitude:  tools.Float64Ptr(102.78108),
   615  						Longitude: tools.Float64Ptr(-116.07064),
   616  						Continent: "NA",
   617  						Country:   "US",
   618  						OriginID:  "clorigin3",
   619  						Percent:   tools.Float64Ptr(100.0),
   620  					},
   621  				},
   622  				Deleted:          false,
   623  				Description:      "Test load balancing configuration.",
   624  				Immutable:        false,
   625  				LastModifiedBy:   "ejnovak",
   626  				LastModifiedDate: "2016-05-02T00:40:02.237Z",
   627  				LivenessSettings: &LivenessSettings{
   628  					HostHeader:       "clorigin3.www.example.com",
   629  					Interval:         25,
   630  					Path:             "/status",
   631  					Port:             443,
   632  					Protocol:         "HTTPS",
   633  					Status3xxFailure: false,
   634  					Status4xxFailure: true,
   635  					Status5xxFailure: false,
   636  					Timeout:          30,
   637  				},
   638  				OriginID: "clorigin3",
   639  				Version:  4,
   640  			},
   641  		},
   642  		"500 internal server error": {
   643  			params: UpdateLoadBalancerVersionRequest{
   644  				OriginID: "clorigin3",
   645  				Version:  4,
   646  				LoadBalancerVersion: LoadBalancerVersion{
   647  					BalancingType: BalancingTypeWeighted,
   648  					DataCenters: []DataCenter{
   649  						{
   650  							CloudService: false,
   651  							LivenessHosts: []string{
   652  								"clorigin3.www.example.com",
   653  							},
   654  							Latitude:  tools.Float64Ptr(102.78108),
   655  							Longitude: tools.Float64Ptr(-116.07064),
   656  							Continent: "NA",
   657  							Country:   "US",
   658  							OriginID:  "clorigin3",
   659  							Percent:   tools.Float64Ptr(100.0),
   660  						},
   661  					},
   662  					Description: "Test load balancing configuration.",
   663  					LivenessSettings: &LivenessSettings{
   664  						HostHeader: "clorigin3.www.example.com",
   665  						Path:       "/status",
   666  						Port:       443,
   667  						Protocol:   "HTTPS",
   668  					},
   669  					OriginID: "clorigin3",
   670  					Version:  4,
   671  				},
   672  			},
   673  			responseStatus: http.StatusInternalServerError,
   674  			responseBody: `
   675  {
   676   "type": "internal_error",
   677   "title": "Internal Server Error",
   678   "detail": "Error creating enrollment",
   679   "status": 500
   680  }`,
   681  			expectedPath: "/cloudlets/api/v2/origins/clorigin3/versions/4",
   682  			withError: &Error{
   683  				Type:       "internal_error",
   684  				Title:      "Internal Server Error",
   685  				Detail:     "Error creating enrollment",
   686  				StatusCode: http.StatusInternalServerError,
   687  			},
   688  		},
   689  		"validation error": {
   690  			params: UpdateLoadBalancerVersionRequest{
   691  				OriginID: "oid1",
   692  				Version:  -1,
   693  			},
   694  			withError: ErrStructValidation,
   695  		},
   696  	}
   697  
   698  	for name, test := range tests {
   699  		t.Run(name, func(t *testing.T) {
   700  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   701  				assert.Equal(t, test.expectedPath, r.URL.String())
   702  				assert.Equal(t, http.MethodPut, r.Method)
   703  
   704  				// Check if received body is valid json
   705  				b, err := ioutil.ReadAll(r.Body)
   706  				assert.NoError(t, err)
   707  				assert.True(t, json.Valid(b))
   708  
   709  				w.WriteHeader(test.responseStatus)
   710  				_, err = w.Write([]byte(test.responseBody))
   711  				assert.NoError(t, err)
   712  			}))
   713  			client := mockAPIClient(t, mockServer)
   714  			result, err := client.UpdateLoadBalancerVersion(context.Background(), test.params)
   715  			if test.withError != nil {
   716  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   717  				return
   718  			}
   719  			require.NoError(t, err)
   720  			assert.Equal(t, test.expectedResponse, result)
   721  		})
   722  	}
   723  }
   724  
   725  func TestListLoadBalancerVersions(t *testing.T) {
   726  	tests := map[string]struct {
   727  		listLoadBalancerVersionsRequest ListLoadBalancerVersionsRequest
   728  		responseStatus                  int
   729  		responseBody                    string
   730  		expectedPath                    string
   731  		expectedResponse                []LoadBalancerVersion
   732  		withError                       func(*testing.T, error)
   733  	}{
   734  		"200 OK": {
   735  			listLoadBalancerVersionsRequest: ListLoadBalancerVersionsRequest{
   736  				OriginID: "clorigin3",
   737  			},
   738  			responseStatus: http.StatusOK,
   739  			responseBody: `[
   740  				{
   741  					"createdBy": "jjones",
   742  					"createdDate": "2015-10-08T11:42:18.690Z",
   743  					"dataCenters": [
   744  						{
   745  							"cloudService": false,
   746  							"livenessHosts": [
   747  								"clorigin3.www.example.com"
   748  							],
   749  							"latitude": 102.78108,
   750  							"longitude": -116.07064,
   751  							"continent": "NA",
   752  							"country": "US",
   753  							"originId": "clorigin3",
   754  							"percent": 100.0
   755  						}
   756  					],
   757  					"deleted": false,
   758  					"description": "Test load balancing configuration.",
   759  					"immutable": false,
   760  					"lastModifiedBy": "jsmith",
   761  					"lastModifiedDate": "2016-05-02T00:40:02.237Z",
   762  					"livenessSettings": {
   763  						"hostHeader": "clorigin3.www.example.com",
   764  						"path": "/status",
   765  						"port": 443,
   766  						"protocol": "HTTPS"
   767  					},
   768  					"originId": "clorigin3",
   769  					"version": 2
   770  				},
   771  				{
   772  					"createdBy": "jjones",
   773  					"createdDate": "2015-10-08T11:42:18.690Z",
   774  					"dataCenters": [
   775  						{
   776  							"cloudService": false,
   777  							"livenessHosts": [
   778  								"clorigin3.www.example.com"
   779  							],
   780  							"latitude": 102.78108,
   781  							"longitude": -116.07064,
   782  							"continent": "NA",
   783  							"country": "US",
   784  							"originId": "clorigin3",
   785  							"percent": 100.0
   786  						}
   787  					],
   788  					"deleted": false,
   789  					"description": "Test load balancing configuration.",
   790  					"immutable": false,
   791  					"lastModifiedBy": "jsmith",
   792  					"lastModifiedDate": "2016-05-02T00:40:02.237Z",
   793  					"livenessSettings": {
   794  						"hostHeader": "clorigin3.www.example.com",
   795  						"path": "/status",
   796  						"port": 443,
   797  						"protocol": "HTTPS"
   798  					},
   799  					"originId": "clorigin3",
   800  					"version": 1
   801  				}
   802  			]`,
   803  			expectedPath: "/cloudlets/api/v2/origins/clorigin3/versions?includeModel=true",
   804  			expectedResponse: []LoadBalancerVersion{
   805  				{
   806  					CreatedBy:   "jjones",
   807  					CreatedDate: "2015-10-08T11:42:18.690Z",
   808  					DataCenters: []DataCenter{
   809  						{
   810  							CloudService: false,
   811  							LivenessHosts: []string{
   812  								"clorigin3.www.example.com",
   813  							},
   814  							Latitude:  tools.Float64Ptr(102.78108),
   815  							Longitude: tools.Float64Ptr(-116.07064),
   816  							Continent: "NA",
   817  							Country:   "US",
   818  							OriginID:  "clorigin3",
   819  							Percent:   tools.Float64Ptr(100.0),
   820  						},
   821  					},
   822  					Deleted:          false,
   823  					Description:      "Test load balancing configuration.",
   824  					Immutable:        false,
   825  					LastModifiedBy:   "jsmith",
   826  					LastModifiedDate: "2016-05-02T00:40:02.237Z",
   827  					OriginID:         "clorigin3",
   828  					LivenessSettings: &LivenessSettings{
   829  						HostHeader: "clorigin3.www.example.com",
   830  						Path:       "/status",
   831  						Port:       443,
   832  						Protocol:   "HTTPS",
   833  					},
   834  					Version: 2,
   835  				},
   836  				{
   837  					CreatedBy:   "jjones",
   838  					CreatedDate: "2015-10-08T11:42:18.690Z",
   839  					DataCenters: []DataCenter{
   840  						{
   841  							CloudService: false,
   842  							LivenessHosts: []string{
   843  								"clorigin3.www.example.com",
   844  							},
   845  							Latitude:  tools.Float64Ptr(102.78108),
   846  							Longitude: tools.Float64Ptr(-116.07064),
   847  							Continent: "NA",
   848  							Country:   "US",
   849  							OriginID:  "clorigin3",
   850  							Percent:   tools.Float64Ptr(100.0),
   851  						},
   852  					},
   853  					Deleted:          false,
   854  					Description:      "Test load balancing configuration.",
   855  					Immutable:        false,
   856  					LastModifiedBy:   "jsmith",
   857  					LastModifiedDate: "2016-05-02T00:40:02.237Z",
   858  					OriginID:         "clorigin3",
   859  					LivenessSettings: &LivenessSettings{
   860  						HostHeader: "clorigin3.www.example.com",
   861  						Path:       "/status",
   862  						Port:       443,
   863  						Protocol:   "HTTPS",
   864  					},
   865  					Version: 1,
   866  				},
   867  			},
   868  		},
   869  	}
   870  
   871  	for name, test := range tests {
   872  		t.Run(name, func(t *testing.T) {
   873  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   874  				assert.Equal(t, test.expectedPath, r.URL.String())
   875  				assert.Equal(t, http.MethodGet, r.Method)
   876  				w.WriteHeader(test.responseStatus)
   877  				_, err := w.Write([]byte(test.responseBody))
   878  				assert.NoError(t, err)
   879  			}))
   880  			client := mockAPIClient(t, mockServer)
   881  			result, err := client.ListLoadBalancerVersions(context.Background(), test.listLoadBalancerVersionsRequest)
   882  			if test.withError != nil {
   883  				test.withError(t, err)
   884  				return
   885  			}
   886  			require.NoError(t, err)
   887  			assert.Equal(t, test.expectedResponse, result)
   888  		})
   889  	}
   890  }