~ubuntu-calendar-dev/ubuntu-calendar-app/ViewRedisign

« back to all changes in this revision

Viewing changes to tests/autopilot/address_book_service_testability/fixture_setup.py

  • Committer: Akiva Avraham
  • Date: 2014-09-08 10:21:19 UTC
  • mfrom: (436 ubuntu-calendar-app)
  • mto: This revision was merged to the branch mainline in revision 437.
  • Revision ID: akiva@linux.com-20140908102119-gi68852t59kd5ep8
MergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2
 
#
3
 
# Copyright (C) 2014 Canonical
4
 
# Author: Omer Akram <omer.akram@canonical.com>
5
 
#
6
 
# This program is free software: you can redistribute it and/or modify
7
 
# it under the terms of the GNU General Public License as published by
8
 
# the Free Software Foundation, either version 3 of the License, or
9
 
# (at your option) any later version.
10
 
#
11
 
# This program is distributed in the hope that it will be useful,
12
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
# GNU General Public License for more details.
15
 
#
16
 
# You should have received a copy of the GNU General Public License
17
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
#
19
 
 
20
 
import os
21
 
import subprocess
22
 
import sysconfig
23
 
import time
24
 
 
25
 
from fixtures import EnvironmentVariable, Fixture
26
 
 
27
 
 
28
 
def get_service_library_path():
29
 
    """Return path of address-book-service binary directory."""
30
 
    architecture = sysconfig.get_config_var('MULTIARCH')
31
 
 
32
 
    return os.path.join(
33
 
        '/usr/lib/',
34
 
        architecture,
35
 
        'address-book-service/')
36
 
 
37
 
 
38
 
class AddressBookServiceDummyBackend(Fixture):
39
 
    """Fixture to load test vcard for client applications
40
 
 
41
 
    Call the fixture without any paramter to load a default vcard
42
 
 
43
 
    :parameter vcard: call the fixture with a vcard to be used by
44
 
                      test application.
45
 
 
46
 
    """
47
 
    def __init__(self, vcard=None):
48
 
        self.contact_data = vcard
49
 
 
50
 
    def setUp(self):
51
 
        super(AddressBookServiceDummyBackend, self).setUp()
52
 
        self.useFixture(SetupEnvironmentVariables(self.contact_data))
53
 
        self.useFixture(RestartService())
54
 
 
55
 
 
56
 
class SetupEnvironmentVariables(Fixture):
57
 
 
58
 
    def __init__(self, vcard):
59
 
        self.vcard = vcard
60
 
 
61
 
    def setUp(self):
62
 
        super(SetupEnvironmentVariables, self).setUp()
63
 
        self._setup_environment()
64
 
 
65
 
    def _setup_environment(self):
66
 
        self.useFixture(EnvironmentVariable(
67
 
            'ALTERNATIVE_CPIM_SERVICE_NAME', 'com.canonical.test.pim'))
68
 
        self.useFixture(EnvironmentVariable(
69
 
            'FOLKS_BACKEND_PATH',
70
 
            os.path.join(get_service_library_path(), 'dummy.so')))
71
 
        self.useFixture(EnvironmentVariable('FOLKS_BACKENDS_ALLOWED', 'dummy'))
72
 
        self.useFixture(EnvironmentVariable('FOLKS_PRIMARY_STORE', 'dummy'))
73
 
        self.useFixture(EnvironmentVariable(
74
 
            'ADDRESS_BOOK_SERVICE_DEMO_DATA',
75
 
            self._get_vcard_location()))
76
 
 
77
 
    def _get_vcard_location(self):
78
 
        if self.vcard:
79
 
            return self.vcard
80
 
 
81
 
        local_location = os.path.dirname(os.path.dirname(os.getcwd()))
82
 
        local_location = os.path.join(
83
 
            local_location,
84
 
            'tests/autopilot/address_book_service_testability/data/vcard.vcf')
85
 
        phablet_location = 'address_book_service_testability/data/vcard.vcf'
86
 
        bin_location = '/usr/share/address-book-service/data/vcard.vcf'
87
 
        cal_location = os.path.join('/usr/lib/python2.7/dist-packages/',
88
 
                                    'address_book_service_testability/data/',
89
 
                                    'vcard.vcf')
90
 
        if os.path.exists(local_location):
91
 
            print('Using %s for vcard' % local_location)
92
 
            return local_location
93
 
        elif os.path.exists(phablet_location):
94
 
            print('Using %s for vcard' % phablet_location)
95
 
            return phablet_location
96
 
        elif os.path.exists(cal_location):
97
 
            print('Using %s for vcard' % cal_location)
98
 
            return cal_location
99
 
        elif os.path.exists(bin_location):
100
 
            print('Using %s for vcard' % bin_location)
101
 
            return bin_location
102
 
        else:
103
 
            raise RuntimeError('No VCARD found in %s or %s or %s or %s' %
104
 
                               (local_location, bin_location,
105
 
                                cal_location, phablet_location))
106
 
 
107
 
 
108
 
class RestartService(Fixture):
109
 
 
110
 
    def setUp(self):
111
 
        super(RestartService, self).setUp()
112
 
        self.addCleanup(self._kill_address_book_service)
113
 
        self._restart_address_book_service()
114
 
 
115
 
    def _kill_address_book_service(self):
116
 
        try:
117
 
            pid = subprocess.check_output(
118
 
                ['pidof', 'address-book-service']).strip()
119
 
            subprocess.call(['kill', '-3', pid])
120
 
        except subprocess.CalledProcessError:
121
 
            # Service not running, so do nothing.
122
 
            pass
123
 
 
124
 
    def _restart_address_book_service(self):
125
 
        self._kill_address_book_service()
126
 
        path = os.path.join(
127
 
            get_service_library_path(), 'address-book-service')
128
 
 
129
 
        subprocess.Popen([path])
130
 
        # FIXME: Wait for 5 seconds before proceeding so that the
131
 
        # service starts,doing this because the dbus interface is
132
 
        # not reliable enough it seems. --om26er 23-07-2014
133
 
        time.sleep(5)