<commit_before>require 'spec_helper'

describe MachineUpdateService do
  describe '.update_from_facts' do
    let(:facts) do
      Puppetdb::Facts.new(
        interfaces: 'eth0,eth1,eth2,lo,Ethernet 2',
        ipaddress_eth0: '172.20.10.7',
        ipaddress6_eth0: '172.20.10.7v6',
        ipaddress_eth1: '10.0.0.1',
        ipaddress_lo: '127.0.0.1',
        "ipaddress_Ethernet 2": '127.0.1.1',
        macaddress_eth0: '6a:a8:6d:e0:a2:a6',
        macaddress_eth1: '3c:97:0e:40:06:be',
        macaddress_eth2: '3c:97:0e:40:06:b1',
        "macaddress_Ethernet 2": '3c:97:0e:40:06:b2',
        netmask_eth0: '255.255.255.240',
        netmask_eth1: '255.255.255.0',
        netmask_lo: '255.0.0.0',
        "netmask_ethernet 2": '255.255.0.0',
        is_virtual: false,
        serialnumber: '42Q6F5J',
        memorysize_mb: '12018.26',
        blockdevices: 'hda,sda',
        blockdevice_hda_size: 2000000,
        blockdevice_sda_size: 1000000,
        lsbdistrelease: '2012 R1'
      )
    end

    let(:factsv4) do
      Puppetdb::FactsV4.new(
        interfaces: 'eth0,eth1,eth2,lo,Ethernet 2',
        ipaddress_eth0: '172.20.10.7',
        ipaddress6_eth0: '172.20.10.7v6',
        ipaddress_eth1: '10.0.0.1',
        ipaddress_lo: '127.0.0.1',
        "ipaddress_Ethernet 2": '127.0.1.1',
        macaddress_eth0: '6a:a8:6d:e0:a2:a6',
        macaddress_eth1: '3c:97:0e:40:06:be',
        macaddress_eth2: '3c:97:0e:40:06:b1',
        "macaddress_Ethernet 2": '3c:97:0e:40:06:b2',
        netmask_eth0: '255.255.255.240',
        netmask_eth1: '255.255.255.0',
        netmask_lo: '255.0.0.0',
        "netmask_ethernet 2": '255.255.0.0',
        is_virtual: false,
        serialnumber: '42Q6F5J',
        memorysize_mb: '12018.26',
        blockdevices: 'hda,sda',
        blockdevice_hda_size: 2000000,
        blockdevice_sda_size: 1000000,
        operatingsystemrelease: '2012 R2'
      )
    end

    let(:machine) do
      Machine.create!(fqdn: 'test.example.com')
    end

    before do
      allow(Puppetdb::FactsV3).to receive(:for).and_return(facts)
      allow(Puppetdb::FactsV3).to receive(:raw_data).and_return(facts.to_s)
      allow(Puppetdb::FactsV4).to receive(:for).and_return(factsv4)
      allow(Puppetdb::FactsV4).to receive(:raw_data).and_return(factsv4.to_s)
      @url = "https://puppetdb.example.com"
    end

    # it 'sets the device type' do
    #   described_class.update_from_facts(machine,  @url)

    #   expect(machine.device_type_id).to_not be_nil
    # end

    it 'sets the device type' do
        machine = Machine.create!(fqdn: 'test.example.com')
        facts["is_virtual"] = true
        allow(Puppetdb::Facts).to receive(:for_node).and_return(facts)
        described_class.update_from_facts(machine, @url)
        machine = Machine.find_by_fqdn(machine.fqdn)
        expect(machine).to be_a VirtualMachine
    end


    it 'sets the serialnumber' do
      described_class.update_from_facts(machine,  @url)

      expect(machine.serialnumber).to eq('42Q6F5J')
    end

    it 'sets the os_release' do
      described_class.update_from_facts(machine,  @url)

      expect(machine.os_release).to eq('2012 R1')
    end

    it 'sets the os_release from v4 facts' do
      described_class.update_from_facts(machine,  @url, "v4")

      expect(machine.os_release).to eq('2012 R2')
    end

    context 'retrieve the installed RAM' do
      it 'sets the RAM' do
        described_class.update_from_facts(machine, @url)
<commit_msg>Use FactoryGirl for machine creation in spec. Stub User.current to get proper results.<commit_after>require 'spec_helper'

