summaryrefslogtreecommitdiffstats
path: root/debian/tests/test-simple
blob: 1eeb8dd0bebcc6c6ba3def6fd492feff82500e27 (plain)
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
112
113
114
115
116
117
118
#!/bin/sh

# Simple test that we can connect to a test server, and send a message with irk
# We use irclib to provide the test server and watch a test client to ensure that
# the correct message is broadcast to the correct channels

set -e

WORKDIR=$(mktemp -d)
trap "rm -rf $WORKDIR" 0 INT QUIT ABRT PIPE TERM
cd $WORKDIR

# setup test client
cat << EOF > testclient.py
# Simple test case for irker
# Based on irccat.py in the irclib package by Joel Rosdahl <joel@rosdahl.net>

import sys

import irc.client

logfile = None

def on_connect(connection, event):
    connection.join("#testchan")

def on_pubmsg(connection, event):
    logfile.write(event.arguments[0])
    logfile.write("\n")
    logfile.write(event.source)
    logfile.write("\n")
    logfile.flush()

def on_disconnect(connection, event):
    raise SystemExit()

def main():
    global logfile
    client = irc.client.IRC()
    try:
        c = client.server().connect("localhost", 6667, "testclient")
    except irc.client.ServerConnectionError:
        print(sys.exc_info()[1])
        raise SystemExit(1)

    logfile = open(sys.argv[1], 'w')

    c.add_global_handler("welcome", on_connect)
    c.add_global_handler("pubmsg", on_pubmsg)

    client.process_forever()

if __name__ == '__main__':
    main()
EOF

# This is needed to make irkerd start on some lxc containers, otherwise
# the irker user can't execute anything, but it will fail in other
# cases, so we make it non-fatal
chmod 755 / || echo "chmod failed - assuming everything is fine"

# Ensure irkerd is running
systemctl restart irkerd

python3 -m irc.server 2>server.log &
sleep 3
python3 ./testclient.py client.log &
# Give time for everything to start up and connect
sleep 3

# Send a message that the client should see
irk 'irc://localhost:6667/testchan' 'Test message 1'
sleep 1
# This should not show up in the client log
irk 'irc://localhost:6667/differentchan' 'Test message 2'
sleep 1
# this should also be seen
irk 'irc://localhost:6667/testchan' 'Test message 3'
sleep 1

# Inspect client log
# check that we saw irker connect
grep -q 'irker[0-9]' client.log

grep -q 'Test message 1' client.log
grep -q 'Test message 3' client.log

if grep -q 'Test message 2' client.log; then
   echo "Found unexpected 'Test message 2' in client.log"
   exit 1
fi

# Manually run irkerhook.py, to ensure that works
# We create a git repo for this, since irkerhook needs a repo to function
mkdir test_repo
cd test_repo
# Redirect git warnings so they're not fatal
git init 2> git.log
git config user.email 'test@localhost'
git config user.name 'Test Author'
git config irker.channels 'irc://localhost:6667/testchan'
touch start
git add start
git commit -m "Test commit" ./start
# Check that irkerhook outputs sane info
irkerhook -n | grep -q '"privmsg": "test_repo: Test Author test_repo:.* : Test commit "'
# Check that irkerhook sends to the right place
irkerhook
# We need a moment for the irc server to process everything
sleep 1
cd ..
grep -q 'test_repo: Test Author test_repo:.* : Test commit' client.log

# kill background tasks
pkill -f testclient.py
pkill -f irc.server

echo "Simple irker daemon tests succeeded"