github.com/sacloud/iaas-api-go@v1.12.0/test/gslb_op_test.go (about)

     1  // Copyright 2022-2023 The sacloud/iaas-api-go Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/sacloud/iaas-api-go"
    21  	"github.com/sacloud/iaas-api-go/testutil"
    22  	"github.com/sacloud/iaas-api-go/types"
    23  )
    24  
    25  func TestGSLBOp_CRUD(t *testing.T) {
    26  	testutil.RunCRUD(t, &testutil.CRUDTestCase{
    27  		Parallel: true,
    28  
    29  		SetupAPICallerFunc: singletonAPICaller,
    30  
    31  		Create: &testutil.CRUDTestFunc{
    32  			Func: testGSLBCreate,
    33  			CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    34  				ExpectValue:  createGSLBExpected,
    35  				IgnoreFields: ignoreGSLBFields,
    36  			}),
    37  		},
    38  
    39  		Read: &testutil.CRUDTestFunc{
    40  			Func: testGSLBRead,
    41  			CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    42  				ExpectValue:  createGSLBExpected,
    43  				IgnoreFields: ignoreGSLBFields,
    44  			}),
    45  		},
    46  
    47  		Updates: []*testutil.CRUDTestFunc{
    48  			{
    49  				Func: testGSLBUpdate,
    50  				CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    51  					ExpectValue:  updateGSLBExpected,
    52  					IgnoreFields: ignoreGSLBFields,
    53  				}),
    54  			},
    55  			{
    56  				Func: testGSLBUpdateSettings,
    57  				CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    58  					ExpectValue:  updateGSLBSettingsExpected,
    59  					IgnoreFields: ignoreGSLBFields,
    60  				}),
    61  			},
    62  			{
    63  				Func: testGSLBUpdateToMin,
    64  				CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    65  					ExpectValue:  updateGSLBToMinExpected,
    66  					IgnoreFields: ignoreGSLBFields,
    67  				}),
    68  			},
    69  		},
    70  
    71  		Delete: &testutil.CRUDTestDeleteFunc{
    72  			Func: testGSLBDelete,
    73  		},
    74  	})
    75  }
    76  
    77  var (
    78  	ignoreGSLBFields = []string{
    79  		"ID",
    80  		"Class",
    81  		"SettingsHash",
    82  		"FQDN",
    83  		"CreatedAt",
    84  		"ModifiedAt",
    85  	}
    86  	createGSLBParam = &iaas.GSLBCreateRequest{
    87  		Name:        testutil.ResourceName("gslb"),
    88  		Description: "desc",
    89  		Tags:        []string{"tag1", "tag2"},
    90  		HealthCheck: &iaas.GSLBHealthCheck{
    91  			Protocol:     types.GSLBHealthCheckProtocols.HTTP,
    92  			HostHeader:   "usacloud.jp",
    93  			Path:         "/index.html",
    94  			ResponseCode: types.StringNumber(200),
    95  		},
    96  		DelayLoop:   20,
    97  		Weighted:    types.StringTrue,
    98  		SorryServer: "8.8.8.8",
    99  		DestinationServers: []*iaas.GSLBServer{
   100  			{
   101  				IPAddress: "192.2.0.1",
   102  				Enabled:   types.StringTrue,
   103  			},
   104  			{
   105  				IPAddress: "192.2.0.2",
   106  				Enabled:   types.StringTrue,
   107  			},
   108  		},
   109  	}
   110  	createGSLBExpected = &iaas.GSLB{
   111  		Name:         createGSLBParam.Name,
   112  		Description:  createGSLBParam.Description,
   113  		Tags:         createGSLBParam.Tags,
   114  		Availability: types.Availabilities.Available,
   115  		DelayLoop:    createGSLBParam.DelayLoop,
   116  		Weighted:     createGSLBParam.Weighted,
   117  		HealthCheck:  createGSLBParam.HealthCheck,
   118  		SorryServer:  createGSLBParam.SorryServer,
   119  		DestinationServers: []*iaas.GSLBServer{
   120  			{
   121  				IPAddress: "192.2.0.1",
   122  				Enabled:   types.StringTrue,
   123  			},
   124  			{
   125  				IPAddress: "192.2.0.2",
   126  				Enabled:   types.StringTrue,
   127  			},
   128  		},
   129  	}
   130  	updateGSLBParam = &iaas.GSLBUpdateRequest{
   131  		Name:        testutil.ResourceName("gslb-upd"),
   132  		Description: "desc-upd",
   133  		Tags:        []string{"tag1-upd", "tag2-upd"},
   134  		HealthCheck: &iaas.GSLBHealthCheck{
   135  			Protocol:     types.GSLBHealthCheckProtocols.HTTPS,
   136  			HostHeader:   "upd.usacloud.jp",
   137  			Path:         "/index-upd.html",
   138  			ResponseCode: types.StringNumber(201),
   139  		},
   140  		DelayLoop:   21,
   141  		Weighted:    types.StringTrue,
   142  		SorryServer: "8.8.4.4",
   143  		DestinationServers: []*iaas.GSLBServer{
   144  			{
   145  				IPAddress: "192.2.0.11",
   146  				Enabled:   types.StringFalse,
   147  				Weight:    types.StringNumber(100),
   148  			},
   149  			{
   150  				IPAddress: "192.2.0.21",
   151  				Enabled:   types.StringFalse,
   152  				Weight:    types.StringNumber(200),
   153  			},
   154  		},
   155  		IconID: testIconID,
   156  	}
   157  	updateGSLBExpected = &iaas.GSLB{
   158  		Name:               updateGSLBParam.Name,
   159  		Description:        updateGSLBParam.Description,
   160  		Tags:               updateGSLBParam.Tags,
   161  		Availability:       types.Availabilities.Available,
   162  		DelayLoop:          updateGSLBParam.DelayLoop,
   163  		Weighted:           updateGSLBParam.Weighted,
   164  		HealthCheck:        updateGSLBParam.HealthCheck,
   165  		SorryServer:        updateGSLBParam.SorryServer,
   166  		DestinationServers: updateGSLBParam.DestinationServers,
   167  		IconID:             testIconID,
   168  	}
   169  	updateGSLBSettingsParam = &iaas.GSLBUpdateSettingsRequest{
   170  		HealthCheck: &iaas.GSLBHealthCheck{
   171  			Protocol:     types.GSLBHealthCheckProtocols.HTTP,
   172  			HostHeader:   "upd2.usacloud.jp",
   173  			Path:         "/index-upd2.html",
   174  			ResponseCode: types.StringNumber(202),
   175  		},
   176  		DelayLoop:   22,
   177  		Weighted:    types.StringFalse,
   178  		SorryServer: "1.1.1.1",
   179  		DestinationServers: []*iaas.GSLBServer{
   180  			{
   181  				IPAddress: "192.2.0.12",
   182  				Enabled:   types.StringFalse,
   183  				Weight:    types.StringNumber(100),
   184  			},
   185  			{
   186  				IPAddress: "192.2.0.22",
   187  				Enabled:   types.StringFalse,
   188  				Weight:    types.StringNumber(200),
   189  			},
   190  		},
   191  	}
   192  	updateGSLBSettingsExpected = &iaas.GSLB{
   193  		Name:               updateGSLBParam.Name,
   194  		Description:        updateGSLBParam.Description,
   195  		Tags:               updateGSLBParam.Tags,
   196  		Availability:       types.Availabilities.Available,
   197  		DelayLoop:          updateGSLBSettingsParam.DelayLoop,
   198  		Weighted:           updateGSLBSettingsParam.Weighted,
   199  		HealthCheck:        updateGSLBSettingsParam.HealthCheck,
   200  		SorryServer:        updateGSLBSettingsParam.SorryServer,
   201  		DestinationServers: updateGSLBSettingsParam.DestinationServers,
   202  		IconID:             testIconID,
   203  	}
   204  	updateGSLBToMinParam = &iaas.GSLBUpdateRequest{
   205  		Name: testutil.ResourceName("gslb-to-min"),
   206  		HealthCheck: &iaas.GSLBHealthCheck{
   207  			Protocol: types.GSLBHealthCheckProtocols.Ping,
   208  		},
   209  	}
   210  	updateGSLBToMinExpected = &iaas.GSLB{
   211  		Name:         updateGSLBToMinParam.Name,
   212  		DelayLoop:    10, // default value
   213  		Availability: types.Availabilities.Available,
   214  		HealthCheck:  updateGSLBToMinParam.HealthCheck,
   215  	}
   216  )
   217  
   218  func testGSLBCreate(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   219  	client := iaas.NewGSLBOp(caller)
   220  	return client.Create(ctx, createGSLBParam)
   221  }
   222  
   223  func testGSLBRead(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   224  	client := iaas.NewGSLBOp(caller)
   225  	return client.Read(ctx, ctx.ID)
   226  }
   227  
   228  func testGSLBUpdate(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   229  	client := iaas.NewGSLBOp(caller)
   230  	return client.Update(ctx, ctx.ID, updateGSLBParam)
   231  }
   232  
   233  func testGSLBUpdateSettings(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   234  	client := iaas.NewGSLBOp(caller)
   235  	return client.UpdateSettings(ctx, ctx.ID, updateGSLBSettingsParam)
   236  }
   237  
   238  func testGSLBUpdateToMin(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   239  	client := iaas.NewGSLBOp(caller)
   240  	return client.Update(ctx, ctx.ID, updateGSLBToMinParam)
   241  }
   242  
   243  func testGSLBDelete(ctx *testutil.CRUDTestContext, caller iaas.APICaller) error {
   244  	client := iaas.NewGSLBOp(caller)
   245  	return client.Delete(ctx, ctx.ID)
   246  }