Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test unit added for query method for TemplateFunctions module #39

Merged
merged 1 commit into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/consult/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def contents

def consul_contents(location)
[@config[location]].compact.flatten.map do |key|
Diplomat::Kv.get(key, options: nil, not_found: :return, found: :return).force_encoding 'utf-8'
Diplomat::Kv.get(key, options: {}, not_found: :return, found: :return).force_encoding 'utf-8'
end.join
end

Expand Down
2 changes: 1 addition & 1 deletion lib/consult/template_functions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def service(key, scope: :all, options: {}, meta: {})
end

# Execute a prepared query
def query(name_or_id, options: nil)
def query(name_or_id, options: {})
Diplomat::Query.execute(name_or_id, options)
end

Expand Down
15 changes: 10 additions & 5 deletions spec/consult_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
expect(Consult.env).to eq('test')
end

it 'renders without error' do
expect { Consult.render! }.to_not raise_exception
context 'when render the templates' do
before { Consult.load config_dir: directory }

# Verify text templates rendered correctly
%w[elements.txt more_elements.txt consul_elements.txt more_consul_elements.txt multi_pass.txt].each do |template|
expect(FileUtils.compare_file("spec/support/expected/#{template}", "spec/support/rendered/#{template}")).to be true
it 'renders without error' do
expect { Consult.render! }.to_not raise_exception
end

it 'renders templates correctly' do
%w[elements.txt more_elements.txt consul_elements.txt more_consul_elements.txt multi_pass.txt query_element.yml].each do |template|
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

query_element.yml was added, this one uses the query method to be renderer

expect(FileUtils.compare_file("spec/support/expected/#{template}", "spec/support/rendered/#{template}")).to be true
end
end
end

Expand Down
9 changes: 9 additions & 0 deletions spec/lib/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@
expect(template.service('postgres').first.ServicePort).to eq 5432
end

it 'can execute a query from consul' do
expect(template.query('postgres-client')).to be_an(OpenStruct)
end

it 'can get query nodes from consul' do
expect(template.query_nodes('postgres-client').length).to eq 1
expect(template.query_nodes('postgres-client').dig(0, 'Node')).to eq 'db1'
end

it 'can read a consul key' do
expect(template.key('infrastructure/db1/dns')).to eq 'db1.local.net'
end
Expand Down
4 changes: 4 additions & 0 deletions spec/support/config/consult.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ shared:
love: 5
aziz: 'Light!'

query_element:
consul_key: templates/db/db1
dest: rendered/query_element.yml

dest_fail:
consul_key: templates/elements/aziz
dest: rendered/nope/dest_fail.keep
Expand Down
16 changes: 16 additions & 0 deletions spec/support/consul-query.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This query was required to test the method query for TemplateFunctions

"Name": "postgres-client",
"Service": {
"Service": "postgres",
"Failover": {
"NearestN": 0,
"Datacenters": null
},
"Near": "",
"OnlyPassing": false,
"Tags": ["primary", "!testing"]
},
"DNS": {
"TTL": ""
}
}
2 changes: 2 additions & 0 deletions spec/support/expected/query_element.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
configuration:
node_name: db1
11 changes: 11 additions & 0 deletions spec/support/populate_consul.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ curl \
-H "Content-Type: application/x-www-form-urlencoded; charset=utf-8" \
--data $'Aziz! <%= vars[:aziz] %> 🙄\n' \
http://localhost:8500/v1/kv/templates/elements/aziz

curl \
Copy link
Contributor Author

@nogazr nogazr Jan 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We upload a template to use the method query

--request PUT \
-H "Content-Type: application/x-www-form-urlencoded; charset=utf-8" \
--data-binary @spec/support/templates/query-test.yml.erb \
http://localhost:8500/v1/kv/templates/db/db1

curl \
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this curl we create the query on consul

--request POST \
--data @spec/support/consul-query.json \
http://localhost:8500/v1/query
5 changes: 5 additions & 0 deletions spec/support/templates/query-test.yml.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
configuration:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this template we test the method query for TemplateFunctions

<%- query('postgres-client').Nodes.each do |node|
node_name = node.dig('Node', 'Node') -%>
node_name: <%= node_name %>
<%- end -%>