Esempio n. 1
0
    def test_unit_path_does_exist_incorrectly(self, stdout, Popen):
        """
        Test the case for when the unit path does exist, but its contents are incorrect. It should
        write it and then start it.
        """
        pipe_output = ('some output', None)
        Popen.return_value = mock.MagicMock()
        Popen.return_value.returncode = 0
        Popen.return_value.communicate.return_value = pipe_output

        with mock.patch('__builtin__.open', autospec=True) as mock_open:
            mock_file = mock.MagicMock(spec=file)
            mock_file.read.return_value = 'This file has the wrong contents.'
            mock_context = mock.MagicMock()
            mock_context.__enter__.return_value = mock_file
            mock_open.return_value = mock_context
            # Set up the unit file to have the incorrect contents
            manage_workers._start_workers()

        # Make sure open was called correctly. It should be called twice, once for reading, and
        # again for writing
        unit_filename = manage_workers._UNIT_FILENAME_TEMPLATE % 0
        expected_path = os.path.join(manage_workers._SYSTEMD_UNIT_PATH,
                                     unit_filename)
        expected_file_contents = manage_workers._WORKER_TEMPLATE % {
            'num': 0,
            'environment_file': manage_workers._ENVIRONMENT_FILE,
            'max_tasks_argument': ''
        }
        self.assertEqual(mock_open.call_count, 2)
        # Let's inspect the read call
        first_open = mock_open.mock_calls[0]
        self.assertEqual(first_open[1], (expected_path, ))
        file_context = first_open()
        file_context.__enter__.assert_called_once_with()
        file_handle = file_context.__enter__()
        file_handle.read.assert_called_once_with()
        file_context.__exit__.assert_called_once_with(None, None, None)
        # Now, let's inspect the write call
        second_open = mock_open.mock_calls[4]
        self.assertEqual(second_open[1], (expected_path, 'w'))
        file_context = second_open()
        file_context.__enter__.assert_called_once_with()
        file_handle = file_context.__enter__()
        file_handle.write.assert_called_once_with(expected_file_contents)
        file_context.__exit__.assert_called_once_with(None, None, None)

        # Make sure we started the worker correctly
        Popen.assert_called_once_with('systemctl start %s' % unit_filename,
                                      stdout=subprocess.PIPE,
                                      stderr=subprocess.PIPE,
                                      shell=True)
        pipe = Popen()
        pipe.communicate.assert_called_once_with()

        # Make sure we printed the output of the pipe
        self.assertEqual(stdout.write.call_count, 2)
        self.assertEqual(stdout.write.mock_calls[0][1][0], pipe_output[0])
        self.assertEqual(stdout.write.mock_calls[1][1][0], '\n')
Esempio n. 2
0
    def test_unit_path_does_exist_incorrectly(self, stdout, Popen):
        """
        Test the case for when the unit path does exist, but its contents are incorrect. It should
        write it and then start it.
        """
        pipe_output = ('some output', None)
        Popen.return_value = mock.MagicMock()
        Popen.return_value.returncode = 0
        Popen.return_value.communicate.return_value = pipe_output

        with mock.patch('__builtin__.open', autospec=True) as mock_open:
            mock_file = mock.MagicMock(spec=file)
            mock_file.read.return_value = 'This file has the wrong contents.'
            mock_context = mock.MagicMock()
            mock_context.__enter__.return_value = mock_file
            mock_open.return_value = mock_context
            # Set up the unit file to have the incorrect contents
            manage_workers._start_workers()

        # Make sure open was called correctly. It should be called twice, once for reading, and
        # again for writing
        unit_filename = manage_workers._UNIT_FILENAME_TEMPLATE % 0
        expected_path = os.path.join(manage_workers._SYSTEMD_UNIT_PATH, unit_filename)
        expected_file_contents = manage_workers._WORKER_TEMPLATE % {
            'num': 0, 'environment_file': manage_workers._ENVIRONMENT_FILE}
        self.assertEqual(mock_open.call_count, 2)
        # Let's inspect the read call
        first_open = mock_open.mock_calls[0]
        self.assertEqual(first_open[1], (expected_path,))
        file_context = first_open()
        file_context.__enter__.assert_called_once_with()
        file_handle = file_context.__enter__()
        file_handle.read.assert_called_once_with()
        file_context.__exit__.assert_called_once_with(None, None, None)
        # Now, let's inspect the write call
        second_open = mock_open.mock_calls[4]
        self.assertEqual(second_open[1], (expected_path, 'w'))
        file_context = second_open()
        file_context.__enter__.assert_called_once_with()
        file_handle = file_context.__enter__()
        file_handle.write.assert_called_once_with(expected_file_contents)
        file_context.__exit__.assert_called_once_with(None, None, None)

        # Make sure we started the worker correctly
        Popen.assert_called_once_with('systemctl start %s' % unit_filename, stdout=subprocess.PIPE,
                                      shell=True)
        pipe = Popen()
        pipe.communicate.assert_called_once_with()

        # Make sure we printed the output of the pipe
        self.assertEqual(stdout.write.call_count, 2)
        self.assertEqual(stdout.write.mock_calls[0][1][0], pipe_output[0])
        self.assertEqual(stdout.write.mock_calls[1][1][0], '\n')
