Sha256: 0b10db02f7519926480ab117882520ce7fa08029fd466ad90835f48f02cd7a04

Contents?: true

Size: 1.98 KB

Versions: 2

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

require 'test_plugin_helper'
require 'dynflow/testing'

module Api
  module V2
    # Tests for the extra methods to play roles on Hostgroup
    class HostgroupsControllerTest < ActionController::TestCase
      setup do
        @ansible_role1 = FactoryBot.create(:ansible_role)
        @host1 = FactoryBot.create(:host, :with_hostgroup)
        @host2 = FactoryBot.create(:host, :with_hostgroup)
        @host3 = FactoryBot.create(:host, :with_hostgroup)
      end

      test 'should return an not_found due to non-existent host_id' do
        post :play_roles, :params => { :id => 'non-existent' }
        response = JSON.parse(@response.body)
        refute_empty response
        assert_response :not_found
      end

      test 'should trigger task on host group' do
        load File.join(ForemanAnsible::Engine.root,
                       '/db/seeds.d/75_job_templates.rb')
        ::JobInvocationComposer.any_instance.expects(:trigger!).returns(true)
        target = @host1
        post :play_roles, :params => { :id => target.hostgroup.id }
        response = JSON.parse(@response.body)
        assert_job_invocation_is_ok(response, target.id)
      end

      test 'should trigger two host group tasks' do
        load File.join(ForemanAnsible::Engine.root,
                       '/db/seeds.d/75_job_templates.rb')
        ::JobInvocationComposer.any_instance.expects(:trigger!).returns(true)
        target = [@host1, @host2]
        post :multiple_play_roles, :params => {
          :hostgroup_ids => target.map(&:hostgroup_id)
        }
        response = JSON.parse(@response.body)
        assert_job_invocation_is_ok(response, target.map(&:id))
      end

      test 'should list ansible roles for a host group' do
        @host3.hostgroup.ansible_roles = [@ansible_role1]
        get :ansible_roles, :params => { :id => @host3.hostgroup.id }
        response = JSON.parse(@response.body)
        assert_equal @ansible_role1.id, response.first['id']
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
foreman_ansible-2.3.1 test/functional/api/v2/hostgroups_controller_test.rb
foreman_ansible-2.3.0 test/functional/api/v2/hostgroups_controller_test.rb