~lutostag/ubuntu/trusty/maas/1.5.4+keystone

« back to all changes in this revision

Viewing changes to src/maastesting/fixtures.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2013-03-04 11:49:44 UTC
  • mto: This revision was merged to the branch mainline in revision 25.
  • Revision ID: package-import@ubuntu.com-20130304114944-azcvu9anlf8mizpa
Tags: upstream-1.3+bzr1452+dfsg
ImportĀ upstreamĀ versionĀ 1.3+bzr1452+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
    ]
19
19
 
20
20
import logging
 
21
import os
 
22
from subprocess import (
 
23
    CalledProcessError,
 
24
    PIPE,
 
25
    Popen,
 
26
    )
21
27
 
22
28
from fixtures import (
23
29
    EnvironmentVariableFixture,
24
30
    Fixture,
 
31
    TempDir,
25
32
    )
26
 
from pyvirtualdisplay import Display
27
33
from sst.actions import (
28
34
    start,
29
35
    stop,
51
57
 
52
58
 
53
59
class DisplayFixture(Fixture):
54
 
    """Fixture to create a virtual display with pyvirtualdisplay.Display."""
55
 
 
56
 
    logger_names = ['easyprocess', 'pyvirtualdisplay']
57
 
 
58
 
    def __init__(self, visible=False, size=(1280, 1024)):
 
60
    """Fixture to create a virtual display with `xvfb-run`.
 
61
 
 
62
    This will set the ``DISPLAY`` environment variable once it's up and
 
63
    running (and reset it when it shuts down).
 
64
    """
 
65
 
 
66
    def __init__(self, size=(1280, 1024), depth=24):
59
67
        super(DisplayFixture, self).__init__()
60
 
        self.visible = visible
61
 
        self.size = size
 
68
        self.width, self.height = size
 
69
        self.depth = depth
 
70
 
 
71
    @property
 
72
    def command(self):
 
73
        """The command this fixture will start.
 
74
 
 
75
        ``xvfb-run`` is the executable used, to which the following arguments
 
76
        are passed:
 
77
 
 
78
          ``--server-args=``
 
79
            ``-ac`` disables host-based access control mechanisms. See
 
80
              Xserver(1).
 
81
            ``-screen`` forces a screen configuration. At the time of writing
 
82
               there is some disagreement between xvfb-run(1) and Xvfb(1)
 
83
               about what the default is.
 
84
 
 
85
          ``--auto-servernum``
 
86
            Try to get a free server number, starting at 99. See xvfb-run(1).
 
87
 
 
88
        ``xvfb-run`` is asked to chain to ``bash``, which echos the
 
89
        ``DISPLAY`` environment variable and execs ``cat``. This lets us shut
 
90
        down the framebuffer simply by closing the process's stdin.
 
91
        """
 
92
        spec = "{self.width}x{self.height}x{self.depth}".format(self=self)
 
93
        args = "-ac -screen 0 %s" % spec
 
94
        return (
 
95
            "xvfb-run", "--server-args", args, "--auto-servernum", "--",
 
96
            "bash", "-c", "echo $DISPLAY && exec cat",
 
97
            )
62
98
 
63
99
    def setUp(self):
64
100
        super(DisplayFixture, self).setUp()
65
 
        self.useFixture(LoggerSilencerFixture(self.logger_names))
66
 
        self.display = Display(
67
 
            visible=self.visible, size=self.size)
68
 
        self.display.start()
69
 
        self.addCleanup(self.display.stop)
 
101
        self.process = Popen(self.command, stdin=PIPE, stdout=PIPE)
 
102
        self.display = self.process.stdout.readline().strip()
 
103
        if not self.display or self.process.poll() is not None:
 
104
            raise CalledProcessError(self.process.returncode, self.command)
 
105
        self.useFixture(EnvironmentVariableFixture("DISPLAY", self.display))
 
106
        self.addCleanup(self.shutdown)
 
107
 
 
108
    def shutdown(self):
 
109
        self.process.stdin.close()
 
110
        if self.process.wait() != 0:
 
111
            raise CalledProcessError(self.process.returncode, self.command)
70
112
 
71
113
 
72
114
class SSTFixture(Fixture):
91
133
        super(ProxiesDisabledFixture, self).setUp()
92
134
        self.useFixture(EnvironmentVariableFixture("http_proxy"))
93
135
        self.useFixture(EnvironmentVariableFixture("https_proxy"))
 
136
 
 
137
 
 
138
class TempWDFixture(TempDir):
 
139
    """Change the current working directory into a temp dir.
 
140
 
 
141
    This will restore the original WD and delete the temp directory on cleanup.
 
142
    """
 
143
 
 
144
    def setUp(self):
 
145
        cwd = os.getcwd()
 
146
        super(TempWDFixture, self).setUp()
 
147
        self.addCleanup(os.chdir, cwd)
 
148
        os.chdir(self.path)