~allanlesage/uci-engine/coverage-extractor

« back to all changes in this revision

Viewing changes to coverage-retriever/coverageretriever/tests/__init__.py

  • Committer: Allan LeSage
  • Date: 2014-10-03 20:48:44 UTC
  • Revision ID: allan.lesage@canonical.com-20141003204844-r0aty01f0y2dm5ml
Initial coverage-retriever for review.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# Ubuntu CI Engine
 
3
#
 
4
# Copyright 2014 Canonical Ltd.
 
5
# This program is free software: you can redistribute it and/or modify it
 
6
# under the terms of the GNU Affero General Public License version 3, as
 
7
# published by the Free Software Foundation.
 
8
#
 
9
# This program is distributed in the hope that it will be useful, but
 
10
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
12
# PURPOSE.  See the GNU Affero General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU Affero General Public License
 
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
import logging
 
18
from mock import patch, MagicMock
 
19
import os
 
20
import unittest
 
21
import xml.etree.ElementTree as etree
 
22
 
 
23
from ci_utils.testing.fixtures import FakeDataStore
 
24
from ci_utils.amqp_worker import AMQPWorker
 
25
 
 
26
CURDIR = os.path.dirname(__file__)
 
27
 
 
28
 
 
29
class CoverageRetrieverWorkerTestCase(unittest.TestCase):
 
30
 
 
31
    def setUp(self):
 
32
        super(CoverageRetrieverWorkerTestCase, self).setUp()
 
33
        create_data_store_patch = patch.object(
 
34
            AMQPWorker, '_create_data_store')
 
35
        create_data_store = create_data_store_patch.start()
 
36
        self.addCleanup(create_data_store_patch.stop)
 
37
        self.fake_data_store = FakeDataStore('fake_ticket_id')
 
38
        self.addCleanup(self.fake_data_store.delete)
 
39
        create_data_store.return_value = self.fake_data_store
 
40
        self.fake_logger = MagicMock(spec=logging.Logger)
 
41
        # we'll need this for a couple of tests
 
42
        with open(os.path.join(CURDIR, 'build.log'), 'r') as f:
 
43
            self.build_log_text = f.read()
 
44
 
 
45
    def put_fake_build_log(self):
 
46
        """Put a fake log in our fake data store.
 
47
 
 
48
        Find this log at filename build.log, depends on finding
 
49
        build.log in tests dir.
 
50
        """
 
51
        self.fake_data_store.put_file(
 
52
            'build.log',
 
53
            self.build_log_text,
 
54
            content_type='text/plain')
 
55
 
 
56
    def validate_coverage_xml(self, coverage_xml):
 
57
        """Parse and verify our retrieved coverage.xml."""
 
58
        root_coverage_node = etree.fromstring(coverage_xml)
 
59
        self.assertAlmostEqual(
 
60
            0.6777005,
 
61
            float(root_coverage_node.attrib['line-rate']))