aboutsummaryrefslogtreecommitdiff
path: root/commands/freebsd/netgraph/list-nodes
blob: 4ebcd634fd074d9b3f68f66b73a25da789ff5ce1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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