github.com/sacloud/libsacloud/v2@v2.32.3/helper/service/disk/apply_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  	"testing"
    19  
    20  	"github.com/sacloud/libsacloud/v2/sacloud/ostype"
    21  
    22  	diskBuilder "github.com/sacloud/libsacloud/v2/helper/builder/disk"
    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 TestDiskService_convertApplyRequest(t *testing.T) {
    29  	caller := testutil.SingletonAPICaller()
    30  
    31  	cases := []struct {
    32  		in     *ApplyRequest
    33  		expect diskBuilder.Builder
    34  	}{
    35  		// blank
    36  		{
    37  			in: &ApplyRequest{
    38  				Zone:          "is1a",
    39  				Name:          "test",
    40  				Description:   "description",
    41  				Tags:          types.Tags{"tag1", "tag2"},
    42  				IconID:        types.ID(1),
    43  				ServerID:      types.ID(2),
    44  				DiskPlanID:    types.DiskPlans.SSD,
    45  				Connection:    types.DiskConnections.VirtIO,
    46  				SizeGB:        20,
    47  				DistantFrom:   nil,
    48  				OSType:        0,
    49  				EditParameter: nil,
    50  				NoWait:        true,
    51  			},
    52  			expect: &diskBuilder.BlankBuilder{
    53  				Name:        "test",
    54  				Description: "description",
    55  				Tags:        types.Tags{"tag1", "tag2"},
    56  				IconID:      types.ID(1),
    57  				SizeGB:      20,
    58  				PlanID:      types.DiskPlans.SSD,
    59  				Connection:  types.DiskConnections.VirtIO,
    60  				Client:      diskBuilder.NewBuildersAPIClient(caller),
    61  				NoWait:      true,
    62  			},
    63  		},
    64  		// linux
    65  		{
    66  			in: &ApplyRequest{
    67  				Zone:       "is1a",
    68  				Name:       "test",
    69  				DiskPlanID: types.DiskPlans.SSD,
    70  				Connection: types.DiskConnections.VirtIO,
    71  				SizeGB:     20,
    72  				OSType:     ostype.Ubuntu,
    73  				EditParameter: &EditParameter{
    74  					HostName: "hostname",
    75  					Password: "password",
    76  				},
    77  				NoWait: true,
    78  			},
    79  			expect: &diskBuilder.FromUnixBuilder{
    80  				OSType:     ostype.Ubuntu,
    81  				Name:       "test",
    82  				SizeGB:     20,
    83  				PlanID:     types.DiskPlans.SSD,
    84  				Connection: types.DiskConnections.VirtIO,
    85  				EditParameter: &diskBuilder.UnixEditRequest{
    86  					HostName: "hostname",
    87  					Password: "password",
    88  				},
    89  				Client: diskBuilder.NewBuildersAPIClient(caller),
    90  				NoWait: true,
    91  				ID:     0,
    92  			},
    93  		},
    94  		// source disk
    95  		{
    96  			in: &ApplyRequest{
    97  				Zone:         "is1a",
    98  				Name:         "test",
    99  				DiskPlanID:   types.DiskPlans.SSD,
   100  				Connection:   types.DiskConnections.VirtIO,
   101  				SourceDiskID: types.ID(1),
   102  				SizeGB:       20,
   103  				EditParameter: &EditParameter{
   104  					HostName: "hostname",
   105  					Password: "password",
   106  				},
   107  				NoWait: true,
   108  			},
   109  			expect: &diskBuilder.FromDiskOrArchiveBuilder{
   110  				Name:         "test",
   111  				SizeGB:       20,
   112  				PlanID:       types.DiskPlans.SSD,
   113  				Connection:   types.DiskConnections.VirtIO,
   114  				SourceDiskID: types.ID(1),
   115  				EditParameter: &diskBuilder.UnixEditRequest{
   116  					HostName: "hostname",
   117  					Password: "password",
   118  				},
   119  				Client: diskBuilder.NewBuildersAPIClient(caller),
   120  				NoWait: true,
   121  				ID:     0,
   122  			},
   123  		},
   124  	}
   125  
   126  	for _, tc := range cases {
   127  		builder, err := tc.in.Builder(caller)
   128  		require.NoError(t, err)
   129  		require.EqualValues(t, tc.expect, builder)
   130  	}
   131  }