~grupoesoc/cubicerp-addons/7.0

« back to all changes in this revision

Viewing changes to report_geraldo/lib/geraldo/site/newsite/site-geraldo/appengine_django/tests/commands_test.py

  • Committer: Cubic ERP
  • Date: 2014-01-07 15:38:09 UTC
  • Revision ID: info@cubicerp.com-20140107153809-4jmif3zoi8rcveve
[ADD] cubicReport

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python2.4
 
2
#
 
3
# Copyright 2008 Google Inc.
 
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
"""
 
18
Tests that the manage.py commands execute correctly.
 
19
 
 
20
These tests only verify that the commands execute and exit with a success code.
 
21
They are intended to catch import exceptions and similar problems, it is left
 
22
up to tests in other modules to verify that the functionality of each command
 
23
works correctly.
 
24
"""
 
25
 
 
26
 
 
27
import os
 
28
import re
 
29
import signal
 
30
import subprocess
 
31
import tempfile
 
32
import time
 
33
import unittest
 
34
 
 
35
from django.db.models import get_models
 
36
 
 
37
from google.appengine.ext import db
 
38
from appengine_django.models import BaseModel
 
39
from appengine_django.models import ModelManager
 
40
from appengine_django.models import ModelOptions
 
41
from appengine_django.models import RegistrationTestModel
 
42
 
 
43
 
 
44
class CommandsTest(unittest.TestCase):
 
45
  """Unit tests for the manage.py commands."""
 
46
 
 
47
  # How many seconds to wait for a command to exit.
 
48
  COMMAND_TIMEOUT = 10
 
49
 
 
50
  def runCommand(self, command, args=None, int_after=None, input=None):
 
51
    """Helper to run the specified command in a child process.
 
52
 
 
53
    Args:
 
54
      command: The name of the command to run.
 
55
      args: List of command arguments to run the command with.
 
56
      int_after: If set to a positive integer, SIGINT will be sent to the
 
57
        running child process after this many seconds to cause an exit. This
 
58
        should be less than the COMMAND_TIMEOUT value (10 seconds).
 
59
      input: A string to write to stdin when the command starts. stdin is
 
60
        closed after the string is written.
 
61
 
 
62
    Returns:
 
63
      rc: The integer return code of the process.
 
64
      output: A string containing the childs output.
 
65
    """
 
66
    if not args:
 
67
      args = []
 
68
    start = time.time()
 
69
    int_sent = False
 
70
    fd = subprocess.PIPE
 
71
 
 
72
    child = subprocess.Popen(["./manage.py", command] + args, stdin=fd,
 
73
                             stdout=fd, stderr=fd, cwd=os.getcwdu())
 
74
    if input:
 
75
      child.stdin.write(input)
 
76
      child.stdin.close()
 
77
 
 
78
    while 1:
 
79
      rc = child.poll()
 
80
      if rc is not None:
 
81
        # Child has exited.
 
82
        break
 
83
      elapsed = time.time() - start
 
84
      if int_after and int_after > 0 and elapsed > int_after and not int_sent:
 
85
        # Sent SIGINT as requested, give child time to exit cleanly.
 
86
        os.kill(child.pid, signal.SIGINT)
 
87
        start = time.time()
 
88
        int_sent = True
 
89
        continue
 
90
      if elapsed < self.COMMAND_TIMEOUT:
 
91
        continue
 
92
      # Command is over time, kill and exit loop.
 
93
      os.kill(child.pid, signal.SIGKILL)
 
94
      time.sleep(2)  # Give time for the signal to be received.
 
95
      break
 
96
 
 
97
    # Return status and output.
 
98
    return rc, child.stdout.read(), child.stderr.read()
 
99
 
 
100
  def assertCommandSucceeds(self, command, *args, **kwargs):
 
101
    """Asserts that the specified command successfully completes.
 
102
 
 
103
    Args:
 
104
      command: The name of the command to run.
 
105
      All other arguments are passed directly through to the runCommand
 
106
      routine.
 
107
 
 
108
    Raises:
 
109
      This function does not return anything but will raise assertion errors if
 
110
      the command does not exit successfully.
 
111
    """
 
112
    rc, stdout, stderr = self.runCommand(command, *args, **kwargs)
 
113
    fd, tempname = tempfile.mkstemp()
 
114
    os.write(fd, stdout)
 
115
    os.close(fd)
 
116
    self.assertEquals(0, rc,
 
117
                      "%s did not return successfully (rc: %d): Output in %s" %
 
118
                      (command, rc, tempname))
 
119
    os.unlink(tempname)
 
120
 
 
121
  def getCommands(self):
 
122
    """Returns a list of valid commands for manage.py.
 
123
 
 
124
    Args:
 
125
      None
 
126
 
 
127
    Returns:
 
128
      A list of valid commands for manage.py as read from manage.py's help
 
129
      output.
 
130
    """
 
131
    rc, stdout, stderr = self.runCommand("help")
 
132
    parts = re.split("Available subcommands:", stderr)
 
133
    if len(parts) < 2:
 
134
      return []
 
135
 
 
136
    return [t.strip() for t in parts[-1].split("\n") if t.strip()]
 
137
 
 
138
  def testDiffSettings(self):
 
139
    """Tests the diffsettings command."""
 
140
    self.assertCommandSucceeds("diffsettings")
 
141
 
 
142
  def testDumpData(self):
 
143
    """Tests the dumpdata command."""
 
144
    self.assertCommandSucceeds("dumpdata")
 
145
 
 
146
  def testFlush(self):
 
147
    """Tests the flush command."""
 
148
    self.assertCommandSucceeds("flush")
 
149
 
 
150
  def testLoadData(self):
 
151
    """Tests the loaddata command."""
 
152
    self.assertCommandSucceeds("loaddata")
 
153
 
 
154
  def testLoadData(self):
 
155
    """Tests the loaddata command."""
 
156
    self.assertCommandSucceeds("loaddata")
 
157
 
 
158
  def testReset(self):
 
159
    """Tests the reste command."""
 
160
    self.assertCommandSucceeds("reset", ["appengine_django"])
 
161
 
 
162
  def testRunserver(self):
 
163
    """Tests the runserver command."""
 
164
    self.assertCommandSucceeds("runserver", int_after=2.0)
 
165
 
 
166
  def testShell(self):
 
167
    """Tests the shell command."""
 
168
    self.assertCommandSucceeds("shell", input="exit")
 
169
 
 
170
  def testUpdate(self):
 
171
    """Tests that the update command exists.
 
172
 
 
173
    Cannot test that it works without mocking out parts of dev_appserver so for
 
174
    now we just assume that if it is present it will work.
 
175
    """
 
176
    cmd_list = self.getCommands()
 
177
    self.assert_("update" in cmd_list)
 
178
 
 
179
  def testZipCommandListFiltersCorrectly(self):
 
180
    """When running under a zipfile test that only valid commands are found."""
 
181
    cmd_list = self.getCommands()
 
182
    self.assert_("__init__" not in cmd_list)
 
183
    self.assert_("base" not in cmd_list)