| package main |
| |
| import ( |
| "context" |
| "strings" |
| "testing" |
| |
| "github.com/stretchr/testify/mock" |
| "github.com/stretchr/testify/require" |
| "go.skia.org/infra/go/testutils/unittest" |
| ) |
| |
| func TestMapping(t *testing.T) { |
| unittest.SmallTest(t) |
| |
| mn := MockBotNameGetter{} |
| mp := MockBotPortGetter{} |
| |
| mn.On("GetBotNamesAddresses").Return([]Bot{ |
| {Hostname: "rpi-001", MACAddress: "12:32", IPV4Address: "1.1.1.1"}, |
| {Hostname: "rpi-002", MACAddress: "AB:BA", IPV4Address: "2.2.2.2"}, |
| {Hostname: "rpi-003", MACAddress: "CA:11", IPV4Address: "3.3.3.3"}, |
| }, nil) |
| mp.On("GetBotPortsAddresses").Return([]Bot{ |
| {MACAddress: "12:32", Port: 10}, |
| {MACAddress: "AB:BA", Port: 20}, |
| {MACAddress: "not_seen_above", Port: 30}, |
| }, nil) |
| |
| defer mn.AssertExpectations(t) |
| defer mp.AssertExpectations(t) |
| |
| ctx := context.Background() |
| botList, err := enumerateBots(ctx, &mn, &mp) |
| |
| require.NoError(t, err) |
| require.Len(t, botList, 2, "Only 2 bots have everything listed") |
| require.Contains(t, botList, Bot{Hostname: "rpi-001", MACAddress: "12:32", Port: 10, IPV4Address: "1.1.1.1"}) |
| require.Contains(t, botList, Bot{Hostname: "rpi-002", MACAddress: "AB:BA", Port: 20, IPV4Address: "2.2.2.2"}) |
| } |
| |
| const TEST_ANSIBLE_DATA = `skia-rpi-master 192.168.1.99 f4:4d:30:b6:22:01 |
| skia-rpi-master-spare 192.168.1.98 f4:4d:30:b6:25:02 |
| skia-rpi-002 192.168.1.102 b8:27:eb:66:6c:03 |
| skia-rpi-001 192.168.1.101 B8:27:eb:16:ba:04` |
| |
| func TestAnsibleParsing(t *testing.T) { |
| unittest.SmallTest(t) |
| |
| bots := parseAnsibleResult(TEST_ANSIBLE_DATA) |
| |
| var expected = []Bot{ |
| {Hostname: "skia-rpi-master", MACAddress: "F4:4D:30:B6:22:01", IPV4Address: "192.168.1.99"}, |
| {Hostname: "skia-rpi-master-spare", MACAddress: "F4:4D:30:B6:25:02", IPV4Address: "192.168.1.98"}, |
| {Hostname: "skia-rpi-002", MACAddress: "B8:27:EB:66:6C:03", IPV4Address: "192.168.1.102"}, |
| {Hostname: "skia-rpi-001", MACAddress: "B8:27:EB:16:BA:04", IPV4Address: "192.168.1.101"}, |
| } |
| |
| require.Equal(t, expected, bots) |
| } |
| |
| const TEST_EDGESWITCH_DATA = `VLAN ID MAC Address Interface IfIndex Status |
| ------- ------------------ --------------------- ------- ------------ |
| 1 40:40:4c:36:80:62 0/21 21 Learned |
| 1 40:40:4C:36:83:62 0/19 19 Learned |
| 1 40:41:03:00:13:D8 0/1 1 Learned |
| 1 40:41:30:01:14:D0 0/1 1 Learned |
| ` |
| |
| var expectedEdgeSwitchResults = []Bot{ |
| {MACAddress: "40:40:4C:36:80:62", Port: 21}, |
| {MACAddress: "40:40:4C:36:83:62", Port: 19}, |
| {MACAddress: "40:41:03:00:13:D8", Port: 1}, |
| {MACAddress: "40:41:30:01:14:D0", Port: 1}, |
| } |
| |
| func TestEdgeSwitchParsing(t *testing.T) { |
| unittest.SmallTest(t) |
| |
| bots, err := parseSSHResult(strings.Split(TEST_EDGESWITCH_DATA, "\n")) |
| require.NoError(t, err) |
| |
| require.Equal(t, expectedEdgeSwitchResults, bots) |
| } |
| |
| func TestEdgeSwitchDeduping(t *testing.T) { |
| unittest.SmallTest(t) |
| |
| deduped := dedupeBots(expectedEdgeSwitchResults) |
| |
| require.Len(t, deduped, 2, "Only 2 bots have unique ports") |
| require.Contains(t, deduped, Bot{MACAddress: "40:40:4C:36:80:62", Port: 21}) |
| require.Contains(t, deduped, Bot{MACAddress: "40:40:4C:36:83:62", Port: 19}) |
| } |
| |
| // MockBotNameGetter is an autogenerated mock type for the BotNameGetter type |
| type MockBotNameGetter struct { |
| mock.Mock |
| } |
| |
| // GetBotNamesAddresses provides a mock function with given fields: |
| func (_m *MockBotNameGetter) GetBotNamesAddresses(ctx context.Context) ([]Bot, error) { |
| ret := _m.Called() |
| |
| var r0 []Bot |
| if rf, ok := ret.Get(0).(func() []Bot); ok { |
| r0 = rf() |
| } else { |
| if ret.Get(0) != nil { |
| r0 = ret.Get(0).([]Bot) |
| } |
| } |
| |
| return r0, ret.Error(1) |
| } |
| |
| // Ensure MockBotNameGetter fulfills BotNameGetter |
| var _ BotNameGetter = (*MockBotNameGetter)(nil) |
| |
| // MockBotPortGetter is an autogenerated mock type for the BotPortGetter type |
| type MockBotPortGetter struct { |
| mock.Mock |
| } |
| |
| // GetBotPortsAddresses provides a mock function with given fields: |
| func (_m *MockBotPortGetter) GetBotPortsAddresses() ([]Bot, error) { |
| ret := _m.Called() |
| |
| var r0 []Bot |
| if rf, ok := ret.Get(0).(func() []Bot); ok { |
| r0 = rf() |
| } else { |
| if ret.Get(0) != nil { |
| r0 = ret.Get(0).([]Bot) |
| } |
| } |
| |
| return r0, ret.Error(1) |
| } |
| |
| // Ensure MockDevNameGetter fulfills BotPortGetter |
| var _ BotPortGetter = (*MockBotPortGetter)(nil) |