|
| 1 | +/* |
| 2 | + * |
| 3 | + * MIT License |
| 4 | + * |
| 5 | + * (C) Copyright 2023-2024 Hewlett Packard Enterprise Development LP |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | + * copy of this software and associated documentation files (the "Software"), |
| 9 | + * to deal in the Software without restriction, including without limitation |
| 10 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 12 | + * Software is furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included |
| 15 | + * in all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 21 | + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 22 | + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 | + * OTHER DEALINGS IN THE SOFTWARE. |
| 24 | + * |
| 25 | + */ |
| 26 | +package export |
| 27 | + |
| 28 | +import ( |
| 29 | + "testing" |
| 30 | + |
| 31 | + "github.com/Cray-HPE/cani/pkg/devicetypes" |
| 32 | + nautobotapi "github.com/Cray-HPE/cani/pkg/nautobot" |
| 33 | +) |
| 34 | + |
| 35 | +func TestComparePosition(t *testing.T) { |
| 36 | + pos42 := 42 |
| 37 | + pos10 := 10 |
| 38 | + |
| 39 | + tests := []struct { |
| 40 | + name string |
| 41 | + device *devicetypes.CaniDeviceType |
| 42 | + remote *nautobotapi.Device |
| 43 | + wantDiff bool |
| 44 | + }{ |
| 45 | + { |
| 46 | + name: "positions differ returns diff", |
| 47 | + device: &devicetypes.CaniDeviceType{RackPosition: 42}, |
| 48 | + remote: &nautobotapi.Device{Position: &pos10}, |
| 49 | + wantDiff: true, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "positions match returns no diff", |
| 53 | + device: &devicetypes.CaniDeviceType{RackPosition: 42}, |
| 54 | + remote: &nautobotapi.Device{Position: &pos42}, |
| 55 | + wantDiff: false, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "local position zero skips comparison", |
| 59 | + device: &devicetypes.CaniDeviceType{RackPosition: 0}, |
| 60 | + remote: &nautobotapi.Device{Position: &pos10}, |
| 61 | + wantDiff: false, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "remote position nil returns diff", |
| 65 | + device: &devicetypes.CaniDeviceType{RackPosition: 5}, |
| 66 | + remote: &nautobotapi.Device{Position: nil}, |
| 67 | + wantDiff: true, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + for _, tt := range tests { |
| 72 | + t.Run(tt.name, func(t *testing.T) { |
| 73 | + diffs := comparePosition(tt.device, tt.remote) |
| 74 | + if tt.wantDiff && len(diffs) == 0 { |
| 75 | + t.Error("expected diff but got none") |
| 76 | + } |
| 77 | + if !tt.wantDiff && len(diffs) > 0 { |
| 78 | + t.Errorf("expected no diff but got %v", diffs) |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestCompareFace(t *testing.T) { |
| 85 | + frontVal := nautobotapi.DeviceFaceValue("front") |
| 86 | + rearVal := nautobotapi.DeviceFaceValue("rear") |
| 87 | + |
| 88 | + tests := []struct { |
| 89 | + name string |
| 90 | + device *devicetypes.CaniDeviceType |
| 91 | + remote *nautobotapi.Device |
| 92 | + wantDiff bool |
| 93 | + }{ |
| 94 | + { |
| 95 | + name: "faces differ returns diff", |
| 96 | + device: &devicetypes.CaniDeviceType{Face: "rear"}, |
| 97 | + remote: &nautobotapi.Device{ |
| 98 | + Face: &nautobotapi.DeviceFace{Value: &frontVal}, |
| 99 | + }, |
| 100 | + wantDiff: true, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "faces match returns no diff", |
| 104 | + device: &devicetypes.CaniDeviceType{Face: "rear"}, |
| 105 | + remote: &nautobotapi.Device{ |
| 106 | + Face: &nautobotapi.DeviceFace{Value: &rearVal}, |
| 107 | + }, |
| 108 | + wantDiff: false, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "empty local face skips comparison", |
| 112 | + device: &devicetypes.CaniDeviceType{Face: ""}, |
| 113 | + remote: &nautobotapi.Device{}, |
| 114 | + wantDiff: false, |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + for _, tt := range tests { |
| 119 | + t.Run(tt.name, func(t *testing.T) { |
| 120 | + diffs := compareFace(tt.device, tt.remote) |
| 121 | + if tt.wantDiff && len(diffs) == 0 { |
| 122 | + t.Error("expected diff but got none") |
| 123 | + } |
| 124 | + if !tt.wantDiff && len(diffs) > 0 { |
| 125 | + t.Errorf("expected no diff but got %v", diffs) |
| 126 | + } |
| 127 | + }) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestPtrStr(t *testing.T) { |
| 132 | + val := "hello" |
| 133 | + |
| 134 | + tests := []struct { |
| 135 | + name string |
| 136 | + input *string |
| 137 | + expected string |
| 138 | + }{ |
| 139 | + { |
| 140 | + name: "non-nil returns value", |
| 141 | + input: &val, |
| 142 | + expected: "hello", |
| 143 | + }, |
| 144 | + { |
| 145 | + name: "nil returns empty string", |
| 146 | + input: nil, |
| 147 | + expected: "", |
| 148 | + }, |
| 149 | + } |
| 150 | + |
| 151 | + for _, tt := range tests { |
| 152 | + t.Run(tt.name, func(t *testing.T) { |
| 153 | + got := ptrStr(tt.input) |
| 154 | + if got != tt.expected { |
| 155 | + t.Errorf("ptrStr() = %q, want %q", got, tt.expected) |
| 156 | + } |
| 157 | + }) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func TestOrNone(t *testing.T) { |
| 162 | + tests := []struct { |
| 163 | + name string |
| 164 | + input string |
| 165 | + expected string |
| 166 | + }{ |
| 167 | + { |
| 168 | + name: "non-empty returns input", |
| 169 | + input: "hello", |
| 170 | + expected: "hello", |
| 171 | + }, |
| 172 | + { |
| 173 | + name: "empty returns (none)", |
| 174 | + input: "", |
| 175 | + expected: "(none)", |
| 176 | + }, |
| 177 | + } |
| 178 | + |
| 179 | + for _, tt := range tests { |
| 180 | + t.Run(tt.name, func(t *testing.T) { |
| 181 | + got := orNone(tt.input) |
| 182 | + if got != tt.expected { |
| 183 | + t.Errorf("orNone() = %q, want %q", got, tt.expected) |
| 184 | + } |
| 185 | + }) |
| 186 | + } |
| 187 | +} |
0 commit comments