class MockEtcd::Client
Attributes
nodes[R]
Public Class Methods
new(opts = {})
click to toggle source
Calls superclass method
# File lib/mock_etcd/client.rb, line 10 def initialize(opts = {}) @index = 0 @nodes = {} WebMock.disable_net_connect! allow_localhost: false super end
Public Instance Methods
api_execute(path, method, options = {})
click to toggle source
Calls superclass method
# File lib/mock_etcd/client.rb, line 19 def api_execute(path, method, options = {}) stub_request! path, method, options super end
Private Instance Methods
create_node(key, value)
click to toggle source
# File lib/mock_etcd/client.rb, line 83 def create_node(key, value) next_index! node = { 'key' => key, 'value' => value, 'modifiedIndex' => @index, 'createdIndex' => @index } @nodes[key] = node end
create_node_with_index(key, value)
click to toggle source
# File lib/mock_etcd/client.rb, line 96 def create_node_with_index(key, value) next_index! node = { 'key' => "#{key}/#{@index}", 'value' => value, 'modifiedIndex' => @index, 'createdIndex' => @index } if @nodes.has_key?(key) @nodes[key] << node else @nodes[key] = [node] end node end
destroy_node(key)
click to toggle source
# File lib/mock_etcd/client.rb, line 115 def destroy_node(key) if node = find_node(key) node['value'] = nil end @nodes.delete(key) end
find_node(key)
click to toggle source
# File lib/mock_etcd/client.rb, line 79 def find_node(key) @nodes[key] end
next_index!()
click to toggle source
# File lib/mock_etcd/client.rb, line 123 def next_index! @index = @index + 1 end
response_body(action, key, node)
click to toggle source
# File lib/mock_etcd/client.rb, line 131 def response_body(action, key, node) if node.is_a?(Array) { 'action' => action, 'node' => { 'key' => key, 'dir' => true, 'nodes' => node }, 'modifiedIndex' => node.first['modifiedIndex'], 'createdIndex' => node.first['createdIndex'] }.to_json else { 'action' => action, 'node' => node }.to_json end end
response_header(node)
click to toggle source
FIXME Raft index and Raft term.
# File lib/mock_etcd/client.rb, line 161 def response_header(node) node = node.is_a?(Array) ? node.last : node { 'Content-Type' => 'application/json', 'X-Etcd-Index' => node['createdIndex'], 'X-Raft-Index' => 300000 * rand, 'X-Raft-Term' => 4 } end
response_key_not_found(key)
click to toggle source
# File lib/mock_etcd/client.rb, line 151 def response_key_not_found(key) { 'errorCode' => 100, 'message' => 'Key not found', 'cause' => key, 'index' => @index }.to_json end
stub_delete_request(key, path, options)
click to toggle source
# File lib/mock_etcd/client.rb, line 66 def stub_delete_request(key, path, options) node = destroy_node(key) stub_key_not_found(:delete, key, path) and return unless node stub_request(:delete, uri(path)) .to_return(status: 200, body: response_body('delete', key, node), headers: response_header(node)) end
stub_get_request(key, path, options)
click to toggle source
# File lib/mock_etcd/client.rb, line 42 def stub_get_request(key, path, options) node = find_node(key) stub_key_not_found(:get, key, path) and return unless node stub_request(:get, uri(path)) .to_return(status: 200, body: response_body('get', key, node), headers: response_header(node)) end
stub_key_not_found(method, key, path)
click to toggle source
# File lib/mock_etcd/client.rb, line 74 def stub_key_not_found(method, key, path) stub_request(method, uri(path)) .to_return(status: 404, headers: {'X-Etcd-Index' => @index}, body: response_key_not_found(key)) end
stub_post_request(key, path, options)
click to toggle source
# File lib/mock_etcd/client.rb, line 50 def stub_post_request(key, path, options) node = create_node_with_index(key, options[:params][:value]) stub_request(:post, uri(path)) .with(body: 'value=' + URI.encode_www_form([options[:params][:value]])) .to_return(status: 201, body: response_body('create', key, node), headers: response_header(node)) end
stub_put_request(key, path, options)
click to toggle source
# File lib/mock_etcd/client.rb, line 58 def stub_put_request(key, path, options) node = create_node(key, options[:params][:value]) stub_request(:put, uri(path)) .with(body: 'value=' + URI.encode_www_form([options[:params][:value]])) .to_return(status: 200, body: response_body('set', key, node), headers: response_header(node)) end
stub_request!(path, method, options)
click to toggle source
# File lib/mock_etcd/client.rb, line 27 def stub_request!(path, method, options) key = path.split(key_endpoint).last case method when :get stub_get_request key, path, options when :post stub_post_request key, path, options when :put stub_put_request key, path, options when :delete stub_delete_request key, path, options end end
uri(path)
click to toggle source
# File lib/mock_etcd/client.rb, line 127 def uri(path) "http://#{host}:#{port}#{path}" end