<commit_before>#!/bin/sh
#
# Copyright (C) 2012	<NAME> <<EMAIL>>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
#

test_description='Test dabba interface capabilities command'

. ./dabba-test-lib.sh

ethtool_port_parse() {
    local ethtool_output="$1"
    local out="{"
    local status=""

    for port in aui mii bnc tp fibre
    do
        # append port and its related status to the output
        grep "Supported ports:" ethtool_output | grep -qi "$port"
        test $? = 0 && status="True" || status="False"
        out="${out}'$port': $status, "
    done

    # replace final comma-space by a closing curly brace
    out=$(echo "$out" | sed -e 's/, $/}/g')

    echo "$out"
}

ethtool_speed_parse() {
    local begin="$1"
    local end="$2"
    local ethtool_output="$3"
    local out="{"
    local status=""

    range_print "$begin" "$end" "$ethtool_output" | tr -d '\n' > range_output

    for speed in 1000 10000 10 100
    do
        out="$out$speed: {"
        for duplex in full half
        do
            grep -qiw "${speed}baseT/${duplex}" range_output
            test $? = 0 && status="True" || status="False"
            out="$out'$duplex': $status, "
        done
        out=$(echo "$out" | sed -e 's/, $/}, /g')
    done

    out=$(echo "$out" | sed -e 's/, $/}/g')
    echo "$out"
}

ethtool_parse() {
    local status="$(awk -F: '/'"$1"'/ {print $NF}' "$2" | tr -d ' ')"
    test "$status" = "Yes" && echo "True" || echo "False"
}

ethtool_supported_speed_parse() {
    ethtool_speed_parse "Supported link modes:" "Supported pause frame use:" "$1"
}

ethtool_supported_pause_parse() {
    ethtool_parse "Supported pause frame use" "$1"
}

ethtool_supported_autoneg_parse() {
    ethtool_parse "Supports auto-negotiation" "$1"
}

ethtool_advertised_speed_parse() {
    ethtool_speed_parse "Advertised link modes:" "Advertised pause frame use:" "$1"
}

ethtool_advertised_pause_parse() {
    ethtool_parse "Advertised pause frame use" "$1"
}

ethtool_advertised_autoneg_parse() {
    ethtool_parse "Advertised auto-negotiation" "$1"
}

ethtool_lp_advertised_speed_parse() {
    ethtool_speed_parse "Link partner advertised link modes:" "Link partner advertised pause frame use:" "$1"
}

ethtool_lp_advertised_pause_parse() {
    ethtool_parse "Link partner advertised pause frame use" "$1"
}

ethtool_lp_advertised_autoneg_parse() {
    ethtool_parse "Link partner advertised auto-negotiation" "$1"
}

test_expect_success "Setup: Stop already running dabbad" "
    test_might_fail killall dabbad
"

test_expect_success 'invoke dabba interface capabilities command w/o dabbad' "
    test_expect_code 22 $DABBA_PATH/dabba interface capabilities get
"

test_expect_success "Setup: Start dabbad" "
    '$DABBAD_PATH'/dabbad --daemonize
"

test_expect_success 'invoke dabba interface capabilities command with dabbad' "
    '$DABBA_PATH'/dabba interface capabilities get > result
"

test_expect_success PYTHON_YAML "Parse interface capabilities YAML output" "
    yaml2dict result > parsed
"

for i in `seq 0 $(($(yaml_number_of_interface_get parsed)-1))`
do
    test_expect_success PYTHON_YAML "Query interface capabilities output" "
        dictkeys2values interfaces $i name < parsed > output_name &&
        dictkeys2values interfaces $i capabilities port < parsed > output_port &&
        dictkeys2values interfaces $i capabilities supported speed < parsed > output_supported_speed &&<commit_msg>test: check interface capabilities output length.<commit_after>#!/bin/sh
#
# Copyright (C) 2012	<NAME> <<EMAIL>>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
#

test_description='Test dabba interface capabilities command'

. ./dabba-test-lib.sh

