github.com/sacloud/libsacloud/v2@v2.32.3/helper/builder/disk/builder_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 "errors" 20 "fmt" 21 "testing" 22 23 "github.com/sacloud/libsacloud/v2/pkg/size" 24 "github.com/sacloud/libsacloud/v2/sacloud" 25 "github.com/sacloud/libsacloud/v2/sacloud/ostype" 26 "github.com/sacloud/libsacloud/v2/sacloud/types" 27 "github.com/stretchr/testify/require" 28 ) 29 30 func TestDiskFromUnixRequest_Validate(t *testing.T) { 31 cases := []struct { 32 msg string 33 in *FromUnixBuilder 34 err error 35 }{ 36 { 37 msg: "invalid ostype", 38 in: &FromUnixBuilder{ 39 OSType: ostype.ArchiveOSType(-1), 40 }, 41 err: fmt.Errorf("invalid OSType: %s", ostype.ArchiveOSType(-1)), 42 }, 43 { 44 msg: "size not found", 45 in: &FromUnixBuilder{ 46 OSType: ostype.CentOS, 47 PlanID: types.DiskPlans.SSD, 48 SizeGB: 1, 49 Client: &APIClient{ 50 DiskPlan: &dummyDiskPlanReader{ 51 diskPlan: &sacloud.DiskPlan{ 52 ID: types.DiskPlans.SSD, 53 Name: "SSDプラン", 54 Size: []*sacloud.DiskPlanSizeInfo{ 55 { 56 Availability: types.Availabilities.Available, 57 SizeMB: 0, 58 }, 59 }, 60 }, 61 }, 62 }, 63 }, 64 err: fmt.Errorf("disk plan[SSDプラン:1GB] is not found"), 65 }, 66 { 67 msg: "invalid disk edit parameter", 68 in: &FromUnixBuilder{ 69 OSType: ostype.CentOS, 70 PlanID: types.DiskPlans.SSD, 71 SizeGB: 1, 72 EditParameter: &UnixEditRequest{ 73 Notes: []*sacloud.DiskEditNote{ 74 {ID: 1}, 75 }, 76 }, 77 Client: &APIClient{ 78 DiskPlan: &dummyDiskPlanReader{ 79 diskPlan: &sacloud.DiskPlan{ 80 ID: types.DiskPlans.SSD, 81 Name: "SSDプラン", 82 Size: []*sacloud.DiskPlanSizeInfo{ 83 { 84 Availability: types.Availabilities.Available, 85 SizeMB: 1 * size.GiB, 86 }, 87 }, 88 }, 89 }, 90 Note: &dummyNoteHandler{ 91 err: errors.New("dummy"), 92 }, 93 }, 94 }, 95 err: errors.New("dummy"), 96 }, 97 } 98 99 for _, tc := range cases { 100 err := tc.in.Validate(context.Background(), "tk1v") 101 require.Equal(t, tc.err, err) 102 } 103 }