# Implementation of scenario steps ## Server aliases IMPLEMENTS GIVEN server is also known as (\S+) import yarnhelper h = yarnhelper.YarnHelper() h.set_variable('SERVER', h.get_next_match()) ## HTTP requests IMPLEMENTS WHEN user fetches (\S+) import yarnhelper h = yarnhelper.YarnHelper() server = h.get_variable('SERVER') url = h.get_next_match() status, body = h.http_get(server, url) h.set_variable('http_status', status) h.set_variable('http_body', body) IMPLEMENTS THEN HTTP status is (\d+) import yarnhelper h = yarnhelper.YarnHelper() expected = h.get_variable('http_status') actual = int(h.get_next_match()) h.assertEqual(expected, actual) IMPLEMENTS THEN HTTP body matches "(.*)" import re, yarnhelper h = yarnhelper.YarnHelper() body = h.get_variable('http_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 (.*) import os, re, cliapp, yarnhelper h = yarnhelper.YarnHelper() server = os.environ['SERVER'] cmd = h.get_next_match() argv = ['ssh', 'git@' + server] + 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:// import os, re, cliapp, yarnhelper h = yarnhelper.YarnHelper() server = os.environ['SERVER'] repo = h.get_next_match() url = 'git://{}/{}'.format(server, 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+) import yarnhelper h = yarnhelper.YarnHelper() code = h.get_next_match() h.assertEqual(h.get_variable('exit'), int(code)) IMPLEMENTS THEN standard output matches "(.+)" import re, cliapp, yarnhelper 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 "(.+)" import re, cliapp, yarnhelper 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+) import yarnhelper, random 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, sys, yarnhelper h = yarnhelper.YarnHelper() to_addr = h.get_next_match() subject_key = h.get_next_match() server_name = h.get_variable('SERVER') 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_name) 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+) import email, imaplib, subprocess, yarnhelper h = yarnhelper.YarnHelper() user = h.get_next_match() subject = h.get_next_match() server = h.get_variable('SERVER') pass_name = 'server-yarns/imap/{}@{}'.format(user, server) p = subprocess.Popen(['pass', 'show', pass_name], stdout=subprocess.PIPE) stdout, stderr = p.communicate() password = stdout.rstrip() m = imaplib.IMAP4_SSL(server) m.login(user, password) m.select('INBOX', True) typ, data = m.search(None, 'ALL') got_it = False for num in data[0].split(): typ, data = m.fetch(num, '(RFC822)') typ, text = data[0] msg = email.message_from_string(text) got_it = got_it or (msg['Subject'] == subject) m.close() m.logout() h.assertEqual(got_it, True)