diff options
Diffstat (limited to 'commands/freebsd/netgraph/list-nodes')
-rwxr-xr-x | commands/freebsd/netgraph/list-nodes | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/commands/freebsd/netgraph/list-nodes b/commands/freebsd/netgraph/list-nodes new file mode 100755 index 0000000..4ebcd63 --- /dev/null +++ b/commands/freebsd/netgraph/list-nodes @@ -0,0 +1,111 @@ +#!./cloyster +include_cmd_dir '_netgraph.sh' + +awkk=$(cat <<AWK +BEGIN { + RS="#"; + FS="|"; +} +{ print $1; } +AWK +) + +_netgraph_list_reformat() { + _input="${1}" + echo "${_input}" | \ + tail -n +2 | \ + grep -v "Local hook" | \ + grep -v "\----------" | \ + sed -e 's:^ ::g' \ + -e 's:Name\: ::g' -e 's:Type\: ::g' -e 's:ID\: ::g' -e 's:Num hooks\: ::g' | \ + tr '\n' ':' | \ + sed -e "s/::/#/g" | \ + awk '{gsub(/ {2,}/, "|")}; $1' | \ + sed -e "s/|$//g" -e "s/|:$//g" | \ + FS='[:]' awk -F'[:]' "${awkk}" + #) || fail "awk exited: $?" +} + +_netgraph_list_json= +_netgraph_list_to_json() { + if [ -z "${_netgraph_list_json}" ]; then + _input="${1}" + _ifs=$IFS + _json=$(jo nodes="{}") + for _line in ${_input}; do + _at=0; _node=; _nodename= + IFS=: + for _row in ${_line}; do + _at=$((_at + 1)) + IFS='|' + set -- "" + _hook=; + for _field in ${_row}; do set -- "${@}" "${_field}"; done + if [ "${_at}" = "1" ]; then + _nodename="${2}" + _node=$(jo node="${_nodename}" type="${3}" id="${4}" links_count="${5}" hooks=[]) + else + _hook=$(jo hook="${2}" peer_name="${3}" peer_type="${4}" peer_id="${5}" peer_hook="${6}") + _node=$(echo "$_node" | jo -f - hooks[]="${_hook}") + fi + done + _json=$(echo "$_json" | jo -d. -f - "nodes.${_nodename}=${_node}") + done + IFS=$_ifs + _netgraph_list_json="${_json}" + else + "${_netgraph_list_json}" + fi + echo "${_netgraph_list_json}" +} + +RUN + netgraph_prerun + list="$(ngctl list -l)" + reformatted=$(_netgraph_list_reformat "${list}") + json=$(_netgraph_list_to_json "${reformatted}") + return_result "${json}" +ENDR + +_test "awk" +TEST + input="$(cat "${CLOYSTER_COMMAND_DIR}/.mock.ngctl.list.l.txt")" + assert_exited $? 0 "could not read mock data file" + assert "test -n \"${input}\"" + (echo "$input" | grep "There are 66 total nodes" >/dev/null) + assert_exited $? 0 "mock input does not contains banner" + (echo "$input" | grep "ng0_" >/dev/null) + assert_exited $? 0 "mock input does not contains ng0_" + + reformatted=$(_netgraph_list_reformat "${input}") + + (echo "$reformatted" | grep "There are 66 total nodes" >/dev/null); s=$? + assert_exited "$s" 1 "reformat output contains banner" + + (echo "$reformatted" | grep "ng0" > /dev/null); s=$? + assert_exited $s 0 "reformat output does not contains ng0" + + json=$(_netgraph_list_to_json "${reformatted}") + + (echo "${json}" | jq >/dev/null) + assert_exited $? 0 "json couldn't get parsed by jq" + + # shellcheck disable=SC2034 + count=$(echo "${json}" | jq '[.nodes[].node] | length') + assert_exited $? 0 "json query failed" + assert "echo \"\${count}\"" "67" "should contain 67 nodes" + +# assert "$(echo ${json}) | jq '[.nodes[].node] | length'" "67" "should contain 67 nodes" +ENDT + +_test "runs on freebsd" .tag:integration .if:freebsd .skip:FIXME +TEST + # ( /bin/su -l $(whoami) /bin/sh ${CLOYSTER_COMMAND_FILE} ) + exit 1 +ENDT + +_test "doesnt run on freebsd" .tag:integration .not:freebsd .refute +TEST + ( ${CLOYSTER_COMMAND_FILE} ) +ENDT + |