github.com/sacloud/libsacloud/v2@v2.32.3/helper/service/mobilegateway/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 mobilegateway
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	mobileGatewayBuilder "github.com/sacloud/libsacloud/v2/helper/builder/mobilegateway"
    22  	"github.com/sacloud/libsacloud/v2/helper/cleanup"
    23  	"github.com/sacloud/libsacloud/v2/sacloud"
    24  	"github.com/sacloud/libsacloud/v2/sacloud/pointer"
    25  	"github.com/sacloud/libsacloud/v2/sacloud/testutil"
    26  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func TestMobileGatewayService_validate(t *testing.T) {
    31  	cases := []struct {
    32  		in          *UpdateRequest
    33  		errorExists bool // 有無だけチェック
    34  	}{
    35  		{
    36  			in:          &UpdateRequest{},
    37  			errorExists: true,
    38  		},
    39  		{
    40  			in: &UpdateRequest{
    41  				Zone: "tk1a",
    42  				ID:   1,
    43  			},
    44  			errorExists: false,
    45  		},
    46  		{
    47  			in: &UpdateRequest{
    48  				Zone: "tk1a",
    49  				ID:   1,
    50  				DNS: &DNSSettingUpdate{
    51  					DNS1: pointer.NewString("8.8.8.8"),
    52  					DNS2: nil,
    53  				},
    54  			},
    55  			errorExists: true,
    56  		},
    57  		{
    58  			in: &UpdateRequest{
    59  				Zone: "tk1a",
    60  				ID:   1,
    61  				DNS: &DNSSettingUpdate{
    62  					DNS1: pointer.NewString("8.8.8.8"),
    63  					DNS2: pointer.NewString("8.8.4.4"),
    64  				},
    65  			},
    66  			errorExists: false,
    67  		},
    68  	}
    69  
    70  	for _, tc := range cases {
    71  		err := tc.in.Validate()
    72  		require.EqualValues(t, tc.errorExists, err != nil, "in: %#+v error: %s", tc.in, err)
    73  	}
    74  }
    75  
    76  func TestMobileGatewayService_convertUpdateRequest(t *testing.T) {
    77  	ctx := context.Background()
    78  	name := testutil.ResourceName("mobile-gateway-service-create")
    79  	zone := testutil.TestZone()
    80  	caller := testutil.SingletonAPICaller()
    81  
    82  	// setup
    83  	swOp := sacloud.NewSwitchOp(caller)
    84  	sw, err := swOp.Create(ctx, zone, &sacloud.SwitchCreateRequest{Name: name})
    85  	if err != nil {
    86  		t.Fatal(err)
    87  	}
    88  
    89  	builder := &mobileGatewayBuilder.Builder{
    90  		Name:                            name,
    91  		Description:                     "description",
    92  		Tags:                            types.Tags{"tag1", "tag2"},
    93  		InternetConnectionEnabled:       true,
    94  		InterDeviceCommunicationEnabled: true,
    95  		Client:                          mobileGatewayBuilder.NewAPIClient(caller),
    96  		TrafficConfig: &sacloud.MobileGatewayTrafficControl{
    97  			TrafficQuotaInMB: 1,
    98  		},
    99  	}
   100  	mgw, err := builder.Build(ctx, zone)
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  
   105  	defer func() {
   106  		cleanup.DeleteMobileGateway(ctx, sacloud.NewMobileGatewayOp(caller), sacloud.NewSIMOp(caller), zone, mgw.ID) // nolint
   107  		swOp.Delete(ctx, zone, sw.ID)                                                                                // nolint
   108  	}()
   109  
   110  	// test
   111  	cases := []struct {
   112  		in     *UpdateRequest
   113  		expect *ApplyRequest
   114  	}{
   115  		{
   116  			in: &UpdateRequest{
   117  				ID:   mgw.ID,
   118  				Zone: zone,
   119  				Name: pointer.NewString(name + "-upd"),
   120  				PrivateInterface: &PrivateInterfaceSettingUpdate{
   121  					SwitchID:       &sw.ID,
   122  					IPAddress:      pointer.NewString("192.168.0.1"),
   123  					NetworkMaskLen: pointer.NewInt(24),
   124  				},
   125  				StaticRoutes: &[]*sacloud.MobileGatewayStaticRoute{
   126  					{
   127  						Prefix:  "192.168.1.0/24",
   128  						NextHop: "192.168.0.2",
   129  					},
   130  				},
   131  				InternetConnectionEnabled:       pointer.NewBool(false),
   132  				InterDeviceCommunicationEnabled: pointer.NewBool(false),
   133  				DNS: &DNSSettingUpdate{
   134  					DNS1: pointer.NewString("8.8.8.8"),
   135  					DNS2: pointer.NewString("8.8.4.4"),
   136  				},
   137  				SIMs: nil,
   138  				TrafficConfig: &TrafficConfigUpdate{
   139  					BandWidthLimitInKbps: pointer.NewInt(128),
   140  					EmailNotifyEnabled:   pointer.NewBool(true),
   141  					AutoTrafficShaping:   pointer.NewBool(true),
   142  				},
   143  				NoWait: true,
   144  			},
   145  			expect: &ApplyRequest{
   146  				ID:          mgw.ID,
   147  				Zone:        zone,
   148  				Name:        name + "-upd",
   149  				Description: "description",
   150  				Tags:        types.Tags{"tag1", "tag2"},
   151  				PrivateInterface: &PrivateInterfaceSetting{
   152  					SwitchID:       sw.ID,
   153  					IPAddress:      "192.168.0.1",
   154  					NetworkMaskLen: 24,
   155  				},
   156  				StaticRoutes: []*sacloud.MobileGatewayStaticRoute{
   157  					{
   158  						Prefix:  "192.168.1.0/24",
   159  						NextHop: "192.168.0.2",
   160  					},
   161  				},
   162  				InternetConnectionEnabled:       false,
   163  				InterDeviceCommunicationEnabled: false,
   164  				DNS: &DNSSetting{
   165  					DNS1: "8.8.8.8",
   166  					DNS2: "8.8.4.4",
   167  				},
   168  				SIMs: nil,
   169  				TrafficConfig: &TrafficConfig{
   170  					TrafficQuotaInMB:     1,
   171  					BandWidthLimitInKbps: 128,
   172  					EmailNotifyEnabled:   true,
   173  					AutoTrafficShaping:   true,
   174  				},
   175  				NoWait: true,
   176  			},
   177  		},
   178  	}
   179  
   180  	for _, tc := range cases {
   181  		req, err := tc.in.ApplyRequest(ctx, caller)
   182  		require.NoError(t, err)
   183  		require.EqualValues(t, tc.expect, req)
   184  	}
   185  }