Exemplo n.º 1
0
class WsgidAppTest(unittest.TestCase):
    @patch('sys.stderr')
    def setUp(self, *args):
        self.init = CommandInit()
        self.empty_apppath = os.path.join(FIXTURE, 'empty-app')
        self.init.run(FakeOptions(app_path=self.empty_apppath))
        self.empty_wsgidapp = WsgidApp(self.empty_apppath)

    def test_check_valid_wsgid_app_folder(self):
        self.assertTrue(self.empty_wsgidapp.is_valid(),
                        "Did not recognize as a valid wsgid app folder")

    def test_return_empty_master_pids(self):
        self.assertEquals([], self.empty_wsgidapp.master_pids())

    def test_return_empty_worker_pids(self):
        self.assertEquals([], self.empty_wsgidapp.worker_pids())

    @patch('sys.stderr')
    def test_return_pids(self, *args):
        app = os.path.join(FIXTURE, 'app-with-pids')
        self.init.run(FakeOptions(app_path=app))
        open(os.path.join(app, 'pid/master/3345.pid'), 'w')
        open(os.path.join(app, 'pid/master/2938.pid'), 'w')
        open(os.path.join(app, 'pid/master/no-pid.pid'), 'w')

        open(os.path.join(app, 'pid/worker/8756.pid'), 'w')
        open(os.path.join(app, 'pid/worker/3948.pid'), 'w')
        open(os.path.join(app, 'pid/worker/invalid.pid'), 'w')

        wsgidapp = WsgidApp(app)
        # Invalid pidfiles must be ignored
        self.assertEquals([2938, 3345], wsgidapp.master_pids())
        self.assertEquals([3948, 8756], wsgidapp.worker_pids())
Exemplo n.º 2
0
class WsgidAppTest(unittest.TestCase):

  @patch('sys.stderr')
  def setUp(self, *args):
    self.init = CommandInit()
    self.empty_apppath = os.path.join(FIXTURE, 'empty-app')
    self.init.run(FakeOptions(app_path=self.empty_apppath))
    self.empty_wsgidapp = WsgidApp(self.empty_apppath)

  def test_check_valid_wsgid_app_folder(self):
    self.assertTrue(self.empty_wsgidapp.is_valid(), "Did not recognize as a valid wsgid app folder")

  def test_return_empty_master_pids(self):
    self.assertEquals([], self.empty_wsgidapp.master_pids())

  def test_return_empty_worker_pids(self):
    self.assertEquals([], self.empty_wsgidapp.worker_pids())

  @patch('sys.stderr')
  def test_return_pids(self, *args):
    app = os.path.join(FIXTURE, 'app-with-pids')
    self.init.run(FakeOptions(app_path=app))
    open(os.path.join(app, 'pid/master/3345.pid'), 'w')
    open(os.path.join(app, 'pid/master/2938.pid'), 'w')
    open(os.path.join(app, 'pid/master/no-pid.pid'), 'w')
    
    open(os.path.join(app, 'pid/worker/8756.pid'), 'w')
    open(os.path.join(app, 'pid/worker/3948.pid'), 'w')
    open(os.path.join(app, 'pid/worker/invalid.pid'), 'w')

    wsgidapp = WsgidApp(app)
    # Invalid pidfiles must be ignored
    self.assertEquals([2938, 3345], wsgidapp.master_pids())
    self.assertEquals([3948, 8756], wsgidapp.worker_pids())
Exemplo n.º 3
0
 def test_start_workers_when_nodaemon(self, *args):
   path = os.path.join(FIXTURES_PATH, 'nodaemon-app')
   initcmd = CommandInit()
   initcmd.run(FakeOptions(app_path=path))
   with patch('daemon.DaemonContext', new=MagicMock()):
     with patch.object(Cli, '_create_worker'):
       with patch.object(Cli, 'validate_input_params'):
         with patch.object(Cli, '_wait_workers'):
           daemon.DaemonContext.__enter__ = Mock()
           daemon.DaemonContext.__exit__ = Mock()
           sys.argv[1:] = ['--workers=3', '--no-daemon', '--app-path={0}'.format(path)]
           cli = Cli()
           cli.run()
           self.assertEquals(3, cli._create_worker.call_count)
           self.assertEquals(1, cli._wait_workers.call_count)
