github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/qrm-plugins/io/handlers/iocost/utils_test.go (about) 1 //go:build linux 2 // +build linux 3 4 /* 5 Copyright 2022 The Katalyst Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 */ 19 20 package iocost 21 22 import ( 23 "fmt" 24 "io/ioutil" 25 "os" 26 "testing" 27 28 "github.com/stretchr/testify/assert" 29 30 "github.com/kubewharf/katalyst-core/pkg/util/general" 31 ) 32 33 func TestGetDevicesIdToModel(t *testing.T) { 34 t.Parallel() 35 36 allDeviceNames, _ := getAllDeviceNames() 37 38 type args struct { 39 deviceNames []string 40 } 41 tests := []struct { 42 name string 43 args args 44 wantErr bool 45 }{ 46 { 47 name: "test getDevicesIdToModel with allDeviceNames", 48 args: args{ 49 deviceNames: allDeviceNames, 50 }, 51 wantErr: false, 52 }, 53 { 54 name: "test getDevicesIdToModel with fake device names", 55 args: args{ 56 deviceNames: []string{"fake"}, 57 }, 58 wantErr: false, 59 }, 60 } 61 for _, tt := range tests { 62 tt := tt 63 t.Run(tt.name, func(t *testing.T) { 64 t.Parallel() 65 _, err := getDevicesIdToModel(tt.args.deviceNames) 66 if (err != nil) != tt.wantErr { 67 t.Errorf("getDevicesIdToModel() error = %v, wantErr %v", err, tt.wantErr) 68 return 69 } 70 }) 71 } 72 } 73 74 func TestLoadJsonConfig(t *testing.T) { 75 t.Parallel() 76 77 type args struct { 78 configAbsPath string 79 configObject interface{} 80 } 81 tests := []struct { 82 name string 83 args args 84 wantErr bool 85 }{ 86 { 87 name: "test LoadJsonConfig", 88 args: args{ 89 configAbsPath: "fakePath", 90 }, 91 wantErr: true, 92 }, 93 } 94 for _, tt := range tests { 95 tt := tt 96 t.Run(tt.name, func(t *testing.T) { 97 t.Parallel() 98 if err := general.LoadJsonConfig(tt.args.configAbsPath, tt.args.configObject); (err != nil) != tt.wantErr { 99 t.Errorf("LoadJsonConfig() error = %v, wantErr %v", err, tt.wantErr) 100 } 101 }) 102 } 103 } 104 105 func TestGetDeviceNameFromID(t *testing.T) { 106 t.Parallel() 107 108 targetDevID := "1234" 109 _, found, err := getDeviceNameFromID(targetDevID) 110 111 assert.NoError(t, err) 112 assert.False(t, found) 113 } 114 115 func TestGetDeviceType(t *testing.T) { 116 t.Parallel() 117 118 testCases := []struct { 119 deviceName string 120 expectedType DeviceType 121 expectedErr error 122 fileContents string 123 rotationalFile string 124 }{ 125 // Test case where device name starts with "sd" and rotational is 1 126 {"sda", HDD, nil, "1\n", ""}, 127 // Test case where device name starts with "sd" and rotational is 0 128 {"sdb", SSD, nil, "0\n", ""}, 129 // Test case where device name doesn't start with "sd" 130 {"nvme0n1", Unknown, fmt.Errorf("not scsi disk"), "", ""}, 131 // Test case where rotational file is not found 132 {"sdc", Unknown, nil, "", "nonexistentfile"}, 133 } 134 135 for _, tc := range testCases { 136 tempFile, err := ioutil.TempFile("", "rotational") 137 if err != nil { 138 t.Fatal(err) 139 } 140 defer os.Remove(tempFile.Name()) 141 142 _, err = tempFile.WriteString(tc.fileContents) 143 if err != nil { 144 t.Fatal(err) 145 } 146 tempFile.Close() 147 148 deviceType, actualErr := getDeviceType(tc.deviceName, tempFile.Name()) 149 if deviceType != tc.expectedType { 150 t.Errorf("Test case failed for deviceName=%s. Got HDD=%v, Err=%v. Expected HDD=%v, Err=%v", 151 tc.deviceName, deviceType, actualErr, tc.expectedType, tc.expectedErr) 152 } else if actualErr == nil { 153 _, err := getDeviceType(tc.deviceName, "notExit") 154 assert.NoError(t, err) 155 _, err = getDeviceType(tc.deviceName, "/tmp/") 156 assert.Error(t, err) 157 } 158 } 159 }