def test_one_running_one_starting_one_stopped(self): http_method = self.respond_with(text="""[ { "attributes": { "bundleName": "test-bundle-1" }, "bundleId": "45e0c477d3e5ea92aa8d85c0d8f3e25c", "bundleExecutions": [{"isStarted": true}], "bundleInstallations": [1] }, { "attributes": { "bundleName": "test-bundle-2" }, "bundleId": "45e0c477d3e5ea92aa8d85c0d8f3e25c-c52e3f8d0c58d8aa29ae5e3d774c0e54", "bundleExecutions": [{"isStarted": false}], "bundleInstallations": [1] }, { "attributes": { "bundleName": "test-bundle-3" }, "bundleId": "45e0c477d3e5ea92aa8d85c0d8f3e25c", "bundleExecutions": [], "bundleInstallations": [1] } ]""") stdout = MagicMock() with patch('requests.get', http_method), patch('sys.stdout', stdout): conduct_info.info(MagicMock(**self.default_args)) http_method.assert_called_with(self.default_url) self.assertEqual( strip_margin("""|ID NAME #REP #STR #RUN |45e0c47 test-bundle-1 1 0 1 |45e0c47-c52e3f8 test-bundle-2 1 1 0 |45e0c47 test-bundle-3 1 0 0 |"""), self.output(stdout))
def test_failure_no_configuration(self): stderr = MagicMock() with patch('sys.stderr', stderr): args = self.default_args.copy() args.update({'configuration': 'no_such.conf'}) conduct_load.load(MagicMock(**args)) self.assertEqual( strip_margin("""|ERROR: File not found: no_such.conf |"""), self.output(stderr))
def test_no_bundles(self): http_method = self.respond_with(text='[]') stdout = MagicMock() with patch('requests.get', http_method), patch('sys.stdout', stdout): conduct_info.info(MagicMock(**self.default_args)) http_method.assert_called_with(self.default_url) self.assertEqual( strip_margin("""|ID NAME #REP #STR #RUN |"""), self.output(stdout))
def test_no_bundles(self): http_method = self.respond_with(200, '[]') stdout = MagicMock() with patch('requests.get', http_method), patch('sys.stdout', stdout): conduct_services.services(MagicMock(**self.default_args)) http_method.assert_called_with(self.default_url) self.assertEqual( strip_margin("""|SERVICE BUNDLE ID BUNDLE NAME STATUS |"""), self.output(stdout))
def test_failure_no_roles(self): stderr = MagicMock() tmpdir, bundle_file = create_temp_bundle( strip_margin("""|nrOfCpus = {} |memory = {} |diskSpace = {} |""").format(self.nr_of_cpus, self.memory, self.disk_space)) with patch('sys.stderr', stderr): args = self.default_args.copy() args.update({'bundle': bundle_file}) conduct_load.load(MagicMock(**args)) self.assertEqual( strip_margin("""|ERROR: Unable to parse bundle.conf. |ERROR: No configuration setting found for key roles. |"""), self.output(stderr)) shutil.rmtree(tmpdir)
def test_failure(self): http_method = self.respond_with(404) stderr = MagicMock() with patch('requests.put', http_method), patch('sys.stderr', stderr): conduct_run.run(MagicMock(**self.default_args)) http_method.assert_called_with(self.default_url) self.assertEqual( strip_margin("""|ERROR: 404 Not Found |"""), self.output(stderr))
def test_failure_roles_not_a_list(self): stderr = MagicMock() tmpdir, bundle_file = create_temp_bundle( strip_margin("""|nrOfCpus = {} |memory = {} |diskSpace = {} |roles = {} |""").format(self.nr_of_cpus, self.memory, self.disk_space, '-'.join(self.roles))) with patch('sys.stderr', stderr): args = self.default_args.copy() args.update({'bundle': bundle_file}) conduct_load.load(MagicMock(**args)) self.assertEqual( strip_margin("""|ERROR: Unable to parse bundle.conf. |ERROR: roles has type 'str' rather than 'list'. |"""), self.output(stderr)) shutil.rmtree(tmpdir)
def test_failure(self): http_method = self.respond_with(404) stderr = MagicMock() openMock = MagicMock(return_value=1) with patch('requests.post', http_method), patch('sys.stderr', stderr), patch('builtins.open', openMock): conduct_load.load(MagicMock(**self.default_args)) openMock.assert_called_with(self.bundle_file, 'rb') http_method.assert_called_with(self.default_url, files=self.default_files) self.assertEqual( strip_margin("""|ERROR: 404 Not Found |"""), self.output(stderr))
def test_one_bundle_starting(self): http_method = self.respond_with_file_contents( 'data/one_bundle_starting.json') stdout = MagicMock() with patch('requests.get', http_method), patch('sys.stdout', stdout): conduct_services.services(MagicMock(**self.default_args)) http_method.assert_called_with(self.default_url) self.assertEqual( strip_margin( """|SERVICE BUNDLE ID BUNDLE NAME STATUS |http://:8010/comp1-endp1 f804d64 multi-comp-multi-endp-1.0.0 Starting |http://:8011/comp1-endp2 f804d64 multi-comp-multi-endp-1.0.0 Starting |http://:9010/comp2-endp1 f804d64 multi-comp-multi-endp-1.0.0 Starting |http://:9011/comp2-endp2 f804d64 multi-comp-multi-endp-1.0.0 Starting |http://my.service f804d64 multi-comp-multi-endp-1.0.0 Starting |"""), self.output(stdout))
def test_stopped_bundle(self): http_method = self.respond_with(text="""[ { "attributes": { "bundleName": "test-bundle" }, "bundleId": "45e0c477d3e5ea92aa8d85c0d8f3e25c", "bundleExecutions": [], "bundleInstallations": [1] } ]""") stdout = MagicMock() with patch('requests.get', http_method), patch('sys.stdout', stdout): conduct_info.info(MagicMock(**self.default_args)) http_method.assert_called_with(self.default_url) self.assertEqual( strip_margin("""|ID NAME #REP #STR #RUN |45e0c47 test-bundle 1 0 0 |"""), self.output(stdout))
def test_two_bundles_mult_components_endpoints(self): http_method = self.respond_with_file_contents('data/two_bundles.json') stdout = MagicMock() with patch('requests.get', http_method), patch('sys.stdout', stdout): conduct_services.services(MagicMock(**self.default_args)) http_method.assert_called_with(self.default_url) self.assertEqual( strip_margin( """|SERVICE BUNDLE ID BUNDLE NAME STATUS |http://:6011/comp2-endp2 6e4560e multi2-comp-multi-endp-1.0.0 Running |http://:7010/comp3-endp1 6e4560e multi2-comp-multi-endp-1.0.0 Running |http://:7011/comp3-endp2 6e4560e multi2-comp-multi-endp-1.0.0 Running |http://:8010/comp1-endp1 f804d64 multi-comp-multi-endp-1.0.0 Running |http://:8011/comp1-endp2 f804d64 multi-comp-multi-endp-1.0.0 Running |http://:9010/comp2-endp1 f804d64 multi-comp-multi-endp-1.0.0 Running |http://:9010/comp2-endp1 6e4560e multi2-comp-multi-endp-1.0.0 Running |http://:9011/comp2-endp2 f804d64 multi-comp-multi-endp-1.0.0 Running | |WARNING: Multiple endpoints found for the following services: /comp2-endp2 |WARNING: Service resolution for these services is undefined. |"""), self.output(stdout))
def default_response(self): return strip_margin("""|{ | "bundleId": "45e0c477d3e5ea92aa8d85c0d8f3e25c" |} |""")
class TestConductLoadCommand(TestCase, CliTestCase): @property def default_response(self): return strip_margin("""|{ | "bundleId": "45e0c477d3e5ea92aa8d85c0d8f3e25c" |} |""") nr_of_cpus = 1.0 memory = 200 disk_space = 100 roles = ['web-server'] tmpdir, bundle_file = create_temp_bundle( strip_margin("""|nrOfCpus = {} |memory = {} |diskSpace = {} |roles = [{}] |""").format(nr_of_cpus, memory, disk_space, ', '.join(roles))) default_args = { 'ip': '127.0.0.1', 'port': 9005, 'verbose': False, 'long_ids': False, 'cli_parameters': '', 'bundle_name': None, 'bundle': bundle_file, 'configuration': None, 'system': None } default_url = 'http://127.0.0.1:9005/bundles' default_files = [('nrOfCpus', str(nr_of_cpus)), ('memory', str(memory)), ('diskSpace', str(disk_space)), ('roles', ' '.join(roles)), ('bundleName', 'bundle'), ('system', 'bundle'), ('bundle', 1)] output_template = """|Bundle loaded. |Start bundle with: conduct run{params} {bundle_id} |Unload bundle with: conduct unload{params} {bundle_id} |Print ConductR info with: conduct info{params} |""" @classmethod def tearDownClass(cls): shutil.rmtree(cls.tmpdir) def default_output(self, params='', bundle_id='45e0c47'): return strip_margin( self.output_template.format(**{ 'params': params, 'bundle_id': bundle_id })) def test_success(self): http_method = self.respond_with(200, self.default_response) stdout = MagicMock() openMock = MagicMock(return_value=1) with patch('requests.post', http_method), patch('sys.stdout', stdout), patch('builtins.open', openMock): conduct_load.load(MagicMock(**self.default_args)) openMock.assert_called_with(self.bundle_file, 'rb') http_method.assert_called_with(self.default_url, files=self.default_files) self.assertEqual(self.default_output(), self.output(stdout)) def test_success_verbose(self): http_method = self.respond_with(200, self.default_response) stdout = MagicMock() openMock = MagicMock(return_value=1) with patch('requests.post', http_method), patch('sys.stdout', stdout), patch('builtins.open', openMock): args = self.default_args.copy() args.update({'verbose': True}) conduct_load.load(MagicMock(**args)) openMock.assert_called_with(self.bundle_file, 'rb') http_method.assert_called_with(self.default_url, files=self.default_files) self.assertEqual(self.default_response + self.default_output(), self.output(stdout)) def test_success_long_ids(self): http_method = self.respond_with(200, self.default_response) stdout = MagicMock() openMock = MagicMock(return_value=1) with patch('requests.post', http_method), patch('sys.stdout', stdout), patch('builtins.open', openMock): args = self.default_args.copy() args.update({'long_ids': True}) conduct_load.load(MagicMock(**args)) openMock.assert_called_with(self.bundle_file, 'rb') http_method.assert_called_with(self.default_url, files=self.default_files) self.assertEqual( self.default_output(bundle_id='45e0c477d3e5ea92aa8d85c0d8f3e25c'), self.output(stdout)) def test_success_custom_ip_port(self): http_method = self.respond_with(200, self.default_response) stdout = MagicMock() openMock = MagicMock(return_value=1) cli_parameters = ' --ip 127.0.1.1 --port 9006' with patch('requests.post', http_method), patch('sys.stdout', stdout), patch('builtins.open', openMock): args = self.default_args.copy() args.update({'cli_parameters': cli_parameters}) conduct_load.load(MagicMock(**args)) openMock.assert_called_with(self.bundle_file, 'rb') http_method.assert_called_with(self.default_url, files=self.default_files) self.assertEqual(self.default_output(params=cli_parameters), self.output(stdout)) def test_success_with_configuration(self): http_method = self.respond_with(200, self.default_response) stdout = MagicMock() openMock = MagicMock(return_value=1) tmpdir, config_file = create_temp_bundle_with_contents( {'config.sh': 'echo configuring'}) with patch('requests.post', http_method), patch('sys.stdout', stdout), patch('builtins.open', openMock): args = self.default_args.copy() args.update({'configuration': config_file}) conduct_load.load(MagicMock(**args)) self.assertEqual( openMock.call_args_list, [call(self.bundle_file, 'rb'), call(config_file, 'rb')]) http_method.assert_called_with(self.default_url, files=self.default_files + [('configuration', 1)]) self.assertEqual(self.default_output(), self.output(stdout)) def test_success_with_bundle_name(self): http_method = self.respond_with(200, self.default_response) stdout = MagicMock() openMock = MagicMock(return_value=1) with patch('requests.post', http_method), patch('sys.stdout', stdout), patch('builtins.open', openMock): args = self.default_args.copy() args.update({'bundle_name': 'test-name'}) conduct_load.load(MagicMock(**args)) openMock.assert_called_with(self.bundle_file, 'rb') http_method.assert_called_with( self.default_url, files=[(file, value if file != 'bundleName' else 'test-name') for file, value in self.default_files]) self.assertEqual(self.default_output(), self.output(stdout)) def test_success_with_system(self): http_method = self.respond_with(200, self.default_response) stdout = MagicMock() openMock = MagicMock(return_value=1) with patch('requests.post', http_method), patch('sys.stdout', stdout), patch('builtins.open', openMock): args = self.default_args.copy() args.update({'system': 'test-system'}) conduct_load.load(MagicMock(**args)) openMock.assert_called_with(self.bundle_file, 'rb') http_method.assert_called_with( self.default_url, files=[(file, value if file != 'system' else 'test-system') for file, value in self.default_files]) self.assertEqual(self.default_output(), self.output(stdout)) def test_failure(self): http_method = self.respond_with(404) stderr = MagicMock() openMock = MagicMock(return_value=1) with patch('requests.post', http_method), patch('sys.stderr', stderr), patch('builtins.open', openMock): conduct_load.load(MagicMock(**self.default_args)) openMock.assert_called_with(self.bundle_file, 'rb') http_method.assert_called_with(self.default_url, files=self.default_files) self.assertEqual( strip_margin("""|ERROR: 404 Not Found |"""), self.output(stderr)) def test_failure_invalid_address(self): http_method = self.raise_connection_error('test reason') stderr = MagicMock() openMock = MagicMock(return_value=1) with patch('requests.post', http_method), patch('sys.stderr', stderr), patch('builtins.open', openMock): conduct_load.load(MagicMock(**self.default_args)) openMock.assert_called_with(self.bundle_file, 'rb') http_method.assert_called_with(self.default_url, files=self.default_files) self.assertEqual( self.default_connection_error.format(self.default_args['ip'], self.default_args['port']), self.output(stderr)) def test_failure_no_nr_of_cpus(self): stderr = MagicMock() tmpdir, bundle_file = create_temp_bundle( strip_margin("""|memory = {} |diskSpace = {} |roles = [{}] |""").format(self.memory, self.disk_space, ', '.join(self.roles))) with patch('sys.stderr', stderr): args = self.default_args.copy() args.update({'bundle': bundle_file}) conduct_load.load(MagicMock(**args)) self.assertEqual( strip_margin("""|ERROR: Unable to parse bundle.conf. |ERROR: No configuration setting found for key nrOfCpus. |"""), self.output(stderr)) shutil.rmtree(tmpdir) def test_failure_no_memory(self): stderr = MagicMock() tmpdir, bundle_file = create_temp_bundle( strip_margin("""|nrOfCpus = {} |diskSpace = {} |roles = [{}] |""").format(self.nr_of_cpus, self.disk_space, ', '.join(self.roles))) with patch('sys.stderr', stderr): args = self.default_args.copy() args.update({'bundle': bundle_file}) conduct_load.load(MagicMock(**args)) self.assertEqual( strip_margin("""|ERROR: Unable to parse bundle.conf. |ERROR: No configuration setting found for key memory. |"""), self.output(stderr)) shutil.rmtree(tmpdir) def test_failure_no_disk_space(self): stderr = MagicMock() tmpdir, bundle_file = create_temp_bundle( strip_margin("""|nrOfCpus = {} |memory = {} |roles = [{}] |""").format(self.nr_of_cpus, self.memory, ', '.join(self.roles))) with patch('sys.stderr', stderr): args = self.default_args.copy() args.update({'bundle': bundle_file}) conduct_load.load(MagicMock(**args)) self.assertEqual( strip_margin("""|ERROR: Unable to parse bundle.conf. |ERROR: No configuration setting found for key diskSpace. |"""), self.output(stderr)) shutil.rmtree(tmpdir) def test_failure_no_roles(self): stderr = MagicMock() tmpdir, bundle_file = create_temp_bundle( strip_margin("""|nrOfCpus = {} |memory = {} |diskSpace = {} |""").format(self.nr_of_cpus, self.memory, self.disk_space)) with patch('sys.stderr', stderr): args = self.default_args.copy() args.update({'bundle': bundle_file}) conduct_load.load(MagicMock(**args)) self.assertEqual( strip_margin("""|ERROR: Unable to parse bundle.conf. |ERROR: No configuration setting found for key roles. |"""), self.output(stderr)) shutil.rmtree(tmpdir) def test_failure_roles_not_a_list(self): stderr = MagicMock() tmpdir, bundle_file = create_temp_bundle( strip_margin("""|nrOfCpus = {} |memory = {} |diskSpace = {} |roles = {} |""").format(self.nr_of_cpus, self.memory, self.disk_space, '-'.join(self.roles))) with patch('sys.stderr', stderr): args = self.default_args.copy() args.update({'bundle': bundle_file}) conduct_load.load(MagicMock(**args)) self.assertEqual( strip_margin("""|ERROR: Unable to parse bundle.conf. |ERROR: roles has type 'str' rather than 'list'. |"""), self.output(stderr)) shutil.rmtree(tmpdir) def test_failure_no_bundle(self): stderr = MagicMock() with patch('sys.stderr', stderr): args = self.default_args.copy() args.update({'bundle': 'no_such.bundle'}) conduct_load.load(MagicMock(**args)) self.assertEqual( strip_margin("""|ERROR: File not found: no_such.bundle |"""), self.output(stderr)) def test_failure_no_configuration(self): stderr = MagicMock() with patch('sys.stderr', stderr): args = self.default_args.copy() args.update({'configuration': 'no_such.conf'}) conduct_load.load(MagicMock(**args)) self.assertEqual( strip_margin("""|ERROR: File not found: no_such.conf |"""), self.output(stderr)) def test_path_to_bundle_name(self): self.assertEqual( conduct_load.path_to_bundle_name('path/to/bundle-5ca1ab1e.zip'), 'bundle') self.assertEqual( conduct_load.path_to_bundle_name('path/to/bundle.zip'), 'bundle') self.assertEqual( conduct_load.path_to_bundle_name('path/to/bundle-1.0.0-M2.zip'), 'bundle-1.0.0-M2')
def default_output(self, params='', bundle_id='45e0c47'): return strip_margin( self.output_template.format(**{ 'params': params, 'bundle_id': bundle_id }))
def test_one_running_one_stopped_verbose(self): http_method = self.respond_with(text="""[ { "attributes": { "bundleName": "test-bundle-1" }, "bundleId": "45e0c477d3e5ea92aa8d85c0d8f3e25c", "bundleExecutions": [{"isStarted": true},{"isStarted": true},{"isStarted": true}], "bundleInstallations": [1,2,3] }, { "attributes": { "bundleName": "test-bundle-2" }, "bundleId": "c52e3f8d0c58d8aa29ae5e3d774c0e54", "bundleExecutions": [], "bundleInstallations": [1,2,3] } ]""") stdout = MagicMock() with patch('requests.get', http_method), patch('sys.stdout', stdout): args = self.default_args.copy() args.update({'verbose': True}) conduct_info.info(MagicMock(**args)) http_method.assert_called_with(self.default_url) self.assertEqual( strip_margin("""|[ | { | "attributes": { | "bundleName": "test-bundle-1" | }, | "bundleExecutions": [ | { | "isStarted": true | }, | { | "isStarted": true | }, | { | "isStarted": true | } | ], | "bundleId": "45e0c477d3e5ea92aa8d85c0d8f3e25c", | "bundleInstallations": [ | 1, | 2, | 3 | ] | }, | { | "attributes": { | "bundleName": "test-bundle-2" | }, | "bundleExecutions": [], | "bundleId": "c52e3f8d0c58d8aa29ae5e3d774c0e54", | "bundleInstallations": [ | 1, | 2, | 3 | ] | } |] |ID NAME #REP #STR #RUN |45e0c47 test-bundle-1 3 0 3 |c52e3f8 test-bundle-2 3 0 0 |"""), self.output(stdout))
def default_output(self, params=''): return strip_margin(self.output_template.format(**{'params': params}))