• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#
3#   Copyright 2017 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17
18class FakeResult(object):
19    """A fake version of the object returned from ShellCommand.run. """
20
21    def __init__(self, exit_status=0, stdout='', stderr=''):
22        self.exit_status = exit_status
23        self.stdout = stdout
24        self.stderr = stderr
25
26
27class MockShellCommand(object):
28    """A fake ShellCommand object.
29
30    Attributes:
31        fake_result: a FakeResult object, or a list of FakeResult objects
32        fake_pids: a dictionary that maps string identifier to list of pids
33    """
34
35    def __init__(self, fake_result=None, fake_pids=[]):
36        self._fake_result = fake_result
37        self._fake_pids = fake_pids
38        self._counter = 0
39
40    def run(self, command, timeout=3600, ignore_status=False):
41        """Returns a FakeResult object.
42
43        Args:
44            Same as ShellCommand.run, but none are used in function
45
46        Returns:
47            The FakeResult object it was initalized with. If it was initialized
48            with a list, returns the next element in list and increments counter
49
50        Raises:
51          IndexError: Function was called more times than num elements in list
52
53        """
54        if isinstance(self._fake_result, list):
55            self._counter += 1
56            return self._fake_result[self._counter - 1]
57        else:
58            return self._fake_result
59
60    def get_command_pids(self, identifier):
61        """Returns a generator of fake pids
62
63        Args:
64          Same as ShellCommand.get_pids, but none are used in the function
65        Returns:
66          A generator of the fake pids it was initialized with
67        """
68
69        for pid in self._fake_pids[identifier]:
70            yield pid
71