github.com/sacloud/libsacloud/v2@v2.32.3/helper/service/loadbalancer/update_test.go (about) 1 // Copyright 2016-2022 The Libsacloud 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 loadbalancer 16 17 import ( 18 "context" 19 "testing" 20 21 "github.com/sacloud/libsacloud/v2/helper/wait" 22 "github.com/sacloud/libsacloud/v2/sacloud" 23 "github.com/sacloud/libsacloud/v2/sacloud/pointer" 24 "github.com/sacloud/libsacloud/v2/sacloud/testutil" 25 "github.com/sacloud/libsacloud/v2/sacloud/types" 26 "github.com/stretchr/testify/require" 27 ) 28 29 func TestLoadBalancerService_convertUpdateRequest(t *testing.T) { 30 caller := testutil.SingletonAPICaller() 31 ctx := context.Background() 32 name := testutil.ResourceName("load-balancer-service") 33 zone := testutil.TestZone() 34 35 // setup 36 swOp := sacloud.NewSwitchOp(caller) 37 sw, err := swOp.Create(ctx, zone, &sacloud.SwitchCreateRequest{Name: name}) 38 if err != nil { 39 t.Fatal(err) 40 } 41 42 current, err := New(caller).CreateWithContext(ctx, &CreateRequest{ 43 Zone: zone, 44 Name: name, 45 Description: "desc", 46 Tags: types.Tags{"tag1", "tag2"}, 47 SwitchID: sw.ID, 48 PlanID: types.LoadBalancerPlans.Standard, 49 VRID: 10, 50 IPAddresses: []string{"192.168.0.101", "192.168.0.102"}, 51 NetworkMaskLen: 24, 52 DefaultRoute: "192.168.0.1", 53 VirtualIPAddresses: []*sacloud.LoadBalancerVirtualIPAddress{ 54 { 55 VirtualIPAddress: "192.168.0.201", 56 Port: 80, 57 DelayLoop: 10, 58 SorryServer: "192.168.0.99", 59 Description: "desc", 60 Servers: []*sacloud.LoadBalancerServer{ 61 { 62 IPAddress: "192.168.0.202", 63 Port: 80, 64 Enabled: true, 65 HealthCheck: &sacloud.LoadBalancerServerHealthCheck{ 66 Protocol: types.LoadBalancerHealthCheckProtocols.HTTP, 67 Path: "/", 68 ResponseCode: 200, 69 }, 70 }, 71 { 72 IPAddress: "192.168.0.203", 73 Port: 80, 74 Enabled: true, 75 HealthCheck: &sacloud.LoadBalancerServerHealthCheck{ 76 Protocol: types.LoadBalancerHealthCheckProtocols.HTTP, 77 Path: "/", 78 ResponseCode: 200, 79 }, 80 }, 81 }, 82 }, 83 }, 84 }) 85 if err != nil { 86 t.Fatal(err) 87 } 88 89 defer func() { 90 lbOp := sacloud.NewLoadBalancerOp(caller) 91 lbOp.Shutdown(ctx, zone, current.ID, &sacloud.ShutdownOption{Force: true}) // nolint 92 wait.UntilLoadBalancerIsDown(ctx, lbOp, zone, current.ID) // nolint 93 lbOp.Delete(ctx, zone, current.ID) // nolint 94 swOp.Delete(ctx, zone, sw.ID) // nolint 95 }() 96 97 // test 98 cases := []struct { 99 in *UpdateRequest 100 expect *ApplyRequest 101 }{ 102 { 103 in: &UpdateRequest{ 104 Zone: zone, 105 ID: current.ID, 106 Name: pointer.NewString(name + "-upd"), 107 VirtualIPAddresses: &sacloud.LoadBalancerVirtualIPAddresses{ 108 { 109 VirtualIPAddress: "192.168.0.202", 110 Port: 80, 111 DelayLoop: 10, 112 SorryServer: "192.168.0.99", 113 Description: "desc", 114 Servers: []*sacloud.LoadBalancerServer{ 115 { 116 IPAddress: "192.168.0.202", 117 Port: 80, 118 Enabled: true, 119 HealthCheck: &sacloud.LoadBalancerServerHealthCheck{ 120 Protocol: types.LoadBalancerHealthCheckProtocols.HTTP, 121 Path: "/", 122 ResponseCode: 200, 123 }, 124 }, 125 { 126 IPAddress: "192.168.0.203", 127 Port: 80, 128 Enabled: true, 129 HealthCheck: &sacloud.LoadBalancerServerHealthCheck{ 130 Protocol: types.LoadBalancerHealthCheckProtocols.HTTP, 131 Path: "/", 132 ResponseCode: 200, 133 }, 134 }, 135 }, 136 }, 137 }, 138 SettingsHash: "aaaaa", 139 NoWait: true, 140 }, 141 expect: &ApplyRequest{ 142 ID: current.ID, 143 Zone: zone, 144 Name: name + "-upd", 145 Description: current.Description, 146 Tags: current.Tags, 147 IconID: current.IconID, 148 SwitchID: current.SwitchID, 149 PlanID: current.PlanID, 150 VRID: current.VRID, 151 IPAddresses: current.IPAddresses, 152 NetworkMaskLen: current.NetworkMaskLen, 153 DefaultRoute: current.DefaultRoute, 154 VirtualIPAddresses: sacloud.LoadBalancerVirtualIPAddresses{ 155 { 156 VirtualIPAddress: "192.168.0.202", 157 Port: 80, 158 DelayLoop: 10, 159 SorryServer: "192.168.0.99", 160 Description: "desc", 161 Servers: []*sacloud.LoadBalancerServer{ 162 { 163 IPAddress: "192.168.0.202", 164 Port: 80, 165 Enabled: true, 166 HealthCheck: &sacloud.LoadBalancerServerHealthCheck{ 167 Protocol: types.LoadBalancerHealthCheckProtocols.HTTP, 168 Path: "/", 169 ResponseCode: 200, 170 }, 171 }, 172 { 173 IPAddress: "192.168.0.203", 174 Port: 80, 175 Enabled: true, 176 HealthCheck: &sacloud.LoadBalancerServerHealthCheck{ 177 Protocol: types.LoadBalancerHealthCheckProtocols.HTTP, 178 Path: "/", 179 ResponseCode: 200, 180 }, 181 }, 182 }, 183 }, 184 }, 185 SettingsHash: "aaaaa", 186 NoWait: true, 187 }, 188 }, 189 } 190 191 for _, tc := range cases { 192 req, err := tc.in.ApplyRequest(ctx, caller) 193 require.NoError(t, err) 194 require.EqualValues(t, tc.expect, req) 195 } 196 }