class TestActivationCommands(ClientTests): @patch(NODE_ACTIVATED_CHECK, return_value=False) @patch(LOAD_CONSUMER_API, return_value=NODE_ID) @patch(NODE_ACTIVATE_API, return_value=Response(200, {})) def test_activate(self, mock_binding, *unused): # Test command = commands.NodeActivateCommand(self.context) keywords = { commands.STRATEGY_OPTION.keyword: constants.DEFAULT_STRATEGY } command.run(**keywords) # Verify delta = { 'notes': { constants.NODE_NOTE_KEY: True, constants.STRATEGY_NOTE_KEY: constants.DEFAULT_STRATEGY } } mock_binding.assert_called_with(NODE_ID, delta) @patch(NODE_ACTIVATED_CHECK, return_value=True) @patch(LOAD_CONSUMER_API, return_value=NODE_ID) @patch(NODE_ACTIVATE_API, return_value=Response(200, {})) def test_activate_already_activated(self, mock_binding, *unused): command = commands.NodeActivateCommand(self.context) keywords = { commands.STRATEGY_OPTION.keyword: constants.DEFAULT_STRATEGY } command.run(**keywords) # Verify self.assertFalse(mock_binding.called) @patch(LOAD_CONSUMER_API, return_value=NODE_ID) @patch(NODE_ACTIVATED_CHECK, return_value=True) @patch(NODE_ACTIVATE_API, return_value=Response(200, {})) def test_deactivate(self, mock_binding, mock_activated, *unused): # Test command = commands.NodeDeactivateCommand(self.context) command.run() # Verify delta = { 'notes': { constants.NODE_NOTE_KEY: None, constants.STRATEGY_NOTE_KEY: None } } mock_activated.assert_called_with(self.context, NODE_ID) mock_binding.assert_called_with(NODE_ID, delta) @patch(LOAD_CONSUMER_API, return_value=NODE_ID) @patch(NODE_ACTIVATED_CHECK, return_value=False) @patch(NODE_ACTIVATE_API, return_value=Response(200, {})) def test_deactivate_not_activated(self, mock_binding, mock_activated, *unused): # Test command = commands.NodeDeactivateCommand(self.context) command.run() # Verify mock_activated.assert_called_with(self.context, NODE_ID) self.assertFalse(mock_binding.called)
class TestPublishCommand(ClientTests): @patch(REPO_ENABLED_CHECK, return_value=True) @patch('pulp.client.commands.polling.PollingCommand.rejected') @patch('pulp.client.commands.polling.PollingCommand.poll') @patch(PUBLISH_API, return_value=Response(200, {})) def test_publish(self, mock_binding, *unused): # Test command = NodeRepoPublishCommand(self.context) keywords = {OPTION_REPO_ID.keyword: REPOSITORY_ID} command.run(**keywords) # Verify self.assertTrue(OPTION_REPO_ID in command.options) mock_binding.assert_called_with(REPOSITORY_ID, constants.HTTP_DISTRIBUTOR, {}) @patch(REPO_ENABLED_CHECK, return_value=False) @patch('pulp.client.commands.polling.PollingCommand.rejected') @patch('pulp.client.commands.polling.PollingCommand.poll') @patch(PUBLISH_API, return_value=Response(200, {})) def test_publish_not_enabled(self, mock_binding, *unused): # Test command = NodeRepoPublishCommand(self.context) keywords = {OPTION_REPO_ID.keyword: REPOSITORY_ID} command.run(**keywords) # Verify self.assertTrue(OPTION_REPO_ID in command.options) self.assertFalse(mock_binding.called)
class TestModel(ServerTests): def setUp(self): super(self.__class__, self).setUp() def tearDown(self): super(self.__class__, self).tearDown() @patch('pulp_node.poller.TaskPoller.join') @patch('pulp.bindings.repository.RepositoryActionsAPI.sync', return_value=Response(httplib.ACCEPTED, TaskResult(0))) @patch('pulp.agent.lib.conduit.Conduit.consumer_id') def test_repository(self, *mocks): # Setup repository = Repository(REPO_ID) progress = Mock() cancelled = Mock(return_value=False) # Test options = { constants.MAX_DOWNLOAD_CONCURRENCY_KEYWORD: MAX_CONCURRENCY, constants.MAX_DOWNLOAD_BANDWIDTH_KEYWORD: MAX_BANDWIDTH, constants.PARENT_SETTINGS: PARENT_SETTINGS, } repository.run_synchronization(progress, cancelled, options) binding = mocks[1] key, certificate = Bundle.split(NODE_CERTIFICATE) expected_conf = { importer_constants.KEY_SSL_VALIDATION: False, importer_constants.KEY_MAX_DOWNLOADS: MAX_CONCURRENCY, importer_constants.KEY_MAX_SPEED: MAX_BANDWIDTH, importer_constants.KEY_SSL_CLIENT_KEY: key, importer_constants.KEY_SSL_CLIENT_CERT: certificate, } # Verify binding.assert_called_with(REPO_ID, expected_conf)
def get(self, request): template = Template(open('templates/test_template.html', 'r').read()) body = str(template.render(test_message=u'Вася Doe').encode('utf-8')) # body = str(u'Вася'.encode('utf-8')) return Response(body=body).response() # class classonlymethod(classmethod): # def __get__(self, instance, owner): # if instance is not None: # raise AttributeError("This method is available only on the view class.") # return super(classonlymethod, self).__get__(instance, owner) # # def call(fun): # fun(5,2) # # class TestCallback(): # @classonlymethod # def sum(self, a, b): # print a*b+b # # # def __call__(self, a, b): # # self.sum(a,b) # # class TC(TestCallback): # pass
class TestUpdateCommands(ClientTests): @patch(POLLING_API) @patch(NODE_ACTIVATED_CHECK, return_value=True) @patch(NODE_UPDATE_API, return_value=Response(202, {})) def test_update(self, mock_update, mock_activated, *unused): # Test command = NodeUpdateCommand(self.context) keywords = { NODE_ID_OPTION.keyword: NODE_ID, MAX_BANDWIDTH_OPTION.keyword: MAX_BANDWIDTH, MAX_CONCURRENCY_OPTION.keyword: MAX_CONCURRENCY } command.run(**keywords) # Verify units = [dict(type_id='node', unit_key=None)] options = { constants.MAX_DOWNLOAD_BANDWIDTH_KEYWORD: MAX_BANDWIDTH, constants.MAX_DOWNLOAD_CONCURRENCY_KEYWORD: MAX_CONCURRENCY, } self.assertTrue(NODE_ID_OPTION in command.options) self.assertTrue(MAX_BANDWIDTH_OPTION in command.options) self.assertTrue(MAX_CONCURRENCY_OPTION in command.options) mock_update.assert_called_with(NODE_ID, units=units, options=options) mock_activated.assert_called_with(self.context, NODE_ID)
class TestListCommands(ClientTests): @patch(CONSUMER_LIST_API, return_value=Response(200, CONSUMERS_ONLY)) def test_list_nodes_no_nodes(self, mock_binding): # Test command = NodeListCommand(self.context) command.run(fields=None) # Verify mock_binding.assert_called_with(bindings=False, details=False) lines = self.recorder.lines self.assertEqual(len(lines), 4) self.assertTrue('Child Nodes' in lines[1]) @patch(CONSUMER_LIST_API, return_value=Response(200, CONSUMERS_AND_NODES)) def test_list_nodes(self, mock_binding): # Test command = NodeListCommand(self.context) command.run(fields=None) # Verify mock_binding.assert_called_with(bindings=False, details=False) lines = self.recorder.lines self.assertEqual(len(lines), 9) self.assertTrue(NODE_LIST_TITLE in lines[1]) @patch(CONSUMER_LIST_API, return_value=Response(200, NODES_WITH_BINDINGS)) def test_list_nodes_with_bindings(self, mock_binding): # Test command = NodeListCommand(self.context) command.run(fields=None) # Verify mock_binding.assert_called_with(bindings=False, details=False) lines = self.recorder.lines self.assertEqual(len(lines), 16) self.assertTrue(NODE_LIST_TITLE in lines[1]) @patch(REPO_LIST_API, return_value=Response(200, ALL_REPOSITORIES)) @patch(DISTRIBUTORS_API, return_value=Response(200, NON_NODES_DISTRIBUTORS_ONLY)) def test_list_repos_disabled_only(self, mock_binding, *unused): # Test command = NodeListRepositoriesCommand(self.context) command.run(details=True, summary=False) # Verify mock_binding.assert_called_with(REPOSITORY_ID) lines = self.recorder.lines self.assertEqual(len(lines), 4) self.assertTrue(REPO_LIST_TITLE in lines[1]) @patch(REPO_LIST_API, return_value=Response(200, ALL_REPOSITORIES)) @patch(DISTRIBUTORS_API, return_value=Response(200, MIXED_DISTRIBUTORS)) def test_list_repos_with_enabled(self, mock_binding, *unused): # Test command = NodeListRepositoriesCommand(self.context) command.run(details=True, summary=False) # Verify mock_binding.assert_called_with(REPOSITORY_ID) lines = self.recorder.lines self.assertEqual(len(lines), 9) self.assertTrue(REPO_LIST_TITLE in lines[1])
def test_post_view(request): body = '<h4>Test POST request</h4><br>' \ '<form method="POST" action="/test_class/">' \ 'Some text: <input name="test_input" /><br>' \ 'Some text: <input name="test_input2" /><br>' \ '<input type="submit" />' \ '</form>' # status = '200 OK' # headers = [ # ('Content-Type', 'text/html'), # ('Content-Length', str(len(body))) # ] return Response(body=body).response()
def index(request): body = ''.join([ '<div><span style="width:150px;">%s:</span> %s</div>' % (key, value) for key, value in sorted(request.environ.items()) ]) length = int(request.environ.get('CONTENT_LENGTH') or '0') body = [ '<h4>All environment variables</h4>\n', '*' * 30 + '<br>', request.environ['wsgi.input'].read(length), '<br>' + '*' * 30 + '<br>', body, '<br>' + '*' * 30, '<br>The End' ] content_length = sum([len(s) for s in body]) status = '200 OK' headers = [('Content-Type', 'text/html'), ('Content-Length', str(content_length))] # body = 'index page' # status = '200 OK' # headers = [ # ('Content-Type', 'text/plain'), # ('Content-Length', str(len(body))) # ] return Response(body=body).response()
class TestBindCommands(ClientTests): @patch(NODE_ACTIVATED_CHECK, return_value=True) @patch(BIND_API, return_value=Response(200, {})) def test_bind(self, mock_binding, *unused): # Test command = NodeBindCommand(self.context) keywords = { OPTION_REPO_ID.keyword: REPOSITORY_ID, NODE_ID_OPTION.keyword: NODE_ID, STRATEGY_OPTION.keyword: constants.DEFAULT_STRATEGY, } command.run(**keywords) # Verify self.assertTrue(OPTION_REPO_ID in command.options) self.assertTrue(NODE_ID_OPTION in command.options) self.assertTrue(STRATEGY_OPTION in command.options) mock_binding.assert_called_with(NODE_ID, REPOSITORY_ID, constants.HTTP_DISTRIBUTOR, notify_agent=False, binding_config={ constants.STRATEGY_KEYWORD: constants.DEFAULT_STRATEGY }) @patch(NODE_ACTIVATED_CHECK, return_value=True) @patch(BIND_API, return_value=Response(200, {})) def test_bind_with_strategy(self, mock_binding, *unused): # Test command = NodeBindCommand(self.context) keywords = { OPTION_REPO_ID.keyword: REPOSITORY_ID, NODE_ID_OPTION.keyword: NODE_ID, STRATEGY_OPTION.keyword: constants.MIRROR_STRATEGY, } command.run(**keywords) # Verify self.assertTrue(OPTION_REPO_ID in command.options) self.assertTrue(NODE_ID_OPTION in command.options) self.assertTrue(STRATEGY_OPTION in command.options) mock_binding.assert_called_with(NODE_ID, REPOSITORY_ID, constants.HTTP_DISTRIBUTOR, notify_agent=False, binding_config={ constants.STRATEGY_KEYWORD: constants.MIRROR_STRATEGY }) @patch(NODE_ACTIVATED_CHECK, return_value=False) @patch(BIND_API, return_value=Response(200, {})) def test_bind_not_activated(self, mock_binding, *unused): # Test command = NodeBindCommand(self.context) keywords = { OPTION_REPO_ID.keyword: REPOSITORY_ID, NODE_ID_OPTION.keyword: NODE_ID, STRATEGY_OPTION.keyword: constants.MIRROR_STRATEGY, } command.run(**keywords) # Verify self.assertTrue(OPTION_REPO_ID in command.options) self.assertTrue(NODE_ID_OPTION in command.options) self.assertTrue(STRATEGY_OPTION in command.options) self.assertFalse(mock_binding.called) @patch(NODE_ACTIVATED_CHECK, return_value=True) @patch(UNBIND_API, return_value=Response(200, {})) def test_unbind(self, mock_binding, *unused): # Test command = NodeUnbindCommand(self.context) keywords = { OPTION_REPO_ID.keyword: REPOSITORY_ID, NODE_ID_OPTION.keyword: NODE_ID, } command.run(**keywords) # Verify self.assertTrue(OPTION_REPO_ID in command.options) self.assertTrue(NODE_ID_OPTION in command.options) mock_binding.assert_called_with(NODE_ID, REPOSITORY_ID, constants.HTTP_DISTRIBUTOR)
def post(self, request): body = '<h4>Test ClassView with POST request</h4>' \ '<br>Params: {}'.format(str(request.data)) return Response(body=body).response()
def test_get_view(request): body = '<h4>Test GET request</h4><br>Params: {}'.format( unicode(request.data)) headers = [('Content-Type', 'text/html;charset=utf-8'), ('Content-Length', str(len(body)))] return Response(body=body, headers=headers).response()