"Fossies" - the Fresh Open Source Software Archive

Member "apt-2.2.4/test/integration/test-apt-cli-json-hooks" (10 Jun 2021, 7301 Bytes) of package /linux/misc/apt-2.2.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/bin/sh
    2 set -e
    3 
    4 TESTDIR="$(readlink -f "$(dirname "$0")")"
    5 . "$TESTDIR/framework"
    6 
    7 setupenvironment
    8 configarchitecture "i386"
    9 
   10 DESCR='Some description that has a unusual word xxyyzz and aabbcc and a UPPERCASE'
   11 DESCR2='Some other description with the unusual aabbcc only'
   12 insertpackage 'unstable' 'foo' 'all' '1.0' '' '' "$DESCR
   13  Long description of stuff and such, with lines
   14  .
   15  and paragraphs and everything."
   16 insertpackage 'testing' 'bar' 'i386' '2.0' '' '' "$DESCR2"
   17 
   18 setupaptarchive
   19 
   20 APTARCHIVE="$(readlink -f ./aptarchive)"
   21 
   22 cat >> json-hook.sh << EOF
   23 #!/bin/bash
   24 trap '' SIGPIPE
   25 while true; do
   26     read request <&\$APT_HOOK_SOCKET || exit 1
   27 
   28     if echo "\$request" | grep -q ".hello"; then
   29         echo "HOOK: HELLO"
   30     fi
   31 
   32     if echo "\$request" | grep -q ".bye"; then
   33         echo "HOOK: BYE"
   34         exit 0;
   35     fi
   36 
   37     echo HOOK: request \$request
   38 
   39     read empty <&\$APT_HOOK_SOCKET || exit 1
   40 
   41     echo HOOK: empty \$empty
   42 
   43     if echo "\$request" | grep -q ".hello"; then
   44         printf '{"jsonrpc": "2.0", "result": {"version": "0.1"}, "id": 0}\n\n' >&\$APT_HOOK_SOCKET 2>/dev/null || exit 1
   45     fi
   46 
   47 
   48 done
   49 EOF
   50 
   51 chmod +x json-hook.sh
   52 
   53 HOOK="$(readlink -f ./json-hook.sh)"
   54 
   55 # Setup all hooks
   56 cat >> rootdir/etc/apt/apt.conf.d/99-json-hooks << EOF
   57     AptCli::Hooks::Install:: "$HOOK";
   58     AptCli::Hooks::Search:: "$HOOK";
   59 EOF
   60 
   61 
   62 ############################# Success search #######################
   63 testsuccessequal 'HOOK: HELLO
   64 HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}}
   65 HOOK: empty
   66 HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.search.pre","params":{"command":"search","search-terms":["foo"],"unknown-packages":[],"packages":[]}}
   67 HOOK: empty
   68 HOOK: BYE
   69 Sorting...
   70 Full Text Search...
   71 foo/unstable 1.0 all
   72   Some description that has a unusual word xxyyzz and aabbcc and a UPPERCASE
   73 
   74 HOOK: HELLO
   75 HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}}
   76 HOOK: empty
   77 HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.search.post","params":{"command":"search","search-terms":["foo"],"unknown-packages":[],"packages":[]}}
   78 HOOK: empty
   79 HOOK: BYE' apt search foo
   80 
   81 ############################# Failed search #######################
   82 testsuccessequal 'HOOK: HELLO
   83 HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}}
   84 HOOK: empty
   85 HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.search.pre","params":{"command":"search","search-terms":["foox"],"unknown-packages":[],"packages":[]}}
   86 HOOK: empty
   87 HOOK: BYE
   88 Sorting...
   89 Full Text Search...
   90 HOOK: HELLO
   91 HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}}
   92 HOOK: empty
   93 HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.search.fail","params":{"command":"search","search-terms":["foox"],"unknown-packages":[],"packages":[]}}
   94 HOOK: empty
   95 HOOK: BYE' apt search foox
   96 
   97 
   98 ############################# Failed install #######################
   99 
  100 testfailureequal 'Reading package lists...
  101 Building dependency tree...
  102 HOOK: HELLO
  103 HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}}
  104 HOOK: empty
  105 HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.fail","params":{"command":"install","search-terms":["foxxx"],"unknown-packages":["foxxx"],"packages":[]}}
  106 HOOK: empty
  107 HOOK: BYE
  108 E: Unable to locate package foxxx' apt install foxxx
  109 
  110 ############################# Success install #######################
  111 
  112 testsuccessequal 'Reading package lists...
  113 Building dependency tree...
  114 HOOK: HELLO
  115 HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}}
  116 HOOK: empty
  117 HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.pre-prompt","params":{"command":"install","search-terms":["foo"],"unknown-packages":[],"packages":[{"id":1,"name":"foo","architecture":"i386","mode":"install","automatic":false,"versions":{"candidate":{"id":1,"version":"1.0","architecture":"all","pin":500},"install":{"id":1,"version":"1.0","architecture":"all","pin":500}}}]}}
  118 HOOK: empty
  119 HOOK: BYE
  120 The following NEW packages will be installed:
  121   foo
  122 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
  123 Inst foo (1.0 unstable [all])
  124 Conf foo (1.0 unstable [all])
  125 HOOK: HELLO
  126 HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}}
  127 HOOK: empty
  128 HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.post","params":{"command":"install","search-terms":["foo"],"unknown-packages":[],"packages":[{"id":1,"name":"foo","architecture":"i386","mode":"install","automatic":false,"versions":{"candidate":{"id":1,"version":"1.0","architecture":"all","pin":500},"install":{"id":1,"version":"1.0","architecture":"all","pin":500}}}]}}
  129 HOOK: empty
  130 HOOK: BYE' apt install foo -s
  131 
  132 ################## Error in hello response #########################
  133 
  134 cat > json-hook.sh << EOF
  135 #!/bin/bash
  136 exec 2>/dev/null
  137 trap '' SIGPIPE
  138 while true; do
  139     read request <&\$APT_HOOK_SOCKET
  140     read empty <&\$APT_HOOK_SOCKET
  141 
  142     if echo "\$request" | grep -q ".hello"; then
  143         printf '{"jsonrpc": "2.0", "error": {"version": "0.1"}, "id": 0}\n\n' >&\$APT_HOOK_SOCKET
  144         break
  145     fi
  146 done
  147 exit 0
  148 EOF
  149 
  150 
  151 testfailureequal 'Reading package lists...
  152 Building dependency tree...
  153 E: Hook '$HOOK' reported an error during hello: {"jsonrpc": "2.0", "error": {"version": "0.1"}, "id": 0}
  154 E: Hook '$HOOK' reported an error during hello: {"jsonrpc": "2.0", "error": {"version": "0.1"}, "id": 0}' apt install foo -s
  155 
  156 ################## Missing separator line #########################
  157 cat > json-hook.sh << EOF
  158 #!/bin/bash
  159 exec 2>/dev/null
  160 trap '' SIGPIPE
  161 while true; do
  162     read request <&\$APT_HOOK_SOCKET
  163     read empty <&\$APT_HOOK_SOCKET
  164 
  165     if echo "\$request" | grep -q ".hello"; then
  166         printf '{"jsonrpc": "2.0", "result": {"version": "0.1"}, "id": 0}\n' >&\$APT_HOOK_SOCKET
  167         break
  168     fi
  169 done
  170 exit 0
  171 EOF
  172 
  173 
  174 testfailureequal 'Reading package lists...
  175 Building dependency tree...
  176 E: Could not read message separator line after handshake from '$HOOK': end of file
  177 E: Could not read message separator line after handshake from '$HOOK': end of file' apt install foo -s
  178 
  179 ################## Wrong separator line #########################
  180 cat > json-hook.sh << EOF
  181 #!/bin/bash
  182 exec 2>/dev/null
  183 trap '' SIGPIPE
  184 while true; do
  185     read request <&\$APT_HOOK_SOCKET
  186     read empty <&\$APT_HOOK_SOCKET
  187 
  188     if echo "\$request" | grep -q ".hello"; then
  189         printf '{"jsonrpc": "2.0", "result": {"version": "0.1"}, "id": 0}\nXX' >&\$APT_HOOK_SOCKET
  190         break
  191     fi
  192 done
  193 exit 0
  194 EOF
  195 
  196 
  197 testfailureequal 'Reading package lists...
  198 Building dependency tree...
  199 E: Expected empty line after handshake from '$HOOK', received XX
  200 E: Expected empty line after handshake from '$HOOK', received XX' apt install foo -s
  201 
  202 ##################### Removed hook || true ############################
  203 cat > rootdir/etc/apt/apt.conf.d/99-json-hooks << EOF
  204     AptCli::Hooks::Install:: "true";
  205     AptCli::Hooks::Search:: "true";
  206 EOF
  207 
  208 testsuccessequal 'Reading package lists...
  209 Building dependency tree...
  210 The following NEW packages will be installed:
  211   foo
  212 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
  213 Inst foo (1.0 unstable [all])
  214 Conf foo (1.0 unstable [all])' apt install foo -s
  215 
  216 
  217