dev_nr=$(number_of_interface_get)

ethtool_port_parse() {
    local ethtool_output="$1"
    local out="{"
    local status=""

    for port in aui mii bnc tp fibre
    do
        # append port and its related status to the output
        grep "Supported ports:" ethtool_output | grep -qi "$port"
        test $? = 0 && status="True" || status="False"
        out="${out}'$port': $status, "
    done

    # replace final comma-space by a closing curly brace
    out=$(echo "$out" | sed -e 's/, $/}/g')

    echo "$out"
}

ethtool_speed_parse() {
    local begin="$1"
    local end="$2"
    local ethtool_output="$3"
    local out="{"
    local status=""

    range_print "$begin" "$end" "$ethtool_output" | tr -d '\n' > range_output

    for speed in 1000 10000 10 100
    do
        out="$out$speed: {"
        for duplex in full half
        do
            grep -qiw "${speed}baseT/${duplex}" range_output
            test $? = 0 && status="True" || status="False"
            out="$out'$duplex': $status, "
        done
        out=$(echo "$out" | sed -e 's/, $/}, /g')
    done

    out=$(echo "$out" | sed -e 's/, $/}/g')
    echo "$out"
}

ethtool_parse() {
    local status="$(awk -F: '/'"$1"'/ {print $NF}' "$2" | tr -d ' ')"
    test "$status" = "Yes" && echo "True" || echo "False"
}

ethtool_supported_speed_parse() {
    ethtool_speed_parse "Supported link modes:" "Supported pause frame use:" "$1"
}

ethtool_supported_pause_parse() {
    ethtool_parse "Supported pause frame use" "$1"
}

ethtool_supported_autoneg_parse() {
    ethtool_parse "Supports auto-negotiation" "$1"
}

ethtool_advertised_speed_parse() {
    ethtool_speed_parse "Advertised link modes:" "Advertised pause frame use:" "$1"
}

ethtool_advertised_pause_parse() {
    ethtool_parse "Advertised pause frame use" "$1"
}

ethtool_advertised_autoneg_parse() {
    ethtool_parse "Advertised auto-negotiation" "$1"
}

ethtool_lp_advertised_speed_parse() {
    ethtool_speed_parse "Link partner advertised link modes:" "Link partner advertised pause frame use:" "$1"
}

ethtool_lp_advertised_pause_parse() {
    ethtool_parse "Link partner advertised pause frame use" "$1"
}

ethtool_lp_advertised_autoneg_parse() {
    ethtool_parse "Link partner advertised auto-negotiation" "$1"
}

test_expect_success "Setup: Stop already running dabbad" "
    test_might_fail killall dabbad
"

test_expect_success 'invoke dabba interface capabilities command w/o dabbad' "
    test_expect_code 22 $DABBA_PATH/dabba interface capabilities get
"

test_expect_success "Setup: Start dabbad" "
    '$DABBAD_PATH'/dabbad --daemonize
"

test_expect_success 'invoke dabba interface capabilities command with dabbad' "
    '$DABBA_PATH'/dabba interface capabilities get > result
"

test_expect_success PYTHON_YAML "Parse interface capabilities YAML output" "
    yaml2dict result > parsed
"

test_expect_success PYTHON_YAML "Check interface capabilities output length" "
    test '$dev_nr' -eq $(yaml_number_of_interface_get parsed)
"

for i in `seq 0 $(($(yaml_number_of_interface_get parsed)-1))`
do
    test_expect_success PYTHON_YAML "Query interface capabilities output" "
        dictkeys2values interfaces $i name < parsed > output_name &&
        dictkeys2values interfaces $i capabilities port < parsed > output_port &&
        dictkeys2values interfaces $i capabilities supported speed < parsed > output_supported_speed &&
{'fraction_non_alphanumeric': 0.11095152603231598, 'fraction_numerical': 0.012208258527827648, 'mean_word_length': 3.788538681948424, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '812524', 'n_tokens_mistral': 2754, 'n_tokens_neox': 2626, 'n_words': 854}