github.com/sacloud/iaas-api-go@v1.12.0/customize_server_test.go (about) 1 // Copyright 2022-2023 The sacloud/iaas-api-go 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 iaas 16 17 import ( 18 "testing" 19 20 "github.com/sacloud/iaas-api-go/types" 21 "github.com/sacloud/packages-go/size" 22 "github.com/stretchr/testify/require" 23 ) 24 25 func TestServer_BandWidthAt(t *testing.T) { 26 cases := []struct { 27 msg string 28 in *Server 29 index int 30 expect int 31 }{ 32 { 33 msg: "no NICs", 34 in: &Server{}, 35 expect: -1, 36 }, 37 { 38 msg: "invalid NIC index", 39 in: &Server{ 40 Interfaces: []*InterfaceView{ 41 { 42 UpstreamType: types.UpstreamNetworkTypes.Unknown, 43 }, 44 }, 45 }, 46 index: 1, 47 expect: -1, 48 }, 49 { 50 msg: "unknown upstream type", 51 in: &Server{ 52 Interfaces: []*InterfaceView{ 53 { 54 UpstreamType: types.UpstreamNetworkTypes.Unknown, 55 }, 56 }, 57 }, 58 expect: -1, 59 }, 60 { 61 msg: "shared", 62 in: &Server{ 63 Interfaces: []*InterfaceView{ 64 { 65 UpstreamType: types.UpstreamNetworkTypes.Shared, 66 }, 67 }, 68 }, 69 expect: 100, 70 }, 71 { 72 msg: "switch with private host", 73 in: &Server{ 74 Interfaces: []*InterfaceView{ 75 { 76 UpstreamType: types.UpstreamNetworkTypes.Switch, 77 }, 78 }, 79 PrivateHostID: 1, 80 }, 81 expect: 0, 82 }, 83 { 84 msg: "memory less than 32GB", 85 in: &Server{ 86 Interfaces: []*InterfaceView{ 87 { 88 UpstreamType: types.UpstreamNetworkTypes.Switch, 89 }, 90 }, 91 MemoryMB: 31 * size.GiB, 92 }, 93 expect: 1000, 94 }, 95 { 96 msg: "switch with memory 32GB", 97 in: &Server{ 98 Interfaces: []*InterfaceView{ 99 { 100 UpstreamType: types.UpstreamNetworkTypes.Switch, 101 }, 102 }, 103 MemoryMB: 32 * size.GiB, 104 }, 105 expect: 2000, 106 }, 107 { 108 msg: "memory less than 128GB", 109 in: &Server{ 110 Interfaces: []*InterfaceView{ 111 { 112 UpstreamType: types.UpstreamNetworkTypes.Switch, 113 }, 114 }, 115 MemoryMB: 127 * size.GiB, 116 }, 117 expect: 2000, 118 }, 119 { 120 msg: "switch with memory 128GB", 121 in: &Server{ 122 Interfaces: []*InterfaceView{ 123 { 124 UpstreamType: types.UpstreamNetworkTypes.Switch, 125 }, 126 }, 127 MemoryMB: 128 * size.GiB, 128 }, 129 expect: 5000, 130 }, 131 { 132 msg: "memory less than 224GB", 133 in: &Server{ 134 Interfaces: []*InterfaceView{ 135 { 136 UpstreamType: types.UpstreamNetworkTypes.Switch, 137 }, 138 }, 139 MemoryMB: 223 * size.GiB, 140 }, 141 expect: 5000, 142 }, 143 { 144 msg: "switch with memory 224GB", 145 in: &Server{ 146 Interfaces: []*InterfaceView{ 147 { 148 UpstreamType: types.UpstreamNetworkTypes.Switch, 149 }, 150 }, 151 MemoryMB: 224 * size.GiB, 152 }, 153 expect: 10000, 154 }, 155 } 156 157 for _, tc := range cases { 158 actual := tc.in.BandWidthAt(tc.index) 159 require.Equal(t, tc.expect, actual, tc.msg) 160 } 161 }