Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Test server configures startup script and server configuration.

  • Loading branch information...
commit 1e63bd67c52a4cd16b67cda3b84d27a1a0a5dcd4 1 parent b863a5b
@seancribbs seancribbs authored
View
3  riak-client/lib/riak.rb
@@ -44,6 +44,9 @@ module Riak
autoload :InvalidResponse, "riak/invalid_response"
autoload :MapReduceError, "riak/map_reduce_error"
+ # Test server
+ autoload :TestServer, "riak/test_server"
+
# Utility classes and mixins
module Util
autoload :Escape, "riak/util/escape"
View
132 riak-client/lib/riak/test_server.rb
@@ -0,0 +1,132 @@
+# Copyright 2010 Sean Cribbs, Sonian Inc., and Basho Technologies, Inc.
+#
+# 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 'riak'
+require 'tempfile'
+
+module Riak
+ class TestServer
+ APP_CONFIG_DEFAULTS = {
+ :riak_core => {
+ :web_ip => "127.0.0.1",
+ :web_port => 9000,
+ :handoff_port => 9001,
+ :ring_creation_size => 64
+ },
+ :riak_kv => {
+ :storage_backend => :riak_kv_ets_backend,
+ :pb_ip => "127.0.0.1",
+ :pb_port => 9002,
+ :js_vm_count => 8,
+ :js_max_vm_mem => 8,
+ :js_thread_stack => 16,
+ :riak_kv_stat => true
+ }
+ }
+ VM_ARGS_DEFAULTS = {
+ "-name" => "riaktest#{rand(1000000).to_s}@127.0.0.1",
+ "-setcookie" => "#{rand(1000000).to_s}_#{rand(1000000).to_s}",
+ "+K" => true,
+ "+A" => 64,
+ "-smp" => "enable",
+ "-env ERL_MAX_PORTS" => 4096,
+ "-env ERL_FULLSWEEP_AFTER" => 10
+ }
+ DEFAULTS = {
+ :app_config => APP_CONFIG_DEFAULTS,
+ :vm_args => VM_ARGS_DEFAULTS,
+ :temp_dir => File.join(Dir.tmpdir,'riaktest'),
+ :user => ::ENV['USER']
+ }
+ attr_accessor :temp_dir
+
+ def initialize(options={})
+ options = deep_merge(DEFAULTS.dup, options)
+ @temp_dir = File.expand_path(options[:temp_dir])
+ @user = options[:user]
+ @bin_dir = File.expand_path(options[:bin_dir])
+ options[:app_config][:riak_core][:ring_state_dir] ||= File.join(@temp_dir, "data", "ring")
+ @app_config = options[:app_config]
+ @vm_args = options[:vm_args]
+ end
+
+ def prepare!
+ create_temp_directories
+ write_riak_script
+ write_vm_args
+ write_app_config
+ end
+
+ private
+ def create_temp_directories
+ %w{bin etc log data}.each do |dir|
+ instance_variable_set("@temp_#{dir}", File.expand_path(File.join(@temp_dir, dir)))
+ FileUtils.mkdir_p(instance_variable_get("@temp_#{dir}"))
+ end
+ end
+
+ def write_riak_script
+ File.open(File.join(@temp_bin, 'riak'), 'wb') do |f|
+ File.readlines(File.join(@bin_dir, 'riak')).each do |line|
+ line.sub!(/(RUNNER_SCRIPT_DIR=)(.*)/, '\1' + @temp_bin)
+ line.sub!(/(RUNNER_ETC_DIR=)(.*)/, '\1' + @temp_etc)
+ line.sub!(/(RUNNER_USER=)(.*)/, '\1' + @user)
+ line.sub!(/(RUNNER_LOG_DIR=)(.*)/, '\1' + @temp_log)
+ if line.strip == "RUNNER_BASE_DIR=${RUNNER_SCRIPT_DIR%/*}"
+ line = "RUNNER_BASE_DIR=#{File.expand_path("..",@bin_dir)}"
+ end
+ f.write line + "\n"
+ end
+ end
+ end
+
+ def write_vm_args
+ File.open(File.join(@temp_etc, 'vm.args'), 'wb') do |f|
+ f.write @vm_args.map {|k,v| "#{k} #{v}" }.join("\n")
+ end
+ end
+
+ def write_app_config
+ File.open(File.join(@temp_etc, 'app.config'), 'wb') do |f|
+ f.write to_erlang_config(@app_config) + '.'
+ end
+ end
+
+ def deep_merge(source, target)
+ source.merge(target) do |key, old_val, new_val|
+ if Hash === old_val && Hash === new_val
+ deep_merge(old_val, new_val)
+ else
+ new_val
+ end
+ end
+ end
+
+ def to_erlang_config(hash, depth = 1)
+ padding = ' ' * depth
+ parent_padding = ' ' * (depth-1)
+ values = hash.map do |k,v|
+ printable = case v
+ when Hash
+ to_erlang_config(v, depth+1)
+ when String
+ "\"#{v}\""
+ else
+ v.to_s
+ end
+ "{#{k}, #{printable}}"
+ end.join(",\n#{padding}")
+ "[\n#{padding}#{values}\n#{parent_padding}]"
+ end
+ end
+end
View
128 riak-client/spec/integration/riak/test_server_spec.rb
@@ -0,0 +1,128 @@
+# Copyright 2010 Sean Cribbs, Sonian Inc., and Basho Technologies, Inc.
+#
+# 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 File.expand_path("../../spec_helper", File.dirname(__FILE__))
+
+begin
+ require 'yaml'
+ $test_config = YAML.load_file(File.expand_path("../../support/test_server.yml", File.dirname(__FILE__)))
+rescue
+ warn "Can't run Riak::TestServer specs. Specify the location of your Riak installation in spec/support/test_server.yml. See Riak::TestServer docs for more info."
+end
+
+if $test_config
+ describe Riak::TestServer do
+ describe "isolation from and modification of the existing install" do
+ before do
+ @server = Riak::TestServer.new($test_config.symbolize_keys)
+ @server.prepare!
+ @riak_bin = "#{@server.temp_dir}/bin/riak"
+ @vm_args = "#{@server.temp_dir}/etc/vm.args"
+ @app_config = "#{@server.temp_dir}/etc/app.config"
+ end
+
+ describe "for app.config" do
+ it "should create the app.config file in the temporary directory" do
+ File.should be_exist(File.expand_path(@app_config))
+ end
+
+ it "should be a correct Erlang config" do
+ config = File.read(@app_config)
+ config[-2..-1].should == '].'
+ config[0..0].should == '['
+ end
+
+ it "should set the backend to use ets" do
+ File.readlines(@app_config).should be_any do |line|
+ line =~ /\{storage_backend\s*,\s*(.*)\}/ && $1 == "riak_kv_ets_backend"
+ end
+ end
+
+ it "should set the default ports to 9000-9002" do
+ config = File.readlines(@app_config)
+ config.should be_any do |line|
+ line =~ /\{web_port\s*,\s*(.*)\}/ && $1 == "9000"
+ end
+ config.should be_any do |line|
+ line =~ /\{handoff_port\s*,\s*(.*)\}/ && $1 == "9001"
+ end
+ config.should be_any do |line|
+ line =~ /\{pb_port\s*,\s*(.*)\}/ && $1 == "9002"
+ end
+ end
+
+ it "should set the ring directory to point to the temporary directory" do
+ config = File.readlines(@app_config)
+ config.should be_any do |line|
+ line =~ /\{ring_state_dir\s*,\s*(.*)\}/ && $1 == File.join(@server.temp_dir, "data", "ring")
+ end
+ end
+ end
+
+ describe "for vm.args" do
+ it "should create the vm.args file in the temporary directory" do
+ File.should be_exist(File.expand_path(@vm_args))
+ end
+
+ it "should set a quasi-random node name" do
+ File.readlines(@vm_args).should be_any do |line|
+ line =~ /^-name (.*)/ && $1 =~ /riaktest\d+@/
+ end
+ end
+
+ it "should set a quasi-random cookie" do
+ File.readlines(@vm_args).should be_any do |line|
+ line =~ /^-setcookie (.*)/ && $1 != "riak"
+ end
+ end
+ end
+
+ describe "for the riak script" do
+ it "should create the script in the temporary directory" do
+ File.should be_exist(File.expand_path(@riak_bin))
+ end
+
+ it "should modify the RUNNER_SCRIPT_DIR to point to the temporary directory" do
+ File.readlines(@riak_bin).should be_any do |line|
+ line =~ /RUNNER_SCRIPT_DIR=(.*)/ && $1 == File.expand_path("#{@server.temp_dir}/bin")
+
+ end
+ end
+
+ it "should modify the RUNNER_ETC_DIR to point to the temporary directory" do
+ File.readlines(@riak_bin).should be_any do |line|
+ line =~ /RUNNER_ETC_DIR=(.*)/ && $1 == File.expand_path("#{@server.temp_dir}/etc")
+ end
+ end
+
+ it "should modify the RUNNER_USER to point to the current user" do
+ File.readlines(@riak_bin).should be_any do |line|
+ line =~ /RUNNER_USER=(.*)/ && $1 == (ENV['USER'] || `whoami`)
+ end
+ end
+
+ it "should modify the RUNNER_LOG_DIR to point to the temporary directory" do
+ File.readlines(@riak_bin).should be_any do |line|
+ line =~ /RUNNER_LOG_DIR=(.*)/ && $1 == File.expand_path("#{@server.temp_dir}/log")
+ end
+ end
+
+ it "should modify the RUNNER_BASE_DIR so that it is not relative" do
+ File.readlines(@riak_bin).should be_any do |line|
+ line =~ /RUNNER_BASE_DIR=(.*)/ && $1.strip != "${RUNNER_SCRIPT_DIR%/*}" && File.directory?($1)
+ end
+ end
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.