def test_positive_run_job_effective_user_by_ip(self, fixture_vmsetup, fixture_org): """Run default job template as effective user on a host by ip :id: 0cd75cab-f699-47e6-94d3-4477d2a94bb7 :expectedresults: Verify the job was successfully run under the effective user identity on host """ self.org = fixture_org self.client = fixture_vmsetup # set connecting to host via ip Host.set_parameter({ 'host': self.client.hostname, 'name': 'remote_execution_connect_by_ip', 'value': 'True', }) # create a user on client via remote job username = gen_string('alpha') filename = gen_string('alpha') make_user_job = make_job_invocation({ 'job-template': 'Run Command - SSH Default', 'inputs': "command='useradd -m {0}'".format(username), 'search-query': "name ~ {0}".format(self.client.hostname), }) try: assert make_user_job[u'success'] == u'1' except AssertionError: result = 'host output: {0}'.format( ' '.join(JobInvocation.get_output({ 'id': make_user_job[u'id'], 'host': self.client.hostname}) ) ) raise AssertionError(result) # create a file as new user invocation_command = make_job_invocation({ 'job-template': 'Run Command - SSH Default', 'inputs': "command='touch /home/{0}/{1}'".format( username, filename), 'search-query': "name ~ {0}".format(self.client.hostname), 'effective-user': '******'.format(username), }) try: assert invocation_command['success'] == u'1' except AssertionError: result = 'host output: {0}'.format( ' '.join(JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': self.client.hostname}) ) ) raise AssertionError(result) # check the file owner result = ssh.command( '''stat -c '%U' /home/{0}/{1}'''.format(username, filename), hostname=self.client.ip_addr ) # assert the file is owned by the effective user assert username == result.stdout[0]
def test_positive_run_job_effective_user(self): """Run default job template as effective user against a single host :id: ecd3f24f-26df-4a2c-9112-6af33b68b601 :expectedresults: Verify the job was successfully run under the effective user identity on host """ # create a user on client via remote job username = gen_string('alpha') filename = gen_string('alpha') make_user_job = make_job_invocation({ 'job-template': 'Run Command - SSH Default', 'inputs': "command='useradd {0}'".format(username), 'search-query': "name ~ {0}".format(self.client.hostname), }) try: self.assertEqual(make_user_job[u'success'], u'1') except AssertionError: result = 'host output: {0}'.format( ' '.join(JobInvocation.get_output({ 'id': make_user_job[u'id'], 'host': self.client.hostname}) ) ) raise AssertionError(result) # create a file as new user invocation_command = make_job_invocation({ 'job-template': 'Run Command - SSH Default', 'inputs': "command='touch /home/{0}/{1}'".format( username, filename), 'search-query': "name ~ {0}".format(self.client.hostname), 'effective-user': '******'.format(username), }) try: self.assertEqual(invocation_command['success'], u'1') except AssertionError: result = 'host output: {0}'.format( ' '.join(JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': self.client.hostname}) ) ) raise AssertionError(result) # check the file owner result = ssh.command( '''stat -c '%U' /home/{0}/{1}'''.format(username, filename), hostname=self.client.hostname ) # assert the file is owned by the effective user self.assertEqual(username, result.stdout[0])
def test_positive_run_reccuring_job(self, fixture_vmsetup, fixture_org): """Tests Ansible REX reccuring job runs successfully multiple times :id: 49b0d31d-58f9-47f1-aa5d-561a1dcb0d66 :Steps: 0. Create a VM and register to SAT and prepare for REX (ssh key) 1. Run recurring Ansible Command job for the host 2. Check the multiple job results at the host :expectedresults: multiple asserts along the code :caseautomation: automated :CaseLevel: System """ self.org = fixture_org self.client = fixture_vmsetup # set connecting to host by ip Host.set_parameter({ 'host': self.client.hostname, 'name': 'remote_execution_connect_by_ip', 'value': 'True', }) invocation_command = make_job_invocation({ 'job-template': 'Run Command - Ansible Default', 'inputs': 'command="ls"', 'search-query': "name ~ {0}".format(self.client.hostname), 'cron-line': '* * * * *', # every minute 'max-iteration': 2, # just two runs }) JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': self.client.hostname }) try: assert invocation_command['status'] == u'queued' except AssertionError: result = 'host output: {0}'.format( ' '.join(JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': self.client.hostname}) ) ) raise AssertionError(result) # Wait until the job runs pending_state = u'1' for _ in range(5): if pending_state != u'0': invocation_info = JobInvocation.info({ 'id': invocation_command[u'id']}) pending_state = invocation_info[u'pending'] sleep(30) rec_logic = RecurringLogic.info({ 'id': invocation_command['recurring-logic-id']}) assert rec_logic['state'] == u'finished' assert rec_logic['iteration'] == u'2'
def test_positive_run_custom_job_template(self): """Run custom job template against a single host :id: 71928f36-61b4-46e6-842c-a051cfd9a68e :expectedresults: Verify the job was successfully ran against the host """ template_name = gen_string('alpha', 7) make_job_template({ u'organizations': self.name, u'name': template_name, u'file': TEMPLATE_FILE }) invocation_command = make_job_invocation({ 'job-template': template_name, 'search-query': "name ~ {0}".format(self.client.hostname), }) try: self.assertEqual(invocation_command['success'], u'1') except AssertionError: result = 'host output: {0}'.format( ' '.join(JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': self.client.hostname}) ) ) raise AssertionError(result)
def test_positive_run_default_job_template_multiple_hosts(self): """Run default job template against multiple hosts :id: 415c0156-be77-4676-918b-c0d4be810b0e :expectedresults: Verify the job was successfully ran against all hosts """ with VirtualMachine( distro=DISTRO_RHEL7, provisioning_server=settings.compute_resources.libvirt_hostname, bridge=settings.vlan_networking.bridge, ) as client2: client2.install_katello_ca() client2.register_contenthost( self.org.label, lce='Library') add_remote_execution_ssh_key(client2.ip_addr) invocation_command = make_job_invocation({ 'job-template': 'Run Command - SSH Default', 'inputs': 'command="ls"', 'search-query': "name ~ {0} or name ~ {1}".format( self.client.hostname, client2.hostname), }) # collect output messages from clients output_msgs = [] for vm in self.client, client2: output_msgs.append( 'host output from {0}: {1}'.format( vm.hostname, ' '.join(JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': vm.hostname}) ) ) ) self.assertEqual(invocation_command['success'], u'2', output_msgs)
def test_positive_run_recurring_job_with_max_iterations(self): """Run default job template multiple times with max iteration :id: 37fb7d77-dbb1-45ae-8bd7-c70be7f6d949 :expectedresults: Verify the job was run not more than the specified number of times. """ invocation_command = make_job_invocation({ 'job-template': 'Run Command - SSH Default', 'inputs': 'command="ls"', 'search-query': "name ~ {0}".format(self.client.hostname), 'cron-line': '* * * * *', # every minute 'max-iteration': 2, # just two runs }) if not bz_bug_is_open(1431190): JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': self.client.hostname }) try: self.assertEqual(invocation_command['status'], u'queued') except AssertionError: result = 'host output: {0}'.format( ' '.join(JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': self.client.hostname}) ) ) raise AssertionError(result) sleep(150) rec_logic = RecurringLogic.info({ 'id': invocation_command['recurring-logic-id']}) self.assertEqual(rec_logic['state'], u'finished') self.assertEqual(rec_logic['iteration'], u'2')
def test_positive_run_default_job_template_by_ip(self): """Run default template on host connected by ip :id: 811c7747-bec6-4a2d-8e5c-b5045d3fbc0d :expectedresults: Verify the job was successfully ran against the host """ # set connecting to host via ip Host.set_parameter({ 'host': self.client.hostname, 'name': 'remote_execution_connect_by_ip', 'value': 'True', }) invocation_command = make_job_invocation({ 'job-template': 'Run Command - SSH Default', 'inputs': 'command="ls"', 'search-query': "name ~ {0}".format(self.client.hostname), }) try: self.assertEqual(invocation_command['success'], u'1') except AssertionError: result = 'host output: {0}'.format( ' '.join(JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': self.client.hostname}) ) ) raise AssertionError(result)
def test_positive_run_custom_job_template_by_ip(self): """Run custom template on host connected by ip :id: 9740eb1d-59f5-42b2-b3ab-659ca0202c74 :expectedresults: Verify the job was successfully ran against the host """ # set connecting to host via ip Host.set_parameter({ 'host': self.client.hostname, 'name': 'remote_execution_connect_by_ip', 'value': 'True', }) template_name = gen_string('alpha', 7) make_job_template({ u'organizations': self.org.name, u'name': template_name, u'file': TEMPLATE_FILE }) invocation_command = make_job_invocation({ 'job-template': template_name, 'search-query': "name ~ {0}".format(self.client.hostname), }) try: self.assertEqual(invocation_command['success'], u'1') except AssertionError: result = 'host output: {0}'.format( ' '.join(JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': self.client.hostname}) ) ) raise AssertionError(result)
def test_positive_install_multiple_packages_with_a_job_by_ip(self): """Run job to install several packages on host by ip :id: 8b73033f-83c9-4024-83c3-5e442a79d320 :expectedresults: Verify the packages were successfully installed on host """ # set connecting to host by ip Host.set_parameter({ 'host': self.client.hostname, 'name': 'remote_execution_connect_by_ip', 'value': 'True', }) packages = ["cow", "dog", "lion"] # Create a custom repo repo = entities.Repository( content_type='yum', product=entities.Product(organization=self.org).create(), url=FAKE_0_YUM_REPO, ).create() repo.sync() prod = repo.product.read() subs = entities.Subscription().search( query={'search': 'name={0}'.format(prod.name)} ) self.assertGreater( len(subs), 0, 'No subscriptions matching the product returned' ) ak = entities.ActivationKey( organization=self.org, content_view=self.org.default_content_view, environment=self.org.library ).create() ak.add_subscriptions(data={'subscriptions': [{'id': subs[0].id}]}) self.client.register_contenthost( org=self.org.label, activation_key=ak.name ) invocation_command = make_job_invocation({ 'job-template': 'Install Package - Katello SSH Default', 'inputs': 'package={0} {1} {2}'.format(*packages), 'search-query': "name ~ {0}".format(self.client.hostname), }) try: self.assertEqual(invocation_command['success'], u'1') except AssertionError: result = 'host output: {0}'.format( ' '.join(JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': self.client.hostname}) ) ) raise AssertionError(result) result = ssh.command( "rpm -q {0}".format(" ".join(packages)), hostname=self.client.ip_addr ) self.assertEqual(result.return_code, 0)
def test_positive_run_custom_job_template(self): """Run custom job template against a single host @id: 71928f36-61b4-46e6-842c-a051cfd9a68e @Assert: Verify the job was successfully ran against the host """ template_name = gen_string("alpha", 7) make_job_template( {u"organizations": self.organization[u"name"], u"name": template_name, u"file": TEMPLATE_FILE} ) invocation_command = make_job_invocation( {"job-template": template_name, "search-query": "name ~ {0}".format(self.client.hostname)} ) self.assertEqual(invocation_command[u"success"], u"1")
def test_positive_run_default_job_template(self): """Run default job template against a single host @id: f4470ed4-f971-4a3c-a2f1-150d45755e48 @Assert: Verify the job was successfully ran against the host """ invocation_command = make_job_invocation( { "job-template": "Run Command - SSH Default", "inputs": 'command="ls"', "search-query": "name ~ {0}".format(self.client.hostname), } ) self.assertEqual(invocation_command["success"], u"1")
def test_positive_run_scheduled_job_template_by_ip(self, fixture_vmsetup, fixture_org): """Schedule a job to be ran against a host :id: 0407e3de-ef59-4706-ae0d-b81172b81e5c :expectedresults: Verify the job was successfully ran after the designated time """ self.org = fixture_org self.client = fixture_vmsetup system_current_time = ssh.command('date --utc +"%b %d %Y %I:%M%p"').stdout[0] current_time_object = datetime.strptime( system_current_time, '%b %d %Y %I:%M%p') plan_time = (current_time_object + timedelta(seconds=30)).strftime( "%Y-%m-%d %H:%M") Host.set_parameter({ 'host': self.client.hostname, 'name': 'remote_execution_connect_by_ip', 'value': 'True', }) invocation_command = make_job_invocation({ 'job-template': 'Run Command - SSH Default', 'inputs': 'command="ls"', 'start-at': plan_time, 'search-query': "name ~ {0}".format(self.client.hostname), }) # Wait until the job runs pending_state = u'1' while pending_state != u'0': invocation_info = JobInvocation.info({ 'id': invocation_command[u'id']}) pending_state = invocation_info[u'pending'] sleep(30) invocation_info = JobInvocation.info({ 'id': invocation_command[u'id']}) try: assert invocation_info['success'] == u'1' except AssertionError: result = 'host output: {0}'.format( ' '.join(JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': self.client.hostname}) ) ) raise AssertionError(result)
def test_positive_run_default_job_template_multiple_hosts_by_ip(self): """Run default job template against multiple hosts by ip :id: 694a21d3-243b-4296-8bd0-4bad9663af15 :expectedresults: Verify the job was successfully ran against all hosts """ Host.set_parameter({ 'host': self.client.hostname, 'name': 'remote_execution_connect_by_ip', 'value': 'True', }) with VirtualMachine( distro=DISTRO_RHEL7, provisioning_server=settings.compute_resources.libvirt_hostname, bridge=settings.vlan_networking.bridge, ) as client2: client2.install_katello_ca() client2.register_contenthost( self.org['label'], lce='Library') add_remote_execution_ssh_key(client2.ip_addr) Host.set_parameter({ 'host': client2.hostname, 'name': 'remote_execution_connect_by_ip', 'value': 'True', }) invocation_command = make_job_invocation({ 'job-template': 'Run Command - SSH Default', 'inputs': 'command="ls"', 'search-query': "name ~ {0} or name ~ {1}".format( self.client.hostname, client2.hostname), }) # collect output messages from clients output_msgs = [] for vm in self.client, client2: output_msgs.append('host output from {0}: {1}'.format( vm.hostname, ' '.join(JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': vm.hostname}) ) ) ) self.assertEqual(invocation_command['success'], u'2', output_msgs)
def test_positive_run_recurring_job_with_max_iterations_by_ip(self, fixture_vmsetup, fixture_org): """Run default job template multiple times with max iteration by ip :id: 0a3d1627-95d9-42ab-9478-a908f2a7c509 :expectedresults: Verify the job was run not more than the specified number of times. """ self.org = fixture_org self.client = fixture_vmsetup # set connecting to host by ip Host.set_parameter({ 'host': self.client.hostname, 'name': 'remote_execution_connect_by_ip', 'value': 'True', }) invocation_command = make_job_invocation({ 'job-template': 'Run Command - SSH Default', 'inputs': 'command="ls"', 'search-query': "name ~ {0}".format(self.client.hostname), 'cron-line': '* * * * *', # every minute 'max-iteration': 2, # just two runs }) if not bz_bug_is_open(1431190): JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': self.client.hostname }) try: assert invocation_command['status'] == u'queued' except AssertionError: result = 'host output: {0}'.format( ' '.join(JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': self.client.hostname}) ) ) raise AssertionError(result) sleep(150) rec_logic = RecurringLogic.info({ 'id': invocation_command['recurring-logic-id']}) assert rec_logic['state'] == u'finished' assert rec_logic['iteration'] == u'2'
def test_positive_install_multiple_packages_with_a_job_by_ip(self): """Run job to install several packages on host by ip :id: 8b73033f-83c9-4024-83c3-5e442a79d320 :expectedresults: Verify the packages were successfully installed on host """ # set connecting to host by ip Host.set_parameter({ 'host': self.client.hostname, 'name': 'remote_execution_connect_by_ip', 'value': 'True', }) packages = ["cow", "dog", "lion"] # Create a custom repo setup_org_for_a_custom_repo({ u'url': FAKE_0_YUM_REPO, u'organization-id': self.org['id'], u'content-view-id': self.content_view['id'], u'lifecycle-environment-id': self.env['id'], u'activationkey-id': self.activation_key['id'], }) invocation_command = make_job_invocation({ 'job-template': 'Install Package - Katello SSH Default', 'inputs': 'package={0} {1} {2}'.format(*packages), 'search-query': "name ~ {0}".format(self.client.hostname), }) self.assertEqual( invocation_command['success'], u'1', 'host output: {0}'.format( ' '.join(JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': self.client.hostname}) ) ) ) result = ssh.command( "rpm -q {0}".format(" ".join(packages)), hostname=self.client.ip_addr ) self.assertEqual(result.return_code, 0)
def test_positive_run_default_job_template(self): """Run default job template against a single host :id: f4470ed4-f971-4a3c-a2f1-150d45755e48 :expectedresults: Verify the job was successfully ran against the host """ invocation_command = make_job_invocation({ 'job-template': 'Run Command - SSH Default', 'inputs': 'command="ls"', 'search-query': "name ~ {0}".format(self.client.hostname), }) try: self.assertEqual(invocation_command['success'], u'1') except AssertionError: result = 'host output: {0}'.format( ' '.join(JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': self.client.hostname}) ) ) raise AssertionError(result)
def test_positive_run_scheduled_job_template(self): """Schedule a job to be ran against a host :id: 1953517b-6908-40aa-858b-747629d2f374 :expectedresults: Verify the job was successfully ran after the designated time """ system_current_time = ssh.command('date +"%b %d %Y %I:%M%p"').stdout[0] current_time_object = datetime.strptime( system_current_time, '%b %d %Y %I:%M%p') plan_time = (current_time_object + timedelta(seconds=30)).strftime( "%Y-%m-%d %H:%M") invocation_command = make_job_invocation({ 'job-template': 'Run Command - SSH Default', 'inputs': 'command="ls"', 'start-at': plan_time, 'search-query': "name ~ {0}".format(self.client.hostname), }) # Wait until the job runs pending_state = u'1' while pending_state != u'0': invocation_info = JobInvocation.info({ 'id': invocation_command[u'id']}) pending_state = invocation_info[u'pending'] sleep(30) invocation_info = JobInvocation.info({ 'id': invocation_command[u'id']}) try: self.assertEqual(invocation_info['success'], u'1') except AssertionError: result = 'host output: {0}'.format( ' '.join(JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': self.client.hostname}) ) ) raise AssertionError(result)
def test_positive_install_multiple_packages_with_a_job(self): """Run job to install several packages on host :id: 1cf2709e-e6cd-46c9-a7b7-c2e542c0e943 :expectedresults: Verify the packages were successfully installed on host """ packages = ["cow", "dog", "lion"] # Create a custom repo setup_org_for_a_custom_repo({ u'url': FAKE_0_YUM_REPO, u'organization-id': self.org['id'], u'content-view-id': self.content_view['id'], u'lifecycle-environment-id': self.env['id'], u'activationkey-id': self.activation_key['id'], }) invocation_command = make_job_invocation({ 'job-template': 'Install Package - Katello SSH Default', 'inputs': 'package={0} {1} {2}'.format(*packages), 'search-query': "name ~ {0}".format(self.client.hostname), }) self.assertEqual( invocation_command['success'], u'1', 'host output: {0}'.format( ' '.join(JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': self.client.hostname}) ) ) ) result = ssh.command( "rpm -q {0}".format(" ".join(packages)), hostname=self.client.hostname ) self.assertEqual(result.return_code, 0)
def test_positive_run_scheduled_job_template(self): """Schedule a job to be ran against a host @id: 1953517b-6908-40aa-858b-747629d2f374 @Assert: Verify the job was successfully ran after the designated time """ system_current_time = ssh.command('date +"%b %d %Y %I:%M%p"').stdout[0] current_time_object = datetime.strptime(system_current_time, "%b %d %Y %I:%M%p") plan_time = (current_time_object + timedelta(seconds=30)).strftime("%Y-%m-%d %H:%M") invocation_command = make_job_invocation( { "job-template": "Run Command - SSH Default", "inputs": 'command="ls"', "start-at": plan_time, "search-query": "name ~ {0}".format(self.client.hostname), } ) for _ in range(5): try: invocation_info = JobInvocation.info({"id": invocation_command[u"id"]}) self.assertEqual(invocation_info[u"success"], u"1") except AssertionError: sleep(30)
def test_positive_run_effective_user_job(self, rex_contenthost): """Tests Ansible REX job having effective user runs successfully :id: a5fa20d8-c2bd-4bbf-a6dc-bf307b59dd8c :Steps: 0. Create a VM and register to SAT and prepare for REX (ssh key) 1. Run Ansible Command job for the host to create a user 2. Run Ansible Command job using effective user 3. Check the job result at the host is done under that user :expectedresults: multiple asserts along the code :CaseAutomation: Automated :CaseLevel: System :parametrized: yes """ client = rex_contenthost # create a user on client via remote job username = gen_string('alpha') filename = gen_string('alpha') make_user_job = make_job_invocation({ 'job-template': 'Run Command - Ansible Default', 'inputs': f"command='useradd -m {username}'", 'search-query': f"name ~ {client.hostname}", }) result = JobInvocation.info({'id': make_user_job['id']}) try: assert result['success'] == '1' except AssertionError: result = 'host output: {}'.format(' '.join( JobInvocation.get_output({ 'id': make_user_job['id'], 'host': client.hostname }))) raise AssertionError(result) # create a file as new user invocation_command = make_job_invocation({ 'job-template': 'Run Command - Ansible Default', 'inputs': f"command='touch /home/{username}/{filename}'", 'search-query': f"name ~ {client.hostname}", 'effective-user': f'{username}', }) result = JobInvocation.info({'id': invocation_command['id']}) try: assert result['success'] == '1' except AssertionError: result = 'host output: {}'.format(' '.join( JobInvocation.get_output({ 'id': invocation_command['id'], 'host': client.hostname }))) raise AssertionError(result) # check the file owner result = client.execute( f'''stat -c '%U' /home/{username}/{filename}''', ) # assert the file is owned by the effective user assert username == result.stdout.strip('\n'), "file ownership mismatch"
def test_positive_rex_against_infra_hosts( self, rex_contenthost, class_rexmanager_user, class_rexinfra_user, target_sat, infra_host, module_org, ): """ Tests related to remote execution against Satellite host :id: 36942e30-b885-4ba3-933b-7f59888935c9 :steps: 1. Run rex job against Satellite and Capsule as admin 2. Run rex job against Satellite and Capsule as a REX admin 3. Run rex job against Satellite and Capsule as a custom user with required permission :expectedresults: Only users with execute_jobs_on_infrastructure_host perm can run rex against Satellite :caseautomation: Automated :parametrized: yes """ client = rex_contenthost infra_host.add_rex_key(satellite=target_sat) Host.update({ 'name': infra_host.hostname, 'new-organization-id': module_org.id }) # run job as admin command = f"echo {gen_string('alpha')}" invocation_command = make_job_invocation({ 'job-template': 'Run Command - SSH Default', 'inputs': f'command={command}', 'search-query': f"name ^ ({client.hostname}, {infra_host.hostname})", }) output_msgs = [] hostnames = [client.hostname, infra_host.hostname] for hostname in hostnames: inv_output = ' '.join( JobInvocation.get_output({ 'id': invocation_command['id'], 'host': hostname })) output_msgs.append(f"host output from {hostname}: { inv_output }") result = JobInvocation.info({'id': invocation_command['id']}) assert result['success'] == '2', output_msgs # run job as regular rex user on all hosts invocation_command = make_job_invocation_with_credentials( { 'job-template': 'Run Command - SSH Default', 'inputs': f'command={command}', 'search-query': f"name ^ ({client.hostname}, {infra_host.hostname})", }, class_rexmanager_user, ) result = JobInvocation.info({'id': invocation_command['id']}) assert result['success'] == '1' # run job as regular rex user just on infra hosts invocation_command = make_job_invocation_with_credentials( { 'job-template': 'Run Command - SSH Default', 'inputs': f'command={command}', 'search-query': f"name ^ ({infra_host.hostname})", }, class_rexmanager_user, ) result = JobInvocation.info({'id': invocation_command['id']}) assert result['success'] == '0' # run job as rex user on Satellite invocation_command = make_job_invocation_with_credentials( { 'job-template': 'Run Command - SSH Default', 'inputs': f'command={command}', 'search-query': f"name ^ ({infra_host.hostname})", }, class_rexinfra_user, ) result = JobInvocation.info({'id': invocation_command['id']}) assert result['success'] == '1'
def test_positive_run_job_effective_user_by_ip(self, fixture_vmsetup, fixture_org): """Run default job template as effective user on a host by ip :id: 0cd75cab-f699-47e6-94d3-4477d2a94bb7 :BZ: 1451675 :expectedresults: Verify the job was successfully run under the effective user identity on host """ self.org = fixture_org self.client = fixture_vmsetup # set connecting to host via ip Host.set_parameter( { 'host': self.client.hostname, 'name': 'remote_execution_connect_by_ip', 'value': 'True', } ) # create a user on client via remote job username = gen_string('alpha') filename = gen_string('alpha') make_user_job = make_job_invocation( { 'job-template': 'Run Command - SSH Default', 'inputs': "command='useradd -m {0}'".format(username), 'search-query': "name ~ {0}".format(self.client.hostname), } ) try: assert make_user_job['success'] == '1' except AssertionError: result = 'host output: {0}'.format( ' '.join( JobInvocation.get_output( {'id': make_user_job['id'], 'host': self.client.hostname} ) ) ) raise AssertionError(result) # create a file as new user invocation_command = make_job_invocation( { 'job-template': 'Run Command - SSH Default', 'inputs': "command='touch /home/{0}/{1}'".format(username, filename), 'search-query': "name ~ {0}".format(self.client.hostname), 'effective-user': '******'.format(username), } ) try: assert invocation_command['success'] == '1' except AssertionError: result = 'host output: {0}'.format( ' '.join( JobInvocation.get_output( {'id': invocation_command['id'], 'host': self.client.hostname} ) ) ) raise AssertionError(result) # check the file owner result = ssh.command( '''stat -c '%U' /home/{0}/{1}'''.format(username, filename), hostname=self.client.ip_addr, ) # assert the file is owned by the effective user assert username == result.stdout[0]
def test_positive_run_effective_user_job(self, fixture_vmsetup, fixture_org): """Tests Ansible REX job having effective user runs successfully :id: a5fa20d8-c2bd-4bbf-a6dc-bf307b59dd8c :Steps: 0. Create a VM and register to SAT and prepare for REX (ssh key) 1. Run Ansible Command job for the host to create a user 2. Run Ansible Command job using effective user 3. Check the job result at the host is done under that user :expectedresults: multiple asserts along the code :CaseAutomation: automated :CaseLevel: System """ self.org = fixture_org self.client = fixture_vmsetup # set connecting to host via ip Host.set_parameter( { 'host': self.client.hostname, 'name': 'remote_execution_connect_by_ip', 'value': 'True', } ) # create a user on client via remote job username = gen_string('alpha') filename = gen_string('alpha') make_user_job = make_job_invocation( { 'job-template': 'Run Command - Ansible Default', 'inputs': "command='useradd -m {0}'".format(username), 'search-query': "name ~ {0}".format(self.client.hostname), } ) try: assert make_user_job['success'] == '1' except AssertionError: result = 'host output: {0}'.format( ' '.join( JobInvocation.get_output( {'id': make_user_job['id'], 'host': self.client.hostname} ) ) ) raise AssertionError(result) # create a file as new user invocation_command = make_job_invocation( { 'job-template': 'Run Command - Ansible Default', 'inputs': "command='touch /home/{0}/{1}'".format(username, filename), 'search-query': "name ~ {0}".format(self.client.hostname), 'effective-user': '******'.format(username), } ) try: assert invocation_command['success'] == '1' except AssertionError: result = 'host output: {0}'.format( ' '.join( JobInvocation.get_output( {'id': invocation_command['id'], 'host': self.client.hostname} ) ) ) raise AssertionError(result) # check the file owner result = ssh.command( '''stat -c '%U' /home/{0}/{1}'''.format(username, filename), hostname=self.client.ip_addr, ) # assert the file is owned by the effective user assert username == result.stdout[0], "file ownership mismatch"
def test_positive_run_packages_and_services_job(self, fixture_vmsetup, fixture_org): """Tests Ansible REX job can install packages and start services :id: 47ed82fb-77ca-43d6-a52e-f62bae5d3a42 :Steps: 0. Create a VM and register to SAT and prepare for REX (ssh key) 1. Run Ansible Package job for the host to install a package 2. Check the package is present at the host 3. Run Ansible Service job for the host to start a service 4. Check the service is started on the host :expectedresults: multiple asserts along the code :CaseAutomation: automated :CaseLevel: System """ self.org = fixture_org self.client = fixture_vmsetup # set connecting to host by ip Host.set_parameter( { 'host': self.client.hostname, 'name': 'remote_execution_connect_by_ip', 'value': 'True', } ) packages = ["cow"] # Create a custom repo repo = entities.Repository( content_type='yum', product=entities.Product(organization=self.org).create(), url=FAKE_0_YUM_REPO, ).create() repo.sync() prod = repo.product.read() subs = entities.Subscription().search(query={'search': 'name={0}'.format(prod.name)}) assert len(subs) > 0, 'No subscriptions matching the product returned' ak = entities.ActivationKey( organization=self.org, content_view=self.org.default_content_view, environment=self.org.library, ).create() ak.add_subscriptions(data={'subscriptions': [{'id': subs[0].id}]}) self.client.register_contenthost(org=self.org.label, activation_key=ak.name) # install package invocation_command = make_job_invocation( { 'job-template': 'Package Action - Ansible Default', 'inputs': 'state=latest, name={}'.format(*packages), 'search-query': "name ~ {0}".format(self.client.hostname), } ) try: assert invocation_command['success'] == '1' except AssertionError: result = 'host output: {0}'.format( ' '.join( JobInvocation.get_output( {'id': invocation_command['id'], 'host': self.client.hostname} ) ) ) raise AssertionError(result) result = ssh.command("rpm -q {0}".format(*packages), hostname=self.client.ip_addr) assert result.return_code == 0 # start a service service = "postfix" ssh.command( "sed -i 's/^inet_protocols.*/inet_protocols = ipv4/' /etc/postfix/main.cf", hostname=self.client.ip_addr, ) invocation_command = make_job_invocation( { 'job-template': 'Service Action - Ansible Default', 'inputs': 'state=started, name={}'.format(service), 'search-query': "name ~ {0}".format(self.client.hostname), } ) try: assert invocation_command['success'] == '1' except AssertionError: result = 'host output: {0}'.format( ' '.join( JobInvocation.get_output( {'id': invocation_command['id'], 'host': self.client.hostname} ) ) ) raise AssertionError(result) result = ssh.command("systemctl status {0}".format(service), hostname=self.client.ip_addr) assert result.return_code == 0
def test_positive_run_receptor_installer(self, default_sat, subscribe_satellite, fixture_enable_receptor_repos): """Run Receptor installer ("Configure Cloud Connector") :CaseComponent: RHCloud-CloudConnector :Assignee: lhellebr :id: 811c7747-bec6-1a2d-8e5c-b5045d3fbc0d :expectedresults: The job passes, installs Receptor that peers with c.r.c :BZ: 1818076 """ result = default_sat.execute('stat /etc/receptor/*/receptor.conf') if result.status == 0: pytest.skip( 'Cloud Connector has already been configured on this system. ' 'It is possible to reconfigure it but then the test would not really ' 'check if everything is correctly configured from scratch. Skipping.' ) # Copy foreman-proxy user's key to root@localhost user's authorized_keys default_sat.add_rex_key(satellite=default_sat) # Set Host parameter source_display_name to something random. # To avoid 'name has already been taken' error when run multiple times # on a machine with the same hostname. host_id = Host.info({'name': default_sat.hostname})['id'] Host.set_parameter({ 'host-id': host_id, 'name': 'source_display_name', 'value': gen_string('alpha') }) template_name = 'Configure Cloud Connector' invocation = make_job_invocation({ 'async': True, 'job-template': template_name, 'inputs': f'satellite_user="******",\ satellite_password="******"', 'search-query': f'name ~ {default_sat.hostname}', }) invocation_id = invocation['id'] wait_for( lambda: entities.JobInvocation(id=invocation_id).read(). status_label in ['succeeded', 'failed'], timeout='1500s', ) result = JobInvocation.get_output({ 'id': invocation_id, 'host': default_sat.hostname }) logger.debug( f'Invocation output>>\n{result}\n<<End of invocation output') # if installation fails, it's often due to missing rhscl repo -> print enabled repos repolist = default_sat.execute('yum repolist') logger.debug(f'Repolist>>\n{repolist}\n<<End of repolist') assert entities.JobInvocation(id=invocation_id).read().status == 0 assert 'project-receptor.satellite_receptor_installer' in result assert 'Exit status: 0' in result # check that there is one receptor conf file and it's only readable # by the receptor user and root result = default_sat.execute( 'stat /etc/receptor/*/receptor.conf --format "%a:%U"') assert all(filestats == '400:foreman-proxy' for filestats in result.stdout.strip().split('\n')) result = default_sat.execute( 'ls -l /etc/receptor/*/receptor.conf | wc -l') assert int(result.stdout.strip()) >= 1
def test_positive_run_packages_and_services_job(self, fixture_vmsetup, fixture_org): """Tests Ansible REX job can install packages and start services :id: 47ed82fb-77ca-43d6-a52e-f62bae5d3a42 :Steps: 0. Create a VM and register to SAT and prepare for REX (ssh key) 1. Run Ansible Package job for the host to install a package 2. Check the package is present at the host 3. Run Ansible Service job for the host to start a service 4. Check the service is started on the host :expectedresults: multiple asserts along the code :caseautomation: automated :CaseLevel: System """ self.org = fixture_org self.client = fixture_vmsetup # set connecting to host by ip Host.set_parameter({ 'host': self.client.hostname, 'name': 'remote_execution_connect_by_ip', 'value': 'True', }) packages = ["cow"] # Create a custom repo repo = entities.Repository( content_type='yum', product=entities.Product(organization=self.org).create(), url=FAKE_0_YUM_REPO, ).create() repo.sync() prod = repo.product.read() subs = entities.Subscription().search( query={'search': 'name={0}'.format(prod.name)} ) assert len(subs) > 0, 'No subscriptions matching the product returned' ak = entities.ActivationKey( organization=self.org, content_view=self.org.default_content_view, environment=self.org.library ).create() ak.add_subscriptions(data={'subscriptions': [{'id': subs[0].id}]}) self.client.register_contenthost( org=self.org.label, activation_key=ak.name ) # install package invocation_command = make_job_invocation({ 'job-template': 'Package Action - Ansible Default', 'inputs': 'state=latest, name={}'.format(*packages), 'search-query': "name ~ {0}".format(self.client.hostname), }) try: assert invocation_command['success'] == u'1' except AssertionError: result = 'host output: {0}'.format( ' '.join(JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': self.client.hostname}) ) ) raise AssertionError(result) result = ssh.command( "rpm -q {0}".format(*packages), hostname=self.client.ip_addr ) assert result.return_code == 0 # start a service service = "postfix" ssh.command( "sed -i 's/^inet_protocols.*/inet_protocols = ipv4/' /etc/postfix/main.cf", hostname=self.client.ip_addr ) invocation_command = make_job_invocation({ 'job-template': 'Service Action - Ansible Default', 'inputs': 'state=started, name={}'.format(service), 'search-query': "name ~ {0}".format(self.client.hostname), }) try: assert invocation_command['success'] == u'1' except AssertionError: result = 'host output: {0}'.format( ' '.join(JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': self.client.hostname}) ) ) raise AssertionError(result) result = ssh.command( "systemctl status {0}".format(service), hostname=self.client.ip_addr ) assert result.return_code == 0
def test_positive_run_effective_user_job(self, fixture_vmsetup, fixture_org): """Tests Ansible REX job having effective user runs successfully :id: a5fa20d8-c2bd-4bbf-a6dc-bf307b59dd8c :Steps: 0. Create a VM and register to SAT and prepare for REX (ssh key) 1. Run Ansible Command job for the host to create a user 2. Run Ansible Command job using effective user 3. Check the job result at the host is done under that user :expectedresults: multiple asserts along the code :caseautomation: automated :CaseLevel: System """ self.org = fixture_org self.client = fixture_vmsetup # set connecting to host via ip Host.set_parameter({ 'host': self.client.hostname, 'name': 'remote_execution_connect_by_ip', 'value': 'True', }) # create a user on client via remote job username = gen_string('alpha') filename = gen_string('alpha') make_user_job = make_job_invocation({ 'job-template': 'Run Command - Ansible Default', 'inputs': "command='useradd -m {0}'".format(username), 'search-query': "name ~ {0}".format(self.client.hostname), }) try: assert make_user_job[u'success'] == u'1' except AssertionError: result = 'host output: {0}'.format( ' '.join(JobInvocation.get_output({ 'id': make_user_job[u'id'], 'host': self.client.hostname}) ) ) raise AssertionError(result) # create a file as new user invocation_command = make_job_invocation({ 'job-template': 'Run Command - Ansible Default', 'inputs': "command='touch /home/{0}/{1}'".format( username, filename), 'search-query': "name ~ {0}".format(self.client.hostname), 'effective-user': '******'.format(username), }) try: assert invocation_command['success'] == u'1' except AssertionError: result = 'host output: {0}'.format( ' '.join(JobInvocation.get_output({ 'id': invocation_command[u'id'], 'host': self.client.hostname}) ) ) raise AssertionError(result) # check the file owner result = ssh.command( '''stat -c '%U' /home/{0}/{1}'''.format(username, filename), hostname=self.client.ip_addr ) # assert the file is owned by the effective user assert username == result.stdout[0], "file ownership mismatch"
def test_positive_run_packages_and_services_job(self, rex_contenthost, module_org): """Tests Ansible REX job can install packages and start services :id: 47ed82fb-77ca-43d6-a52e-f62bae5d3a42 :Steps: 0. Create a VM and register to SAT and prepare for REX (ssh key) 1. Run Ansible Package job for the host to install a package 2. Check the package is present at the host 3. Run Ansible Service job for the host to start a service 4. Check the service is started on the host :expectedresults: multiple asserts along the code :CaseAutomation: Automated :CaseLevel: System :bz: 1872688, 1811166 :CaseImportance: Critical :customerscenario: true :parametrized: yes """ self.org = module_org client = rex_contenthost packages = ['tapir'] # Create a custom repo repo = entities.Repository( content_type='yum', product=entities.Product(organization=self.org).create(), url=settings.repos.yum_3.url, ).create() repo.sync() prod = repo.product.read() subs = entities.Subscription(organization=self.org).search( query={'search': f'name={prod.name}'}) assert len(subs), 'No subscriptions matching the product returned' ak = entities.ActivationKey( organization=self.org, content_view=self.org.default_content_view, environment=self.org.library, ).create() ak.add_subscriptions(data={'subscriptions': [{'id': subs[0].id}]}) client.register_contenthost(org=self.org.label, activation_key=ak.name) # install package invocation_command = make_job_invocation({ 'job-template': 'Package Action - Ansible Default', 'inputs': 'state=latest, name={}'.format(*packages), 'search-query': f'name ~ {client.hostname}', }) result = JobInvocation.info({'id': invocation_command['id']}) try: assert result['success'] == '1' except AssertionError: result = 'host output: {}'.format(' '.join( JobInvocation.get_output({ 'id': invocation_command['id'], 'host': client.hostname }))) raise AssertionError(result) result = client.run(f'rpm -q {" ".join(packages)}') assert result.status == 0 # stop a service service = "rsyslog" invocation_command = make_job_invocation({ 'job-template': 'Service Action - Ansible Default', 'inputs': f'state=stopped, name={service}', 'search-query': f"name ~ {client.hostname}", }) result = JobInvocation.info({'id': invocation_command['id']}) try: assert result['success'] == '1' except AssertionError: result = 'host output: {}'.format(' '.join( JobInvocation.get_output({ 'id': invocation_command['id'], 'host': client.hostname }))) raise AssertionError(result) result = client.execute(f'systemctl status {service}') assert result.status == 3 # start it again invocation_command = make_job_invocation({ 'job-template': 'Service Action - Ansible Default', 'inputs': f'state=started, name={service}', 'search-query': f'name ~ {client.hostname}', }) result = JobInvocation.info({'id': invocation_command['id']}) try: assert result['success'] == '1' except AssertionError: result = 'host output: {}'.format(' '.join( JobInvocation.get_output({ 'id': invocation_command['id'], 'host': client.hostname }))) raise AssertionError(result) result = client.execute(f'systemctl status {service}') assert result.status == 0