3
3
import pathlib
4
4
import sqlite3
5
5
import time
6
+ import sys
6
7
7
8
import pytest
8
9
import pytest_twisted as pytest_tw
11
12
from twisted .internet import task
12
13
from twisted .internet import endpoints
13
14
from twisted .internet .error import ProcessDone
15
+ from twisted import logger
14
16
15
17
import clients
16
-
17
-
18
- HERE = pathlib .Path (__name__ ).parent .resolve ()
19
- SCHEMA = HERE .parent .joinpath ('db.schema' )
20
-
21
- SERVER = 'localhost'
22
- SERVER_PORT = 6667
23
- INFOBOB_NICK = 'infotest'
24
- INFOBOB_PASS = 'infotestpass'
25
- INFOBOB_WEB_PORT = 8888
18
+ from config import (
19
+ INFOBOB_PYTHON ,
20
+ INFOTEST ,
21
+ MONITOR ,
22
+ SCHEMA_PATH ,
23
+ IRCD_HOST ,
24
+ IRCD_PORT ,
25
+ WEBUI_PORT ,
26
+ )
27
+
28
+
29
+ @pytest .fixture (scope = 'session' , autouse = True )
30
+ def fixture_start_logging ():
31
+ """
32
+ Start up twisted.logger machinery.
33
+ """
34
+ observers = [logger .textFileLogObserver (sys .stderr )]
35
+ logger .globalLogBeginner .beginLoggingTo (
36
+ observers , redirectStandardIO = False )
26
37
27
38
28
39
@pytest .fixture (name = 'start_infobob' )
29
40
def fixture_start_infobob (tmp_path ):
30
- infobob_python = pathlib .Path (os .environ ['INFOBOB_PYTHON' ]).resolve ()
31
- infobob_twistd = str (infobob_python .parent .joinpath ('twistd' ))
41
+ infobob_twistd = str (INFOBOB_PYTHON .parent .joinpath ('twistd' ))
32
42
called = False
33
43
spawned = None
34
44
ended = None
@@ -44,11 +54,11 @@ def start_infobob(channelsconf, autojoin):
44
54
dbpath = tmp_path .joinpath ('infobob.db' )
45
55
conf = {
46
56
'irc' : {
47
- 'server' : SERVER ,
48
- 'port' : SERVER_PORT ,
57
+ 'server' : IRCD_HOST ,
58
+ 'port' : IRCD_PORT ,
49
59
'ssl' : False ,
50
- 'nickname' : INFOBOB_NICK ,
51
- 'password' : INFOBOB_PASS ,
60
+ 'nickname' : INFOTEST . nickname ,
61
+ 'password' : INFOTEST . password ,
52
62
'nickserv_pw' : None ,
53
63
'autojoin' : autojoin ,
54
64
},
@@ -61,18 +71,20 @@ def start_infobob(channelsconf, autojoin):
61
71
** channelsconf ,
62
72
},
63
73
'database' : {'sqlite' : {'db_file' : dbpath .name }},
64
- 'web' : {'port' : INFOBOB_WEB_PORT },
74
+ 'web' : {
75
+ 'port' : WEBUI_PORT ,
76
+ 'url' : f'http://localhost:{ WEBUI_PORT } ' ,
77
+ },
65
78
'misc' : {'manhole' : {'socket' : None }},
66
79
}
67
80
conn = sqlite3 .connect (str (dbpath ))
68
81
with conn :
69
- conn .executescript (SCHEMA .read_text ())
82
+ conn .executescript (SCHEMA_PATH .read_text ())
70
83
conn .close ()
71
84
confpath .write_text (json .dumps (conf ))
72
85
from twisted .internet import reactor
73
86
proto = InfobobProcessProtocol ()
74
87
args = [infobob_twistd , '-n' , 'infobob' , confpath .name ]
75
- print ('running' , args )
76
88
childFDs = {
77
89
0 : open (os .devnull , 'rb' ).fileno (),
78
90
#1: open(os.devnull, 'wb').fileno(),
@@ -101,39 +113,46 @@ def cbStop(procTransport):
101
113
102
114
103
115
class InfobobProcessProtocol (protocol .ProcessProtocol ):
116
+ log = logger .Logger ()
117
+
104
118
def __init__ (self ):
105
119
self .ended = defer .Deferred ()
106
120
107
121
def connectionMade (self ):
108
- print ('Infobob started' )
122
+ self . log . info ('Infobob started' )
109
123
self .transport .closeStdin ()
110
124
111
125
def outReceived (self , data : bytes ):
112
- print ( 'Got chunk of stdout:' , data .decode ('utf-8' ).rstrip ('\r \n ' ))
126
+ self . log . info ( " stdout: " + data .decode ('utf-8' ).rstrip ('\r \n ' ))
113
127
114
128
def errReceived (self , data : bytes ):
115
- print ( 'Got chunk of stderr:' , data .decode ('utf-8' ).rstrip ('\r \n ' ))
129
+ self . log . info ( " stderr: " + data .decode ('utf-8' ).rstrip ('\r \n ' ))
116
130
117
131
def processEnded (self , status ):
118
- print ('Infobob exited' , status .value )
119
132
if status .check (ProcessDone ) is None :
133
+ self .log .warn ('Infobob exited: {status}' , status = status )
120
134
self .ended .errback (status )
121
135
else :
136
+ self .log .info ('Infobob exited cleanly' )
122
137
self .ended .callback (None )
123
138
124
139
125
140
@pytest_tw .inlineCallbacks
126
141
def test_infobob_basic (start_infobob ):
127
142
from twisted .internet import reactor
128
143
129
- monitor = clients .ComposedIRCClientFactory ('monitor' )
144
+ monitor = clients .ComposedIRCClientFactory (
145
+ MONITOR .nickname , MONITOR .password )
130
146
endpoint = endpoints .TCP4ClientEndpoint (
131
- reactor , SERVER , SERVER_PORT , timeout = 5 )
147
+ reactor , IRCD_HOST , IRCD_PORT , timeout = 5 )
132
148
yield endpoint .connect (monitor ).addCallback (lambda p : p .signOnComplete )
133
149
yield defer .gatherResults ([
134
150
monitor .joinChannel ('#project' ),
135
151
monitor .joinChannel ('##offtopic' ),
136
152
])
137
153
138
- start_infobob (channelsconf = {}, autojoin = ['#project' , '##offtopic' ])
139
- yield task .deferLater (reactor , 5 , lambda : None )
154
+ start_infobob (channelsconf = {
155
+ '#project' : {'have_ops' : True },
156
+ '##offtopic' : {'have_ops' : True },
157
+ }, autojoin = ['#project' , '##offtopic' ])
158
+ yield task .deferLater (reactor , 300 , lambda : None )
0 commit comments