<commit_before>describe ConversionHost do
  let(:conversion_host) { FactoryBot.create(:conversion_host, :resource => vm, :ssh_transport_supported => true) }
  let(:params) do
    {
      :name          => 'transformer',
      :resource_type => vm.class.name,
      :resource_id   => vm.id,
      :resource      => vm
    }
  end

  context "processing configuration requests" do
    let(:vm) { FactoryBot.create(:vm_openstack) }

    before(:each) do
      allow(ConversionHost).to receive(:new).and_return(conversion_host)
    end

    context ".enable" do
      let(:expected_notify) do
        {
          :type    => :conversion_host_config_success,
          :options => {
            :op_name => "enable",
            :op_arg  => "type=#{params[:resource_type]} id=#{params[:resource_id]}"
          }
        }
      end

      it "to succeed and send notification" do
        allow(conversion_host).to receive(:enable_conversion_host_role)
        expect(Notification).to receive(:create).with(expected_notify)
        expect(described_class.enable(params)).to be_a(described_class)
      end

      it "to fail and send notification" do
        expected_notify[:type] = :conversion_host_config_failure
        allow(conversion_host).to receive(:enable_conversion_host_role).and_raise
        expect(Notification).to receive(:create).with(expected_notify)
        expect { described_class.enable(params) }.to raise_error(StandardError)
      end

      it "tags the associated resource as expected" do
        allow(conversion_host).to receive(:enable_conversion_host_role)
        taggings = conversion_host.resource.taggings
        tag_names = taggings.map { |tagging| tagging.tag.name }

        expect(tag_names).to contain_exactly(
          '/user/v2v_transformation_host/true',
          '/user/v2v_transformation_method/ssh'
        )
      end
    end

    context "#disable" do
      let(:expected_notify) do
        {
          :type    => :conversion_host_config_success,
          :options => {
            :op_name => "disable",
            :op_arg  => "type=#{vm.class.name} id=#{vm.id}"
          }
        }
      end

      it "to succeed and send notification" do
        allow(conversion_host).to receive(:disable_conversion_host_role)
        expect(Notification).to receive(:create).with(expected_notify)
        conversion_host.disable
      end

      it "to fail and send notification" do
        expected_notify[:type] = :conversion_host_config_failure
        allow(conversion_host).to receive(:disable_conversion_host_role).and_raise
        expect(Notification).to receive(:create).with(expected_notify)
        expect { conversion_host.disable }.to raise_error(StandardError)
      end

      it "tags the associated resource as expected" do
        allow(conversion_host).to receive(:disable_conversion_host_role)
        expect(Notification).to receive(:create).with(expected_notify)
        conversion_host.disable
        taggings = conversion_host.resource.taggings
        tag_names = taggings.map { |tagging| tagging.tag.name }

        expect(tag_names).to contain_exactly('/user/v2v_transformation_host/false')
      end
    end
  end

  context "queuing configuration requests" do
    let(:ext_management_system) { FactoryBot.create(:ext_management_system) }
    let(:vm) { FactoryBot.create(:vm_openstack, :ext_management_system => ext_management_system) }
    let(:expected_task_action) { "Configuring a conversion_host: operation=#{op} resource=(name: #{vm.name} type: #{vm.class.name} id:#{vm.id})" }

    context ".enable_queue" do
      let(:op) { 'enable' }

      it "to queue with a task" do
        task_id = described_class.enable_queue(params)
        expect(MiqTask.find(task_id)).to have_attributes(:name => expected_task_action)
        expect(MiqQueue.first).to have_attributes(
          :args        => [params.merge(:task_id => task_id).except(:resource)],
          :class_name  => described_class.name,
          :method_name => "enable",
          :priority    => MiqQueue::NORMAL_PRIORITY,
          :role        => "ems_operations",
          :zone        => vm.ext_management_system.my_zone
        )
      end
    end<commit_msg>Update spec to reflect added space.<commit_after>describe ConversionHost do
  let(:conversion_host) { FactoryBot.create(:conversion_host, :resource => vm, :ssh_transport_supported => true) }
  let(:params) do
    {
      :name          => 'transformer',
      :resource_type => vm.class.name,
      :resource_id   => vm.id,
      :resource      => vm
    }
  end

  context "processing configuration requests" do
    let(:vm) { FactoryBot.create(:vm_openstack) }

    before(:each) do
      allow(ConversionHost).to receive(:new).and_return(conversion_host)
    end

    context ".enable" do
      let(:expected_notify) do
        {
          :type    => :conversion_host_config_success,
          :options => {
            :op_name => "enable",
            :op_arg  => "type=#{params[:resource_type]} id=#{params[:resource_id]}"
          }
        }
      end

      it "to succeed and send notification" do
        allow(conversion_host).to receive(:enable_conversion_host_role)
        expect(Notification).to receive(:create).with(expected_notify)
        expect(described_class.enable(params)).to be_a(described_class)
      end

      it "to fail and send notification" do
        expected_notify[:type] = :conversion_host_config_failure
        allow(conversion_host).to receive(:enable_conversion_host_role).and_raise
        expect(Notification).to receive(:create).with(expected_notify)
        expect { described_class.enable(params) }.to raise_error(StandardError)
      end

      it "tags the associated resource as expected" do
        allow(conversion_host).to receive(:enable_conversion_host_role)
        taggings = conversion_host.resource.taggings
        tag_names = taggings.map { |tagging| tagging.tag.name }

        expect(tag_names).to contain_exactly(
          '/user/v2v_transformation_host/true',
          '/user/v2v_transformation_method/ssh'
        )
      end
    end

    context "#disable" do
      let(:expected_notify) do
        {
          :type    => :conversion_host_config_success,
          :options => {
            :op_name => "disable",
            :op_arg  => "type=#{vm.class.name} id=#{vm.id}"
          }
        }
      end

      it "to succeed and send notification" do
        allow(conversion_host).to receive(:disable_conversion_host_role)
        expect(Notification).to receive(:create).with(expected_notify)
        conversion_host.disable
      end

      it "to fail and send notification" do
        expected_notify[:type] = :conversion_host_config_failure
        allow(conversion_host).to receive(:disable_conversion_host_role).and_raise
        expect(Notification).to receive(:create).with(expected_notify)
        expect { conversion_host.disable }.to raise_error(StandardError)
      end

      it "tags the associated resource as expected" do
        allow(conversion_host).to receive(:disable_conversion_host_role)
        expect(Notification).to receive(:create).with(expected_notify)
        conversion_host.disable
        taggings = conversion_host.resource.taggings
        tag_names = taggings.map { |tagging| tagging.tag.name }

        expect(tag_names).to contain_exactly('/user/v2v_transformation_host/false')
      end
    end
  end

  context "queuing configuration requests" do
    let(:ext_management_system) { FactoryBot.create(:ext_management_system) }
    let(:vm) { FactoryBot.create(:vm_openstack, :ext_management_system => ext_management_system) }
    let(:expected_task_action) { "Configuring a conversion_host: operation=#{op} resource=(name: #{vm.name} type: #{vm.class.name} id: #{vm.id})" }

    context ".enable_queue" do
      let(:op) { 'enable' }

      it "to queue with a task" do
        task_id = described_class.enable_queue(params)
        expect(MiqTask.find(task_id)).to have_attributes(:name => expected_task_action)
        expect(MiqQueue.first).to have_attributes(
          :args        => [params.merge(:task_id => task_id).except(:resource)],
          :class_name  => described_class.name,
          :method_name => "enable",
          :priority    => MiqQueue::NORMAL_PRIORITY,
          :role        => "ems_operations",
          :zone        => vm.ext_management_system.my_zone
        )
      end
    end
{'fraction_non_alphanumeric': 0.10923971059186337, 'fraction_numerical': 0.0007116593523899894, 'mean_word_length': 2.8678899082568807, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6954537', 'n_tokens_mistral': 2484, 'n_tokens_neox': 2401, 'n_words': 453}