#!/bin/bash
#
# More complex VLANd test script

set -e

NAME="complex-4switches-2vlans-1"

DESCRIPTION="Check VLAN isolation for 4 machines in 2 VLANs scattered across multiple switches."

# List all the switches and hosts we need to use, if not using all of
# them. We can make tests run faster by not involving *all* of them in
# every test.
HOSTS="imx5301 panda01 arndale02 arndale03"      # 4 machines for
						 # primary testing
HOSTS="$HOSTS imx5302 arndale01 panda02 panda03" # 4 machines on the
						 # same switches as
						 # observers

#SWITCHES="vlandswitch05"   # Let the system work things out

# Show more detail during test output
VERBOSE=0

# And give a filename for logging
LOGFILE=$0-$$.log

# Include the core test wrapper code that makes life easier
DIR=$(dirname $0)
. ${DIR}/test-common

# Ensure all the ports we're using are on their base VLANs
log "checking base VLANs"
verify_all_hosts_are_base
log "$HOSTS are all on their base VLANs - good"

# Clear old logfiles from our test machines
stop_logging
clear_logs

# Start all the test machines logging, then wait 60s to let all of
# them show baseline results before we start testing
start_logging
log "CHECK INIT START"
log "CHECK INIT CHECK VLAN_BASE:imx5301:panda01:arndale02:arndale03:imx5302:arndale01:panda02:panda03"
pause 60
log "CHECK INIT END"

# Create 2 VLANs: tag 30, named "test30" and tag 31, named "test31"
log "Creating new VLAN tag 30"
OUTPUT=$(run_admin_command create_vlan --name test30 --tag 30 --is_base_vlan false)
VLAN_ID1=$(run_admin_command lookup_vlan_by_tag --tag 30)

log "Creating new VLAN tag 31"
OUTPUT=$(run_admin_command create_vlan --name test31 --tag 31 --is_base_vlan false)
VLAN_ID2=$(run_admin_command lookup_vlan_by_tag --tag 31)
log "Created new VLANs with IDs $VLAN_ID1 and $VLAN_ID2"

# Wait 10s for everything to settle
pause 10

# Move some of the test machines to these new VLANs, pausing at each
# setup
log "Moving imx5301 to VLAN ID $VLAN_ID1"
OUTPUT=$(run_admin_command set_port_current_vlan --port_id ${imx5301_PORT_ID} --vlan_id $VLAN_ID1)
pause 60
log "CHECK STEP1 START"
log "CHECK STEP1 CHECK VLAN_BASE:panda01:arndale02:arndale03:imx5302:arndale01:panda02:panda03"
pause 60
log "CHECK STEP1 END"

log "Moving panda01 to VLAN ID $VLAN_ID1"
OUTPUT=$(run_admin_command set_port_current_vlan --port_id ${panda01_PORT_ID} --vlan_id $VLAN_ID1)
log "Done moving ports to VLAN ID $VLAN_ID1"
pause 60
log "CHECK STEP2 START"
log "CHECK STEP2 CHECK VLAN_30:imx5301:panda01 VLAN_BASE:arndale02:arndale03:imx5302:arndale01:panda02:panda03"
pause 60
log "CHECK STEP2 END"

log "Moving arndale02 to VLAN ID $VLAN_ID2"
OUTPUT=$(run_admin_command set_port_current_vlan --port_id ${arndale02_PORT_ID} --vlan_id $VLAN_ID2)
pause 60
log "CHECK STEP3 START"
log "CHECK STEP3 CHECK VLAN_30:imx5301:panda01 VLAN_BASE:arndale03:imx5302:arndale01:panda02:panda03"
pause 60
log "CHECK STEP3 END"

log "Moving arndale03 to VLAN ID $VLAN_ID2"
OUTPUT=$(run_admin_command set_port_current_vlan --port_id ${arndale03_PORT_ID} --vlan_id $VLAN_ID2)
log "Done moving ports to VLAN ID $VLAN_ID2"
pause 60
log "CHECK STEP4 START"
log "CHECK STEP4 CHECK VLAN_30:imx5301:panda01 VLAN_31:arndale02:arndale03 VLAN_BASE:imx5302:arndale01:panda02:panda03"
pause 60
log "CHECK STEP4 END"

# Move test machines back to their base VLANs
log "Moving ports back to base"
OUTPUT=$(run_admin_command restore_port_to_base_vlan --port_id ${imx5301_PORT_ID})
OUTPUT=$(run_admin_command restore_port_to_base_vlan --port_id ${panda01_PORT_ID})
OUTPUT=$(run_admin_command restore_port_to_base_vlan --port_id ${arndale02_PORT_ID})
OUTPUT=$(run_admin_command restore_port_to_base_vlan --port_id ${arndale03_PORT_ID})
log "Done moving ports back to base"

# Wait 60s for everything to settle
pause 60

log "CHECK FINI START"
log "CHECK FINI CHECK VLAN_BASE:imx5301:panda01:arndale02:arndale03:imx5302:arndale01:panda02:panda03"
pause 60
log "CHECK FINI END"

# Check that they're all back on their base VLANs
log "Checking base VLANs after the test"
verify_all_hosts_are_base

log "Delete the test VLANs"
OUTPUT=$(run_admin_command delete_vlan --vlan_id ${VLAN_ID1})
OUTPUT=$(run_admin_command delete_vlan --vlan_id ${VLAN_ID2})

# Stop all the test machines logging (and wait 60s)
stop_logging
pause 20

log "Test done, grab logs"
# Grab logs from the machines
grab_logs >> $LOGFILE

# Clear old logs
clear_logs

# How did the test do?
check_test_steps

# DONE!
