From 0bce1e630796d08e0ac660e514d74a967838310e Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 4 Oct 2019 17:26:06 -0700 Subject: [PATCH] Update tests for checksum and filechecksum --- .../commands/cloud/provider/create_test.rb | 6 +-- .../commands/cloud/provider/update_test.rb | 6 +-- .../provisioners/shell/provisioner_test.rb | 52 +++++-------------- test/unit/vagrant/util/downloader_test.rb | 52 +++++++------------ 4 files changed, 38 insertions(+), 78 deletions(-) diff --git a/test/unit/plugins/commands/cloud/provider/create_test.rb b/test/unit/plugins/commands/cloud/provider/create_test.rb index 770b88ee6..841f2cc1d 100644 --- a/test/unit/plugins/commands/cloud/provider/create_test.rb +++ b/test/unit/plugins/commands/cloud/provider/create_test.rb @@ -48,7 +48,7 @@ describe VagrantPlugins::CloudCommand::ProviderCommand::Command::Create do it "creates a provider" do allow(VagrantCloud::Provider).to receive(:new). - with(version, "virtualbox", nil, nil, "vagrant", "box-name", client.token). + with(version, "virtualbox", nil, nil, "vagrant", "box-name", client.token, nil, nil, nil). and_return(provider) expect(VagrantPlugins::CloudCommand::Util).to receive(:format_box_results) @@ -59,7 +59,7 @@ describe VagrantPlugins::CloudCommand::ProviderCommand::Command::Create do it "displays an error if encoutering a problem with the request" do allow(VagrantCloud::Provider).to receive(:new). - with(version, "virtualbox", nil, nil, "vagrant", "box-name", client.token). + with(version, "virtualbox", nil, nil, "vagrant", "box-name", client.token, nil, nil, nil). and_return(provider) allow(provider).to receive(:create_provider). @@ -73,7 +73,7 @@ describe VagrantPlugins::CloudCommand::ProviderCommand::Command::Create do it "creates a provider" do allow(VagrantCloud::Provider).to receive(:new). - with(version, "virtualbox", nil, "https://box.com/box", "vagrant", "box-name", client.token). + with(version, "virtualbox", nil, "https://box.com/box", "vagrant", "box-name", client.token, nil, nil, nil). and_return(provider) expect(VagrantPlugins::CloudCommand::Util).to receive(:format_box_results) diff --git a/test/unit/plugins/commands/cloud/provider/update_test.rb b/test/unit/plugins/commands/cloud/provider/update_test.rb index 0a03d61c3..8d2727919 100644 --- a/test/unit/plugins/commands/cloud/provider/update_test.rb +++ b/test/unit/plugins/commands/cloud/provider/update_test.rb @@ -48,7 +48,7 @@ describe VagrantPlugins::CloudCommand::ProviderCommand::Command::Update do it "updates a provider" do allow(VagrantCloud::Provider).to receive(:new). - with(version, "virtualbox", nil, nil, "vagrant", "box-name", client.token). + with(version, "virtualbox", nil, nil, "vagrant", "box-name", client.token, nil, nil, nil). and_return(provider) expect(VagrantPlugins::CloudCommand::Util).to receive(:format_box_results) @@ -59,7 +59,7 @@ describe VagrantPlugins::CloudCommand::ProviderCommand::Command::Update do it "displays an error if encoutering a problem with the request" do allow(VagrantCloud::Provider).to receive(:new). - with(version, "virtualbox", nil, nil, "vagrant", "box-name", client.token). + with(version, "virtualbox", nil, nil, "vagrant", "box-name", client.token, nil, nil, nil). and_return(provider) allow(provider).to receive(:update). @@ -73,7 +73,7 @@ describe VagrantPlugins::CloudCommand::ProviderCommand::Command::Update do it "creates a provider" do allow(VagrantCloud::Provider).to receive(:new). - with(version, "virtualbox", nil, "https://box.com/box", "vagrant", "box-name", client.token). + with(version, "virtualbox", nil, "https://box.com/box", "vagrant", "box-name", client.token, nil, nil, nil). and_return(provider) expect(VagrantPlugins::CloudCommand::Util).to receive(:format_box_results) diff --git a/test/unit/plugins/provisioners/shell/provisioner_test.rb b/test/unit/plugins/provisioners/shell/provisioner_test.rb index a31e02b51..b117d76be 100644 --- a/test/unit/plugins/provisioners/shell/provisioner_test.rb +++ b/test/unit/plugins/provisioners/shell/provisioner_test.rb @@ -175,8 +175,16 @@ describe "Vagrant::Shell::Provisioner" do end context "with remote script" do + let(:filechecksum) { double("filechecksum", checksum: checksum_value) } + let(:checksum_value) { double("checksum_value") } + + before do + allow(FileChecksum).to receive(:new).and_return(filechecksum) + allow_any_instance_of(Vagrant::Util::Downloader).to receive(:execute_curl).and_return(true) + end context "that does not have matching sha1 checksum" do + let(:checksum_value) { "INVALID_VALUE" } let(:config) { double( :config, @@ -196,14 +204,6 @@ describe "Vagrant::Shell::Provisioner" do ) } - let(:digest){ double("digest") } - before do - allow_any_instance_of(Vagrant::Util::Downloader).to receive(:execute_curl).and_return(true) - allow(digest).to receive(:file).and_return(digest) - expect(Digest::SHA1).to receive(:new).and_return(digest) - expect(digest).to receive(:hexdigest).and_return('INVALID_VALUE') - end - it "should raise an exception" do vsp = VagrantPlugins::Shell::Provisioner.new(machine, config) @@ -212,6 +212,7 @@ describe "Vagrant::Shell::Provisioner" do end context "that does not have matching sha256 checksum" do + let(:checksum_value) { "INVALID_VALUE" } let(:config) { double( :config, @@ -231,14 +232,6 @@ describe "Vagrant::Shell::Provisioner" do ) } - let(:digest){ double("digest") } - before do - allow_any_instance_of(Vagrant::Util::Downloader).to receive(:execute_curl).and_return(true) - allow(digest).to receive(:file).and_return(digest) - expect(Digest::SHA256).to receive(:new).and_return(digest) - expect(digest).to receive(:hexdigest).and_return('INVALID_VALUE') - end - it "should raise an exception" do vsp = VagrantPlugins::Shell::Provisioner.new(machine, config) @@ -247,6 +240,7 @@ describe "Vagrant::Shell::Provisioner" do end context "that does not have matching sha384 checksum" do + let(:checksum_value) { "INVALID_VALUE" } let(:config) { double( :config, @@ -266,14 +260,6 @@ describe "Vagrant::Shell::Provisioner" do ) } - let(:digest){ double("digest") } - before do - allow_any_instance_of(Vagrant::Util::Downloader).to receive(:execute_curl).and_return(true) - allow(digest).to receive(:file).and_return(digest) - expect(Digest::SHA384).to receive(:new).and_return(digest) - expect(digest).to receive(:hexdigest).and_return('INVALID_VALUE') - end - it "should raise an exception" do vsp = VagrantPlugins::Shell::Provisioner.new(machine, config) @@ -282,6 +268,7 @@ describe "Vagrant::Shell::Provisioner" do end context "that does not have matching sha512 checksum" do + let(:checksum_value) { "INVALID_VALUE" } let(:config) { double( :config, @@ -301,14 +288,6 @@ describe "Vagrant::Shell::Provisioner" do ) } - let(:digest){ double("digest") } - before do - allow_any_instance_of(Vagrant::Util::Downloader).to receive(:execute_curl).and_return(true) - allow(digest).to receive(:file).and_return(digest) - expect(Digest::SHA512).to receive(:new).and_return(digest) - expect(digest).to receive(:hexdigest).and_return('INVALID_VALUE') - end - it "should raise an exception" do vsp = VagrantPlugins::Shell::Provisioner.new(machine, config) @@ -317,6 +296,7 @@ describe "Vagrant::Shell::Provisioner" do end context "that does not have matching md5 checksum" do + let(:checksum_value) { "INVALID_VALUE" } let(:config) { double( :config, @@ -336,14 +316,6 @@ describe "Vagrant::Shell::Provisioner" do ) } - let(:digest){ double("digest") } - before do - allow_any_instance_of(Vagrant::Util::Downloader).to receive(:execute_curl).and_return(true) - allow(digest).to receive(:file).and_return(digest) - expect(Digest::MD5).to receive(:new).and_return(digest) - expect(digest).to receive(:hexdigest).and_return('INVALID_VALUE') - end - it "should raise an exception" do vsp = VagrantPlugins::Shell::Provisioner.new(machine, config) diff --git a/test/unit/vagrant/util/downloader_test.rb b/test/unit/vagrant/util/downloader_test.rb index a59318571..25e9014d1 100644 --- a/test/unit/vagrant/util/downloader_test.rb +++ b/test/unit/vagrant/util/downloader_test.rb @@ -175,11 +175,10 @@ describe Vagrant::Util::Downloader do context "with checksum" do let(:checksum_expected_value){ 'MD5_CHECKSUM_VALUE' } let(:checksum_invalid_value){ 'INVALID_VALUE' } - let(:digest){ double("digest") } + let(:filechecksum) { double("filechecksum", checksum: checksum_value) } + let(:checksum_value) { double("checksum_value") } - before do - allow(digest).to receive(:file).and_return(digest) - end + before { allow(FileChecksum).to receive(:new).with(any_args).and_return(filechecksum) } [Digest::MD5, Digest::SHA1, Digest::SHA256, Digest::SHA384, Digest::SHA512].each do |klass| short_name = klass.to_s.split("::").last.downcase @@ -188,10 +187,7 @@ describe Vagrant::Util::Downloader do subject { described_class.new(source, destination, short_name.to_sym => checksum_expected_value) } context "that matches expected value" do - before do - expect(klass).to receive(:new).and_return(digest) - expect(digest).to receive(:hexdigest).and_return(checksum_expected_value) - end + let(:checksum_value) { checksum_expected_value } it "should not raise an exception" do expect(subject.download!).to be(true) @@ -199,10 +195,7 @@ describe Vagrant::Util::Downloader do end context "that does not match expected value" do - before do - expect(klass).to receive(:new).and_return(digest) - expect(digest).to receive(:hexdigest).and_return(checksum_invalid_value) - end + let(:checksum_value) { checksum_invalid_value } it "should raise an exception" do expect{ subject.download! }.to raise_error(Vagrant::Errors::DownloaderChecksumError) @@ -213,13 +206,9 @@ describe Vagrant::Util::Downloader do context "using both md5 and sha1 digests" do context "that both match expected values" do - subject { described_class.new(source, destination, md5: checksum_expected_value, sha1: checksum_expected_value) } + let(:checksum_value) { checksum_expected_value } - before do - expect(Digest::MD5).to receive(:new).and_return(digest) - expect(Digest::SHA1).to receive(:new).and_return(digest) - expect(digest).to receive(:hexdigest).and_return(checksum_expected_value).exactly(2).times - end + subject { described_class.new(source, destination, md5: checksum_expected_value, sha1: checksum_expected_value) } it "should not raise an exception" do expect(subject.download!).to be(true) @@ -227,12 +216,14 @@ describe Vagrant::Util::Downloader do end context "that only sha1 matches expected value" do - subject { described_class.new(source, destination, md5: checksum_invalid_value, sha1: checksum_expected_value) } + subject { described_class.new(source, destination, md5: checksum_expected_value, sha1: checksum_expected_value) } + + let(:valid_checksum) { double("valid_checksum", checksum: checksum_expected_value) } + let(:invalid_checksum) { double("invalid_checksum", checksum: checksum_invalid_value) } before do - allow(Digest::MD5).to receive(:new).and_return(digest) - allow(Digest::SHA1).to receive(:new).and_return(digest) - expect(digest).to receive(:hexdigest).and_return(checksum_expected_value).at_least(:once) + allow(FileChecksum).to receive(:new).with(anything, :sha1).and_return(valid_checksum) + allow(FileChecksum).to receive(:new).with(anything, :md5).and_return(invalid_checksum) end it "should raise an exception" do @@ -241,12 +232,14 @@ describe Vagrant::Util::Downloader do end context "that only md5 matches expected value" do - subject { described_class.new(source, destination, md5: checksum_expected_value, sha1: checksum_invalid_value) } + subject { described_class.new(source, destination, md5: checksum_expected_value, sha1: checksum_expected_value) } + + let(:valid_checksum) { double("valid_checksum", checksum: checksum_expected_value) } + let(:invalid_checksum) { double("invalid_checksum", checksum: checksum_invalid_value) } before do - allow(Digest::MD5).to receive(:new).and_return(digest) - allow(Digest::SHA1).to receive(:new).and_return(digest) - expect(digest).to receive(:hexdigest).and_return(checksum_expected_value).at_least(:once) + allow(FileChecksum).to receive(:new).with(anything, :md5).and_return(valid_checksum) + allow(FileChecksum).to receive(:new).with(anything, :sha1).and_return(invalid_checksum) end it "should raise an exception" do @@ -255,14 +248,9 @@ describe Vagrant::Util::Downloader do end context "that none match expected value" do + let(:checksum_value) { checksum_expected_value } subject { described_class.new(source, destination, md5: checksum_invalid_value, sha1: checksum_invalid_value) } - before do - allow(Digest::MD5).to receive(:new).and_return(digest) - allow(Digest::SHA1).to receive(:new).and_return(digest) - expect(digest).to receive(:hexdigest).and_return(checksum_expected_value).at_least(:once) - end - it "should raise an exception" do expect{ subject.download! }.to raise_error(Vagrant::Errors::DownloaderChecksumError) end