| 1 | #!/bin/sh |
| 2 | set -e |
| 3 | # |
| 4 | # Check: |
| 5 | # * module-assistant installation |
| 6 | # * Create dynamic local spans |
| 7 | # * Generate spans and configure them, in both DAHDI and Asterisk levels. |
| 8 | # * a DAHDI call in Asterisk using the DAHD-dynamic module |
| 9 | # |
| 10 | |
| 11 | data_dir="debian/tests/dynamic-loc-data" |
| 12 | test_results_dir="/var/log/asterisk/testresults" |
| 13 | num_spans=2 |
| 14 | num_chans=48 |
| 15 | m_a="m-a -i -t -q" |
| 16 | |
| 17 | die() { |
| 18 | echo "[E] $@" |
| 19 | exit 1 |
| 20 | } |
| 21 | |
| 22 | log() { |
| 23 | echo "[I] $@" |
| 24 | } |
| 25 | |
| 26 | cleanup() { |
| 27 | dahdi_span_assignments remove 2>&1 || : |
| 28 | /usr/share/dahdi/dahdi-modules unload || : |
| 29 | packages_to_remove=`dpkg-query -W 'dahdi-modules-*' 2>/dev/null | awk '{print $1}'` |
| 30 | if [ "$packages_to_remove" ]; then |
| 31 | dpkg --purge $packages_to_remove |
| 32 | fi |
| 33 | } |
| 34 | |
| 35 | setup() { |
| 36 | cp -a $data_dir/dahdi/* /etc/dahdi/ |
| 37 | cp -a $data_dir/asterisk/* /etc/asterisk/ |
| 38 | systemctl restart asterisk || : # Also makes sure it is running |
| 39 | } |
| 40 | |
| 41 | # Build and install module using module-assistant. |
| 42 | # Does nothing if module is already built. |
| 43 | m_a_mod_build() { |
| 44 | $m_a prepare 2>&1 |
| 45 | $m_a a-i dahdi 2>&1 |
| 46 | if ! m-a list dahdi | grep -q 'deb$'; then |
| 47 | die "Module Assistant dahdi installation: failed for $(uname -r)." |
| 48 | fi |
| 49 | log "Module Assistant dahdi module installed." |
| 50 | } |
| 51 | |
| 52 | # local "dynamic" spans don't need hardware. They are dummy spans that |
| 53 | # use much of the DAHDI mechanism without any hardware |
| 54 | load_dynamic_spans() { |
| 55 | # An explicit modprobe is needed because dahdi_cfg uses the dahdi devices: |
| 56 | modprobe dahdi |
| 57 | if [ $? != 0 ]; then |
| 58 | die "Failed to load module dahdi: $?" |
| 59 | fi |
| 60 | |
| 61 | # Generate the devices for the dynamic spans: |
| 62 | dahdi_cfg -c /etc/dahdi/dynamic.conf |
| 63 | if [ `dahdi_span_assignments list | wc -l ` != "$num_spans" ]; then |
| 64 | dahdi_span_assignments list |
| 65 | die "Dynamic spans were not generated." |
| 66 | fi |
| 67 | |
| 68 | # The spans will get automatically created and configured in Asterisk |
| 69 | # using the udev hooks. It should take less than a second |
| 70 | sleep 5 |
| 71 | if [ `lsdahdi | grep '(In use)' | wc -l ` != "$num_chans" ]; then |
| 72 | lsdahdi |
| 73 | die "Spans not created or channels not properly registered in Asterisk." |
| 74 | fi |
| 75 | log "Asterisk configured with DAHDI channels." |
| 76 | } |
| 77 | |
| 78 | # Make a test call using the TestClient and TestServer applications. |
| 79 | # We make sure that the call was set up (which shows that the D-channels |
| 80 | # on both spans work OK) and that they can properly talk to each other |
| 81 | # (which probably shows that the B-channels in both spans are OK). |
| 82 | test_asterisk_call() { |
| 83 | id=`date +%H%M%S` # would be nice to avoid collisions |
| 84 | rm -f "$test_results_dir/$id"-*.txt |
| 85 | asterisk -rx "channel originate DAHDI/1/1234567890 application TestClient $id" |
| 86 | for i in `seq 90`; do |
| 87 | if grep -q FAIL "$test_results_dir/$id"-*.txt 2>/dev/null; then |
| 88 | cat "$test_results_dir/$id"-*.txt |
| 89 | die "Test DAHDI call in Asterisk failed (id: $id)." |
| 90 | fi |
| 91 | end_lines=`grep -- "-- END TEST--" "$test_results_dir/$id"-*.txt 2>/dev/null | wc -l` |
| 92 | if [ "$end_lines" = "2" ]; then # Test ended, that is: OK, in both sides |
| 93 | log "Test DAHDI call in Asterisk ended OK." |
| 94 | return |
| 95 | fi |
| 96 | sleep 1 |
| 97 | done |
| 98 | die "Test DAHDI call in Asterisk timed out." |
| 99 | } |
| 100 | |
| 101 | # initial cleanup: start with a clean slate. |
| 102 | cleanup 2>&1 |
| 103 | setup |
| 104 | |
| 105 | m_a_mod_build |
| 106 | load_dynamic_spans |
| 107 | test_asterisk_call |
| 108 | |
| 109 | # Final cleanup, because I don't want to leave the DAHDI modules loaded. |
| 110 | # Asterisk is left running. |
| 111 | cleanup |
| 112 | |
| 113 | if lsmod | grep dahdi; then |
| 114 | die "a DAHDI module is still loaded. Aborting" |
| 115 | fi |
| 116 | |
| 117 | exit 0 |
| 118 |