<commit_before>require 'spec_helper'
require 'support/sharedcontext'
require 'support/libvirt_context'

require 'vagrant-libvirt/errors'
require 'vagrant-libvirt/action/forward_ports'

describe VagrantPlugins::ProviderLibvirt::Action::ForwardPorts do
  subject { described_class.new(app, env) }

  include_context 'unit'

  let(:machine_config) { double("machine_config") }
  let(:vm_config) { double("vm_config") }

  before (:each) do
    allow(machine).to receive(:config).and_return(machine_config)
    allow(machine_config).to receive(:vm).and_return(vm_config)
    allow(vm_config).to receive(:networks).and_return([])
  end

  describe '#call' do
    context 'with none defined' do
      it 'should skip calling forward_ports' do
        expect(subject).to_not receive(:forward_ports)
        expect(subject.call(env)).to be_nil
      end
    end

    context 'with forwarded port defined' do
      context 'when host port in protected range' do
        let(:port_options){ {guest: 8080, host: 80} }

        it 'should emit a warning' do
          expect(vm_config).to receive(:networks).and_return([[:forwarded_port, port_options]])
          expect(ui).to receive(:warn).with(include("You are trying to forward to privileged ports"))
          expect(subject).to receive(:forward_ports).and_return(nil)

          expect(subject.call(env)).to be_nil
        end
      end

      context 'when guest port in protected range' do
        let(:port_options){ {guest: 80, host: 8080} }

        it 'should not trigger any warning' do
          expect(vm_config).to receive(:networks).and_return([[:forwarded_port, port_options]])
          expect(ui).to_not receive(:warn)
          expect(subject).to receive(:forward_ports).and_return(nil)

          expect(subject.call(env)).to be_nil
        end
      end
    end

    context 'when udp protocol is selected' do<commit_msg>Adjust forwarded ports test to include other network (#1287)<commit_after>require 'spec_helper'
require 'support/sharedcontext'
require 'support/libvirt_context'

require 'vagrant-libvirt/errors'
require 'vagrant-libvirt/action/forward_ports'

describe VagrantPlugins::ProviderLibvirt::Action::ForwardPorts do
  subject { described_class.new(app, env) }

  include_context 'unit'

  let(:machine_config) { double("machine_config") }
  let(:vm_config) { double("vm_config") }

  before (:each) do
    allow(machine).to receive(:config).and_return(machine_config)
    allow(machine_config).to receive(:vm).and_return(vm_config)
    allow(vm_config).to receive(:networks).and_return([])
  end

  describe '#call' do
    context 'with none defined' do
      it 'should skip calling forward_ports' do
        expect(subject).to_not receive(:forward_ports)
        expect(subject.call(env)).to be_nil
      end
    end

    context 'with network includes a forwarded port' do
      let(:networks) { [
        [:private_network, {:ip=>"10.20.30.40", :protocol=>"tcp", :id=>"6b8175ed-3220-4b63-abaf-0bb8d7cdd723"}],
        [:forwarded_port, port_options],
      ]}

      let(:port_options){ {guest: 80, host: 8080} }

      it 'should be called only once' do
        expect(vm_config).to receive(:networks).and_return(networks)
        expect(ui).to_not receive(:warn)
        expect(subject).to receive(:forward_ports).and_return(nil)

        expect(subject.call(env)).to be_nil
      end

      context 'when host port in protected range' do
        let(:port_options){ {guest: 8080, host: 80} }

        it 'should emit a warning' do
          expect(vm_config).to receive(:networks).and_return(networks)
          expect(ui).to receive(:warn).with(include("You are trying to forward to privileged ports"))
          expect(subject).to receive(:forward_ports).and_return(nil)

          expect(subject.call(env)).to be_nil
        end
      end
    end

    context 'when udp protocol is selected' do
{'fraction_non_alphanumeric': 0.11852615305333677, 'fraction_numerical': 0.013913939706261274, 'mean_word_length': 3.5995260663507107, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '3615370', 'n_tokens_mistral': 1230, 'n_tokens_neox': 1161, 'n_words': 273}