#!/bin/bash # Copyright 2021 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. cd "$(dirname "$0")" source gbmc-ip-monitor.sh if [ -e ../gbmc-ip-monitor.bb ]; then source '../../test/test-sh/lib.sh' else source "$SYSROOT/usr/share/test/lib.sh" fi test_init_empty() { ip() { return 0 } str="$(gbmc_ip_monitor_generate_init)" || fail expect_streq "$str" '[INIT]' } test_init_link_populated() { ip() { if [ "$1" = 'link' ]; then cat < mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eno2: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether aa:aa:aa:aa:aa:aa brd ff:ff:ff:ff:ff:ff altname enp0s31f6 EOF fi return 0 } str="$(gbmc_ip_monitor_generate_init)" || fail expect_streq "$str" < mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 [LINK]2: eno2: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether aa:aa:aa:aa:aa:aa brd ff:ff:ff:ff:ff:ff altname enp0s31f6 [INIT] EOF } test_init_addr_populated() { ip() { if [ "$1" = 'addr' ]; then cat < mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eno2: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether aa:aa:aa:aa:aa:aa brd ff:ff:ff:ff:ff:ff altname enp0s31f6 inet 192.168.242.57/23 brd 192.168.243.255 scope global dynamic noprefixroute eno2 valid_lft 83967sec preferred_lft 83967sec inet6 fd01:ff2:5687:4:cf03:45f3:983a:96eb/64 scope global temporary dynamic valid_lft 518788sec preferred_lft 183sec EOF fi return 0 } str="$(gbmc_ip_monitor_generate_init)" || fail expect_streq "$str" < mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000" \ < <(echo 'link/ether aa:aa:aa:aa:aa:aa brd ff:ff:ff:ff:ff:ff') expect_streq "$change" 'link' expect_streq "$action" 'add' expect_streq "$intf" 'eno2' expect_streq "$mac" 'aa:aa:aa:aa:aa:aa' } testParseLinkDel() { expect_err 0 gbmc_ip_monitor_parse_line "[LINK]Deleted 2: eno2: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000" \ < <(echo 'link/ether aa:aa:aa:aa:aa:aa brd ff:ff:ff:ff:ff:ff') expect_streq "$change" 'link' expect_streq "$action" 'del' expect_streq "$intf" 'eno2' expect_streq "$mac" 'aa:aa:aa:aa:aa:aa' } main