github.com/sacloud/libsacloud/v2@v2.32.3/helper/service/vpcrouter/update_standard_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 vpcrouter
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/sacloud/libsacloud/v2/sacloud"
    22  	"github.com/sacloud/libsacloud/v2/sacloud/pointer"
    23  	"github.com/sacloud/libsacloud/v2/sacloud/testutil"
    24  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func TestVPCRouterService_convertUpdateStandardRequest(t *testing.T) {
    29  	ctx := context.Background()
    30  	caller := testutil.SingletonAPICaller()
    31  	zone := testutil.TestZone()
    32  	name := testutil.ResourceName("vpc-router-service-update")
    33  
    34  	// setup
    35  	swOp := sacloud.NewSwitchOp(caller)
    36  	additionalSwitch, err := swOp.Create(ctx, zone, &sacloud.SwitchCreateRequest{Name: name})
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  
    41  	vpcRouter, err := New(caller).CreateStandardWithContext(ctx, &CreateStandardRequest{
    42  		Zone:        zone,
    43  		Name:        name,
    44  		Description: "desc",
    45  		Tags:        types.Tags{"tag1", "tag2"},
    46  		AdditionalNICSettings: []*AdditionalStandardNICSetting{
    47  			{
    48  				SwitchID:       additionalSwitch.ID,
    49  				IPAddress:      "192.168.0.101",
    50  				NetworkMaskLen: 24,
    51  				Index:          1,
    52  			},
    53  		},
    54  		RouterSetting: &RouterSetting{
    55  			VRID:                      1,
    56  			InternetConnectionEnabled: true,
    57  			L2TPIPsecServer: &sacloud.VPCRouterL2TPIPsecServer{
    58  				RangeStart:      "192.168.0.250",
    59  				RangeStop:       "192.168.0.254",
    60  				PreSharedSecret: "presharedsecret",
    61  			},
    62  			RemoteAccessUsers: []*sacloud.VPCRouterRemoteAccessUser{
    63  				{
    64  					UserName: "username",
    65  					Password: "password",
    66  				},
    67  			},
    68  		},
    69  		NoWait: false,
    70  	})
    71  
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  
    76  	defer func() {
    77  		sacloud.NewVPCRouterOp(caller).Delete(ctx, zone, vpcRouter.ID) // nolint
    78  		swOp.Delete(ctx, zone, additionalSwitch.ID)                    // nolint
    79  	}()
    80  
    81  	// test
    82  	cases := []struct {
    83  		in     *UpdateStandardRequest
    84  		expect *ApplyRequest
    85  	}{
    86  		{
    87  			in: &UpdateStandardRequest{
    88  				ID:     vpcRouter.ID,
    89  				Zone:   zone,
    90  				Name:   pointer.NewString(name + "-upd"),
    91  				NoWait: true,
    92  			},
    93  			expect: &ApplyRequest{
    94  				ID:          vpcRouter.ID,
    95  				Zone:        zone,
    96  				Name:        name + "-upd",
    97  				Description: "desc",
    98  				Tags:        types.Tags{"tag1", "tag2"},
    99  				PlanID:      types.VPCRouterPlans.Standard,
   100  				NICSetting:  &StandardNICSetting{},
   101  				AdditionalNICSettings: []AdditionalNICSettingHolder{
   102  					&AdditionalStandardNICSetting{
   103  						SwitchID:       additionalSwitch.ID,
   104  						IPAddress:      "192.168.0.101",
   105  						NetworkMaskLen: 24,
   106  						Index:          1,
   107  					},
   108  				},
   109  				RouterSetting: &RouterSetting{
   110  					VRID:                      1,
   111  					InternetConnectionEnabled: true,
   112  					L2TPIPsecServer: &sacloud.VPCRouterL2TPIPsecServer{
   113  						RangeStart:      "192.168.0.250",
   114  						RangeStop:       "192.168.0.254",
   115  						PreSharedSecret: "presharedsecret",
   116  					},
   117  					RemoteAccessUsers: []*sacloud.VPCRouterRemoteAccessUser{
   118  						{
   119  							UserName: "username",
   120  							Password: "password",
   121  						},
   122  					},
   123  				},
   124  				NoWait: true,
   125  			},
   126  		},
   127  	}
   128  
   129  	for _, tc := range cases {
   130  		req, err := tc.in.ApplyRequest(ctx, caller)
   131  		require.NoError(t, err)
   132  		require.EqualValues(t, tc.expect, req)
   133  	}
   134  }