github.com/sacloud/libsacloud/v2@v2.32.3/helper/service/disk/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 disk
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/sacloud/libsacloud/v2/helper/wait"
    22  
    23  	"github.com/sacloud/libsacloud/v2/pkg/size"
    24  	"github.com/sacloud/libsacloud/v2/sacloud"
    25  	"github.com/sacloud/libsacloud/v2/sacloud/testutil"
    26  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    27  	"github.com/stretchr/testify/require"
    28  
    29  	"github.com/sacloud/libsacloud/v2/sacloud/pointer"
    30  )
    31  
    32  func TestDiskUpdateRequest_Update(t *testing.T) {
    33  	v := &UpdateRequest{
    34  		Zone:        "tk1a",
    35  		ID:          1,
    36  		Name:        pointer.NewString(""),
    37  		Description: pointer.NewString(""),
    38  	}
    39  	err := v.Validate()
    40  	if err == nil {
    41  		t.Fatalf("no error with: %#v", v)
    42  	}
    43  }
    44  
    45  func TestDiskService_convertUpdateRequest(t *testing.T) {
    46  	ctx := context.Background()
    47  	caller := testutil.SingletonAPICaller()
    48  	name := testutil.ResourceName("disk-service-update")
    49  	zone := testutil.TestZone()
    50  
    51  	diskOp := sacloud.NewDiskOp(caller)
    52  	disk, err := diskOp.Create(context.Background(), zone, &sacloud.DiskCreateRequest{
    53  		DiskPlanID: types.DiskPlans.SSD,
    54  		Connection: types.DiskConnections.VirtIO,
    55  		SizeMB:     20 * size.GiB,
    56  		Name:       name,
    57  	}, []types.ID{})
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	v, err := wait.UntilDiskIsReady(ctx, diskOp, zone, disk.ID)
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	disk = v
    66  
    67  	defer func() {
    68  		diskOp.Delete(context.Background(), zone, disk.ID) // nolint
    69  	}()
    70  
    71  	var cases = []struct {
    72  		in     *UpdateRequest
    73  		expect *ApplyRequest
    74  	}{
    75  		{
    76  			in: &UpdateRequest{
    77  				Zone: zone,
    78  				ID:   disk.ID,
    79  				Name: pointer.NewString(name + "-upd"),
    80  				EditParameter: &EditParameter{
    81  					HostName: "hostname",
    82  					Password: "password",
    83  				},
    84  				NoWait: false,
    85  			},
    86  			expect: &ApplyRequest{
    87  				Zone:            zone,
    88  				ID:              disk.ID,
    89  				Name:            name + "-upd",
    90  				Description:     disk.Description,
    91  				Tags:            disk.Tags,
    92  				IconID:          disk.IconID,
    93  				DiskPlanID:      disk.DiskPlanID,
    94  				Connection:      disk.Connection,
    95  				SourceDiskID:    disk.SourceDiskID,
    96  				SourceArchiveID: disk.SourceArchiveID,
    97  				ServerID:        disk.ServerID,
    98  				SizeGB:          disk.GetSizeGB(),
    99  				EditParameter: &EditParameter{
   100  					HostName: "hostname",
   101  					Password: "password",
   102  				},
   103  				NoWait: false,
   104  			},
   105  		},
   106  	}
   107  	for _, tc := range cases {
   108  		req, err := tc.in.ApplyRequest(context.Background(), caller)
   109  		require.NoError(t, err)
   110  		require.EqualValues(t, tc.expect, req)
   111  	}
   112  }