|
| 1 | +// This file is part of MinIO DirectPV |
| 2 | +// Copyright (c) 2023 MinIO, Inc. |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package device |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types" |
| 23 | + "github.com/minio/directpv/pkg/types" |
| 24 | +) |
| 25 | + |
| 26 | +func newDrive(name string, totalCapacity int64, make, volume string) *types.Drive { |
| 27 | + drive := types.NewDrive( |
| 28 | + directpvtypes.DriveID(name+"-id"), |
| 29 | + types.DriveStatus{ |
| 30 | + TotalCapacity: totalCapacity, |
| 31 | + Make: make, |
| 32 | + }, |
| 33 | + directpvtypes.NodeID("nodeId"), |
| 34 | + directpvtypes.DriveName(name), |
| 35 | + directpvtypes.AccessTierDefault, |
| 36 | + ) |
| 37 | + drive.AddVolumeFinalizer(volume) |
| 38 | + return drive |
| 39 | +} |
| 40 | + |
| 41 | +func newTestDevice(name string, totalCapacity int64, dmname string) device { |
| 42 | + return device{ |
| 43 | + TotalCapacity: totalCapacity, |
| 44 | + Device: Device{ |
| 45 | + Name: name, |
| 46 | + DMName: dmname, |
| 47 | + }, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestSyncDrive(t *testing.T) { |
| 52 | + testCases := []struct { |
| 53 | + drive *types.Drive |
| 54 | + device device |
| 55 | + updated bool |
| 56 | + expectedDriveName directpvtypes.DriveName |
| 57 | + expectedDriveCapacity int64 |
| 58 | + expectedMake string |
| 59 | + }{ |
| 60 | + { |
| 61 | + drive: newDrive("sda", 100, "dmname", "volume-1"), |
| 62 | + device: newTestDevice("sda", 100, "dmname"), |
| 63 | + updated: false, |
| 64 | + expectedDriveName: "sda", |
| 65 | + expectedDriveCapacity: 100, |
| 66 | + expectedMake: "dmname", |
| 67 | + }, |
| 68 | + { |
| 69 | + drive: newDrive("sda", 100, "dmname", "volume-1"), |
| 70 | + device: newTestDevice("sda", 200, "dmname"), |
| 71 | + updated: true, |
| 72 | + expectedDriveName: "sda", |
| 73 | + expectedDriveCapacity: 200, |
| 74 | + expectedMake: "dmname", |
| 75 | + }, |
| 76 | + { |
| 77 | + drive: newDrive("sda", 100, "dmname", "volume-1"), |
| 78 | + device: newTestDevice("sda", 100, "dmname-new"), |
| 79 | + updated: true, |
| 80 | + expectedDriveName: "sda", |
| 81 | + expectedDriveCapacity: 100, |
| 82 | + expectedMake: "dmname-new", |
| 83 | + }, |
| 84 | + { |
| 85 | + drive: newDrive("sda", 100, "dmname", "volume-1"), |
| 86 | + device: newTestDevice("sdb", 100, "dmname"), |
| 87 | + updated: true, |
| 88 | + expectedDriveName: "sdb", |
| 89 | + expectedDriveCapacity: 100, |
| 90 | + expectedMake: "dmname", |
| 91 | + }, |
| 92 | + { |
| 93 | + drive: newDrive("sda", 100, "dmname", "volume-1"), |
| 94 | + device: newTestDevice("sda", 100, "dmname"), |
| 95 | + updated: false, |
| 96 | + expectedDriveName: "sda", |
| 97 | + expectedDriveCapacity: 100, |
| 98 | + expectedMake: "dmname", |
| 99 | + }, |
| 100 | + } |
| 101 | + |
| 102 | + for _, testCase := range testCases { |
| 103 | + updated := syncDrive(testCase.drive, testCase.device) |
| 104 | + if updated != testCase.updated { |
| 105 | + t.Errorf("expected updated value: %v; but got %v", testCase.updated, updated) |
| 106 | + } |
| 107 | + if testCase.drive.GetDriveName() != testCase.expectedDriveName { |
| 108 | + t.Errorf("expected drive name: %v; but got %v", testCase.expectedDriveName, testCase.drive.GetDriveName()) |
| 109 | + } |
| 110 | + if testCase.drive.Status.TotalCapacity != testCase.expectedDriveCapacity { |
| 111 | + t.Errorf("expected drive capacity: %v; but got %v", testCase.expectedDriveCapacity, testCase.drive.Status.TotalCapacity) |
| 112 | + } |
| 113 | + if testCase.drive.Status.Make != testCase.expectedMake { |
| 114 | + t.Errorf("expected drive make: %v; but got %v", testCase.expectedMake, testCase.drive.Status.Make) |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments