diff options
author | Tim Smith <tsmith@chef.io> | 2018-03-04 20:07:16 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-04 20:07:16 -0800 |
commit | 1922d1b8c2b9bae06a83de1ccaaddd7f054b1961 (patch) | |
tree | 6755f4dd7141c71d2b1498e507dce6ffd18f2126 /spec | |
parent | 599f0dfec0c8c2b0d6d6eaf2a594abeebf40ff66 (diff) | |
parent | 661f1f7464bf311d05d2b19d16a8f733b8caac22 (diff) | |
download | chef-1922d1b8c2b9bae06a83de1ccaaddd7f054b1961.tar.gz |
Merge pull request #6498 from chef/apt_repo_cleanup
Apt repo cleanup and testing expansion
Diffstat (limited to 'spec')
-rw-r--r-- | spec/unit/provider/apt_repository_spec.rb | 139 |
1 files changed, 108 insertions, 31 deletions
diff --git a/spec/unit/provider/apt_repository_spec.rb b/spec/unit/provider/apt_repository_spec.rb index cabf6551c9..ca85831a06 100644 --- a/spec/unit/provider/apt_repository_spec.rb +++ b/spec/unit/provider/apt_repository_spec.rb @@ -47,7 +47,6 @@ EOF describe Chef::Provider::AptRepository do let(:new_resource) { Chef::Resource::AptRepository.new("multiverse") } - let(:shellout_env) { { env: { "LANG" => "en_US", "LANGUAGE" => "en_US" } } } let(:provider) do node = Chef::Node.new events = Chef::EventDispatch::Dispatcher.new @@ -60,15 +59,22 @@ describe Chef::Provider::AptRepository do end let(:apt_key_finger) do - r = double("Mixlib::ShellOut", stdout: APT_KEY_FINGER, exitstatus: 0, live_stream: true) - allow(r).to receive(:run_command) - r + double("shell_out", stdout: APT_KEY_FINGER, exitstatus: 0, error?: false) end let(:gpg_finger) do - r = double("Mixlib::ShellOut", stdout: GPG_FINGER, exitstatus: 0, live_stream: true) - allow(r).to receive(:run_command) - r + double("shell_out", stdout: GPG_FINGER, exitstatus: 0, error?: false) + end + + let(:gpg_shell_out_success) do + double("shell_out", :stdout => "pub 2048R/7BD9BF62 2011-08-19 nginx signing key <signing-key@nginx.com>", + :exitstatus => 0, :error? => false) + end + + let(:gpg_shell_out_failure) do + double("shell_out", :stderr => "gpg: no valid OpenPGP data found.\n + gpg: processing message failed: eof", + :exitstatus => 1, :error? => true) end let(:apt_fingerprints) do @@ -83,35 +89,39 @@ C5986B4F1257FFA86632CBA746181433FBB75451 end describe "#is_key_id?" do - it "should detect a key" do + it "detects a key" do expect(provider.is_key_id?("A4FF2279")).to be_truthy end - it "should detect a key with a hex signifier" do + it "detects a key with a hex signifier" do expect(provider.is_key_id?("0xA4FF2279")).to be_truthy end - it "should reject a key with the wrong length" do + it "rejects a key with the wrong length" do expect(provider.is_key_id?("4FF2279")).to be_falsey end - it "should reject a key with non-hex characters" do + it "rejects a key with non-hex characters" do expect(provider.is_key_id?("A4KF2279")).to be_falsey end end describe "#extract_fingerprints_from_cmd" do - before do - expect(Mixlib::ShellOut).to receive(:new).and_return(apt_key_finger) - end - - it "should run the desired command" do - expect(apt_key_finger).to receive(:run_command) + it "runs the desired command" do + expect(provider).to receive(:shell_out).and_return(apt_key_finger) provider.extract_fingerprints_from_cmd(apt_key_finger_cmd) end - it "should return a list of key fingerprints" do + it "returns a list of key fingerprints" do + expect(provider).to receive(:shell_out).and_return(apt_key_finger) expect(provider.extract_fingerprints_from_cmd(apt_key_finger_cmd)).to eql(apt_fingerprints) end end + describe "#cookbook_name" do + it "returns 'test' when the cookbook property is set" do + new_resource.cookbook("test") + expect(provider.cookbook_name).to eq("test") + end + end + describe "#no_new_keys?" do before do allow(provider).to receive(:extract_fingerprints_from_cmd).with(apt_key_finger_cmd).and_return(apt_fingerprints) @@ -119,14 +129,14 @@ C5986B4F1257FFA86632CBA746181433FBB75451 let(:file) { "/tmp/remote-gpg-keyfile" } - it "should match a set of keys" do + it "matches a set of keys" do allow(provider).to receive(:extract_fingerprints_from_cmd) .with("gpg --with-fingerprint --with-colons #{file}") .and_return(Array(apt_fingerprints.first)) expect(provider.no_new_keys?(file)).to be_truthy end - it "should notice missing keys" do + it "notices missing keys" do allow(provider).to receive(:extract_fingerprints_from_cmd) .with("gpg --with-fingerprint --with-colons #{file}") .and_return(%w{ F36A89E33CC1BD0F71079007327574EE02A818DD }) @@ -134,11 +144,71 @@ C5986B4F1257FFA86632CBA746181433FBB75451 end end + describe "#key_type" do + it "returns :remote_file with an http URL" do + expect(provider.key_type("https://www.chef.io/key")).to eq(:remote_file) + end + + it "returns :cookbook_file with a chef managed file" do + expect(provider).to receive(:has_cookbook_file?).and_return(true) + expect(provider.key_type("/foo/bar.key")).to eq(:cookbook_file) + end + + it "throws exception if an unknown file specified" do + expect(provider).to receive(:has_cookbook_file?).and_return(false) + expect { provider.key_type("/foo/bar.key") }.to raise_error(Chef::Exceptions::FileNotFound) + end + end + + describe "#keyserver_install_cmd" do + it "returns keyserver install command" do + expect(provider.keyserver_install_cmd("ABC", "gpg.mit.edu")).to eq("apt-key adv --recv --keyserver hkp://gpg.mit.edu:80 ABC") + end + + it "uses proxy if key_proxy property is set" do + new_resource.key_proxy("proxy.mycorp.dmz:3128") + expect(provider.keyserver_install_cmd("ABC", "gpg.mit.edu")).to eq("apt-key adv --recv --keyserver-options http-proxy=proxy.mycorp.dmz:3128 --keyserver hkp://gpg.mit.edu:80 ABC") + end + + it "properly handles keyservers passed with hkp:// URIs" do + expect(provider.keyserver_install_cmd("ABC", "hkp://gpg.mit.edu")).to eq("apt-key adv --recv --keyserver hkp://gpg.mit.edu ABC") + end + end + + describe "#is_ppa_url" do + it "returns true if the URL starts with ppa:" do + expect(provider.is_ppa_url?("ppa://example.com")).to be_truthy + end + + it "returns false if the URL does not start with ppa:" do + expect(provider.is_ppa_url?("example.com")).to be_falsey + end + end + + describe "#repo_components" do + it "returns 'main' if a PPA and components property not set" do + expect(provider).to receive(:is_ppa_url?).and_return(true) + expect(provider.repo_components).to eq("main") + end + + it "returns components property if a PPA and components is set" do + new_resource.components(["foo"]) + expect(provider).to receive(:is_ppa_url?).and_return(true) + expect(provider.repo_components).to eq(["foo"]) + end + + it "returns components property if not a PPA" do + new_resource.components(["foo"]) + expect(provider).to receive(:is_ppa_url?).and_return(false) + expect(provider.repo_components).to eq(["foo"]) + end + end + describe "#install_ppa_key" do let(:url) { "https://launchpad.net/api/1.0/~chef/+archive/main" } let(:key) { "C5986B4F1257FFA86632CBA746181433FBB75451" } - it "should get a key" do + it "gets a key" do simples = double("HTTP") allow(simples).to receive(:get).and_return("\"#{key}\"") expect(Chef::HTTP::Simple).to receive(:new).with(url).and_return(simples) @@ -148,42 +218,49 @@ C5986B4F1257FFA86632CBA746181433FBB75451 end describe "#make_ppa_url" do - it "should ignore non-ppa repositories" do - expect(provider.make_ppa_url("some_string")).to be_nil - end - - it "should create a URL" do + it "creates a URL" do expect(provider).to receive(:install_ppa_key).with("chef", "main").and_return(true) expect(provider.make_ppa_url("ppa:chef/main")).to eql("http://ppa.launchpad.net/chef/main/ubuntu") end end describe "#build_repo" do - it "should create a repository string" do + it "creates a repository string" do target = %Q{deb "http://test/uri" unstable main\n} expect(provider.build_repo("http://test/uri", "unstable", "main", false, nil)).to eql(target) end - it "should create a repository string with no distribution" do + it "creates a repository string with no distribution" do target = %Q{deb "http://test/uri" main\n} expect(provider.build_repo("http://test/uri", nil, "main", false, nil)).to eql(target) end - it "should create a repository string with source" do + it "creates a repository string with source" do target = %Q{deb "http://test/uri" unstable main\ndeb-src "http://test/uri" unstable main\n} expect(provider.build_repo("http://test/uri", "unstable", "main", false, nil, true)).to eql(target) end - it "should create a repository string with options" do + it "creates a repository string with options" do target = %Q{deb [trusted=yes] "http://test/uri" unstable main\n} expect(provider.build_repo("http://test/uri", "unstable", "main", true, nil)).to eql(target) end - it "should handle a ppa repo" do + it "handles a ppa repo" do target = %Q{deb "http://ppa.launchpad.net/chef/main/ubuntu" unstable main\n} expect(provider).to receive(:make_ppa_url).with("ppa:chef/main").and_return("http://ppa.launchpad.net/chef/main/ubuntu") expect(provider.build_repo("ppa:chef/main", "unstable", "main", false, nil)).to eql(target) end end + describe "#keyfile_is_invalid?" do + it "returns true if the file is invalid" do + expect(provider).to receive(:shell_out).and_return(gpg_shell_out_failure) + expect(provider.keyfile_is_invalid?("/foo/bar.key")).to be_truthy + end + + it "returns false if the file is valid" do + expect(provider).to receive(:shell_out).and_return(gpg_shell_out_success) + expect(provider.keyfile_is_invalid?("/foo/bar.key")).to be_falsey + end + end end |