Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #290 from paxtonhare/dev
Browse files Browse the repository at this point in the history
Issue #288 - requiring confirmation before wiping non-local environment.
  • Loading branch information
paxtonhare committed Oct 29, 2014
2 parents 22beaed + 4dabeeb commit a9ea040
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 73 deletions.
29 changes: 26 additions & 3 deletions deploy/lib/server_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class ServerConfig < MLClient
@@path = @@is_jar ? "./deploy" : "../.."
@@context = @@is_jar ? Dir.pwd : __FILE__

def self.path
@@path
end

def initialize(options)
@options = options

Expand Down Expand Up @@ -436,6 +440,26 @@ def bootstrap
end

def wipe
if @environment != "local"
expected_response = %Q{I WANT TO WIPE #{@environment.upcase}}
print %Q{
*******************************************************************************
WARNING!!! You are attempting to wipe your #{@environment.upcase} environment!
This will remove everything that Roxy has bootstrapped. It's quite dangerous.
*******************************************************************************
Are you sure you want to do this?
In order to proceed please type: #{expected_response}
:> }
response = $stdin.gets.chomp
if response != expected_response
logger.info "\nAborting wipe on #{@environment}"
return
end
end

appbuilder = find_arg(['--app-builder'])
setup = File.read(ServerConfig.expand_path("#{@@path}/lib/xquery/setup.xqy"))

Expand Down Expand Up @@ -868,12 +892,11 @@ def credentials
end
properties["user"] = user
properties["password"] = password
open(properties_file, 'w') {
|f|
File.open(properties_file, 'w') do |f|
properties.each do |k,v|
f.write "#{k}=#{v}\n"
end
}
end
logger.info "wrote #{properties_file}"
end

Expand Down
239 changes: 169 additions & 70 deletions deploy/test/test_server_config.rb
Original file line number Diff line number Diff line change
@@ -1,98 +1,197 @@
require 'test/unit'
require 'minitest/autorun'
require 'minitest/mock'
require 'server_config'
require 'util'

class TestProperties < Test::Unit::TestCase
describe ServerConfig do

def teardown
Logger.new(STDOUT).info "Teardown: Wiping self-test deployment.." if @s
@s.wipe if @s
describe "load_properties" do
before do
@properties = ServerConfig.load_properties(File.expand_path("../data/ml6-properties/default.properties", __FILE__), "test.")
end

it "should load the properties" do
@properties.must_be_kind_of Hash
@properties['test.user'].must_equal 'admin'
@properties['test.password'].must_equal 'admin'
@properties['test.app-name'].must_equal 'roxy'
end
end

def test_load_properties
properties = ServerConfig.load_properties(File.expand_path("../data/ml6-properties/default.properties", __FILE__), "test.")
assert(properties.is_a?(Hash))
assert_equal('admin', properties['test.user'])
assert_equal('admin', properties['test.password'])
assert_equal('roxy', properties['test.app-name'])
describe "substitute_properties" do

it "should substitute properties" do
sub_me = { 'username' => 'bob-${last-name}', 'password' => '123' }
with_me = { 'last-name' => 'smith' }
properties = ServerConfig.substitute_properties(sub_me, with_me)

properties.must_be_kind_of Hash

properties['username'].must_equal 'bob-smith'
properties['password'].must_equal '123'
end

it "should substitute properties with a prefix" do
sub_me = { 'username' => 'bob-${last-name}', 'password' => '123' }
with_me = { 'ml.last-name' => 'smith' }
properties = ServerConfig.substitute_properties(sub_me, with_me, "ml.")

properties.must_be_kind_of Hash

properties['username'].must_equal 'bob-smith'
properties['password'].must_equal '123'
end
end

def test_substitute_properties
sub_me = { 'username' => 'bob-${last-name}', 'password' => '123' }
with_me = { 'last-name' => 'smith' }
properties = ServerConfig.substitute_properties(sub_me, with_me)
assert(properties.is_a?(Hash))
assert_equal('bob-smith', properties['username'])
assert_equal('123', properties['password'])

sub_me = { 'username' => 'bob-${last-name}', 'password' => '123' }
with_me = { 'ml.last-name' => 'smith' }
properties = ServerConfig.substitute_properties(sub_me, with_me, "ml.")
assert(properties.is_a?(Hash))
assert_equal('bob-smith', properties['username'])
assert_equal('123', properties['password'])
describe "load properties" do

it "should load properties from a file" do
properties = ServerConfig.properties(File.expand_path("../data/ml6-properties/", __FILE__))

properties.must_be_kind_of Hash
properties['ml.user'].must_equal 'admin'
properties['ml.password'].must_equal 'admin'
properties['ml.app-name'].must_equal 'roxy-deployer-tester'
end
end

def test_properties
properties = ServerConfig.properties(File.expand_path("../data/ml6-properties/", __FILE__))
assert(properties.is_a?(Hash))
assert_equal('admin', properties['ml.user'])
assert_equal('admin', properties['ml.password'])
assert_equal('roxy-deployer-tester', properties['ml.app-name'])
describe "bootstrap" do

before do
@version = ENV['ROXY_TEST_SERVER_VERSION'] || 7
@logger = Logger.new(STDOUT)
@logger.info "Testing against MarkLogic version #{@version}.."

# cheat the local environment into the command line
ARGV << "local"

@properties = ServerConfig.properties(File.expand_path("../data/ml#{@version}-properties/", __FILE__))
@s = ServerConfig.new({
:config_file => File.expand_path("../data/ml#{@version}-config.xml", __FILE__),
:properties => @properties,
:logger => @logger
})

@s.bootstrap.must_equal true
@s.validate_install.must_equal true
end

it "should bootstrap successfully twice consecutively" do
@s.bootstrap.must_equal true
@s.validate_install.must_equal true
end

it "should bootstrap a changed config file" do
changed_config = File.expand_path("../data/ml#{@version}-config-changed.xml", __FILE__)

if File.exists?(changed_config)
@s = ServerConfig.new({
:config_file => changed_config,
:properties => @properties,
:logger => @logger
})

@s.bootstrap.must_equal true
@s.validate_install.must_equal true
end
end

after do
@logger.info "Wiping self-test deployment.."
@s.wipe

sleep(10)
end
end

def test_build_config
# issue #228
describe "load properties from command" do

before do
ARGV << "--ml.yoda-age=900"
ARGV << "--ml.missing-key=val1"

@logger = MiniTest::Mock.new
@logger.expect :warn, nil, ["Property ml.missing-key does not exist. It will be skipped."]

ServerConfig.logger = @logger
@properties = ServerConfig.properties(File.expand_path("../data/ml7-properties/", __FILE__))
end

it "should warn the user when missing keys are provided" do
@logger.verify.must_equal true
end

it "should load valid properites from a command" do
@properties['ml.yoda-age'].must_equal '900'
end

it "should not set missing keys" do
@properties.has_key?('missing-key').wont_equal true
end

after do
ARGV.shift
ARGV.shift
end
end

def bootstrap_version(version)
# cheat the local environment into the command line
ARGV << "local"
describe "test_credentials" do

before do
ARGV << "local"

test_env = "blah"
properties = ServerConfig.properties

properties = ServerConfig.properties(File.expand_path("../data/ml#{version}-properties/", __FILE__))
@s = ServerConfig.new({
:config_file => File.expand_path("../data/ml#{version}-config.xml", __FILE__),
properties["environment"] = test_env
properties["ml.environment"] = test_env

@s = ServerConfig.new({
:config_file => File.expand_path(properties["ml.config.file"], __FILE__),
:properties => properties,
:logger => Logger.new(STDOUT)
})

assert(@s.bootstrap, "Boostrap should succeeded")
assert(@s.validate_install, "Bootstrap passes validation")
filename = "#{test_env}.properties"
path = ServerConfig.path
@properties_file = ServerConfig.expand_path("#{path}/#{filename}")
end

assert(@s.bootstrap, "Boostrap should succeeded")
assert(@s.validate_install, "Bootstrap passes validation")
it "should prompt the user for credentials" do
File.exists?(@properties_file).must_equal false

if File.exist? File.expand_path("../data/ml#{version}-config-changed.xml", __FILE__)
@s = ServerConfig.new({
:config_file => File.expand_path("../data/ml#{version}-config-changed.xml", __FILE__),
:properties => properties,
:logger => Logger.new(STDOUT)
})
with_stdin do |user|
user.puts "bob"
user.puts "smith"
@s.credentials
end

File.exists?(@properties_file).must_equal true

props_data = File.read(@properties_file)

assert(@s.bootstrap, "Boostrap should succeeded")
assert(@s.validate_install, "Bootstrap passes validation")
user = $1 if props_data =~ /user=(\w+)/
password = $1 if props_data =~ /password=(\w+)/

user.must_equal 'bob'
password.must_equal 'smith'
end

# TODO: temporary fix to wipe self-test until teardown is fixed
Logger.new(STDOUT).info "Wiping self-test deployment.."
@s.wipe
end

def test_bootstrap
version = ENV['ROXY_TEST_SERVER_VERSION'] || 7
Logger.new(STDOUT).info "Testing against MarkLogic version #{version}.."
bootstrap_version version
end
after do
File.delete(@properties_file)
@s = nil
end

# issue #228
def test_load_properties_from_command
ARGV << "--ml.yoda-age=900"
ARGV << "--ml.missing-key=val1"
Logger.new(STDOUT).info "A warning about a non-existing property named 'ml.missing-key' will follow, that is supposed to happen.."
properties = ServerConfig.properties(File.expand_path("../data/ml7-properties/", __FILE__))
assert_equal('900', properties['ml.yoda-age'])
assert(!properties.has_key?('missing-key'))
ARGV.shift
ARGV.shift
end


def with_stdin
stdin = $stdin # remember $stdin
$stdin, write = IO.pipe # create pipe assigning its "read end" to $stdin
yield write # pass pipe's "write end" to block
ensure
write.close # close pipe
$stdin = stdin # restore $stdin
end
end

0 comments on commit a9ea040

Please sign in to comment.