summaryrefslogtreecommitdiff
path: root/tests/integration/tests/util/report.py
blob: c3c1007cf32a4b844c3f76c67c959e7f195c2cee (plain)
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
"""Creates a report for the test.
"""

import os
import shutil
from os import path
from trove.tests.config import CONFIG

USE_LOCAL_OVZ = CONFIG.use_local_ovz


class Reporter(object):
    """Saves the logs from a test run."""

    def __init__(self, root_path):
        self.root_path = root_path
        if not path.exists(self.root_path):
            os.mkdir(self.root_path)
        for file in os.listdir(self.root_path):
            if file.endswith(".log"):
                os.remove(path.join(self.root_path, file))

    def _find_all_instance_ids(self):
        instances = []
        if USE_LOCAL_OVZ:
            for dir in os.listdir("/var/lib/vz/private"):
                instances.append(dir)
        return instances

    def log(self, msg):
        with open("%s/report.log" % self.root_path, 'a') as file:
            file.write(str(msg) + "\n")

    def _save_syslog(self):
        try:
            shutil.copyfile("/var/log/syslog", "host-syslog.log")
        except (shutil.Error, IOError) as err:
            self.log("ERROR logging syslog : %s" % (err))

    def _update_instance(self, id):
        root = "%s/%s" % (self.root_path, id)

        def save_file(path, short_name):
            if USE_LOCAL_OVZ:
                try:
                    shutil.copyfile("/var/lib/vz/private/%s/%s" % (id, path),
                                    "%s-%s.log" % (root, short_name))
                except (shutil.Error, IOError) as err:
                    self.log("ERROR logging %s for instance id %s! : %s"
                             % (path, id, err))
            else:
                #TODO: Can we somehow capture these (maybe SSH to the VM)?
                pass

        save_file("/var/log/firstboot", "firstboot")
        save_file("/var/log/syslog", "syslog")
        save_file("/var/log/nova/guest.log", "nova-guest")

    def _update_instances(self):
        for id in self._find_all_instance_ids():
            self._update_instance(id)

    def update(self):
        self._update_instances()
        self._save_syslog()


REPORTER = Reporter(CONFIG.report_directory)


def log(msg):
    REPORTER.log(msg)


def update():
    REPORTER.update()