local_pac 0.5.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -1
- data/Gemfile.lock +1 -3
- data/app/controllers/git_hook_controller.rb +25 -4
- data/app/controllers/lookup_controller.rb +1 -0
- data/app/locales/en.yml +9 -1
- data/bin/local_pac +5 -129
- data/config.ru +2 -2
- data/features/{show_status.feature_ → show_status.feature} +2 -1
- data/features/step_definitions.rb +4 -0
- data/lib/local_pac.rb +9 -2
- data/lib/local_pac/actions/get_system_information.rb +2 -2
- data/lib/local_pac/actions/handle_error.rb +26 -0
- data/lib/local_pac/actions/initialize_application.rb +71 -0
- data/lib/local_pac/actions/reload_local_storage.rb +2 -0
- data/lib/local_pac/actions/show_application_status.rb +39 -0
- data/lib/local_pac/actions/show_pac_file.rb +29 -0
- data/lib/local_pac/actions/validate_pac_file.rb +34 -0
- data/lib/local_pac/cli/main.rb +122 -0
- data/lib/local_pac/cli/reload.rb +8 -4
- data/lib/local_pac/cli/show.rb +34 -0
- data/lib/local_pac/cli/validate.rb +22 -0
- data/lib/local_pac/config.rb +1 -1
- data/lib/local_pac/error_handler.rb +60 -0
- data/lib/local_pac/exceptions.rb +6 -0
- data/lib/local_pac/git_storage.rb +30 -10
- data/lib/local_pac/pac_file_validator.rb +17 -2
- data/lib/local_pac/version.rb +1 -1
- data/local_pac.gemspec +0 -2
- data/script/acceptance_test +4 -0
- data/script/bootstrap +5 -0
- data/script/ci +3 -0
- data/script/release +3 -0
- data/script/unit_test +3 -0
- data/spec/actions/handle_error_spec.rb +29 -0
- data/spec/{initializer_spec.rb → actions/initialize_application_spec.rb} +3 -3
- data/spec/{application_status_spec.rb → actions/show_application_status_spec.rb} +5 -5
- data/spec/actions/show_pac_file_spec.rb +36 -0
- data/spec/actions/validate_pac_file_spec.rb +62 -0
- data/spec/error_handler_spec.rb +70 -0
- data/spec/features/check_git_push_spec.rb +34 -6
- data/spec/git_repository_spec.rb +1 -0
- data/spec/git_storage_spec.rb +33 -0
- data/spec/pac_file_validator_spec.rb +19 -10
- metadata +30 -26
- data/lib/local_pac/application_status.rb +0 -37
- data/lib/local_pac/initializer.rb +0 -69
data/lib/local_pac/version.rb
CHANGED
data/local_pac.gemspec
CHANGED
@@ -36,12 +36,10 @@ EOS
|
|
36
36
|
spec.add_runtime_dependency 'sinatra'
|
37
37
|
spec.add_runtime_dependency 'activesupport'
|
38
38
|
spec.add_runtime_dependency 'pac'
|
39
|
-
spec.add_runtime_dependency 'git_hook-pre_receive', '~>0.1.0'
|
40
39
|
spec.add_runtime_dependency 'facter'
|
41
40
|
spec.add_runtime_dependency 'pager'
|
42
41
|
spec.add_runtime_dependency 'sys-proctable'
|
43
42
|
spec.add_runtime_dependency 'sinatra-param'
|
44
43
|
spec.add_runtime_dependency 'sinatra-contrib'
|
45
44
|
spec.add_runtime_dependency 'rugged'
|
46
|
-
# spec.add_runtime_dependency 'fedux_org-stdlib'
|
47
45
|
end
|
data/script/bootstrap
ADDED
data/script/ci
ADDED
data/script/release
ADDED
data/script/unit_test
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Actions::HandleError do
|
5
|
+
context '#initialize' do
|
6
|
+
it 'requires an exception' do
|
7
|
+
expect {
|
8
|
+
Actions::HandleError.new(StandardError.new)
|
9
|
+
}.not_to raise_error
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context '#run' do
|
14
|
+
it 'handles errors' do
|
15
|
+
action = Actions::HandleError.new(RuntimeError.new)
|
16
|
+
|
17
|
+
silence(:stderr) do
|
18
|
+
begin
|
19
|
+
action.run
|
20
|
+
rescue SystemExit => e
|
21
|
+
@err = e
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
expect(@err).to be_kind_of(SystemExit)
|
26
|
+
expect(@err.status).to be(99)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
|
-
describe
|
4
|
+
describe Actions::InitializeApplication do
|
5
5
|
|
6
6
|
context '#run' do
|
7
7
|
it 'creates all files/directories neccessary to run local_pac' do
|
@@ -18,7 +18,7 @@ describe Initializer do
|
|
18
18
|
config_file = create_file('config_spec.yaml', config_string)
|
19
19
|
|
20
20
|
config = LocalPac::Config.new(config_file)
|
21
|
-
initializer =
|
21
|
+
initializer = Actions::InitializeApplication.new({
|
22
22
|
create_pid_directory: true,
|
23
23
|
create_log_directory: true,
|
24
24
|
create_sass_cache: true,
|
@@ -54,7 +54,7 @@ describe Initializer do
|
|
54
54
|
config_file = create_file('config_spec.yaml', config_string)
|
55
55
|
|
56
56
|
config = LocalPac::Config.new(config_file)
|
57
|
-
initializer =
|
57
|
+
initializer = Actions::InitializeApplication.new({
|
58
58
|
pre_seed: true,
|
59
59
|
create_local_storage: true,
|
60
60
|
}, config)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
|
-
describe
|
4
|
+
describe Actions::ShowApplicationStatus do
|
5
5
|
let(:valid_pac_file) do <<-EOS.strip_heredoc.chomp
|
6
6
|
function FindProxyForURL(url, host) {
|
7
7
|
return "DIRECT";
|
@@ -12,18 +12,18 @@ describe ApplicationStatus do
|
|
12
12
|
context '#initialize' do
|
13
13
|
it 'has no special requirements' do
|
14
14
|
expect {
|
15
|
-
|
15
|
+
Actions::ShowApplicationStatus.new
|
16
16
|
}.not_to raise_error
|
17
17
|
end
|
18
18
|
|
19
19
|
it 'let you deactivate paging' do
|
20
20
|
expect {
|
21
|
-
|
21
|
+
Actions::ShowApplicationStatus.new(pager: false)
|
22
22
|
}.not_to raise_error
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
-
context '#
|
26
|
+
context '#run' do
|
27
27
|
it 'show application status' do
|
28
28
|
git_repo = ::File.join(working_directory, 'data', 'storage.git')
|
29
29
|
|
@@ -41,7 +41,7 @@ describe ApplicationStatus do
|
|
41
41
|
config = LocalPac::Config.new(config_file)
|
42
42
|
|
43
43
|
result = capture(:stdout) do
|
44
|
-
|
44
|
+
Actions::ShowApplicationStatus.new(pager: false, config: config).run
|
45
45
|
end
|
46
46
|
|
47
47
|
expect(result).to include('osfamily')
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Actions::ShowPacFile do
|
5
|
+
let(:valid_pac_file) do <<-EOS.strip_heredoc.chomp
|
6
|
+
function FindProxyForURL(url, host) {
|
7
|
+
return "DIRECT";
|
8
|
+
}
|
9
|
+
EOS
|
10
|
+
end
|
11
|
+
|
12
|
+
context '#initialize' do
|
13
|
+
it 'requires a git repository' do
|
14
|
+
expect {
|
15
|
+
Actions::ShowPacFile.new(working_directory, 'proxy.pac')
|
16
|
+
}.not_to raise_error
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context '#run' do
|
21
|
+
it 'shows proxy.pac file' do
|
22
|
+
repo = GitRepository.create(working_directory)
|
23
|
+
repo.add_content('proxy.pac', 'asdf()')
|
24
|
+
repo.add_content('proxy1.pac', 'asd1f()')
|
25
|
+
|
26
|
+
action = Actions::ShowPacFile.new('proxy.pac', working_directory)
|
27
|
+
|
28
|
+
result = capture(:stdout) do
|
29
|
+
LocalPac.ui_logger.level = :info
|
30
|
+
action.run
|
31
|
+
end
|
32
|
+
|
33
|
+
expect(result).to include('asdf()')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Actions::ValidatePacFile do
|
5
|
+
let(:valid_pac_file) do <<-EOS.strip_heredoc.chomp
|
6
|
+
function FindProxyForURL(url, host) {
|
7
|
+
return "DIRECT";
|
8
|
+
}
|
9
|
+
EOS
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:invalid_pac_file) do <<-EOS.strip_heredoc.chomp
|
13
|
+
function FindProxyForURL(url, host) {
|
14
|
+
aasdf();
|
15
|
+
}
|
16
|
+
EOS
|
17
|
+
end
|
18
|
+
|
19
|
+
context '#initialize' do
|
20
|
+
it 'requires a file' do
|
21
|
+
file = create_file 'pac_file', valid_pac_file
|
22
|
+
|
23
|
+
expect {
|
24
|
+
Actions::ValidatePacFile.new(file)
|
25
|
+
}.not_to raise_error
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context '#run' do
|
30
|
+
it 'outputs an info if pac file is valid' do
|
31
|
+
file = create_file 'pac_file', valid_pac_file
|
32
|
+
|
33
|
+
action = Actions::ValidatePacFile.new(file)
|
34
|
+
|
35
|
+
result = capture(:stdout) do
|
36
|
+
action.run
|
37
|
+
end
|
38
|
+
|
39
|
+
expect(result).to include('is a valid pac file')
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'outputs an warning if pac file is invalid' do
|
43
|
+
file = create_file 'pac_file', invalid_pac_file
|
44
|
+
|
45
|
+
action = Actions::ValidatePacFile.new(file)
|
46
|
+
|
47
|
+
result = capture(:stdout) do
|
48
|
+
action.run
|
49
|
+
end
|
50
|
+
|
51
|
+
expect(result).to include('is not a valid pac file')
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'raises an error if path is not a file' do
|
55
|
+
action = Actions::ValidatePacFile.new(working_directory)
|
56
|
+
|
57
|
+
expect {
|
58
|
+
action.run
|
59
|
+
}.to raise_error Exceptions::PacFileInvalid
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe ErrorHandler do
|
5
|
+
let(:exception) { StandardError.new }
|
6
|
+
|
7
|
+
context '.create' do
|
8
|
+
it 'requires an exception class, a message and a exit code' do
|
9
|
+
expect {
|
10
|
+
ErrorHandler.create(
|
11
|
+
exception: StandardError,
|
12
|
+
message: 'This is a standard error',
|
13
|
+
exit_code: 1,
|
14
|
+
)
|
15
|
+
}.not_to raise_error
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context '.find' do
|
20
|
+
it 'finds a suitable error handler' do
|
21
|
+
ErrorHandler.create(
|
22
|
+
exception: StandardError,
|
23
|
+
message: 'This is a standard error',
|
24
|
+
exit_code: 1,
|
25
|
+
)
|
26
|
+
|
27
|
+
expect(ErrorHandler.find(exception)).not_to be_nil
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'returns a default exception if the requested one cannote be found' do
|
31
|
+
expect(ErrorHandler.find(RuntimeError.new).exception).to be(StandardError)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context '#exception' do
|
36
|
+
it 'returns the exception' do
|
37
|
+
handler = ErrorHandler.create(
|
38
|
+
exception: StandardError,
|
39
|
+
message: 'This is a standard error',
|
40
|
+
exit_code: 1,
|
41
|
+
)
|
42
|
+
|
43
|
+
expect(handler.exception).to eq(StandardError)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context '#run' do
|
48
|
+
it 'runs the error handler' do
|
49
|
+
handler = ErrorHandler.new(
|
50
|
+
exception: StandardError,
|
51
|
+
message: 'This is a standard error',
|
52
|
+
exit_code: 1,
|
53
|
+
)
|
54
|
+
handler.original_message = 'blub'
|
55
|
+
|
56
|
+
LocalPac.ui_logger.level = :debug
|
57
|
+
|
58
|
+
result = capture(:stderr) do
|
59
|
+
begin
|
60
|
+
handler.run
|
61
|
+
rescue SystemExit => e
|
62
|
+
expect(e.status).to eq(1)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
expect(result).to include('standard error')
|
67
|
+
expect(result).to include('blub')
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -40,8 +40,12 @@ describe 'Lookup proxy for url' do
|
|
40
40
|
::File.join(working_directory, 'git_repo.git')
|
41
41
|
end
|
42
42
|
|
43
|
+
def reload_storage_signal
|
44
|
+
'USR2'
|
45
|
+
end
|
46
|
+
|
43
47
|
def api_key
|
44
|
-
|
48
|
+
'THIS_IS_THE_API_KEY'
|
45
49
|
end
|
46
50
|
end.new
|
47
51
|
|
@@ -62,7 +66,7 @@ describe 'Lookup proxy for url' do
|
|
62
66
|
old_commit_id: commit1.oid,
|
63
67
|
new_commit_id: commit2.oid,
|
64
68
|
reference: 'refs/heads/master',
|
65
|
-
api_key:
|
69
|
+
api_key: 'THIS_IS_THE_API_KEY',
|
66
70
|
}
|
67
71
|
|
68
72
|
response = page.driver.post('/pre-receive', params )
|
@@ -85,7 +89,7 @@ describe 'Lookup proxy for url' do
|
|
85
89
|
old_commit_id: commit1.oid,
|
86
90
|
new_commit_id: commit3.oid,
|
87
91
|
reference: 'refs/heads/master',
|
88
|
-
api_key:
|
92
|
+
api_key: 'THIS_IS_THE_API_KEY',
|
89
93
|
}
|
90
94
|
|
91
95
|
response = page.driver.post('/pre-receive', params )
|
@@ -97,9 +101,33 @@ describe 'Lookup proxy for url' do
|
|
97
101
|
end
|
98
102
|
|
99
103
|
it 'returns a error message for an invalid url' do
|
100
|
-
|
101
|
-
|
102
|
-
|
104
|
+
params = {
|
105
|
+
old_commit_id: 'asdf',
|
106
|
+
new_commit_id: 'asdf',
|
107
|
+
reference: 'refs/heads/master',
|
108
|
+
api_key: 'THIS_IS_THE_API_KEY',
|
109
|
+
}
|
110
|
+
|
111
|
+
response = page.driver.post('/blub-gith-hook', params )
|
112
|
+
json = JSON.parse(response.body)
|
113
|
+
|
114
|
+
expect(json['error_summary']).to eq('Unknown git hook...')
|
115
|
+
expect(json['error_details']).to include('/blub-gith-hook')
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'fails on invalid api key' do
|
119
|
+
params = {
|
120
|
+
old_commit_id: 'asdf',
|
121
|
+
new_commit_id: 'asdf',
|
122
|
+
reference: 'refs/heads/master',
|
123
|
+
api_key: 'invalid',
|
124
|
+
}
|
125
|
+
|
126
|
+
response = page.driver.post('/pre-receive', params )
|
127
|
+
result = JSON.parse(response.body)['error_summary']
|
128
|
+
|
129
|
+
expect(result).to eq('Invalid API-key...')
|
130
|
+
expect(response.status).to eq(403)
|
103
131
|
end
|
104
132
|
end
|
105
133
|
end
|
data/spec/git_repository_spec.rb
CHANGED
data/spec/git_storage_spec.rb
CHANGED
@@ -79,4 +79,37 @@ describe GitStorage do
|
|
79
79
|
expect(result).to eq([pac_file])
|
80
80
|
end
|
81
81
|
end
|
82
|
+
|
83
|
+
context '#each_added_pac_file' do
|
84
|
+
it 'iterates over all added pac files' do
|
85
|
+
repo_path = File.join(working_directory, 'git_repo')
|
86
|
+
GitRepository.create(repo_path)
|
87
|
+
|
88
|
+
file_creator = double('file_creator')
|
89
|
+
null_file = double('null_file')
|
90
|
+
|
91
|
+
repo = GitRepository.new(repo_path, file_creator, null_file)
|
92
|
+
oid1 = repo.add_content('file1.pac', 'asdf1')
|
93
|
+
repo.add_content('file2.pac', 'asdf2')
|
94
|
+
oid3 = repo.add_content('file3.txt', 'asdf3')
|
95
|
+
|
96
|
+
pac_file = double('pac_file')
|
97
|
+
allow(pac_file).to receive(:extension?).with('.pac').and_return(true)
|
98
|
+
allow(pac_file).to receive(:path).and_return('proxy.pac')
|
99
|
+
allow(pac_file).to receive(:name).and_return(:proxy)
|
100
|
+
|
101
|
+
vcs = double('vcs')
|
102
|
+
allow(vcs).to receive(:added_files).and_return([pac_file])
|
103
|
+
|
104
|
+
vcs_engine = double('vcs_engine')
|
105
|
+
allow(vcs_engine).to receive(:new).and_return(vcs)
|
106
|
+
|
107
|
+
storage = GitStorage.new(git_repo, compressor, vcs_engine)
|
108
|
+
|
109
|
+
result = []
|
110
|
+
storage.each_added_pac_file(oid1, oid3) { |file| result << file}
|
111
|
+
|
112
|
+
expect(result).to eq([pac_file])
|
113
|
+
end
|
114
|
+
end
|
82
115
|
end
|
@@ -1,26 +1,26 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe PacFileValidator do
|
4
|
-
|
5
|
-
let(:valid_pac_file) do <<-EOS.strip_heredoc
|
4
|
+
let(:valid_pac_file) do <<-EOS.strip_heredoc
|
6
5
|
function FindProxyForURL(url, host) {
|
7
6
|
return "DIRECT"
|
8
7
|
}
|
9
|
-
|
10
|
-
|
8
|
+
EOS
|
9
|
+
end
|
11
10
|
|
12
|
-
|
11
|
+
let(:invalid_pac_file1) do <<-EOS.strip_heredoc
|
13
12
|
function FindProxyForURL(url, host) {
|
14
|
-
|
15
|
-
|
13
|
+
EOS
|
14
|
+
end
|
16
15
|
|
17
|
-
|
16
|
+
let(:invalid_pac_file2) do <<-EOS.strip_heredoc
|
18
17
|
function FindProxyForURL(url, host) {
|
19
18
|
asdf();
|
20
19
|
}
|
21
|
-
|
22
|
-
|
20
|
+
EOS
|
21
|
+
end
|
23
22
|
|
23
|
+
context '#valid?' do
|
24
24
|
it 'returns true if file is valid' do
|
25
25
|
file = double('PacFile')
|
26
26
|
expect(file).to receive(:content).and_return(valid_pac_file)
|
@@ -42,4 +42,13 @@ describe PacFileValidator do
|
|
42
42
|
expect(validator.valid?(file)).to be_false
|
43
43
|
end
|
44
44
|
end
|
45
|
+
|
46
|
+
context '#errors' do
|
47
|
+
it 'returns errors found during validation' do
|
48
|
+
file = double('PacFile')
|
49
|
+
expect(file).to receive(:content).and_return(invalid_pac_file2)
|
50
|
+
validator = PacFileValidator.new
|
51
|
+
expect(validator.errors(file)).to eq('asdf is not defined')
|
52
|
+
end
|
53
|
+
end
|
45
54
|
end
|