Skip to content

Commit

Permalink
Test unit added for query method for TemplateFunctions module
Browse files Browse the repository at this point in the history
  • Loading branch information
nogazr committed Jan 12, 2023
1 parent fd498e3 commit 28b88b3
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 6 deletions.
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|
expect(FileUtils.compare_file("spec/support/expected/#{template}", "spec/support/rendered/#{template}")).to be true
end
end
end

Expand Down
8 changes: 8 additions & 0 deletions spec/lib/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@
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').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 @@
{
"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 \
--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 \
--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:
<%- query('postgres-client').Nodes.each do |node|
node_name = node.dig('Node', 'Node') -%>
node_name: <%= node_name %>
<%- end -%>

0 comments on commit 28b88b3

Please sign in to comment.