diff --git a/lib/consult/template.rb b/lib/consult/template.rb index ef57f6c..603bc83 100644 --- a/lib/consult/template.rb +++ b/lib/consult/template.rb @@ -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 diff --git a/lib/consult/template_functions.rb b/lib/consult/template_functions.rb index a0fb3f8..7f38100 100644 --- a/lib/consult/template_functions.rb +++ b/lib/consult/template_functions.rb @@ -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 diff --git a/spec/consult_spec.rb b/spec/consult_spec.rb index 85d73e1..b6ea3ec 100644 --- a/spec/consult_spec.rb +++ b/spec/consult_spec.rb @@ -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 diff --git a/spec/lib/template_spec.rb b/spec/lib/template_spec.rb index f67e357..f24e177 100644 --- a/spec/lib/template_spec.rb +++ b/spec/lib/template_spec.rb @@ -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 diff --git a/spec/support/config/consult.yml b/spec/support/config/consult.yml index d972c03..b7cd067 100644 --- a/spec/support/config/consult.yml +++ b/spec/support/config/consult.yml @@ -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 diff --git a/spec/support/consul-query.json b/spec/support/consul-query.json new file mode 100644 index 0000000..dc566c2 --- /dev/null +++ b/spec/support/consul-query.json @@ -0,0 +1,16 @@ +{ + "Name": "postgres-client", + "Service": { + "Service": "postgres", + "Failover": { + "NearestN": 0, + "Datacenters": null + }, + "Near": "", + "OnlyPassing": false, + "Tags": ["primary", "!testing"] + }, + "DNS": { + "TTL": "" + } +} diff --git a/spec/support/expected/query_element.yml b/spec/support/expected/query_element.yml new file mode 100644 index 0000000..4d3f426 --- /dev/null +++ b/spec/support/expected/query_element.yml @@ -0,0 +1,2 @@ +configuration: + node_name: db1 diff --git a/spec/support/populate_consul.sh b/spec/support/populate_consul.sh index 3d40928..36c2511 100755 --- a/spec/support/populate_consul.sh +++ b/spec/support/populate_consul.sh @@ -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 diff --git a/spec/support/templates/query-test.yml.erb b/spec/support/templates/query-test.yml.erb new file mode 100644 index 0000000..1fc430d --- /dev/null +++ b/spec/support/templates/query-test.yml.erb @@ -0,0 +1,5 @@ +configuration: +<%- query('postgres-client').Nodes.each do |node| + node_name = node.dig('Node', 'Node') -%> + node_name: <%= node_name %> +<%- end -%>