<commit_before>require 'spec_helper'
require 'vagrant-windows-domain/provisioner'
require 'vagrant-windows-domain/config'
require 'rspec/its'

describe VagrantPlugins::WindowsDomain::Provisioner do
  include_context "unit"

  let(:root_path)           { (Pathname.new(Dir.mktmpdir)).to_s }
  let(:ui)                  { double("ui") }
  let(:machine)             { double("machine", ui: ui, id: "1234") }
  let(:env)                 { double("environment", root_path: root_path, ui: ui) }
  let(:vm)                  { double ("vm") }
  let(:communicator)        { double ("communicator") }
  let(:shell)               { double ("shell") }
  let(:powershell)          { double ("powershell") }
  let(:guest)               { double ("guest") }
  let(:configuration_file)  { "manifests/MyWebsite.ps1" }
  let(:module_path)         { ["foo/modules", "foo/modules2"] }
  let(:root_config)         { VagrantPlugins::WindowsDomain::Config.new }
  subject                   { described_class.new machine, root_config }

  describe "configure" do
    before do
      allow(machine).to receive(:root_config).and_return(root_config)
      machine.stub(config: root_config, env: env, id: "1234")
      allow(ui).to receive(:say).with(any_args)
      allow(machine).to receive(:communicate).and_return(communicator)
      allow(communicator).to receive(:shell).and_return(shell)
      allow(shell).to receive(:powershell).with("$env:COMPUTERNAME").and_yield(:stdout, "myoldcomputername")
      allow(root_config).to receive(:vm).and_return(vm)
      allow(vm).to receive(:communicator).and_return(:winrm)
      root_config.finalize!
      root_config.validate(machine)
    end

    it "should confirm if the OS is Windows" do
      allow(communicator).to receive(:sudo).twice
      expect(subject.windows?).to eq(true)
      subject.configure(root_config)
    end

    it "should error if the detected OS is not Windows" do
      allow(vm).to receive(:communicator).and_return(:ssh)
      expect { subject.configure(root_config) }.to raise_error("Unsupported platform detected. Vagrant Windows Domain only works on Windows guest environments.")
    end

  end

  describe "provision" do

    before do
      allow(machine).to receive(:root_config).and_return(root_config)
      machine.stub(config: root_config, env: env, id: "1234")
      allow(ui).to receive(:say).with(any_args)
      allow(machine).to receive(:communicate).and_return(communicator)
      allow(communicator).to receive(:shell).and_return(shell)
      allow(shell).to receive(:powershell).with("$env:COMPUTERNAME").and_yield(:stdout, "myoldcomputername")
      allow(root_config).to receive(:vm).and_return(vm)
      allow(vm).to receive(:communicator).and_return(:winrm)
      allow(communicator).to receive(:sudo).with("which Add-Computer", {:error_class=>VagrantPlugins::WindowsDomain::WindowsDomainError, :error_key=>:binary_not_detected, :domain=>"foo.com", :binary=>"Add-Computer"})
      allow(communicator).to receive(:sudo).with("which Remove-Computer", {:error_class=>VagrantPlugins::WindowsDomain::WindowsDomainError, :error_key=>:binary_not_detected, :domain=>"foo.com", :binary=>"Remove-Computer"})

      root_config.domain = "foo.com"
      root_config.username = "username"
      root_config.password = "password"

      root_config.finalize!
      root_config.validate(machine)
      subject.configure(root_config)
    end

    it "should join the domain" do
      allow(communicator).to receive(:upload)
      allow(ui).to receive(:info)
      allow(shell).to receive(:powershell).with("powershell -ExecutionPolicy Bypass -OutputFormat Text -file c:/tmp/vagrant-windows-domain-runner.ps1")
      expect(communicator).to receive(:sudo).with("del c:/tmp/vagrant-windows-domain-runner.ps1")
      expect(machine).to receive(:action). with(:reload, {:provision_ignore_sentinel=>false})
      expect(communicator).to receive(:ready?).and_return(true)
      subject.restart_sleep_duration = 0
      subject.provision
      expect(subject.old_computer_name).to eq("myoldcomputername")
    end

    it "should restart the machine on a successful domain join" do<commit_msg>Expect powershell command to be run during join domain action<commit_after>require 'spec_helper'
require 'vagrant-windows-domain/provisioner'
require 'vagrant-windows-domain/config'
require 'rspec/its'

