vagrant-proxyconf 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,26 +20,176 @@ describe VagrantPlugins::ProxyConf::Action::IsEnabled do
20
20
  end
21
21
  end
22
22
 
23
- [false, ''].each do |value|
24
- context "with `config.proxy.enabled=#{value.inspect}`" do
25
- let(:enabled) { value }
23
+ describe "#has_proxy_env_var?" do
24
+ subject do
25
+ is_enabled = described_class.new(app, env)
26
26
 
27
- it "results to falsy" do
28
- described_class.new(app, {}).call(env)
29
- expect(env[:result]).to be_falsey
27
+ is_enabled.send(:has_proxy_env_var?, var)
28
+ end
29
+
30
+ context "when HTTP_PROXY is set in the environment and config.proxy.enabled=true" do
31
+ let(:enabled) { true }
32
+ let(:var) { 'HTTP_PROXY' }
33
+
34
+ before :each do
35
+ ENV[var] = 'http://localhost:8888'
36
+ end
37
+
38
+ after :each do
39
+ ENV[var] = nil
40
+ end
41
+
42
+ it { expect(ENV[var]).to eq 'http://localhost:8888' }
43
+
44
+ it { is_expected.to eq true }
45
+ end
46
+
47
+ context "when HTTPS_PROXY="" is set in the environment and config.proxy.enabled=true" do
48
+ let(:enabled) { true }
49
+ let(:var) { 'HTTPS_PROXY' }
50
+
51
+ before :each do
52
+ ENV[var] = ''
53
+ end
54
+
55
+ after :each do
56
+ ENV[var] = nil
57
+ end
58
+
59
+ it { expect(ENV[var]).to eq '' }
60
+ it { is_expected.to eq false }
61
+ end
62
+
63
+ end
64
+
65
+ describe "#plugin_disabled?" do
66
+
67
+ subject do
68
+ is_enabled = described_class.new(app, env)
69
+ is_enabled.send(:plugin_disabled?, env[:machine].config.proxy)
70
+ end
71
+
72
+ context "given config.proxy.enabled=false" do
73
+ let(:enabled) { false }
74
+
75
+ it { is_expected.to eq true }
76
+ end
77
+
78
+ context "given config.proxy.enabled=''" do
79
+ let(:enabled) { "" }
80
+
81
+ it { is_expected.to eq true }
82
+ end
83
+
84
+ context "given config.proxy.enabled=nil" do
85
+ let(:enabled) { false }
86
+
87
+ it { is_expected.to eq true }
88
+ end
89
+
90
+ context "given config.proxy.enabled={}" do
91
+ let(:enabled) { false }
92
+
93
+ it { is_expected.to eq true }
94
+ end
95
+
96
+ context "given config.proxy.enabled={:foo => 'bar'}" do
97
+ let(:enabled) do
98
+ {:foo => 'bar'}
30
99
  end
100
+
101
+ it { is_expected.to eq false }
102
+ end
103
+
104
+ context "given config.proxy.enabled=true" do
105
+ let(:enabled) { true }
106
+
107
+ it { is_expected.to eq false }
31
108
  end
109
+
110
+ context "given config.proxy.enabled='http://localhost:8080'" do
111
+ let(:enabled) { 'http://localhost:8080' }
112
+
113
+ it { is_expected.to eq false }
114
+ end
115
+
32
116
  end
33
117
 
34
- [nil, true, :auto, 'yes please'].each do |value|
35
- context "with `config.proxy.enabled=#{value.inspect}`" do
36
- let(:enabled) { value }
118
+ describe "#plugin_enabled?" do
119
+ subject do
120
+ is_enabled = described_class.new(app, env)
121
+ is_enabled.send(:plugin_enabled?, env[:machine].config.proxy)
122
+ end
123
+
124
+ context "when config.proxy.enabled=false and ENV['HTTP_PROXY']='http://localhost:8888'" do
125
+ let(:enabled) { false }
126
+ let(:var) { 'HTTP_PROXY' }
127
+
128
+ before :each do
129
+ ENV[var] = 'http://localhost:8888'
130
+ end
37
131
 
38
- it "results to truthy" do
39
- described_class.new(app, {}).call(env)
40
- expect(env[:result]).to be_truthy
132
+ after :each do
133
+ ENV[var] = nil
41
134
  end
135
+
136
+ it { is_expected.to eq false }
42
137
  end
