File: command_test.rb

package info (click to toggle)
vagrant 2.3.7%2Bgit20230731.5fc64cde%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,616 kB
  • sloc: ruby: 111,820; sh: 462; makefile: 123; ansic: 34; lisp: 1
file content (142 lines) | stat: -rw-r--r-- 4,736 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
require File.expand_path("../../../../base", __FILE__)

require Vagrant.source_root.join("plugins/commands/powershell/command")

describe VagrantPlugins::CommandPS::Command do
  include_context "unit"

  let(:iso_env) do
    # We have to create a Vagrantfile so there is a root path
    env = isolated_environment
    env.vagrantfile("")
    env.create_vagrant_env
  end

  let(:guest)   { double("guest") }
  let(:host)    { double("host") }
  let(:config)  {
    double("config",
      vm: double("vm", communicator: communicator_name),
      winrm: double("winrm", username: winrm_username, password: winrm_password)
    )
  }
  let(:communicator_name) { :winrm }
  let(:winrm_info) { {host: winrm_host, port: winrm_port} }
  let(:winrm_username) { double("winrm_username") }
  let(:winrm_password) { double("winrm_password") }
  let(:winrm_host) { double("winrm_host") }
  let(:winrm_port) { double("winrm_port") }

  let(:remoting_ready_result) { {} }

  let(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) }

  let(:argv) { [] }

  subject { described_class.new(argv, iso_env) }

  before do
    allow(subject).to receive(:with_target_vms) { |&block| block.call machine }
    allow(iso_env).to receive(:host).and_return(host)
    allow(host).to receive(:capability?).with(:ps_client).and_return(true)

    allow(machine.communicate).to receive(:ready?).and_return(true)
    allow(machine).to receive(:config).and_return(config)

    allow(VagrantPlugins::CommunicatorWinRM::Helper).to receive(:winrm_info).and_return(winrm_info)
    allow(subject).to receive(:ready_ps_remoting_for).and_return(remoting_ready_result)
    allow(host).to receive(:capability).with(:ps_client, any_args)

    # Ignore loading up translations
    allow_any_instance_of(Vagrant::Errors::VagrantError).to receive(:translate_error)
  end

  describe "#execute" do
    context "when communicator is not ready" do
      before { expect(machine.communicate).to receive(:ready?).and_return(false) }

      it "should raise error that machine is not created" do
        expect { subject.execute }.to raise_error(Vagrant::Errors::VMNotCreatedError)
      end
    end

    context "when communicator is not winrm" do
      let(:communicator_name) { :ssh }

      context "when command is provided" do
        let(:argv) { ["-c", "command"] }

        it "should raise an error that winrm is not ready" do
          expect { subject.execute }.to raise_error(VagrantPlugins::CommunicatorWinRM::Errors::WinRMNotReady)
        end
      end

      context "when no command is provided" do
        it "should create a powershell session" do
          expect(host).to receive(:capability).with(:ps_client, any_args)
          subject.execute
        end
      end
    end

    context "when host does not support ps_client" do
      before { allow(host).to receive(:capability?).with(:ps_client).and_return(false) }

      context "when no command is provided" do
        it "should raise an error for unsupported host" do
          expect { subject.execute }.to raise_error(VagrantPlugins::CommandPS::Errors::HostUnsupported)
        end
      end

      context "when command is provided" do
        let(:argv) { ["-c", "command"] }

        it "should execute command when command is provided" do
          expect(machine.communicate).to receive(:execute).with("command", any_args).and_return(0)
          subject.execute
        end
      end
    end

    context "with command provided" do
      let(:argv) { ["-c", "command"] }

      it "executes the command on the guest" do
        expect(machine.communicate).to receive(:execute).with("command", any_args).and_return(0)
        subject.execute
      end

      context "with elevated flag" do
        let(:argv) { ["-e", "-c", "command"] }

        it "should execute the command with elevated option" do
          expect(machine.communicate).to receive(:execute).
            with("command", hash_including(elevated: true)).and_return(0)
          subject.execute
        end
      end
    end

    context "with elevated flag and no command" do
      let(:argv) { ["-e"] }

      it "should raise error that command must be provided" do
        expect { subject.execute }.to raise_error(VagrantPlugins::CommandPS::Errors::ElevatedNoCommand)
      end
    end

    it "should start a new session" do
      expect(host).to receive(:capability).with(:ps_client, any_args)
      subject.execute
    end

    context "when setup returns PreviousTrustedHosts" do
      let(:remoting_ready_result) { {"PreviousTrustedHosts" => true} }

      it "should reset the powershell remoting" do
        expect(subject).to receive(:reset_ps_remoting_for)
        subject.execute
      end
    end
  end
end