-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve issue #6 regexp and add tests: quote yaml strings containing …
…. or :
- Loading branch information
Showing
3 changed files
with
160 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# encoding: UTF-8 | ||
# | ||
# Author:: Xabier de Zuazo (<xabier@zuazo.org>) | ||
# Copyright:: Copyright (c) 2017 Xabier de Zuazo | ||
# License:: Apache License, Version 2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
require_relative '../spec_helper' | ||
require 'chef/mixin/template' | ||
|
||
# Emulates the Chef template render engine. | ||
class TemplateRender | ||
def initialize(f) | ||
file(f) | ||
end | ||
|
||
def attributes_dir | ||
File.join(::File.dirname(__FILE__), '..', '..', '..', 'attributes') | ||
end | ||
|
||
def template_dir | ||
File.join( | ||
::File.dirname(__FILE__), '..', '..', '..', 'templates', 'default' | ||
) | ||
end | ||
|
||
def file(arg) | ||
@path = ::File.join(template_dir, arg) | ||
end | ||
|
||
def simulate_ohai(node) | ||
node.name('node001') | ||
node.automatic['platform_family'] = node.automatic['platform'] = 'debian' | ||
node.automatic['platform_version'] = 8 | ||
node | ||
end | ||
|
||
def node | ||
@node ||= begin | ||
n = Chef::Node.new | ||
simulate_ohai(n) | ||
Dir.glob(::File.join(attributes_dir, '*.rb')) { |f| n.from_file(f) } | ||
n | ||
end | ||
end | ||
|
||
def render(variables = {}) | ||
context = Chef::Mixin::Template::TemplateContext.new(variables) | ||
context[:node] = node | ||
context.render_template(@path) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# encoding: UTF-8 | ||
# | ||
# Author:: Xabier de Zuazo (<xabier@zuazo.org>) | ||
# Copyright:: Copyright (c) 2017 Xabier de Zuazo | ||
# License:: Apache License, Version 2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
require 'yaml' | ||
require_relative '../spec_helper' | ||
require_relative '../support/template_render' | ||
|
||
describe 'kong.yml template', order: :random do | ||
let(:template) { TemplateRender.new('kong.yml.erb') } | ||
let(:node) { template.node } | ||
let(:variables) { { config: node['kong']['kong.yml'] } } | ||
|
||
context 'with default configuration' do | ||
it 'does not quote database' do | ||
expect(template.render(variables)).to match(/^\s*database:\s+[a-zA-Z]+$/) | ||
end | ||
|
||
it 'does not quote keyspace' do | ||
expect(template.render(variables)).to match(/^\s*keyspace:\s+[a-zA-Z]+$/) | ||
end | ||
|
||
it 'does not quote timeout' do | ||
expect(template.render(variables)).to match(/^\s*timeout:\s+[0-9]+$/) | ||
end | ||
|
||
it 'quotes contact_points' do | ||
expect(template.render(variables)).to match(/^\s*-\s+"localhost:9042"$/) | ||
end | ||
end | ||
|
||
context 'with IP address configuration value' do | ||
let(:value) { '127.0.0.1' } | ||
before { node.default['kong']['kong.yml']['cluster_listen'] = value } | ||
|
||
it 'quotes the value' do | ||
expect(template.render(variables)) | ||
.to match(/^\s*cluster_listen:\s+"#{Regexp.escape(value)}"$/) | ||
end | ||
end | ||
|
||
context 'with port address configuration value' do | ||
let(:value) { 'myhost:7946' } | ||
before { node.default['kong']['kong.yml']['cluster_listen'] = value } | ||
|
||
it 'quotes the value' do | ||
expect(template.render(variables)) | ||
.to match(/^\s*cluster_listen:\s+"#{Regexp.escape(value)}"$/) | ||
end | ||
end | ||
|
||
context 'when yaml engine is expected to add ""' do | ||
let(:value) { "0.0.0.0:7946\n " } | ||
let(:escaped_value) { value.gsub("\n", '\\\\n') } | ||
before { node.default['kong']['kong.yml']['noneedtoquote'] = value } | ||
|
||
it 'yaml engine adds ""' do | ||
expect({ k: value }.to_yaml).to include("\"#{escaped_value}\"") | ||
end | ||
|
||
it 'does not quote it again' do | ||
expect(template.render(variables)) | ||
.to match(/^\s*noneedtoquote:\s+"#{Regexp.escape(escaped_value)}"$/) | ||
end | ||
end | ||
|
||
context "when yaml engine is forced to use ''" do | ||
let(:value) { '0.0.0.0:7946 ' } | ||
before { node.default['kong']['kong.yml']['noneedtoquote'] = value } | ||
|
||
it "yaml engine adds ''" do | ||
expect({ k: value }.to_yaml).to include("'#{value}'") | ||
end | ||
|
||
it 'does not quote it again' do | ||
expect(template.render(variables)) | ||
.to match(/^\s*noneedtoquote:\s+'#{Regexp.escape(value)}'$/) | ||
end | ||
end | ||
end |