spec/beaker/hypervisor/vagrant_spec.rb in beaker-vagrant-0.6.7 vs spec/beaker/hypervisor/vagrant_spec.rb in beaker-vagrant-0.7.0

- old
+ new

@@ -28,20 +28,18 @@ :platform => Beaker::Platform.new('centos-8-x86_64') }) end it "stores the vagrant file in $WORKINGDIR/.vagrant/beaker_vagrant_files/beaker_sample.cfg" do - allow( vagrant ).to receive( :randmac ).and_return( "0123456789" ) path = vagrant.instance_variable_get( :@vagrant_path ) expect( path ).to be === File.join(Dir.pwd, '.vagrant', 'beaker_vagrant_files', 'beaker_sample.cfg') end it "can make a Vagrantfile for a set of hosts" do path = vagrant.instance_variable_get( :@vagrant_path ) - allow( vagrant ).to receive( :randmac ).and_return( "0123456789" ) vagrant.make_vfile( @hosts ) vagrantfile = File.read( File.expand_path( File.join( path, "Vagrantfile"))) puts "file is #{path}\n" @@ -51,11 +49,11 @@ c.vm.define 'vm1' do |v| v.vm.hostname = 'vm1' v.vm.box = 'vm2vm1_of_my_box' v.vm.box_url = 'http://address.for.my.box.vm1' v.vm.box_check_update = 'true' - v.vm.network :private_network, ip: "ip.address.for.vm1", :netmask => "255.255.0.0", :mac => "0123456789" + v.vm.network :private_network, ip: "ip.address.for.vm1", :netmask => "255.255.0.0" v.vm.synced_folder './', '/temp', create: true v.vm.synced_folder '../', '/tmp', create: true v.vm.network :forwarded_port, guest: 80, host: 10080 v.vm.network :forwarded_port, guest: 443, host: 4443 v.vm.network :forwarded_port, guest: 8080, host: 8080 @@ -66,11 +64,11 @@ c.vm.define 'vm2' do |v| v.vm.hostname = 'vm2' v.vm.box = 'vm2vm2_of_my_box' v.vm.box_url = 'http://address.for.my.box.vm2' v.vm.box_check_update = 'true' - v.vm.network :private_network, ip: "ip.address.for.vm2", :netmask => "255.255.0.0", :mac => "0123456789" + v.vm.network :private_network, ip: "ip.address.for.vm2", :netmask => "255.255.0.0" v.vm.synced_folder './', '/temp', create: true v.vm.synced_folder '../', '/tmp', create: true v.vm.network :forwarded_port, guest: 80, host: 10080 v.vm.network :forwarded_port, guest: 443, host: 4443 v.vm.network :forwarded_port, guest: 8080, host: 8080 @@ -81,11 +79,11 @@ c.vm.define 'vm3' do |v| v.vm.hostname = 'vm3' v.vm.box = 'vm2vm3_of_my_box' v.vm.box_url = 'http://address.for.my.box.vm3' v.vm.box_check_update = 'true' - v.vm.network :private_network, ip: "ip.address.for.vm3", :netmask => "255.255.0.0", :mac => "0123456789" + v.vm.network :private_network, ip: "ip.address.for.vm3", :netmask => "255.255.0.0" v.vm.synced_folder './', '/temp', create: true v.vm.synced_folder '../', '/tmp', create: true v.vm.network :forwarded_port, guest: 80, host: 10080 v.vm.network :forwarded_port, guest: 443, host: 4443 v.vm.network :forwarded_port, guest: 8080, host: 8080 @@ -97,22 +95,20 @@ EOF end it "can make a Vagrantfile with ssh agent forwarding enabled" do path = vagrant.instance_variable_get( :@vagrant_path ) - allow( vagrant ).to receive( :randmac ).and_return( "0123456789" ) hosts = make_hosts({},1) vagrant.make_vfile( hosts, options ) vagrantfile = File.read( File.expand_path( File.join( path, "Vagrantfile"))) expect( vagrantfile ).to match(/(ssh.forward_agent = true)/) end it "can replace underscores in host.name with hypens" do path = vagrant.instance_variable_get( :@vagrant_path ) - allow( vagrant ).to receive( :randmac ).and_return( "0123456789" ) host = make_host( 'name-with_underscore', {} ) vagrant.make_vfile( [host,], options ) vagrantfile = File.read( File.expand_path( File.join( path, "Vagrantfile"))) @@ -120,49 +116,28 @@ end it "can make a Vagrantfile with synced_folder disabled" do path = vagrant.instance_variable_get( :@vagrant_path ) - allow( vagrant ).to receive( :randmac ).and_return( "0123456789" ) hosts = make_hosts({:synced_folder => 'disabled'},1) vagrant.make_vfile( hosts, options ) vagrantfile = File.read( File.expand_path( File.join( path, "Vagrantfile"))) expect( vagrantfile ).to match(/v.vm.synced_folder .* disabled: true/) end - it "can make a Vagrantfile with network mac autogenerated" do - path = vagrant.instance_variable_get( :@vagrant_path ) - - hosts = make_hosts({},1) - vagrant.make_vfile( hosts, options ) - - vagrantfile = File.read( File.expand_path( File.join( path, "Vagrantfile"))) - expect( vagrantfile ).to match(/v.vm.network :private_network, ip: "ip.address.for.vm1", :netmask => "255.255.0.0", :mac => ".+/) - end - it "can make a Vagrantfile with network mac specified" do path = vagrant.instance_variable_get( :@vagrant_path ) hosts = make_hosts({:network_mac => 'b6:33:ae:19:48:f9'},1) vagrant.make_vfile( hosts, options ) vagrantfile = File.read( File.expand_path( File.join( path, "Vagrantfile"))) expect( vagrantfile ).to match(/v.vm.network :private_network, ip: "ip.address.for.vm1", :netmask => "255.255.0.0", :mac => "b6:33:ae:19:48:f9/) end - it "can make a Vagrantfile with network mac disabled" do - path = vagrant.instance_variable_get( :@vagrant_path ) - - hosts = make_hosts({:network_mac => 'false'},1) - vagrant.make_vfile( hosts, options ) - - vagrantfile = File.read( File.expand_path( File.join( path, "Vagrantfile"))) - expect( vagrantfile ).to match(/v.vm.network :private_network, ip: "ip.address.for.vm1", :netmask => "255.255.0.0"/) - end - it "can make a Vagrantfile with improper keys for synced folders" do path = vagrant.instance_variable_get( :@vagrant_path ) hosts = make_hosts({:mount_folders => { :test_invalid1 => {:host_path => '/invalid1', :container_path => '/invalid1'}, @@ -180,11 +155,10 @@ expect( vagrantfile ).to match(/v.vm.synced_folder '\/valid', '\/valid', create: true/) end it "can make a Vagrantfile with optional shell provisioner" do path = vagrant.instance_variable_get( :@vagrant_path ) - allow( vagrant ).to receive( :randmac ).and_return( "0123456789" ) shell_path = '/path/to/shell/script' hosts = make_hosts({ :shell_provisioner => { :path => shell_path @@ -196,11 +170,10 @@ expect( vagrantfile ).to match(/v.vm.provision 'shell', :path => '#{shell_path}'/) end it "can make a Vagrantfile with optional shell provisioner with args" do path = vagrant.instance_variable_get( :@vagrant_path ) - allow( vagrant ).to receive( :randmac ).and_return( "0123456789" ) shell_path = '/path/to/shell/script.sh' shell_args = 'arg1 arg2' hosts = make_hosts({ :shell_provisioner => { @@ -214,19 +187,17 @@ expect( vagrantfile ).to match(/v.vm.provision 'shell', :path => '#{shell_path}', :args => '#{shell_args}'/) end it "raises an error if path is not set on shell_provisioner" do path = vagrant.instance_variable_get( :@vagrant_path ) - allow( vagrant ).to receive( :randmac ).and_return( "0123456789" ) hosts = make_hosts({:shell_provisioner => {}}, 1) expect{ vagrant.make_vfile( hosts, options ) }.to raise_error RuntimeError, /No path defined for shell_provisioner or path empty/ end it "raises an error if path is EMPTY on shell_provisioner" do path = vagrant.instance_variable_get( :@vagrant_path ) - allow( vagrant ).to receive( :randmac ).and_return( "0123456789" ) empty_shell_path = '' hosts = make_hosts({ :shell_provisioner => { :path => empty_shell_path @@ -236,11 +207,10 @@ end context "when generating a windows config" do before do path = vagrant.instance_variable_get( :@vagrant_path ) - allow( vagrant ).to receive( :randmac ).and_return( "0123456789" ) @hosts[0][:platform] = 'windows' vagrant.make_vfile( @hosts ) @generated_file = File.read( File.expand_path( File.join( path, "Vagrantfile") ) ) @@ -268,11 +238,10 @@ end context 'when generating a freebsd config' do before do path = vagrant.instance_variable_get( :@vagrant_path ) - allow( vagrant ).to receive( :randmac ).and_return( "0123456789" ) @hosts[0][:platform] = 'freebsd' vagrant.make_vfile( @hosts ) @generated_file = File.read( File.expand_path( File.join( path, "Vagrantfile") ) ) @@ -283,19 +252,14 @@ end it 'has the proper guest setting' do expect( @generated_file ).to match /v.vm.guest = :freebsd\n/ end - - it 'sets the vm.base_mac setting' do - expect( @generated_file ).to match /v.vm.base_mac = '0123456789'\n/ - end end it "uses the memsize defined per vagrant host" do path = vagrant.instance_variable_get( :@vagrant_path ) - allow( vagrant ).to receive( :randmac ).and_return( "0123456789" ) vagrant.make_vfile( @hosts, {'vagrant_memsize' => 'hello!'} ) generated_file = File.read( File.expand_path( File.join( path, "Vagrantfile") ) ) @@ -305,11 +269,10 @@ end it "uses the cpus defined per vagrant host" do path = vagrant.instance_variable_get( :@vagrant_path ) - allow( vagrant ).to receive( :randmac ).and_return( "0123456789" ) vagrant.make_vfile( @hosts, {'vagrant_cpus' => 'goodbye!'} ) generated_file = File.read( File.expand_path( File.join( path, "Vagrantfile") ) ) @@ -320,11 +283,10 @@ end context "port forwarding rules" do it "supports all Vagrant parameters" do path = vagrant.instance_variable_get( :@vagrant_path ) - allow( vagrant ).to receive( :randmac ).and_return( "0123456789" ) hosts = make_hosts( { :forwarded_ports => { :http => { @@ -342,11 +304,10 @@ expect( vagrantfile ).to match(/v.vm.network :forwarded_port, protocol: 'udp', guest_ip: '0.0.0.0', guest: 80, host_ip: '127.0.0.1', host: 10080/) end it "supports supports from_ip" do path = vagrant.instance_variable_get( :@vagrant_path ) - allow( vagrant ).to receive( :randmac ).and_return( "0123456789" ) hosts = make_hosts( { :forwarded_ports => { :http => { @@ -362,11 +323,10 @@ expect( vagrantfile ).to match(/v.vm.network :forwarded_port, guest: 80, host_ip: '127.0.0.1', host: 10080/) end it "supports all to_ip" do path = vagrant.instance_variable_get( :@vagrant_path ) - allow( vagrant ).to receive( :randmac ).and_return( "0123456789" ) hosts = make_hosts( { :forwarded_ports => { :http => { @@ -382,11 +342,10 @@ expect( vagrantfile ).to match(/v.vm.network :forwarded_port, guest_ip: '0.0.0.0', guest: 80, host: 10080/) end it "supports all protocol" do path = vagrant.instance_variable_get( :@vagrant_path ) - allow( vagrant ).to receive( :randmac ).and_return( "0123456789" ) hosts = make_hosts( { :forwarded_ports => { :http => { @@ -449,61 +408,61 @@ end end describe "set_ssh_config" do - let( :out ) { double( 'stdout' ) } + let( :out ) do + <<-CONFIG + Host #{name} + HostName 127.0.0.1 + User vagrant + Port 2222 + UserKnownHostsFile /dev/null + StrictHostKeyChecking no + PasswordAuthentication no + IdentityFile /home/root/.vagrant.d/insecure_private_key + IdentitiesOnly yes + CONFIG + end let( :host ) { @hosts[0] } let( :name ) { host.name } - let( :file ) { double( 'file' ) } + let( :override_options ) { {} } before :each do - allow( Dir ).to receive( :chdir ).and_yield() - wait_thr = OpenStruct.new - state = double( 'state' ) - allow( state ).to receive( :success? ).and_return( true ) - wait_thr.value = state + # FakeFS is just broken with Tempfile + FakeFS.deactivate! + Dir.mktmpdir do |dir| + vagrant.instance_variable_get(:@options).merge!(override_options) + vagrant.instance_variable_set(:@vagrant_path, dir) + state = double( 'state' ) + allow( state ).to receive( :success? ).and_return( true ) + allow( Open3 ).to receive( :capture3 ).with( {"RUBYLIB"=>"", "RUBYOPT"=>""}, 'vagrant', 'ssh-config', name ).and_return( [ out, "", state ]) - allow( Open3 ).to receive( :popen3 ).with( {"RUBYLIB"=>"", "RUBYOPT"=>""}, 'vagrant', 'ssh-config', name ).and_return( [ "", out, "", wait_thr ]) + vagrant.set_ssh_config( host, 'root' ) + end + end - allow( file ).to receive( :path ).and_return( '/path/sshconfig' ) - allow( file ).to receive( :rewind ).and_return( true ) + it 'sets the user to root' do + expect(host['user']).to be === 'root' + end - allow( out ).to receive( :read ).and_return("Host #{name} - HostName 127.0.0.1 - User vagrant - Port 2222 - UserKnownHostsFile /dev/null - StrictHostKeyChecking no - PasswordAuthentication no - IdentityFile /home/root/.vagrant.d/insecure_private_key - IdentitiesOnly yes") + it 'sets the ssh user to root' do + expect(host['ssh']['user']).to be === 'root' end - it "can generate a ssh-config file" do - expect( Tempfile ).to receive( :new ).with( "#{host.name}").and_return( file ) - expect( file ).to receive( :write ).with("Host ip.address.for.#{name}\n HostName 127.0.0.1\n User root\n Port 2222\n UserKnownHostsFile /dev/null\n StrictHostKeyChecking no\n PasswordAuthentication no\n IdentityFile /home/root/.vagrant.d/insecure_private_key\n IdentitiesOnly no") - - vagrant.set_ssh_config( host, 'root' ) - expect( host[:vagrant_ssh_config] ).to be === '/path/sshconfig' - expect( host['ssh'][:config]).to be === false - expect( host['user']).to be === 'root' + # This is because forward_ssh_agent is true by default + it 'sets IdentitiesOnly to no' do + expect(host['ssh'][:keys_only]).to be === false end context "when :forward_ssh_agent is false" do - it "should not change IdentitiesOnly to no" do - options = vagrant.instance_variable_get( :@options ) - options['forward_ssh_agent'] = false - options = vagrant.instance_variable_set( :@options, options ) + let(:override_options) do + {forward_ssh_agent: false} + end - expect( Tempfile ).to receive( :new ).with( "#{host.name}").and_return( file ) - expect( file ).to receive( :write ).with("Host ip.address.for.#{name}\n HostName 127.0.0.1\n User root\n Port 2222\n UserKnownHostsFile /dev/null\n StrictHostKeyChecking no\n PasswordAuthentication no\n IdentityFile /home/root/.vagrant.d/insecure_private_key\n IdentitiesOnly yes") - - vagrant.set_ssh_config( host, 'root' ) - expect( host[:vagrant_ssh_config] ).to be === '/path/sshconfig' - expect( host['ssh'][:config]).to be === false - expect( host['user']).to be === 'root' + it "should keep IdentitiesOnly to yes" do + expect( host['ssh'][:keys_only]).to be === true end end end context 'with options[:provision] = false' do @@ -513,21 +472,10 @@ it 'raises an error' do expect { vagrant.configure }.to raise_error RuntimeError, /no vagrant file was found/ end end - it 'calls #get_ip_from_vagrant_file' do - vagrant.make_vfile(@hosts) - - @hosts.each do |host| - allow(vagrant).to receive(:set_ssh_config).with(host, anything) - expect(vagrant).to receive(:get_ip_from_vagrant_file).with(host.name) - end - - vagrant.configure - end - it 'calls #set_all_ssh_config' do vagrant.make_vfile(@hosts) expect(vagrant).to receive(:set_all_ssh_config) vagrant.configure end @@ -564,34 +512,9 @@ end it 'calls #hack_etc_hosts' do expect(vagrant).to receive(:hack_etc_hosts).with(@hosts, options) vagrant.set_all_ssh_config - end - end - - describe "get_ip_from_vagrant_file" do - before :each do - allow( vagrant ).to receive( :randmac ).and_return( "0123456789" ) - vagrant.make_vfile( @hosts ) - end - - it "can find the correct ip for the provided hostname" do - @hosts.each do |host| - expect( vagrant.get_ip_from_vagrant_file(host.name) ).to be === host[:ip] - end - - end - - it "returns nil if it is unable to find an ip" do - expect( vagrant.get_ip_from_vagrant_file("unknown") ).to be_nil - end - - it "raises an error if no Vagrantfile is present" do - File.delete( vagrant.instance_variable_get( :@vagrant_file ) ) - @hosts.each do |host| - expect{ vagrant.get_ip_from_vagrant_file(host.name) }.to raise_error RuntimeError, /No vagrant file found/ - end end end describe "provisioning and cleanup" do