aboutsummaryrefslogtreecommitdiff
path: root/deprecated/automation/common/command_executer_test.py
blob: 2caaa1460e99a24cc16d54cf5919267133ca150d (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/python2
#
# Copyright 2011 Google Inc. All Rights Reserved.
#

__author__ = 'kbaclawski@google.com (Krystian Baclawski)'

import cStringIO
import logging
import os
import signal
import socket
import sys
import time
import unittest


def AddScriptDirToPath():
  """Required for remote python script execution."""
  path = os.path.abspath(__file__)

  for _ in range(3):
    path, _ = os.path.split(path)

  if not path in sys.path:
    sys.path.append(path)


AddScriptDirToPath()

from automation.common.command_executer import CommandExecuter


class LoggerMock(object):

  def LogCmd(self, cmd, machine='', user=''):
    if machine:
      logging.info('[%s] Executing: %s', machine, cmd)
    else:
      logging.info('Executing: %s', cmd)

  def LogError(self, msg):
    logging.error(msg)

  def LogWarning(self, msg):
    logging.warning(msg)

  def LogOutput(self, msg):
    logging.info(msg)


class CommandExecuterUnderTest(CommandExecuter):

  def __init__(self):
    CommandExecuter.__init__(self, logger_to_set=LoggerMock())

    # We will record stdout and stderr.
    self._stderr = cStringIO.StringIO()
    self._stdout = cStringIO.StringIO()

  @property
  def stdout(self):
    return self._stdout.getvalue()

  @property
  def stderr(self):
    return self._stderr.getvalue()

  def DataReceivedOnOutput(self, data):
    self._stdout.write(data)

  def DataReceivedOnError(self, data):
    self._stderr.write(data)


class CommandExecuterLocalTests(unittest.TestCase):
  HOSTNAME = None

  def setUp(self):
    self._executer = CommandExecuterUnderTest()

  def tearDown(self):
    pass

  def RunCommand(self, method, **kwargs):
    program = os.path.abspath(sys.argv[0])

    return self._executer.RunCommand('%s runHelper %s' % (program, method),
                                     machine=self.HOSTNAME,
                                     **kwargs)

  def testCommandTimeout(self):
    exit_code = self.RunCommand('SleepForMinute', command_timeout=3)

    self.assertTrue(-exit_code in [signal.SIGTERM, signal.SIGKILL],
                    'Invalid exit code: %d' % exit_code)

  def testCommandTimeoutIfSigTermIgnored(self):
    exit_code = self.RunCommand('IgnoreSigTerm', command_timeout=3)

    self.assertTrue(-exit_code in [signal.SIGTERM, signal.SIGKILL])

  def testCommandSucceeded(self):
    self.assertFalse(self.RunCommand('ReturnTrue'))

  def testCommandFailed(self):
    self.assertTrue(self.RunCommand('ReturnFalse'))

  def testStringOnOutputStream(self):
    self.assertFalse(self.RunCommand('EchoToOutputStream'))
    self.assertEquals(self._executer.stderr, '')
    self.assertEquals(self._executer.stdout, 'test')

  def testStringOnErrorStream(self):
    self.assertFalse(self.RunCommand('EchoToErrorStream'))
    self.assertEquals(self._executer.stderr, 'test')
    self.assertEquals(self._executer.stdout, '')

  def testOutputStreamNonInteractive(self):
    self.assertFalse(
        self.RunCommand('IsOutputStreamInteractive'),
        'stdout stream is a terminal!')

  def testErrorStreamNonInteractive(self):
    self.assertFalse(
        self.RunCommand('IsErrorStreamInteractive'),
        'stderr stream is a terminal!')

  def testAttemptToRead(self):
    self.assertFalse(self.RunCommand('WaitForInput', command_timeout=3))

  def testInterruptedProcess(self):
    self.assertEquals(self.RunCommand('TerminateBySigAbrt'), -signal.SIGABRT)


class CommandExecuterRemoteTests(CommandExecuterLocalTests):
  HOSTNAME = socket.gethostname()

  def testCommandTimeoutIfSigTermIgnored(self):
    exit_code = self.RunCommand('IgnoreSigTerm', command_timeout=6)

    self.assertEquals(exit_code, 255)

    lines = self._executer.stdout.splitlines()
    pid = int(lines[0])

    try:
      with open('/proc/%d/cmdline' % pid) as f:
        cmdline = f.read()
    except IOError:
      cmdline = ''

    self.assertFalse('IgnoreSigTerm' in cmdline, 'Process is still alive.')


class CommandExecuterTestHelpers(object):

  def SleepForMinute(self):
    time.sleep(60)
    return 1

  def ReturnTrue(self):
    return 0

  def ReturnFalse(self):
    return 1

  def EchoToOutputStream(self):
    sys.stdout.write('test')
    return 0

  def EchoToErrorStream(self):
    sys.stderr.write('test')
    return 0

  def IsOutputStreamInteractive(self):
    return sys.stdout.isatty()

  def IsErrorStreamInteractive(self):
    return sys.stderr.isatty()

  def IgnoreSigTerm(self):
    os.write(1, '%d' % os.getpid())
    signal.signal(signal.SIGTERM, signal.SIG_IGN)
    time.sleep(30)
    return 0

  def WaitForInput(self):
    try:
      # can only read end-of-file marker
      return os.read(0, 1) != ''
    except OSError:
      # that means that stdin descriptor is closed
      return 0

  def TerminateBySigAbrt(self):
    os.kill(os.getpid(), signal.SIGABRT)
    return 0


if __name__ == '__main__':
  FORMAT = '%(asctime)-15s %(levelname)s %(message)s'
  logging.basicConfig(format=FORMAT, level=logging.DEBUG)

  if len(sys.argv) > 1:
    if sys.argv[1] == 'runHelper':
      helpers = CommandExecuterTestHelpers()
      sys.exit(getattr(helpers, sys.argv[2])())

  unittest.main()