Skip to content

Commit

Permalink
Replace unit tests with new suite of unit tests with better coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
peterellisjones committed May 26, 2021
1 parent 675ea21 commit 1a65ea2
Show file tree
Hide file tree
Showing 35 changed files with 3,657 additions and 238 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
37 changes: 37 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require:
- rubocop-rspec
- rubocop-rake

AllCops:
NewCops: enable

Layout/ArgumentAlignment:
Enabled: true
EnforcedStyle: with_fixed_indentation

Layout/HeredocArgumentClosingParenthesis:
Enabled: true

Metrics/BlockLength:
Enabled: false

Layout/LineLength:
Enabled: false

Naming/FileName:
Enabled: false

RSpec/MultipleMemoizedHelpers:
Enabled: false

RSpec/NestedGroups:
Max: 4

RSpec/MultipleExpectations:
Enabled: false

RSpec/ExampleLength:
Enabled: false

RSpec/DescribeClass:
Enabled: false
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0.1
19 changes: 11 additions & 8 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# frozen_string_literal: true

source 'https://rubygems.org'

group :test do
gem 'bosh-template'
gem 'rspec', '~> 3.0'
gem 'haproxy-tools'
gem 'pry'
end
gem 'rubocop', '~> 0.49.0'
gem 'semi_semantic', '~> 1.2'
gem 'bosh-template'
gem 'deep_merge'
gem 'guard-rspec'
gem 'haproxy-tools'
gem 'rake'
gem 'rspec'
gem 'rubocop'
gem 'rubocop-rake'
gem 'rubocop-rspec'
128 changes: 85 additions & 43 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,65 +1,107 @@
GEM
remote: https://rubygems.org/
specs:
ast (2.4.0)
bosh-template (2.2.0)
ast (2.4.2)
bosh-template (2.2.1)
semi_semantic (~> 1.2.0)
coderay (1.1.2)
diff-lcs (1.3)
coderay (1.1.3)
deep_merge (1.2.1)
diff-lcs (1.4.4)
ffi (1.15.1)
formatador (0.2.5)
guard (2.17.0)
formatador (>= 0.2.4)
listen (>= 2.7, < 4.0)
lumberjack (>= 1.0.12, < 2.0)
nenv (~> 0.1)
notiffany (~> 0.0)
pry (>= 0.9.12)
shellany (~> 0.0)
thor (>= 0.18.1)
guard-compat (1.2.1)
guard-rspec (4.7.3)
guard (~> 2.1)
guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0)
haproxy-tools (0.6.0)
net-scp
treetop
method_source (0.9.2)
net-scp (2.0.0)
net-ssh (>= 2.6.5, < 6.0.0)
net-ssh (5.2.0)
parallel (1.17.0)
parser (2.6.3.0)
ast (~> 2.4.0)
listen (3.5.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
lumberjack (1.2.8)
method_source (1.0.0)
nenv (0.3.0)
net-scp (3.0.0)
net-ssh (>= 2.6.5, < 7.0.0)
net-ssh (6.1.0)
notiffany (0.1.3)
nenv (~> 0.1)
shellany (~> 0.0)
parallel (1.20.1)
parser (3.0.1.1)
ast (~> 2.4.1)
polyglot (0.3.5)
powerpack (0.1.2)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
rainbow (2.2.2)
rake
rake (12.3.3)
rspec (3.8.0)
rspec-core (~> 3.8.0)
rspec-expectations (~> 3.8.0)
rspec-mocks (~> 3.8.0)
rspec-core (3.8.0)
rspec-support (~> 3.8.0)
rspec-expectations (3.8.3)
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
rainbow (3.0.0)
rake (13.0.3)
rb-fsevent (0.11.0)
rb-inotify (0.10.1)
ffi (~> 1.0)
regexp_parser (2.1.1)
rexml (3.2.5)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
rspec-mocks (~> 3.10.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-mocks (3.8.0)
rspec-support (~> 3.10.0)
rspec-mocks (3.10.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.0)
rubocop (0.49.1)
rspec-support (~> 3.10.0)
rspec-support (3.10.2)
rubocop (1.15.0)
parallel (~> 1.10)
parser (>= 2.3.3.1, < 3.0)
powerpack (~> 0.1)
rainbow (>= 1.99.1, < 3.0)
parser (>= 3.0.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.5.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.10.0)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.5.0)
parser (>= 3.0.1.1)
rubocop-rake (0.5.1)
rubocop
rubocop-rspec (2.3.0)
rubocop (~> 1.0)
rubocop-ast (>= 1.1.0)
ruby-progressbar (1.11.0)
semi_semantic (1.2.0)
treetop (1.6.10)
shellany (0.0.1)
thor (1.1.0)
treetop (1.6.11)
polyglot (~> 0.3)
unicode-display_width (1.6.0)
unicode-display_width (2.0.0)

PLATFORMS
ruby
x86_64-darwin-20

DEPENDENCIES
bosh-template
deep_merge
guard-rspec
haproxy-tools
pry
rspec (~> 3.0)
rubocop (~> 0.49.0)
semi_semantic (~> 1.2)
rake
rspec
rubocop
rubocop-rake
rubocop-rspec

BUNDLED WITH
2.0.1
2.2.15
7 changes: 7 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

guard :rspec, cmd: 'rspec' do
watch(%r{jobs/(.*)/(.*)/(.*)\.erb$}) { |m| "spec/#{m[1]}/#{m[2]}/#{m[3]}_spec.rb" }

watch(%r{spec/(.*)$}) { |m| "spec/#{m[1]}" }
end
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,27 @@ KEEPALIVED_VIP=10.244.50.2 templates/make_manifest warden 10.244.0.22
Feel free to contribute back to this via a pull request on a feature branch! Once merged, we'll
cut a new final release for you.

### Config Tests
### Unit Tests and Linting

If you add a spec value, please add a corresponding test to `spec/haproxy_templates_spec.rb`.
If you change any erb logic in the jobs directory please add a corresponding test to `spec`.

To run these tests:
```
cd haproxy_boshrelease
bundle install
bundle exec rspec spec/haproxy_templates_spec.rb
bundle exec rake spec
```

To run lint with rubocop
```
cd haproxy_boshrelease
bundle install
bundle exec rake lint
```

To watch the tests while developing
```
cd haproxy_boshrelease
bundle install
bundle exec guard
```
25 changes: 18 additions & 7 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
desc "Generates a properties file for each job based on properties.X.Y used in templates"
# frozen_string_literal: true

require 'rspec/core/rake_task'
require 'rubocop/rake_task'

RSpec::Core::RakeTask.new(:spec)
task default: :spec

desc 'Lint using RuboCop'
RuboCop::RakeTask.new(:lint)

desc 'Generates a properties file for each job based on properties.X.Y used in templates'
task :job_properties do
require "fileutils"
Dir["jobs/*"].each do |path|
require 'fileutils'
Dir['jobs/*'].each do |path|
puts "Searching job #{File.basename(path)}..."
FileUtils.chdir(path) do
properties = []
Dir["templates/*.erb"].each do |template_path|
properties |= File.read(template_path).scan(/\bproperties\.[\w\.]*\b/)
Dir['templates/*.erb'].each do |template_path|
properties |= File.read(template_path).scan(/\bproperties\.[\w.]*\b/)
puts properties.join("\n")
File.open("properties", "w") { |file| file << properties.join("\n") }
File.open('properties', 'w') { |file| file << properties.join("\n") }
end
end
end
end
end
23 changes: 23 additions & 0 deletions spec/haproxy/templates/backend-ca-certs_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require 'rspec'

describe 'config/backend-ca-certs.pem' do
let(:template) { haproxy_job.template('config/backend-ca-certs.pem') }

describe 'ha_proxy.backend_ca_file' do
it 'has the correct contents' do
expect(template.render({
'ha_proxy' => {
'backend_ca_file' => 'foobarbaz'
}
})).to eq("\nfoobarbaz\n\n")
end

context 'when ha_proxy.backend_ca_file is not provided' do
it 'is empty' do
expect(template.render({})).to be_a_blank_string
end
end
end
end
23 changes: 23 additions & 0 deletions spec/haproxy/templates/backend-crt_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require 'rspec'

describe 'config/backend-crt.pem' do
let(:template) { haproxy_job.template('config/backend-crt.pem') }

describe 'ha_proxy.backend_crt' do
it 'has the correct contents' do
expect(template.render({
'ha_proxy' => {
'backend_crt' => 'foobarbaz'
}
})).to eq("\nfoobarbaz\n\n")
end

context 'when ha_proxy.backend_crt is not provided' do
it 'is empty' do
expect(template.render({})).to be_a_blank_string
end
end
end
end
60 changes: 60 additions & 0 deletions spec/haproxy/templates/blacklist_cidrs.txt_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

require 'rspec'

describe 'config/blacklist_cidrs.txt' do
let(:template) { haproxy_job.template('config/blacklist_cidrs.txt') }

context 'when ha_proxy.cidr_blacklist is provided' do
context 'when an array of cidrs is provided' do
it 'has the correct contents' do
expect(template.render({
'ha_proxy' => {
'cidr_blacklist' => [
'10.0.0.0/8',
'192.168.2.0/24'
]
}
})).to eq(<<~EXPECTED)
# generated from blacklist_cidrs.txt.erb
# BEGIN blacklist cidrs
# detected cidrs provided as array in cleartext format
10.0.0.0/8
192.168.2.0/24
# END blacklist cidrs
EXPECTED
end
end

context 'when a base64-encoded, gzipped config is provided' do
it 'has the correct contents' do
expect(template.render({
'ha_proxy' => {
'cidr_blacklist' => gzip_and_b64_encode(<<~INPUT)
10.0.0.0/8
192.168.2.0/24
INPUT
}
})).to eq(<<~EXPECTED)
# generated from blacklist_cidrs.txt.erb
# BEGIN blacklist cidrs
10.0.0.0/8
192.168.2.0/24
# END blacklist cidrs
EXPECTED
end
end
end

context 'when ha_proxy.cidr_blacklist is not provided' do
it 'is empty' do
expect(template.render({})).to be_a_blank_string
end
end
end
Loading

0 comments on commit 1a65ea2

Please sign in to comment.