avocado-journal-replay 5.1 KB
Newer Older
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
#!/usr/bin/env python

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; specifically version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See LICENSE for more details.
#
# Copyright: Red Hat Inc. 2014
# Author: Cleber Rosa <cleber@redhat.com>

"""
Avocado Journal Replay Utility. Sends test journal data to an Avocado Server.
"""

import sys
import sqlite3
import argparse

import requests


class ArgumentParser(argparse.ArgumentParser):

    def __init__(self):
        super(ArgumentParser, self).__init__(
            prog='avocado-journal-replay',
32
            description=('Avocado Test Journal Replay. Sends test journal data '
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
                         'to an Avocado Server'))

        self.add_argument('-s', '--server-uri',
                          help=('Avocado server http URI. Example: '
                                'http://avocado-server:8000'))
        self.add_argument('-u', '--username',
                          help='Username to authenticate to Avocado Server')
        self.add_argument('-p', '--password',
                          help='Password to authenticate to Avocado Server')
        self.add_argument('-n', '--job-name',
                          help='Name to give to job on the Avocado Server')
        self.add_argument('-j', '--journal-path',
                          help='Path to the journal file (.journal.sqlite)')


class App(object):

    def __init__(self):
        self.app_parser = ArgumentParser()
        self.args = None

    def replay_test_activity(self, job, tag, time, action, status):
        action_uri = 'jobs/%s/test_activity' % job
        full_uri = "%s/%s/" % (self.args.server_uri, action_uri)

        data = {'activity': action,
                'status': status,
                'job': job,
                'time': time,
                'test_tag': tag}

        r = requests.post(full_uri,
                          auth=(self.args.username,
                                self.args.password),
                          data=data)

        if r.status_code == 200:
            return True
        else:
            return False

    def create_job(self, unique_id):
        action_uri = 'jobs'
        full_uri = "%s/%s/" % (self.args.server_uri, action_uri)

        data = {'name': self.args.job_name,
                'uniqueident': str(unique_id)}

        r = requests.post(full_uri,
                          auth=(self.args.username,
                                self.args.password),
                          data=data)

        if r.status_code == 201:
            return r.json()['id']
        else:
            print("Failed to add job. reason(s): %s" % r.text)
            return False

    def replay(self):
        journal = sqlite3.connect(self.args.journal_path)
        journal_cursor = journal.cursor()
        job_unique_id_sql = 'SELECT unique_id FROM job_info'
        job_unique_id = journal_cursor.execute(job_unique_id_sql).fetchone()[0]

        job_result = self.create_job(job_unique_id)
        if not job_result:
            return -1

        while True:
            journal = sqlite3.connect(self.args.journal_path)
            journal_cursor = journal.cursor()

            backlog_count_sql = "SELECT COUNT(*) FROM test_journal WHERE flushed==0"
            backlog_count = journal_cursor.execute(backlog_count_sql).fetchone()[0]

            journal_cursor.close()
            journal.close()

            if backlog_count == 0:
                break

            entry_sql = ("SELECT tag, time, action, status "
                         "FROM test_journal WHERE flushed==0 "
                         "ORDER BY time LIMIT 1")

            journal = sqlite3.connect(self.args.journal_path)
            journal_cursor = journal.cursor()
            entry = journal_cursor.execute(entry_sql).fetchone()
            journal_cursor.close()
            journal.close()

            tag, time, action, status = entry
            activity_result = self.replay_test_activity(job_result,
                                                        tag,
                                                        time,
                                                        action,
                                                        status)
            if activity_result:
                flush_sql = ("UPDATE test_journal SET flushed=1 WHERE "
                             "tag=='%s' AND action=='%s' AND flushed==0")
                flush_sql = flush_sql % (tag, action)

                journal = sqlite3.connect(self.args.journal_path)
                journal_cursor = journal.cursor()
138
                journal_cursor.execute(flush_sql)
139 140 141 142 143 144 145 146 147 148 149 150
                journal.commit()
                journal_cursor.close()
                journal.close()

    def run(self):
        self.args = self.app_parser.parse_args()
        return self.replay()


if __name__ == '__main__':
    app = App()
    sys.exit(app.run())