Esempio n. 3
0
    def test_unit_path_does_exist_correctly(self, stdout, Popen):
        """
        Test the case for when the unit path does exist, and its contents are correct. It should
        start it, but should not write it again.
        """
        pipe_output = ('some output', None)
        Popen.return_value = mock.MagicMock()
        Popen.return_value.returncode = 0
        Popen.return_value.communicate.return_value = pipe_output
        expected_read_data = manage_workers._WORKER_TEMPLATE % {
            'num': 0,
            'environment_file': manage_workers._ENVIRONMENT_FILE,
            'max_tasks_argument': ''
        }

        with mock.patch('__builtin__.open', autospec=True) as mock_open:
            mock_file = mock.MagicMock(spec=file)
            mock_file.read.return_value = expected_read_data
            mock_context = mock.MagicMock()
            mock_context.__enter__.return_value = mock_file
            mock_open.return_value = mock_context
            manage_workers._start_workers()

        # Make sure open was called correctly
        unit_filename = manage_workers._UNIT_FILENAME_TEMPLATE % 0
        expected_path = os.path.join(manage_workers._SYSTEMD_UNIT_PATH,
                                     unit_filename)
        mock_open.assert_called_once_with(expected_path)
        file_context = mock_open()
        file_context.__enter__.assert_called_once_with()
        file_handle = file_context.__enter__()
        file_handle.read.assert_called_once_with()
        file_context.__exit__.assert_called_once_with(None, None, None)

        # Make sure we started the worker correctly
        Popen.assert_called_once_with('systemctl start %s' % unit_filename,
                                      stdout=subprocess.PIPE,
                                      stderr=subprocess.PIPE,
                                      shell=True)
        pipe = Popen()
        pipe.communicate.assert_called_once_with()

        # Make sure we printed the output of the pipe
        self.assertEqual(stdout.write.call_count, 2)
        self.assertEqual(stdout.write.mock_calls[0][1][0], pipe_output[0])
        self.assertEqual(stdout.write.mock_calls[1][1][0], '\n')
Esempio n. 4
0
    def test_unit_path_does_not_exist(self, stdout, Popen):
        """
        Test the case for when the unit path does not exist. It should write it and start it.
        """
        pipe_output = ('some output', None)
        Popen.return_value = mock.MagicMock()
        Popen.return_value.returncode = 0
        Popen.return_value.communicate.return_value = pipe_output

        with mock.patch('__builtin__.open', autospec=True) as mock_open:
            mock_file = mock.MagicMock(spec=file)
            mock_context = mock.MagicMock()
            mock_context.__enter__.return_value = mock_file
            mock_open.return_value = mock_context
            manage_workers._start_workers()

        # Make sure open was called correctly. It should be called once for writing
        unit_filename = manage_workers._UNIT_FILENAME_TEMPLATE % 0
        expected_path = os.path.join(manage_workers._SYSTEMD_UNIT_PATH,
                                     unit_filename)
        expected_file_contents = manage_workers._WORKER_TEMPLATE % {
            'num': 0,
            'environment_file': manage_workers._ENVIRONMENT_FILE
        }
        # Now, let's inspect the write call
        mock_open.assert_called_once_with(expected_path, 'w')
        file_context = mock_open()
        file_context.__enter__.assert_called_once_with()
        file_handle = file_context.__enter__()
        file_handle.write.assert_called_once_with(expected_file_contents)
        file_context.__exit__.assert_called_once_with(None, None, None)

        # Make sure we started the worker correctly
        Popen.assert_called_once_with('systemctl start %s' % unit_filename,
                                      stdout=subprocess.PIPE,
                                      shell=True)
        pipe = Popen()
        pipe.communicate.assert_called_once_with()

        # Make sure we printed the output of the pipe
        self.assertEqual(stdout.write.call_count, 2)
        self.assertEqual(stdout.write.mock_calls[0][1][0], pipe_output[0])
        self.assertEqual(stdout.write.mock_calls[1][1][0], '\n')
Esempio n. 5
0
    def test_unit_path_does_exist_correctly(self, stdout, Popen):
        """
        Test the case for when the unit path does exist, and its contents are correct. It should
        start it, but should not write it again.
        """
        pipe_output = ('some output', None)
        Popen.return_value = mock.MagicMock()
        Popen.return_value.returncode = 0
        Popen.return_value.communicate.return_value = pipe_output
        expected_read_data = manage_workers._WORKER_TEMPLATE % {
            'num': 0, 'environment_file': manage_workers._ENVIRONMENT_FILE}

        with mock.patch('__builtin__.open', autospec=True) as mock_open:
            mock_file = mock.MagicMock(spec=file)
            mock_file.read.return_value = expected_read_data
            mock_context = mock.MagicMock()
            mock_context.__enter__.return_value = mock_file
            mock_open.return_value = mock_context
            manage_workers._start_workers()

        # Make sure open was called correctly
        unit_filename = manage_workers._UNIT_FILENAME_TEMPLATE % 0
        expected_path = os.path.join(manage_workers._SYSTEMD_UNIT_PATH, unit_filename)
        mock_open.assert_called_once_with(expected_path)
        file_context = mock_open()
        file_context.__enter__.assert_called_once_with()
        file_handle = file_context.__enter__()
        file_handle.read.assert_called_once_with()
        file_context.__exit__.assert_called_once_with(None, None, None)

        # Make sure we started the worker correctly
        Popen.assert_called_once_with('systemctl start %s' % unit_filename, stdout=subprocess.PIPE,
                                      shell=True)
        pipe = Popen()
        pipe.communicate.assert_called_once_with()

        # Make sure we printed the output of the pipe
        self.assertEqual(stdout.write.call_count, 2)
        self.assertEqual(stdout.write.mock_calls[0][1][0], pipe_output[0])
        self.assertEqual(stdout.write.mock_calls[1][1][0], '\n')