describe VagrantPlugins::WindowsDomain::Provisioner do
  include_context "unit"

  let(:root_path)           { (Pathname.new(Dir.mktmpdir)).to_s }
  let(:ui)                  { double("ui") }
  let(:machine)             { double("machine", ui: ui, id: "1234") }
  let(:env)                 { double("environment", root_path: root_path, ui: ui) }
  let(:vm)                  { double ("vm") }
  let(:communicator)        { double ("communicator") }
  let(:shell)               { double ("shell") }
  let(:powershell)          { double ("powershell") }
  let(:guest)               { double ("guest") }
  let(:configuration_file)  { "manifests/MyWebsite.ps1" }
  let(:module_path)         { ["foo/modules", "foo/modules2"] }
  let(:root_config)         { VagrantPlugins::WindowsDomain::Config.new }
  subject                   { described_class.new machine, root_config }

  describe "configure" do
    before do
      allow(machine).to receive(:root_config).and_return(root_config)
      machine.stub(config: root_config, env: env, id: "1234")
      allow(ui).to receive(:say).with(any_args)
      allow(machine).to receive(:communicate).and_return(communicator)
      allow(communicator).to receive(:shell).and_return(shell)
      allow(shell).to receive(:powershell).with("$env:COMPUTERNAME").and_yield(:stdout, "myoldcomputername")
      allow(root_config).to receive(:vm).and_return(vm)
      allow(vm).to receive(:communicator).and_return(:winrm)
      root_config.finalize!
      root_config.validate(machine)
    end

    it "should confirm if the OS is Windows" do
      allow(communicator).to receive(:sudo).twice
      expect(subject.windows?).to eq(true)
      subject.configure(root_config)
    end

    it "should error if the detected OS is not Windows" do
      allow(vm).to receive(:communicator).and_return(:ssh)
      expect { subject.configure(root_config) }.to raise_error("Unsupported platform detected. Vagrant Windows Domain only works on Windows guest environments.")
    end

  end

  describe "provision" do

    before do
      allow(machine).to receive(:root_config).and_return(root_config)
      machine.stub(config: root_config, env: env, id: "1234")
      allow(ui).to receive(:say).with(any_args)
      allow(machine).to receive(:communicate).and_return(communicator)
      allow(communicator).to receive(:shell).and_return(shell)
      allow(shell).to receive(:powershell).with("$env:COMPUTERNAME").and_yield(:stdout, "myoldcomputername")
      allow(root_config).to receive(:vm).and_return(vm)
      allow(vm).to receive(:communicator).and_return(:winrm)
      allow(communicator).to receive(:sudo).with("which Add-Computer", {:error_class=>VagrantPlugins::WindowsDomain::WindowsDomainError, :error_key=>:binary_not_detected, :domain=>"foo.com", :binary=>"Add-Computer"})
      allow(communicator).to receive(:sudo).with("which Remove-Computer", {:error_class=>VagrantPlugins::WindowsDomain::WindowsDomainError, :error_key=>:binary_not_detected, :domain=>"foo.com", :binary=>"Remove-Computer"})

      root_config.domain = "foo.com"
      root_config.username = "username"
      root_config.password = "password"

      root_config.finalize!
      root_config.validate(machine)
      subject.configure(root_config)
    end

    it "should join the domain" do
      allow(communicator).to receive(:upload)
      allow(ui).to receive(:info)
      expect(shell).to receive(:powershell).with("powershell -ExecutionPolicy Bypass -OutputFormat Text -file c:/tmp/vagrant-windows-domain-runner.ps1")
      expect(communicator).to receive(:sudo).with("del c:/tmp/vagrant-windows-domain-runner.ps1")
      expect(machine).to receive(:action). with(:reload, {:provision_ignore_sentinel=>false})
      expect(communicator).to receive(:ready?).and_return(true)
      subject.restart_sleep_duration = 0
      subject.provision
      expect(subject.old_computer_name).to eq("myoldcomputername")
    end

    it "should restart the machine on a successful domain join" do
{'fraction_non_alphanumeric': 0.1373184491657664, 'fraction_numerical': 0.004081142719961589, 'mean_word_length': 3.92434988179669, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '232197', 'n_tokens_mistral': 2623, 'n_tokens_neox': 2478, 'n_words': 454}