~brendan-donegan/qakit/checkbox_testplan

« back to all changes in this revision

Viewing changes to qakit/practitest/export_checkbox_practitest.py

  • Committer: Brendan Donegan
  • Date: 2015-07-06 08:16:15 UTC
  • Revision ID: brendan.donegan@canonical.com-20150706081615-zlzemqoudlfiypvx
First draft of export_checkbox_practitest

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os.path
 
2
import sys
 
3
import tempfile
 
4
import urllib.request
 
5
import xlrd
 
6
import zipfile
 
7
 
 
8
from argparse import ArgumentParser
 
9
 
 
10
from qakit.practitest import practitest
 
11
from qakit.practitest.report_subunit_results_to_practitest import _read_config 
 
12
 
 
13
 
 
14
def steps_for_test(sheet, test_name):
 
15
    # Find the test steps in the sheet
 
16
    steps = ''
 
17
    step_number = 1
 
18
    for row in range(1, sheet.nrows):
 
19
        row_values = sheet.row(row)
 
20
        if row_values[1].value == test_name:
 
21
            steps += ' {}.'.format(step_number)
 
22
            step_number += 1
 
23
            if row_values[3].value:
 
24
                for line in row_values[3].value.split('\n'):
 
25
                    if not line:
 
26
                        line = '.'
 
27
                    steps += ' ' + line + '\n'
 
28
            if row_values[4].value:
 
29
                for line in row_values[4].value.split('\n'):
 
30
                    if not line:
 
31
                        line = '.'
 
32
                    steps += '  ' + line + '\n'
 
33
    return steps
 
34
 
 
35
def main():
 
36
    parser = ArgumentParser(
 
37
        'Create a Checkbox provider from a Practitest testset'
 
38
    )
 
39
    parser.add_argument('testset', help='The ID of the testset to use.')
 
40
    parser.add_argument(
 
41
        '--whitelist',
 
42
        action='store_true',
 
43
        help='Generate whitelist instead' # REMOVE
 
44
    )
 
45
    args = parser.parse_args()
 
46
    # These are the instances that haven't been run
 
47
    pt = practitest.PractitestSession(
 
48
        **_read_config(os.path.join(os.path.dirname(__name__), 'config.ini'))
 
49
    )
 
50
    instances = pt.get_instances(args.testset)
 
51
    notrun = [instance for instance in instances if instance['run_status']['value'] not in ('PASSED','FAILED')]
 
52
    # This does the test step export
 
53
    export = pt.get_export(410)
 
54
    url = export.json()['url']
 
55
    tempdir = tempfile.mkdtemp()
 
56
    r = urllib.request.urlretrieve(url, os.path.join(tempdir, 'steps.zip'))
 
57
    # Now we extract the spreadsheet from the zip
 
58
    stepszip = open(r[0], 'rb')
 
59
    z = zipfile.ZipFile(stepszip)
 
60
    xlsx = ''
 
61
    for name in z.namelist():
 
62
        if name.endswith('.xlsx'):
 
63
            z.extract(name, tempdir)
 
64
            xlsx = os.path.join(tempdir, name)
 
65
    # Now we open the spreadsheet and get the tests sheet
 
66
    sheet = xlrd.open_workbook(xlsx).sheets()[0]
 
67
    # First we create the provider file
 
68
    with open(os.path.join(tempdir, 'practitest.provider'), 'w') as provider:
 
69
        provider.write("""[PlainBox Provider]
 
70
description = The Practitest provider
 
71
location = {}
 
72
name = 2015.com.canonical:qa-practitest
 
73
version = 1.0""".format(tempdir))
 
74
    # Now we create the jobs and whitelist
 
75
    if not os.path.exists(os.path.join(tempdir, 'jobs')):
 
76
        os.makedirs(os.path.join(tempdir, 'jobs'))
 
77
    if not os.path.exists(os.path.join(tempdir, 'whitelists')):
 
78
        os.makedirs(os.path.join(tempdir, 'whitelists'))
 
79
    jobs = open(os.path.join(tempdir, 'jobs', 'practitest.txt'), 'w')
 
80
    whitelist = open(
 
81
        os.path.join(tempdir, 'whitelists', 'practitest.whitelist'), 'w'
 
82
    )
 
83
    for test in notrun:
 
84
        test_name = ''.join(
 
85
            [c for c in test['name'] if c.isalnum() or c is ' ']
 
86
        )
 
87
        cb_id = 'practitest/{}/{}'.format(
 
88
            '-'.join([word.lower() for word in test_name.split()]),
 
89
            test['id']
 
90
        )
 
91
        whitelist.write(cb_id + '\n')
 
92
        jobs.write('plugin: manual\n')
 
93
        jobs.write('id: {}\n'.format(cb_id))
 
94
        jobs.write('summary: {}\n'.format(test['name']))
 
95
        jobs.write('description:\n')
 
96
        jobs.write(steps_for_test(sheet, test['name']))
 
97
        jobs.write('\n')
 
98
    jobs.close()
 
99
    whitelist.close()
 
100
    print(tempdir)
 
101
 
 
102
if __name__ == "__main__":
 
103
    sys.exit(main())