#!/usr/bin/env bats # Integration tests for parse-hcn.sh # Tests complete end-to-end workflows (module renamed from 99hcnmgr to 99hcn) FIXTURE_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)" SCRIPT_PATH="${FIXTURE_DIR}/../../live-root/usr/lib/dracut/modules.d/99hcn/parse-hcn.sh" setup_file() { export BATS_TMPDIR="${BATS_TEST_TMPDIR:-/tmp}/hcn-integration" mkdir -p "$BATS_TMPDIR" } teardown_file() { [ -d "$BATS_TMPDIR" ] && rm -rf "$BATS_TMPDIR" } setup() { TEST_WORK_DIR="$(mktemp -d "$BATS_TMPDIR/test.XXXXXX")" export TEST_WORK_DIR } teardown() { [ -d "$TEST_WORK_DIR" ] && rm -rf "$TEST_WORK_DIR" } # ======================================== # Integration Test: Complete Workflow # ======================================== @test "integration: complete bond discovery and configuration workflow" { # This test simulates the complete workflow: # 1. Device discovery from device-tree # 2. MAPPINGS generation # 3. Bond configuration generation # 4. Command line argument generation # 5. nm-initrd-generator invocation (mocked) # 6. Connection fixup # Setup mock environment export MOCK_PROC="$FIXTURE_DIR/proc" export MOCK_SYS="$FIXTURE_DIR/sys" # Expected MAPPINGS based on fixtures EXPECTED_MAPPINGS="bond333e80f5 enP32775p1s0 2e:7a:30:83:f5:00 primary bond333e80f5 env6 2e:7a:32:2d:3d:06 none" # Verify MAPPINGS would be built correctly # Device 1: PCI ethernet (primary) hcn_id_1=$(hexdump -n 4 -ve '/1 "%02x"' "$MOCK_PROC/device-tree/pci@800000029008007/ethernet@0/ibm,hcn-id") mode_1=$(tr -d '\0' <"$MOCK_PROC/device-tree/pci@800000029008007/ethernet@0/ibm,hcn-mode" 2>/dev/null | tr -d ' ') mac_1=$(hexdump -ve '/1 "%02x:"' "$MOCK_PROC/device-tree/pci@800000029008007/ethernet@0/local-mac-address" | sed 's/:$//') dev_1=$(cat "$MOCK_PROC/device-tree/pci@800000029008007/ethernet@0/ofpathname") [ "$hcn_id_1" = "333e80f5" ] [ "$mode_1" = "primary" ] [ "$mac_1" = "2e:7a:30:83:f5:00" ] [ "$dev_1" = "enP32775p1s0" ] # Device 2: VNIC (backup) hcn_id_2=$(hexdump -n 4 -ve '/1 "%02x"' "$MOCK_PROC/device-tree/vdevice/vnic@30000006/ibm,hcn-id") mode_2=$(tr -d '\0' <"$MOCK_PROC/device-tree/vdevice/vnic@30000006/ibm,hcn-mode" 2>/dev/null | tr -d ' ') mac_2=$(hexdump -ve '/1 "%02x:"' "$MOCK_PROC/device-tree/vdevice/vnic@30000006/local-mac-address" | sed 's/:$//') dev_2=$(cat "$MOCK_PROC/device-tree/vdevice/vnic@30000006/ofpathname") [ "$hcn_id_2" = "333e80f5" ] [ "$mode_2" = "backup" ] [ "$mac_2" = "2e:7a:32:2d:3d:06" ] [ "$dev_2" = "env6" ] # Both devices should map to same bond [ "$hcn_id_1" = "$hcn_id_2" ] } @test "integration: bond configuration matches expected format" { # Verify the bond configuration generated would be correct BONDNAME="bond333e80f5" MAPPINGS="bond333e80f5 enP32775p1s0 2e7a3083f500 primary bond333e80f5 env6 2e7a322d3d06 none" # Extract slaves SLAVES="" PRIMARY="" set -- $MAPPINGS while [ $# -ge 4 ]; do if [ "$1" = "$BONDNAME" ]; then SLAVES="${SLAVES:+$SLAVES,}$2" [ "$4" = "primary" ] && PRIMARY="$2" fi shift 4 done # Verify extracted values [ "$SLAVES" = "enP32775p1s0,env6" ] [ "$PRIMARY" = "enP32775p1s0" ] # Build bond options BOND_OPTS="mode=1,miimon=100,fail_over_mac=2${PRIMARY:+,primary=$PRIMARY}" # Verify bond options [ "$BOND_OPTS" = "mode=1,miimon=100,fail_over_mac=2,primary=enP32775p1s0" ] # Verify complete bond argument BOND_ARG="bond=$BONDNAME:$SLAVES:$BOND_OPTS" [ "$BOND_ARG" = "bond=bond333e80f5:enP32775p1s0,env6:mode=1,miimon=100,fail_over_mac=2,primary=enP32775p1s0" ] } @test "integration: connection fixup transforms UUID to bond name" { # Copy nm-initrd-generator connections to test directory mkdir -p "$TEST_WORK_DIR/connections" cp -r "$FIXTURE_DIR/nm-initrd-generator-connections"/* "$TEST_WORK_DIR/connections/" # Verify initial state (UUID controller) grep -q "controller=e2a7b4b8-8297-4185-8421-ed32cf47db1c" \ "$TEST_WORK_DIR/connections/enP32775p1s0.nmconnection" # Source the parse_nm_connection function source <(sed -n '/^parse_nm_connection()/,/^}/p' "$SCRIPT_PATH") # Parse the bond connection to get UUID mapping IFS='|' read -r bond_id bond_uuid _rest <