File: command_test.rb

package info (click to toggle)
vagrant 2.2.14%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,800 kB
  • sloc: ruby: 97,301; sh: 375; makefile: 16; lisp: 1
file content (182 lines) | stat: -rw-r--r-- 5,198 bytes parent folder | download | duplicates (3)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
require File.expand_path("../../../../base", __FILE__)

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

describe VagrantPlugins::CommandSSHConfig::Command do
  include_context "unit"
  include_context "virtualbox"

  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(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) }

  let(:argv)     { [] }
  let(:ssh_info) {{
    host:             "testhost.vagrant.dev",
    port:             1234,
    username:         "testuser",
    keys_only:        true,
    verify_host_key:         false,
    private_key_path: ["/home/vagrant/.private/keys.key"],
    forward_agent:    false,
    forward_x11:      false
  }}

  subject { described_class.new(argv, iso_env) }

  before do
    allow(machine).to receive(:ssh_info).and_return(ssh_info)
    allow(subject).to receive(:with_target_vms) { |&block| block.call machine }
  end

  describe "execute" do
    it "prints out the ssh config for the given machine" do
      output = ""
      allow(subject).to receive(:safe_puts) do |data|
        output += data if data
      end

      subject.execute

      expect(output).to eq(<<-SSHCONFIG)
Host #{machine.name}
  HostName testhost.vagrant.dev
  User testuser
  Port 1234
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/vagrant/.private/keys.key
  IdentitiesOnly yes
  LogLevel FATAL
      SSHCONFIG
    end

    it "turns on agent forwarding when it is configured" do
      allow(machine).to receive(:ssh_info) { ssh_info.merge(forward_agent: true) }

      output = ""
      allow(subject).to receive(:safe_puts) do |data|
        output += data if data
      end

      subject.execute

      expect(output).to include("ForwardAgent yes")
    end

    it "turns on x11 forwarding when it is configured" do
      allow(machine).to receive(:ssh_info) { ssh_info.merge(forward_x11: true) }

      output = ""
      allow(subject).to receive(:safe_puts) do |data|
        output += data if data
      end

      subject.execute

      expect(output).to include("ForwardX11 yes")
    end

    it "handles multiple private key paths" do
      allow(machine).to receive(:ssh_info) { ssh_info.merge(private_key_path: ["foo", "bar"]) }

      output = ""
      allow(subject).to receive(:safe_puts) do |data|
        output += data if data
      end

      subject.execute

      expect(output).to include("IdentityFile foo")
      expect(output).to include("IdentityFile bar")
    end

    it "puts quotes around an identityfile path if it has a space" do
      allow(machine).to receive(:ssh_info) { ssh_info.merge(private_key_path: ["with a space"]) }
      output = ""
      allow(subject).to receive(:safe_puts) do |data|
        output += data if data
      end

      subject.execute

      expect(output).to include('IdentityFile "with a space"')
    end

    it "omits IdentitiesOnly when keys_only is false" do
      allow(machine).to receive(:ssh_info) { ssh_info.merge(keys_only: false) }

      output = ""
      allow(subject).to receive(:safe_puts) do |data|
        output += data if data
      end

      subject.execute

      expect(output).not_to include('IdentitiesOnly')
    end

    it "omits StrictHostKeyChecking and UserKnownHostsFile when verify_host_key is true" do
      allow(machine).to receive(:ssh_info) { ssh_info.merge(verify_host_key: true) }

      output = ""
      allow(subject).to receive(:safe_puts) do |data|
        output += data if data
      end

      subject.execute

      expect(output).not_to include('StrictHostKeyChecking ')
      expect(output).not_to include('UserKnownHostsFile ')
    end

    it "formats windows paths if windows" do
      allow(machine).to receive(:ssh_info) { ssh_info.merge(private_key_path: ["C:\\path\\to\\vagrant\\home.key"]) }
      allow(Vagrant::Util::Platform).to receive(:format_windows_path).and_return("/home/vagrant/home.key")
      allow(Vagrant::Util::Platform).to receive(:windows?).and_return(true)

      output = ""
      allow(subject).to receive(:safe_puts) do |data|
        output += data if data
      end

      subject.execute
      expect(output).to include('IdentityFile /home/vagrant/home.key')
    end

    it "handles verify_host_key :never value" do
      allow(machine).to receive(:ssh_info) { ssh_info.merge(verify_host_key: :never) }

      output = ""
      allow(subject).to receive(:safe_puts) do |data|
        output += data if data
      end

      subject.execute

      expect(output).to include('StrictHostKeyChecking ')
      expect(output).to include('UserKnownHostsFile ')
    end

    it "includes custom ssh_config path when provided" do
      allow(machine).to receive(:ssh_info) { ssh_info.merge(config: "/custom/ssh/config") }

      output = ""
      allow(subject).to receive(:safe_puts) do |data|
        output += data if data
      end

      subject.execute

      expect(output).to include("Include /custom/ssh/config")
    end
  end
end