138
+
139
+ end
140
+
141
+ describe "#call" do
142
+ [false, '', {}, nil].each do |value|
143
+ context "with `config.proxy.enabled=#{value.inspect}`" do
144
+ let(:enabled) { value }
145
+
146
+ it "results to falsy" do
147
+ described_class.new(app, {}).call(env)
148
+ expect(env[:result]).to be_falsey
149
+ end
150
+ end
151
+ end
152
+
153
+ [true, :auto, 'yes please', {:foo => 'yes'}].each do |value|
154
+ context "with `config.proxy.enabled=#{value.inspect}` and HTTP_PROXY=http://localhost:8888" do
155
+ let(:enabled) { value }
156
+ let(:var) { 'HTTP_PROXY' }
157
+
158
+ before :each do
159
+ ENV[var] = 'http://localhost:8888'
160
+ end
161
+
162
+ after :each do
163
+ ENV[var] = nil
164
+ end
165
+
166
+ it "results to truthy" do
167
+ described_class.new(app, {}).call(env)
168
+ expect(env[:result]).to be_truthy
169
+ end
170
+ end
171
+ end
172
+
173
+ [true, :auto, 'yes please', {:foo => 'yes'}].each do |value|
174
+ context "with `config.proxy.enabled=#{value.inspect}` and HTTP_PROXY=''" do
175
+ let(:enabled) { value }
176
+ let(:var) { 'HTTP_PROXY' }
177
+
178
+ before :each do
179
+ ENV[var] = ''
180
+ end
181
+
182
+ after :each do
183
+ ENV[var] = nil
184
+ end
185
+
186
+ it "results to truthy" do
187
+ described_class.new(app, {}).call(env)
188
+ expect(env[:result]).to be_falsey
189
+ end
190
+ end
191
+ end
192
+
43
193
  end
44
194
 