Exemplo n.º 4
0
 def test_start_workers_when_nodaemon(self, *args):
     path = os.path.join(FIXTURES_PATH, 'nodaemon-app')
     initcmd = CommandInit()
     initcmd.run(FakeOptions(app_path=path))
     with patch('daemon.DaemonContext', new=MagicMock()):
         with patch.object(Cli, '_create_worker'):
             with patch.object(Cli, 'validate_input_params'):
                 with patch.object(Cli, '_wait_workers'):
                     daemon.DaemonContext.__enter__ = Mock()
                     daemon.DaemonContext.__exit__ = Mock()
                     sys.argv[1:] = [
                         '--workers=3', '--no-daemon',
                         '--app-path={0}'.format(path)
                     ]
                     cli = Cli()
                     cli.run()
                     self.assertEquals(3, cli._create_worker.call_count)
                     self.assertEquals(1, cli._wait_workers.call_count)
Exemplo n.º 5
0
 def test_clean_pid_files_on_keyboard_interrupt(self):
     path = os.path.join(FIXTURES_PATH, 'clean-pids-app')
     initcmd = CommandInit()
     opts = FakeOptions(app_path=path)
     initcmd.run(opts)
     open(os.path.join(path, 'pid/master/3340.pid'), 'w')
     open(os.path.join(path, 'pid/worker/2736.pid'), 'w')
     open(os.path.join(path, 'pid/worker/3847.pid'), 'w')
     with patch('os.wait'):
         with patch('os.getpid'):
             os.getpid.side_effect = lambda: 3340
             os.wait.side_effect = KeyboardInterrupt()
             cli = Cli()
             cli.options = opts
             cli.log = Mock()
             cli.workers = [2736, 3847]
             cli._wait_workers()
             app = WsgidApp(path)
             self.assertEquals([], app.master_pids())
             self.assertEquals([], app.worker_pids())
Exemplo n.º 6
0
 def test_clean_pid_files_on_keyboard_interrupt(self, *args):
     path = os.path.join(FIXTURES_PATH, 'clean-pids-app')
     initcmd = CommandInit()
     opts = FakeOptions(app_path=path)
     initcmd.run(opts)
     open(os.path.join(path, 'pid/master/3340.pid'), 'w')
     open(os.path.join(path, 'pid/worker/2736.pid'), 'w')
     open(os.path.join(path, 'pid/worker/3847.pid'), 'w')
     with patch('os.wait'):
         with patch('os.getpid'):
             os.getpid.side_effect = lambda: 3340
             os.wait.side_effect = KeyboardInterrupt()
             cli = Cli()
             cli.options = opts
             cli.log = Mock()
             cli.workers = [2736, 3847]
             cli._wait_workers()
             app = WsgidApp(path)
             self.assertEquals([], app.master_pids())
             self.assertEquals([], app.worker_pids())
Exemplo n.º 7
0
    def setUp(self, *args):
        self.cli = Cli()
        # As we are dealing with a command line test, we have do clean the passed arguments
        # so the tested applications does not try to use them
        sys.argv[1:] = []
        self.fake_app_path = os.path.join(fullpath(__file__), 'app-path')

        # Ok, not pretty but better than re-implementing this in python
        os.system("rm -rf {0}".format(os.path.join(self.fake_app_path,
                                                   'pid/')))
        self.cli.options = parser._parse_args()
        self.cli.options.app_path = self.fake_app_path

        CommandInit().run(FakeOptions(app_path=self.fake_app_path))
Exemplo n.º 8
0
 def setUp(self, *args):
     self.init = CommandInit()
     self.empty_apppath = os.path.join(FIXTURE, 'empty-app')
     self.init.run(FakeOptions(app_path=self.empty_apppath))
     self.empty_wsgidapp = WsgidApp(self.empty_apppath)
Exemplo n.º 9
0
 def setUp(self, *args):
   self.init = CommandInit()
   self.empty_apppath = os.path.join(FIXTURE, 'empty-app')
   self.init.run(FakeOptions(app_path=self.empty_apppath))
   self.empty_wsgidapp = WsgidApp(self.empty_apppath)