From e750c07583262d7283792f46be008e24f5387677 Mon Sep 17 00:00:00 2001 From: Teemu Matilainen Date: Sat, 22 Jun 2013 19:13:23 -0300 Subject: [PATCH] Add unit tests for AptProxyConfig --- Gemfile | 3 +- Gemfile.lock | 10 ++ Rakefile | 12 +- spec/spec_helper.rb | 10 ++ spec/unit/support/shared/apt_proxy_config.rb | 103 ++++++++++++++++++ .../vagrant-cachier/apt_proxy_config_spec.rb | 34 ++++++ 6 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 spec/spec_helper.rb create mode 100644 spec/unit/support/shared/apt_proxy_config.rb create mode 100644 spec/unit/vagrant-cachier/apt_proxy_config_spec.rb diff --git a/Gemfile b/Gemfile index f4760c3..8b3af2e 100644 --- a/Gemfile +++ b/Gemfile @@ -4,7 +4,8 @@ source 'https://rubygems.org' gemspec group :development do + gem 'rake' + gem 'rspec', '~> 2.11' gem 'vagrant', github: 'mitchellh/vagrant' gem 'vagrant-lxc', github: 'fgrehm/vagrant-lxc' - gem 'rake' end diff --git a/Gemfile.lock b/Gemfile.lock index 630680f..789071d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -27,6 +27,7 @@ GEM specs: childprocess (0.3.9) ffi (~> 1.0, >= 1.0.11) + diff-lcs (1.2.4) erubis (2.7.0) ffi (1.8.1) i18n (0.6.4) @@ -36,12 +37,21 @@ GEM net-ssh (>= 2.6.5) net-ssh (2.6.7) rake (10.0.4) + rspec (2.13.0) + rspec-core (~> 2.13.0) + rspec-expectations (~> 2.13.0) + rspec-mocks (~> 2.13.0) + rspec-core (2.13.1) + rspec-expectations (2.13.0) + diff-lcs (>= 1.1.3, < 2.0) + rspec-mocks (2.13.1) PLATFORMS ruby DEPENDENCIES rake + rspec (~> 2.11) vagrant! vagrant-cachier! vagrant-lxc! diff --git a/Rakefile b/Rakefile index 09df0ec..3004b1d 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,11 @@ -Dir['./tasks/**/*.rake'].each { |f| load f } - require 'bundler/gem_tasks' +require 'rspec/core/rake_task' -task :ci => ['spec:unit'] +task :default => [:spec] + +namespace :spec do + RSpec::Core::RakeTask.new('unit') do |t| + t.pattern = 'spec/unit/**/*_spec.rb' + end +end +task :spec => ['spec:unit'] diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..9a15a42 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,10 @@ +RSpec.configure do |config| + config.expect_with :rspec do |c| + c.syntax = :expect + end + config.color = true + config.tty = true +end + +require 'tempfile' +require 'vagrant-cachier' diff --git a/spec/unit/support/shared/apt_proxy_config.rb b/spec/unit/support/shared/apt_proxy_config.rb new file mode 100644 index 0000000..b5958a6 --- /dev/null +++ b/spec/unit/support/shared/apt_proxy_config.rb @@ -0,0 +1,103 @@ +def config_with(options) + instance.tap do |c| + options.each_pair { |k, v| c.send("#{k}=".to_sym, v) } + c.finalize! + end +end + +def conf_line(proto, name, port = 3142) + if name == :direct + %Q{Acquire::#{proto}::Proxy "DIRECT";\n} + else + %Q{Acquire::#{proto}::Proxy "#{proto}://#{name}:#{port}";\n} + end +end + +def conf_line_pattern(proto, name, port = 3142) + "^#{Regexp.escape(conf_line(proto, name, port))}" +end + +shared_examples "apt proxy config" do |proto| + context "#{proto} proxy" do + + context "with ip" do + subject { config_with(proto => "10.1.2.3") } + its(:enabled?) { should be_true } + its(:to_s) { should eq conf_line(proto, "10.1.2.3") } + end + + context "with name" do + subject { config_with(proto => "proxy.example.com") } + its(:enabled?) { should be_true } + its(:to_s) { should eq conf_line(proto, "proxy.example.com") } + end + + context "with name and port" do + subject { config_with(proto => "acng:8080") } + its(:enabled?) { should be_true } + its(:to_s) { should eq conf_line(proto, "acng", 8080) } + end + + context "with protocol and name" do + subject { config_with(proto => "#{proto}://proxy.foo.tld") } + its(:enabled?) { should be_true } + its(:to_s) { should eq conf_line(proto, "proxy.foo.tld") } + end + + context "with protocol and name and port" do + subject { config_with(proto => "#{proto}://prism.nsa.gov:666") } + its(:enabled?) { should be_true } + its(:to_s) { should eq conf_line(proto, "prism.nsa.gov", 666) } + end + + ["DIRECT", "direct"].each do |direct| + context "with #{direct.inspect}" do + subject { config_with(proto => direct) } + its(:enabled?) { should be_true } + its(:to_s) { should eq conf_line(proto, :direct) } + end + end + + [false, ""].each do |unset| + context "with #{unset.inspect}" do + subject { config_with(proto => unset) } + its(:enabled?) { should be_true } + its(:to_s) { should eq "" } + end + end + + context "with nil" do + subject { config_with(proto => nil) } + its(:enabled?) { should be_false } + its(:to_s) { should eq "" } + end + + end +end + +shared_examples "apt proxy env var" do |var, proto| + context var do + let(:conf) { config_with(http: "acng:8080", https: "ssl-proxy:8443") } + + it "sets #{proto} proxy" do + ENV[var] = "myproxy" + expect(conf.to_s).to match conf_line_pattern(proto, "myproxy") + end + + it "does not set other proxies" do + ENV[var] = "myproxy:2345" + conf = config_with({}) + expect(conf.to_s).to eq conf_line(proto, "myproxy", 2345) + end + + it "sets empty configuration" do + ENV[var] = "" + expect(conf.to_s).to_not match %r{#{proto}://} + end + + it "sets direct configuration" do + ENV[var] = "direct" + expect(conf.to_s).to match conf_line_pattern(proto, :direct) + end + end +end diff --git a/spec/unit/vagrant-cachier/apt_proxy_config_spec.rb b/spec/unit/vagrant-cachier/apt_proxy_config_spec.rb new file mode 100644 index 0000000..e740f17 --- /dev/null +++ b/spec/unit/vagrant-cachier/apt_proxy_config_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' +require 'unit/support/shared/apt_proxy_config' +require 'vagrant-cachier/apt_proxy_config' + +describe VagrantPlugins::Cachier::AptProxyConfig do + let(:instance) { described_class.new } + + before :each do + # Ensure tests are not affected by environment variables + %w[APT_PROXY_HTTP APT_PROXY_HTTPS].each { |k| ENV.delete(k) } + end + + context "defaults" do + subject { config_with({}) } + its(:enabled?) { should be_false } + its(:to_s) { should eq "" } + end + + include_examples "apt proxy config", "http" + include_examples "apt proxy config", "https" + + context "with both http and https proxies" do + subject { config_with(http: "10.2.3.4", https: "ssl-proxy:8443") } + its(:enabled?) { should be_true } + its(:to_s) { should match conf_line_pattern("http", "10.2.3.4") } + its(:to_s) { should match conf_line_pattern("https", "ssl-proxy", 8443) } + end + + context "with env var" do + include_examples "apt proxy env var", "APT_PROXY_HTTP", "http" + include_examples "apt proxy env var", "APT_PROXY_HTTPS", "https" + end + +end