45
195
  end
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
@@ -0,0 +1,47 @@
1
+ FROM centos:7
2
+
3
+ ENV CI_USERNAME vagrant
4
+ ENV CI_PASSWORD vagrant
5
+ ENV CI_HOMEDIR /home/vagrant
6
+ ENV CI_SHELL /bin/bash
7
+
8
+ EXPOSE 8888
9
+
10
+ RUN yum clean all && \
11
+ yum makecache fast && \
12
+ yum -y install epel-release && \
13
+ yum clean expire-cache && \
14
+ yum -y install \
15
+ curl \
16
+ initscripts \
17
+ openssh-clients \
18
+ openssh-server \
19
+ sudo \
20
+ tinyproxy
21
+
22
+ RUN /usr/sbin/sshd-keygen && \
23
+ mkdir -p /var/run/sshd && \
24
+ rm -f /usr/lib/tmpfiles.d/systemd-nologin.conf
25
+
26
+ RUN if ! getent passwd $CI_USERNAME; then \
27
+ useradd -m -d ${CI_HOMEDIR} -s ${CI_SHELL} $CI_USERNAME; \
28
+ fi && \
29
+ echo "${CI_USERNAME}:${CI_PASSWORD}" | chpasswd && \
30
+ echo "${CI_USERNAME} ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers && \
31
+ mkdir -p /etc/sudoers.d && \
32
+ echo "${CI_USERNAME} ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers.d/${CI_USERNAME} && \
33
+ chmod 0440 /etc/sudoers.d/${CI_USERNAME} && \
34
+ mkdir -p ${CI_HOMEDIR}/.ssh && \
35
+ chown -R ${CI_USERNAME}:${CI_USERNAME} ${CI_HOMEDIR}/.ssh && \
36
+ chmod 0700 ${CI_HOMEDIR}/.ssh && \
37
+ curl -L https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant.pub > ${CI_HOMEDIR}/.ssh/vagrant.pub && \
38
+ touch ${CI_HOMEDIR}/.ssh/authorized_keys && \
39
+ grep -q "$(cat ${CI_HOMEDIR}/.ssh/vagrant.pub | awk '{print $2}')" ${CI_HOMEDIR}/.ssh/authorized_keys || cat ${CI_HOMEDIR}/.ssh/vagrant.pub >> ${CI_HOMEDIR}/.ssh/authorized_keys && \
40
+ chown ${CI_USERNAME}:${CI_USERNAME} ${CI_HOMEDIR}/.ssh/authorized_keys && \
41
+ chmod 0600 ${CI_HOMEDIR}/.ssh/authorized_keys
42
+
43
+ COPY tinyproxy.conf /etc/tinyproxy/tinyproxy.conf
44
+ COPY entrypoint.sh /entrypoint.sh
45
+
46
+ ENTRYPOINT ["/entrypoint.sh"]
47
+ CMD [ "start" ]
@@ -0,0 +1,31 @@
1
+ Tests
2
+ -----
3
+
4
+ If you are testing the current release of this plugin via bundler
5
+
6
+ ```
7
+ bundle exec vagrant up default
8
+ ```
9
+
10
+ ## Expect
11
+
12
+
13
+ ### Box `default``
14
+
15
+ - The box `default` is a docker container that will be a reverse
16
+ proxy. It should provision itself and work without errors.
17
+
18
+ - You can check that the proxy is working by
19
+ `tail -f /var/log/tinyproxy/tinyproxy.log` inside the container
20
+
21
+ - **NOTE**: You'll need to use `docker exec <hash> -it bash` to get into the container
22
+
23
+
24
+ ### Box `docker-host`
25
+
26
+ - Vagrant should automatically instally docker-ce.
27
+ - The box should come up and provision itself with the proxy settings
28
+ configured in your Vagrantfile.
29
+
30
+
31
+ - **NOTE**: You can use `ssh` to connect to this container.
@@ -0,0 +1,27 @@
1
+ require 'rake'
2
+ require 'rspec/core/rake_task'
3
+
4
+ task :spec => 'spec:all'
5
+ task :default => :spec
6
+
7
+ namespace :spec do
8
+ targets = []
9
+ Dir.glob('./spec/*').each do |dir|
10
+ next unless File.directory?(dir)
11
+ target = File.basename(dir)
12
+ target = "_#{target}" if target == "default"
13
+ targets << target
14
+ end
15
+
16
+ task :all => targets
17
+ task :default => :all
18
+
19
+ targets.each do |target|
20
+ original_target = target == "_default" ? target[1..-1] : target
21
+ desc "Run serverspec tests to #{original_target}"
22
+ RSpec::Core::RakeTask.new(target.to_sym) do |t|
23
+ ENV['TARGET_HOST'] = original_target
24
+ t.pattern = "spec/#{original_target}/*_spec.rb"
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,31 @@
1
+
2
+ Vagrant.configure("2") do |config|
3
+
4
+ config.vm.define 'default' do |c|
5
+ c.vm.box = nil
6
+
7
+ if Vagrant.has_plugin?('vagrant-proxyconf')
8
+ c.proxy.enabled = false
9
+ end
10
+
11
+ c.vm.provider "docker" do |d|
12
+ d.build_dir = "."
13
+ d.expose = ['8888']
14
+ d.has_ssh = true
15
+ d.ports = ['8888:8888']
16
+ end
17
+ end
18
+
19
+ config.vm.define 'docker_host' do |c|
20
+ c.vm.box = "centos/7"
21
+
22
+ if Vagrant.has_plugin?('vagrant-proxyconf')
23
+ c.proxy.http = ENV['HTTP_PROXY']
24
+ c.proxy.https = ENV['HTTPS_PROXY']
25
+ c.proxy.no_proxy = ENV['NO_PROXY']
26
+ end
27
+
28
+ c.vm.provision "docker"
29
+ end
30
+
31
+ end
@@ -0,0 +1,50 @@
1
+ #!/bin/bash
2
+ set -ex
3
+
4
+ export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
5
+
6
+ start() {
7
+ # start ssh if sshd is installed
8
+ if [ -f /usr/sbin/sshd ]; then
9
+
10
+ /usr/sbin/sshd-keygen
11
+ /usr/sbin/sshd -t
12
+ /usr/sbin/sshd
13
+
14
+ else
15
+
16
+ true
17
+
18
+ fi
19
+
20
+ # start tinyproxy
21
+ /usr/sbin/tinyproxy \
22
+ -d \
23
+ -c "/etc/tinyproxy/tinyproxy.conf"
24
+ }
25
+
26
+ stop() {
27
+
28
+ pgrep -f 'sshd' | while read _pid
29
+ do
30
+ kill -9 $_pid
31
+ done
32
+
33
+ pgrep -f 'tinyproxy' | while read _pid
34
+ do
35
+ kill -9 $_pid
36
+ done
37
+
38
+ }
39
+
40
+ case "${1}" in
41
+
42
+ start)
43
+ start
44
+ ;;
45
+
46
+ stop)
47
+ stop
48
+ ;;
49
+
50
+ esac
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe package('tinyproxy') do
4
+ it { should be_installed }
5
+ end
6
+
7
+ describe service('tinyproxy') do
8
+ it { should be_enabled }
9
+ it { should be_running }
10
+ end
11
+
12
+
13
+ describe port(8888) do
14
+ it { should be_listening }
15
+ end
@@ -0,0 +1,165 @@
1
+ require 'spec_helper'
2
+
3
+ PROXY_HOST = "10.0.2.2"
4
+
5
+ describe service('docker') do
6
+ it { should be_running }
7
+ it { should be_enabled }
8
+ end
9
+
10
+ describe file('/etc/docker/config.json') do
11
+ it { should be_file }
12
+ it { should exist }
13
+ it { should be_mode 600 }
14
+ it { should be_owned_by "root" }
15
+ it { should be_grouped_into "root" }
16
+ end
17
+
18
+ context 'when proxy is enabled' do
19
+
20
+ before(:context) do
21
+ ENV['HTTP_PROXY'] = "http://#{PROXY_HOST}:8888"
22
+ ENV['HTTPS_PROXY'] = "http://#{PROXY_HOST}:8888"
23
+ ENV['NO_PROXY'] = "*.example.com"
24
+
25
+ `vagrant provision #{ENV['TARGET_HOST']}`
26
+ `sleep 3`
27
+ end
28
+
29
+ describe file('/etc/docker/config.json') do
30
+ let(:expected_content) do
31
+ {
32
+ "proxies" => {
33
+ "default" => {
34
+ "httpProxy" => "http://10.0.2.2:8888",
35
+ "httpsProxy" => "http://10.0.2.2:8888",
36
+ "noProxy" => "*.example.com",
37
+ }
38
+ }
39
+ }
40
+ end
41
+
42
+ its(:content_as_json) do
43
+ should include(expected_content)
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ context 'when HTTP_PROXY=""' do
50
+
51
+ before(:context) do
52
+ ENV['HTTP_PROXY'] = ""
53
+ ENV['HTTPS_PROXY'] = "https://#{PROXY_HOST}:8888"
54
+ ENV['NO_PROXY'] = "*.example.com"
55
+
56
+ `vagrant provision #{ENV['TARGET_HOST']}`
57
+ `sleep 3`
58
+ end
59
+
60
+ describe file('/etc/docker/config.json') do
61
+ let(:expected_content) do
62
+ {
63
+ "proxies" => {
64
+ "default" => {
65
+ "httpsProxy" => "https://#{PROXY_HOST}:8888",
66
+ "noProxy" => "*.example.com",
67
+ }
68
+ }
69
+ }
70
+ end
71
+
72
+ its(:content_as_json) do
73
+ should include(expected_content)
74
+ end
75
+ end
76
+
77
+ end
78
+
79
+ context 'when HTTPS_PROXY=""' do
80
+
81
+ before(:context) do
82
+ ENV['HTTP_PROXY'] = "http://#{PROXY_HOST}:8888"
83
+ ENV['HTTPS_PROXY'] = ""
84
+ ENV['NO_PROXY'] = "*.example.com"
85
+
86
+ `vagrant provision #{ENV['TARGET_HOST']}`
87
+ end
88
+
89
+ describe file('/etc/docker/config.json') do
90
+ let(:expected_content) do
91
+ {
92
+ "proxies" => {
93
+ "default" => {
94
+ "httpProxy" => "http://#{PROXY_HOST}:8888",
95
+ "noProxy" => "*.example.com",
96
+ }
97
+ }
98
+ }
99
+ end
100
+
101
+ its(:content_as_json) do
102
+ should include(expected_content)
103
+ end
104
+ end
105
+
106
+ end
107
+
108
+ context 'when HTTPS_PROXY="" and HTTP_PROXY=""' do
109
+
110
+ before(:context) do
111
+ ENV['HTTP_PROXY'] = ""
112
+ ENV['HTTPS_PROXY'] = ""
113
+ ENV['NO_PROXY'] = "*.example.com"
114
+
115
+ `vagrant provision #{ENV['TARGET_HOST']}`
116
+ `sleep 3`
117
+ end
118
+
119
+ describe file('/etc/docker/config.json') do
120
+ let(:expected_content) do
121
+ {
122
+ "proxies" => {
123
+ "default" => {
124
+ "noProxy" => "*.example.com",
125
+ }
126
+ }
127
+ }
128
+ end
129
+
130
+ its(:content_as_json) do
131
+ should include(expected_content)
132
+ end
133
+ end
134
+
135
+ end
136
+
137
+ context 'when NO_PROXY=""' do
138
+
139
+ before(:context) do
140
+ ENV['HTTP_PROXY'] = "http://#{PROXY_HOST}:8888"
141
+ ENV['HTTPS_PROXY'] = "https://#{PROXY_HOST}:8888"
142
+ ENV['NO_PROXY'] = ""
143
+
144
+ `vagrant provision #{ENV['TARGET_HOST']}`
145
+ `sleep 3`
146
+ end
147
+
148
+ describe file('/etc/docker/config.json') do
149
+ let(:expected_content) do
150
+ {
151
+ "proxies" => {
152
+ "default" => {
153
+ "httpProxy" => "http://#{PROXY_HOST}:8888",
154
+ "httpsProxy" => "https://#{PROXY_HOST}:8888",
155
+ }
156
+ }
157
+ }
158
+ end
159
+
160
+ its(:content_as_json) do
161
+ should include(expected_content)
162
+ end
163
+ end
164
+
165
+ end