def handler(name, groups):
     h = Host(name=name, inventory=inventory)
     h.save()
     h = Host.objects.get(name=name, inventory=inventory)
     for g in groups:
         h.groups.add(g)
     h.save()
     h = Host.objects.get(name=name, inventory=inventory)
     return h
def test_callback_find_matching_hosts(mocker, get, job_template_prompts,
                                      admin_user):
    job_template = job_template_prompts(False)
    job_template.host_config_key = "foo"
    job_template.save()
    host_with_alias = Host(name='localhost', inventory=job_template.inventory)
    host_with_alias.save()
    with mocker.patch('awx.main.access.BaseAccess.check_license'):
        r = get(reverse('api:job_template_callback',
                        kwargs={'pk': job_template.pk}),
                user=admin_user,
                expect=200)
        assert tuple(r.data['matching_hosts']) == ('localhost', )
def test_callback_extra_var_takes_priority_over_host_name(
        mocker, get, job_template_prompts, admin_user):
    job_template = job_template_prompts(False)
    job_template.host_config_key = "foo"
    job_template.save()
    host_with_alias = Host(name='localhost',
                           variables={'ansible_host': 'foobar'},
                           inventory=job_template.inventory)
    host_with_alias.save()
    with mocker.patch('awx.main.access.BaseAccess.check_license'):
        r = get(reverse('api:job_template_callback',
                        kwargs={'pk': job_template.pk}),
                user=admin_user,
                expect=200)
        assert not r.data['matching_hosts']