<commit_before>describe ManageIQ::Providers::Vmware::InfraManager::Vm::Reconfigure do
  let(:vm) do
    FactoryGirl.create(
      :vm_vmware,
      :name            => 'test_vm',
      :raw_power_state => 'poweredOff',
      :storage         => FactoryGirl.create(:storage, :name => 'storage'),
      :hardware        => FactoryGirl.create(:hardware, :virtual_hw_version => "07")
    )
  end

  it "#reconfigurable?" do
    expect(vm.reconfigurable?).to be_truthy
  end

  context "#max_total_vcpus" do
    before do
      @host = FactoryGirl.create(:host, :hardware => FactoryGirl.create(:hardware, :cpu_total_cores => 160))
      vm.host = @host
    end
    subject { vm.max_total_vcpus }

    context "vitural_hw_version" do
      it "07" do
        expect(subject).to eq(8)
      end

      it "08" do
        vm.hardware.update_attributes(:virtual_hw_version => "08")
        expect(subject).to eq(32)
      end

      it "09" do
        vm.hardware.update_attributes(:virtual_hw_version => "09")
        expect(subject).to eq(64)
      end

      it "10" do
        vm.hardware.update_attributes(:virtual_hw_version => "10")
        expect(subject).to eq(64)
      end

      it "11" do
        vm.hardware.update_attributes(:virtual_hw_version => "11")
        expect(subject).to eq(128)
      end
    end

    it "small host logical cpus" do
      @host.hardware.update_attributes(:cpu_total_cores => 4)
      expect(subject).to eq(4)
    end

    it "big host logical cpus" do
      expect(subject).to eq(8)
    end

    it 'when no host' do
      vm.update_attributes(:host_id => nil)
      expect(subject).to eq(vm.max_total_vcpus_by_version)
    end
  end

  context "#build_config_spec" do
    let(:options ) { {:vm_memory => '1024', :number_of_cpus => '8', :cores_per_socket => '2'} }
    subject { vm.build_config_spec(options) }

    it "memoryMB" do
      expect(subject["memoryMB"]).to eq(1024)
    end

    it "numCPUs" do
      expect(subject["numCPUs"]).to eq(8)
    end

    context "numCoresPerSocket" do
      it "vm_vmware virtual_hw_version = 07" do
        expect(subject["extraConfig"]).to eq([{"key" => "cpuid.coresPerSocket", "value" => "2"}])
      end

      it "vm_vmware virtual_hw_version != 07" do
        vm.hardware.update_attributes(:virtual_hw_version => "08")
        expect(subject["numCoresPerSocket"]).to eq(2)
      end
    end
  end

  context "#add_disk_config_spec" do
    before do
      @vmcs    = VimHash.new("VirtualMachineConfigSpec")
      @options = {:disk_size_in_mb => 10, :controller_key => 1000, :unit_number => 2}
    end
    subject { vm.add_disk_config_spec(@vmcs, @options).first }<commit_msg>Add tests for running vm reconfigure<commit_after>describe ManageIQ::Providers::Vmware::InfraManager::Vm::Reconfigure do
  let(:vm) do
    FactoryGirl.create(
      :vm_vmware,
      :name            => 'test_vm',
      :raw_power_state => 'poweredOff',
      :storage         => FactoryGirl.create(:storage, :name => 'storage'),
      :hardware        => FactoryGirl.create(:hardware, :cpu2x2, :ram1GB, :virtual_hw_version => "07")
    )
  end

  it "#reconfigurable?" do
    expect(vm.reconfigurable?).to be_truthy
  end

  context "#max_total_vcpus" do
    before do
      @host = FactoryGirl.create(:host, :hardware => FactoryGirl.create(:hardware, :cpu_total_cores => 160))
      vm.host = @host
    end
    subject { vm.max_total_vcpus }

    context "vitural_hw_version" do
      it "07" do
        expect(subject).to eq(8)
      end

      it "08" do
        vm.hardware.update_attributes(:virtual_hw_version => "08")
        expect(subject).to eq(32)
      end

      it "09" do
        vm.hardware.update_attributes(:virtual_hw_version => "09")
        expect(subject).to eq(64)
      end

      it "10" do
        vm.hardware.update_attributes(:virtual_hw_version => "10")
        expect(subject).to eq(64)
      end

      it "11" do
        vm.hardware.update_attributes(:virtual_hw_version => "11")
        expect(subject).to eq(128)
      end
    end

    it "small host logical cpus" do
      @host.hardware.update_attributes(:cpu_total_cores => 4)
      expect(subject).to eq(4)
    end

    it "big host logical cpus" do
      expect(subject).to eq(8)
    end

    it 'when no host' do
      vm.update_attributes(:host_id => nil)
      expect(subject).to eq(vm.max_total_vcpus_by_version)
    end
  end

  context "#build_config_spec" do
    let(:options ) { {:vm_memory => '1024', :number_of_cpus => '8', :cores_per_socket => '2'} }
    subject { vm.build_config_spec(options) }

    it "memoryMB" do
      expect(subject["memoryMB"]).to eq(1024)
    end

    it "numCPUs" do
      expect(subject["numCPUs"]).to eq(8)
    end

    context "numCoresPerSocket" do
      it "vm_vmware virtual_hw_version = 07" do
        expect(subject["extraConfig"]).to eq([{"key" => "cpuid.coresPerSocket", "value" => "2"}])
      end

      it "vm_vmware virtual_hw_version != 07" do
        vm.hardware.update_attributes(:virtual_hw_version => "08")
        expect(subject["numCoresPerSocket"]).to eq(2)
      end
    end

    context "Running VM" do
      before do
        vm.update_attributes(:raw_power_state => 'poweredOn')
      end

      context "with CPU Hot-Add disabled" do
        it "raises an exception when adding CPUs" do
          expect { subject }.to raise_error(MiqException::MiqVmError, "CPU Hot-Add not enabled")
        end
      end

      context "with CPU Hot-Add enabled" do
        before do
          vm.update_attributes(:cpu_hot_add_enabled    => true,
                               :cpu_hot_remove_enabled => false)
        end

        it "raises an exception when removing CPUs" do
          options[:number_of_cpus] = '2'

          expect { subject }.to raise_error(MiqException::MiqVmError, "Cannot remove CPUs from a running VM")
        end

        it "raises an exception when changing numCoresPerSocket" do
          options[:cores_per_socket] = 4

          expect { subject }.to raise_error(MiqException::MiqVmError, "Cannot change CPU cores per socket on a running VM")
        end

        it "sets numCPUs correctly" do
          expect(subject["numCPUs"]).to eq(8)
        end
      end

      context "with Memory Hot-Add disabled" do
        it "raises an exception when adding RAM" do
          options[:vm_memory]      = '2048'
          options[:number_of_cpus] = vm.cpu_total_cores.to_s

          expect { subject }.to raise_error(MiqException::MiqVmError, "Memory Hot-Add not enabled")
        end
      end

      context "with Memory Hot-Add enabled" do
        before do
          vm.update_attributes(:memory_hot_add_enabled => true,
                               :memory_hot_add_limit   => 2048)
        end

        it "raises an exception when removing memory" do
          options[:vm_memory]      = '512'
          options[:number_of_cpus] = vm.cpu_total_cores.to_s

          expect { subject }.to raise_error(MiqException::MiqVmError, "Cannot remove memory from a running VM")
        end

        it "raises an exception if adding more than the memory limit" do
          options[:vm_memory]      = '4096'
          options[:number_of_cpus] = vm.cpu_total_cores.to_s

          expect { subject }.to raise_error(MiqException::MiqVmError, "Cannot add more than 2048MB to this VM")
        end

        it "sets memoryMB correctly" do
          options[:vm_memory] = '1536'
          options[:number_of_cpus] = vm.cpu_total_cores.to_s

          expect(subject["memoryMB"]).to eq(1536)
        end
      end
    end
  end

  context "#add_disk_config_spec" do
    before do
      @vmcs    = VimHash.new("VirtualMachineConfigSpec")
      @options = {:disk_size_in_mb => 10, :controller_key => 1000, :unit_number => 2}
    end
    subject { vm.add_disk_config_spec(@vmcs, @options).first }
{'fraction_non_alphanumeric': 0.1143953934740883, 'fraction_numerical': 0.020089571337172103, 'mean_word_length': 2.5543428831286947, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 54, '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': '377877', 'n_tokens_mistral': 2672, 'n_tokens_neox': 2495, 'n_words': 587}