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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
# Implementation of scenario steps
## Server aliases
IMPLEMENTS GIVEN server is also known as (\S+)
h = yarnhelper.YarnHelper()
h.set_variable('ALIAS', h.get_next_match())
## HTTP requests
IMPLEMENTS WHEN user fetches (\S+)
h = yarnhelper.YarnHelper()
address = os.environ['ADDRESS']
url = h.get_next_match()
status, body = h.http_get(address, url)
h.set_variable('http_status', status)
h.set_variable('http_body', body)
IMPLEMENTS THEN HTTP status is (\d+)
h = yarnhelper.YarnHelper()
expected = h.get_variable('http_status')
actual = int(h.get_next_match())
h.assertEqual(expected, actual)
IMPLEMENTS THEN HTTP body matches "(.*)"
h = yarnhelper.YarnHelper()
body = h.get_variable('http_body')
sys.stdout.write('Body:\n{}'.format(repr(body)))
pattern = h.get_next_match()
m = re.search(pattern, body)
h.assertNotEqual(m, None)
## Running commands and checking results
IMPLEMENTS WHEN user runs Gitano (.*)
h = yarnhelper.YarnHelper()
address = os.environ['ADDRESS']
cmd = h.get_next_match()
argv = ['ssh', 'git@' + address] + cmd.split()
exit, out, err = cliapp.runcmd_unchecked(argv)
h.set_variable('argv', argv)
h.set_variable('exit', int(exit))
h.set_variable('stdout', out)
h.set_variable('stderr', err)
IMPLEMENTS WHEN user clones the (\S+) repository over git://
h = yarnhelper.YarnHelper()
address = os.environ['ADDRESS']
repo = h.get_next_match()
url = 'git://{}/{}'.format(address, repo)
argv = ['git', 'clone', url]
exit, out, err = cliapp.runcmd_unchecked(argv, cwd=os.environ['DATADIR'])
h.set_variable('argv', argv)
h.set_variable('exit', int(exit))
h.set_variable('stdout', out)
h.set_variable('stderr', err)
IMPLEMENTS THEN exit code is (\d+)
h = yarnhelper.YarnHelper()
code = h.get_next_match()
h.assertEqual(h.get_variable('exit'), int(code))
IMPLEMENTS THEN standard output matches "(.+)"
h = yarnhelper.YarnHelper()
pattern = h.get_next_match()
stdout = h.get_variable('stdout')
m = re.search(pattern, stdout)
print 'pattern:', repr(pattern)
print 'argv:', repr(h.get_variable('argv'))
print 'stdout:', repr(stdout)
h.assertNotEqual(m, None)
IMPLEMENTS THEN standard error matches "(.+)"
h = yarnhelper.YarnHelper()
pattern = h.get_next_match()
stderr = h.get_variable('stderr')
m = re.search(pattern, stderr)
print 'pattern:', repr(pattern)
print 'argv:', repr(h.get_variable('argv'))
print 'stderr:', repr(stderr)
h.assertNotEqual(m, None)
# Mail handling tests
## Generate a random number
IMPLEMENTS GIVEN large random number (\S+)
h = yarnhelper.YarnHelper()
varname = h.get_next_match()
mini = 2**32
maxi = 2**64
r = random.randint(mini, maxi)
h.set_variable(varname, str(r))
## Send mail over SMTP+TLS
IMPLEMENTS WHEN someone sends mail to (\S+) with subject \$(\S+)
import smtplib
h = yarnhelper.YarnHelper()
to_addr = h.get_next_match()
subject_key = h.get_next_match()
subject = h.get_variable(subject_key)
address = os.environ['ADDRESS']
from_addr = 'someone@example.com'
msg = 'From: {}\nTo: {}\nSubject: {}\n\nThis is the body\n'.format(
from_addr, to_addr, subject)
server = smtplib.SMTP()
server.connect(host=address, port=587)
server.set_debuglevel(True)
server.starttls()
server.sendmail(from_addr, to_addr, msg)
server.quit()
## Check for a mail in a mailbox
IMPLEMENTS THEN mailbox of (\S+) has a mail with subject \$(\S+)
h = yarnhelper.YarnHelper()
user = h.get_next_match()
subject_key = h.get_next_match()
subject = h.get_variable(subject_key)
print 'Wanted:', subject
address = os.environ['ADDRESS']
pass_name = 'pieni.net/{}'.format(user)
pass_home = os.environ['PASS_HOME']
password = h.get_password_with_pass(pass_home, pass_name)
got_it = False
def match(imapobj, msgid, msg):
global got_it
print 'Subject:', msg['Subject']
got_it = got_it or (msg['Subject'] == subject)
h.iterate_mails_in_imap_mailbox(address, user, password, match, False)
h.assertEqual(got_it, True)
## Delete mails
IMPLEMENTS THEN mails for (\S+) with subject \$(\S+) get deleted
h = yarnhelper.YarnHelper()
user = h.get_next_match()
subject_key = h.get_next_match()
subject = h.get_variable(subject_key)
print 'Wanted:', subject
address = os.environ['ADDRESS']
pass_name = 'pieni.net/{}'.format(user)
pass_home = os.environ['PASS_HOME']
password = h.get_password_with_pass(pass_home, pass_name)
def delete_matching(imapobj, msgid, msg):
if msg['Subject'] == subject:
imapobj.store(msgid, '+FLAGS', '(\\Deleted)')
h.iterate_mails_in_imap_mailbox(
address, user, password, delete_matching, True)
## Run sequence of SMTP commands, check SMTP status
IMPLEMENTS WHEN client SMTP authenticates as (\S+)
h = yarnhelper.YarnHelper()
user = h.get_next_match()
h.set_variable('smtp_username', user)
IMPLEMENTS WHEN client sends (.*)
h = yarnhelper.YarnHelper()
command = h.get_next_match()
smtp_commands = h.get_variable('smtp_commands', [])
smtp_commands.append(command)
h.set_variable('smtp_commands', smtp_commands)
IMPLEMENTS THEN SMTP status code is (\d+)
import smtplib
h = yarnhelper.YarnHelper()
wanted_status = int(h.get_next_match())
smtp_commands = h.get_variable('smtp_commands')
smtp_username = h.get_variable('smtp_username')
if smtp_username is not None:
pass_home = os.environ['PASS_HOME']
pass_user = 'pieni.net/{}'.format(smtp_username)
smtp_password = h.get_password_with_pass(pass_home, pass_user)
address = os.environ['ADDRESS']
server = smtplib.SMTP()
print 'connecting to', address
server.connect(host=address, port=587)
server.set_debuglevel(True)
print 'starttls'
server.starttls()
if smtp_username is not None:
print 'Authenticating as', smtp_username
print server.login(smtp_username, smtp_password)
for cmd in smtp_commands:
print 'Command:', cmd
actual_status, msg = server.docmd(cmd)
server.quit()
h.assertEqual(actual_status, wanted_status)
|