|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Intel Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package networkmanager |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "os" |
| 22 | + "testing" |
| 23 | +) |
| 24 | + |
| 25 | +type MockNetworkManager struct { |
| 26 | + mockVersionQuery func() (string, error) |
| 27 | + mockGetAllDevices func() ([]DeviceWrapperIf, error) |
| 28 | +} |
| 29 | + |
| 30 | +func (m *MockNetworkManager) GetPropertyVersion() (string, error) { |
| 31 | + return m.mockVersionQuery() |
| 32 | +} |
| 33 | +func (m *MockNetworkManager) GetAllDevices() ([]DeviceWrapperIf, error) { |
| 34 | + return m.mockGetAllDevices() |
| 35 | +} |
| 36 | + |
| 37 | +type MockDevice struct { |
| 38 | + mockIface func() (string, error) |
| 39 | + mockSetManaged func(bool) error |
| 40 | +} |
| 41 | + |
| 42 | +func (d *MockDevice) GetPropertyInterface() (string, error) { |
| 43 | + return d.mockIface() |
| 44 | +} |
| 45 | +func (d *MockDevice) SetPropertyManaged(manage bool) error { |
| 46 | + return d.mockSetManaged(manage) |
| 47 | +} |
| 48 | + |
| 49 | +// Not sure if this works in CI, but it takes the coverage from ~50% to 90%. |
| 50 | +func TestDisableNetworkManagerForInterfacesOnHost(t *testing.T) { |
| 51 | + interfaces := []string{"ethXYZ", "ethZYX"} |
| 52 | + |
| 53 | + nm, err := NewNetworkManager() |
| 54 | + if nm == nil || err != nil { |
| 55 | + t.Fatalf("NewNetworkManager failed") |
| 56 | + } |
| 57 | + |
| 58 | + err = DisableNetworkManagerForInterfaces(nm, interfaces) |
| 59 | + if err != nil { |
| 60 | + t.Errorf("DisableNetworkManagerForInterfaces failed: %v", err) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestDisableNetworkManagerForInterfaces(t *testing.T) { |
| 65 | + interfaces := []string{"ethXYZ", "ethZYX"} |
| 66 | + |
| 67 | + nm := &MockNetworkManager{ |
| 68 | + mockVersionQuery: func() (string, error) { |
| 69 | + return "1.0.0", nil |
| 70 | + }, |
| 71 | + mockGetAllDevices: func() ([]DeviceWrapperIf, error) { |
| 72 | + return []DeviceWrapperIf{ |
| 73 | + &MockDevice{ |
| 74 | + mockIface: func() (string, error) { |
| 75 | + return "ethXYZ", nil |
| 76 | + }, |
| 77 | + mockSetManaged: func(manage bool) error { |
| 78 | + return nil |
| 79 | + }, |
| 80 | + }, |
| 81 | + &MockDevice{ |
| 82 | + mockIface: func() (string, error) { |
| 83 | + return "ethZYX", nil |
| 84 | + }, |
| 85 | + mockSetManaged: func(manage bool) error { |
| 86 | + return nil |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, nil |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + err := DisableNetworkManagerForInterfaces(nm, interfaces) |
| 94 | + if err != nil { |
| 95 | + t.Errorf("DisableNetworkManagerForInterfaces failed: %v", err) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +type TestCase struct { |
| 100 | + name string |
| 101 | + ifaces []string |
| 102 | + versionErr error |
| 103 | + getDevicesErr error |
| 104 | + ifaceErr error |
| 105 | + setManagedErr error |
| 106 | + expectedErr error |
| 107 | +} |
| 108 | + |
| 109 | +var testCases = []TestCase{ |
| 110 | + { |
| 111 | + name: "TestDisableNetworkManagerVersionQueryFails", |
| 112 | + versionErr: os.ErrInvalid, |
| 113 | + expectedErr: nil, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "TestDisableNetworkManagerGetDevicesFails", |
| 117 | + getDevicesErr: os.ErrDeadlineExceeded, |
| 118 | + expectedErr: os.ErrDeadlineExceeded, |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "TestDisableNetworkManagerGetDeviceInterfaceFails", |
| 122 | + ifaces: []string{"ethXYZ", "ethZYX"}, |
| 123 | + ifaceErr: os.ErrPermission, |
| 124 | + expectedErr: os.ErrPermission, |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "TestDisableNetworkManagerSetDeviceManagedFails", |
| 128 | + ifaces: []string{"ethXYZ", "ethZYX"}, |
| 129 | + setManagedErr: os.ErrProcessDone, |
| 130 | + expectedErr: os.ErrProcessDone, |
| 131 | + }, |
| 132 | +} |
| 133 | + |
| 134 | +func TestDisableNetworkManagerForInterfacesError(t *testing.T) { |
| 135 | + interfaces := []string{"ethXYZ", "ethZYX"} |
| 136 | + |
| 137 | + for _, tc := range testCases { |
| 138 | + |
| 139 | + nm := &MockNetworkManager{ |
| 140 | + mockVersionQuery: func() (string, error) { |
| 141 | + return "1.0.0", tc.versionErr |
| 142 | + }, |
| 143 | + mockGetAllDevices: func() ([]DeviceWrapperIf, error) { |
| 144 | + if tc.getDevicesErr != nil { |
| 145 | + return nil, tc.getDevicesErr |
| 146 | + } |
| 147 | + |
| 148 | + ret := []DeviceWrapperIf{} |
| 149 | + for _, iface := range tc.ifaces { |
| 150 | + ret = append(ret, &MockDevice{ |
| 151 | + mockIface: func() (string, error) { |
| 152 | + if tc.ifaceErr != nil { |
| 153 | + return "", tc.ifaceErr |
| 154 | + } |
| 155 | + return iface, nil |
| 156 | + }, |
| 157 | + mockSetManaged: func(manage bool) error { |
| 158 | + if tc.setManagedErr != nil { |
| 159 | + return tc.setManagedErr |
| 160 | + } |
| 161 | + return nil |
| 162 | + }, |
| 163 | + }) |
| 164 | + } |
| 165 | + |
| 166 | + return ret, nil |
| 167 | + }, |
| 168 | + } |
| 169 | + |
| 170 | + err := DisableNetworkManagerForInterfaces(nm, interfaces) |
| 171 | + if !errors.Is(err, tc.expectedErr) { |
| 172 | + t.Errorf("%s should have failed: %v", tc.name, err) |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments