github.com/intel/goresctrl@v0.5.0/pkg/blockio/oci_test.go (about) 1 // Copyright 2019-2021 Intel Corporation. All Rights Reserved. 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 blockio 16 17 import ( 18 "testing" 19 20 oci "github.com/opencontainers/runtime-spec/specs-go" 21 22 "github.com/intel/goresctrl/pkg/cgroups" 23 "github.com/intel/goresctrl/pkg/testutils" 24 ) 25 26 // TestOciLinuxBlockIO: unit tests for OciLinuxBlockIO(). 27 func TestOciLinuxBlockIO(t *testing.T) { 28 tcases := []struct { 29 name string 30 class string 31 blockIOClasses map[string]cgroups.BlockIOParameters 32 expectedBlockIO *oci.LinuxBlockIO 33 // It would be great to define expected 34 // oci.LinuxBlockIO with a single literal. But that 35 // is impossible because Major and Minor fields are 36 // inside non-exported oci.linuxBlockIODevice 37 // struct. Therefore here are expected triplets of 38 // major/minor/(weight|rate). 39 expectedWeight uint16 40 expectedWeightDevices [][3]uint16 41 expectedThrottleReadBpsDevices [][3]uint64 42 expectedThrottleWriteBpsDevices [][3]uint64 43 expectedThrottleReadIOPSDevices [][3]uint64 44 expectedThrottleWriteIOPSDevices [][3]uint64 45 expectedErrorSubstrings []string 46 }{ 47 { 48 name: "unknown class", 49 class: "foobar", 50 blockIOClasses: nil, 51 expectedErrorSubstrings: []string{"foobar"}, 52 }, 53 { 54 name: "all fields", 55 class: "allfields", 56 blockIOClasses: map[string]cgroups.BlockIOParameters{ 57 "allfields": cgroups.BlockIOParameters{ 58 Weight: 10, 59 WeightDevice: cgroups.DeviceWeights{ 60 {Major: 20, Minor: 21, Weight: 22}, 61 {Major: 23, Minor: 24, Weight: 25}, 62 }, 63 ThrottleReadBpsDevice: cgroups.DeviceRates{ 64 {Major: 30, Minor: 31, Rate: 32}, 65 {Major: 33, Minor: 34, Rate: 35}, 66 }, 67 ThrottleWriteBpsDevice: cgroups.DeviceRates{ 68 {Major: 40, Minor: 41, Rate: 42}, 69 {Major: 43, Minor: 44, Rate: 45}, 70 }, 71 ThrottleReadIOPSDevice: cgroups.DeviceRates{ 72 {Major: 50, Minor: 51, Rate: 52}, 73 {Major: 53, Minor: 54, Rate: 55}, 74 }, 75 ThrottleWriteIOPSDevice: cgroups.DeviceRates{ 76 {Major: 60, Minor: 61, Rate: 62}, 77 {Major: 63, Minor: 64, Rate: 65}, 78 }, 79 }, 80 }, 81 expectedWeight: 10, 82 expectedWeightDevices: [][3]uint16{{20, 21, 22}, {23, 24, 25}}, 83 expectedThrottleReadBpsDevices: [][3]uint64{{30, 31, 32}, {33, 34, 35}}, 84 expectedThrottleWriteBpsDevices: [][3]uint64{{40, 41, 42}, {43, 44, 45}}, 85 expectedThrottleReadIOPSDevices: [][3]uint64{{50, 51, 52}, {53, 54, 55}}, 86 expectedThrottleWriteIOPSDevices: [][3]uint64{{60, 61, 62}, {63, 64, 65}}, 87 expectedBlockIO: &oci.LinuxBlockIO{}, 88 }, 89 } 90 for _, tc := range tcases { 91 t.Run(tc.name, func(t *testing.T) { 92 classBlockIO = tc.blockIOClasses 93 gotBlockIO, gotError := OciLinuxBlockIO(tc.class) 94 expectedErrorCount := 0 95 if len(tc.expectedErrorSubstrings) > 0 { 96 expectedErrorCount = 1 97 } 98 testutils.VerifyError(t, gotError, expectedErrorCount, tc.expectedErrorSubstrings) 99 if tc.expectedBlockIO != nil { 100 tc.expectedBlockIO.Weight = &tc.expectedWeight 101 for _, wd := range tc.expectedWeightDevices { 102 tc.expectedBlockIO.WeightDevice = append(tc.expectedBlockIO.WeightDevice, linuxWeightDevice(wd)) 103 } 104 for _, rd := range tc.expectedThrottleReadBpsDevices { 105 tc.expectedBlockIO.ThrottleReadBpsDevice = append(tc.expectedBlockIO.ThrottleReadBpsDevice, linuxThrottleDevice(rd)) 106 } 107 for _, rd := range tc.expectedThrottleWriteBpsDevices { 108 tc.expectedBlockIO.ThrottleWriteBpsDevice = append(tc.expectedBlockIO.ThrottleWriteBpsDevice, linuxThrottleDevice(rd)) 109 } 110 for _, rd := range tc.expectedThrottleReadIOPSDevices { 111 tc.expectedBlockIO.ThrottleReadIOPSDevice = append(tc.expectedBlockIO.ThrottleReadIOPSDevice, linuxThrottleDevice(rd)) 112 } 113 for _, rd := range tc.expectedThrottleWriteIOPSDevices { 114 tc.expectedBlockIO.ThrottleWriteIOPSDevice = append(tc.expectedBlockIO.ThrottleWriteIOPSDevice, linuxThrottleDevice(rd)) 115 } 116 } 117 testutils.VerifyDeepEqual(t, "OCI BlockIO", tc.expectedBlockIO, gotBlockIO) 118 }) 119 } 120 } 121 122 func linuxWeightDevice(triplet [3]uint16) oci.LinuxWeightDevice { 123 wd := oci.LinuxWeightDevice{} 124 wd.Major = int64(triplet[0]) 125 wd.Minor = int64(triplet[1]) 126 wd.Weight = &triplet[2] 127 return wd 128 } 129 130 func linuxThrottleDevice(triplet [3]uint64) oci.LinuxThrottleDevice { 131 rd := oci.LinuxThrottleDevice{} 132 rd.Major = int64(triplet[0]) 133 rd.Minor = int64(triplet[1]) 134 rd.Rate = triplet[2] 135 return rd 136 }