~allanlesage/+junk/test-here

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
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
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-

#
# Ubuntu System Tests
# Copyright (C) 2015 Canonical
#
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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 the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import fixtures

from ubuntu_system_tests import config
from ubuntu_system_tests.helpers import testbed as tb

STOP_SERVICE_COMMAND = '/sbin/initctl stop ubuntu-location-service'
START_SERVICE_COMMAND = '/sbin/initctl start ubuntu-location-service'
UPSTART_OVERRIDE_FILE = '/etc/init/ubuntu-location-service.override'


def _get_location_service_exec_command(latitude, longitude):
    """Return the command to start the dummy location backend."""
    return '/usr/bin/ubuntu-location-serviced ' \
           '--bus system ' \
           '--provider dummy::Provider ' \
           '--dummy::Provider::ReferenceLocationLat={} ' \
           '--dummy::Provider::ReferenceLocationLon={}'.format(latitude,
                                                               longitude)


def _get_location_upstart_job_override_exec_command(latitude, longitude):
    """Return the command to put into the override upstart job to
    load the dummy backend.

    """
    command = _get_location_service_exec_command(latitude, longitude)
    return 'echo \"exec {}\"'.format(command)


class DummyLocation(fixtures.Fixture):
    """Fixture to get a default location fix."""

    def __init__(self, latitude, longitude):
        self.latitude = latitude
        self.longitude = longitude

    def setUp(self):
        super().setUp()
        config_stack = config.get_device_config_stack()
        self.password = config_stack.get('device_password')
        with tb.AskPass(self.password):
            self.stop_location_service()
            self.start_location_service_with_dummy_fix()

    def stop_location_service(self):
        """
        Stop the already running location service
        """
        tb.run_command_with_sudo(STOP_SERVICE_COMMAND)
        self.addCleanup(tb.run_command_with_sudo, START_SERVICE_COMMAND)

    def start_location_service(self):
        """
        Start the location service
        """
        tb.run_command_with_sudo(START_SERVICE_COMMAND)
        self.addCleanup(tb.run_command_with_sudo, STOP_SERVICE_COMMAND)

    def start_location_service_with_dummy_fix(self):
        """
        Start the location service with the dummy backend
        """
        tb.make_system_writable()
        self.addCleanup(tb.make_system_read_only)
        self.add_upstart_override()
        # Cause minimum disruption, just add the override file and
        # make the system read only, to have a close-to-stock environment.
        tb.make_system_read_only()
        self.start_location_service()

    def clear_upstart_override_file(self):
        """Remove the upstart override file to load the dummy backend."""
        tb.make_system_writable()
        remove_override_file_command = 'rm {}'.format(UPSTART_OVERRIDE_FILE)
        tb.run_command_with_sudo(remove_override_file_command)

    def add_upstart_override(self):
        """Add an upstart override file in /etc/init to override the
        exec command for the location service to be able to load the dummy
        backend."""
        override_command = _get_location_upstart_job_override_exec_command(
            self.latitude, self.longitude)
        tb.add_upstart_override_for_job(UPSTART_OVERRIDE_FILE,
                                        override_command)
        self.addCleanup(self.clear_upstart_override_file)