describe MachineUpdateService do
  describe '.update_from_facts' do
    let(:facts) do
      Puppetdb::Facts.new(
        interfaces: 'eth0,eth1,eth2,lo,Ethernet 2',
        ipaddress_eth0: '172.20.10.7',
        ipaddress6_eth0: '172.20.10.7v6',
        ipaddress_eth1: '10.0.0.1',
        ipaddress_lo: '127.0.0.1',
        "ipaddress_Ethernet 2": '127.0.1.1',
        macaddress_eth0: '6a:a8:6d:e0:a2:a6',
        macaddress_eth1: '3c:97:0e:40:06:be',
        macaddress_eth2: '3c:97:0e:40:06:b1',
        "macaddress_Ethernet 2": '3c:97:0e:40:06:b2',
        netmask_eth0: '255.255.255.240',
        netmask_eth1: '255.255.255.0',
        netmask_lo: '255.0.0.0',
        "netmask_ethernet 2": '255.255.0.0',
        is_virtual: false,
        serialnumber: '42Q6F5J',
        memorysize_mb: '12018.26',
        blockdevices: 'hda,sda',
        blockdevice_hda_size: 2000000,
        blockdevice_sda_size: 1000000,
        lsbdistrelease: '2012 R1'
      )
    end

    let(:factsv4) do
      Puppetdb::FactsV4.new(
        interfaces: 'eth0,eth1,eth2,lo,Ethernet 2',
        ipaddress_eth0: '172.20.10.7',
        ipaddress6_eth0: '172.20.10.7v6',
        ipaddress_eth1: '10.0.0.1',
        ipaddress_lo: '127.0.0.1',
        "ipaddress_Ethernet 2": '127.0.1.1',
        macaddress_eth0: '6a:a8:6d:e0:a2:a6',
        macaddress_eth1: '3c:97:0e:40:06:be',
        macaddress_eth2: '3c:97:0e:40:06:b1',
        "macaddress_Ethernet 2": '3c:97:0e:40:06:b2',
        netmask_eth0: '255.255.255.240',
        netmask_eth1: '255.255.255.0',
        netmask_lo: '255.0.0.0',
        "netmask_ethernet 2": '255.255.0.0',
        is_virtual: false,
        serialnumber: '42Q6F5J',
        memorysize_mb: '12018.26',
        blockdevices: 'hda,sda',
        blockdevice_hda_size: 2000000,
        blockdevice_sda_size: 1000000,
        operatingsystemrelease: '2012 R2'
      )
    end

    let(:machine) do
      current_user = FactoryGirl.create(:user, admin: true)
      owner = FactoryGirl.create(:owner, users: [current_user])
      allow(User).to receive(:current).and_return(current_user)
      machine = FactoryGirl.create(:machine, fqdn: 'test.example.com', owner: owner)
    end

    before do
      allow(Puppetdb::FactsV3).to receive(:for).and_return(facts)
      allow(Puppetdb::FactsV3).to receive(:raw_data).and_return(facts.to_s)
      allow(Puppetdb::FactsV4).to receive(:for).and_return(factsv4)
      allow(Puppetdb::FactsV4).to receive(:raw_data).and_return(factsv4.to_s)
      @url = "https://puppetdb.example.com"
    end

    # it 'sets the device type' do
    #   described_class.update_from_facts(machine,  @url)

    #   expect(machine.device_type_id).to_not be_nil
    # end

    it 'sets the device type' do
        fqdn = machine.fqdn
        facts["is_virtual"] = true
        allow(Puppetdb::Facts).to receive(:for_node).and_return(facts)
        described_class.update_from_facts(machine, @url)
        machine = Machine.find_by_fqdn(fqdn)
        expect(machine).to be_a VirtualMachine
    end


    it 'sets the serialnumber' do
      described_class.update_from_facts(machine,  @url)

      expect(machine.serialnumber).to eq('42Q6F5J')
    end

    it 'sets the os_release' do
      described_class.update_from_facts(machine,  @url)

      expect(machine.os_release).to eq('2012 R1')
    end

    it 'sets the os_release from v4 facts' do
      described_class.update_from_facts(machine,  @url, "v4")

      expect(machine.os_release).to eq('2012 R2')
    end

    context 'retrieve the installed RAM' do
      it 'sets the RAM' do
        described_class.update_from_facts(machine, @url)

{'fraction_non_alphanumeric': 0.1348174778761062, 'fraction_numerical': 0.08849557522123894, 'mean_word_length': 2.824960338445267, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 36, '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': '3944602', 'n_tokens_mistral': 3107, 'n_tokens_neox': 2768, 'n_words': 443}