Skip to content

Commit

Permalink
Merge pull request #257 from jgawor/install_feature_fix_2
Browse files Browse the repository at this point in the history
Collect features from configDropins directory
  • Loading branch information
caijj committed Oct 20, 2015
2 parents 0f30df8 + 32667fd commit ebe1761
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 73 deletions.
44 changes: 34 additions & 10 deletions lib/liberty_buildpack/container/feature_manager.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Encoding: utf-8
# IBM WebSphere Application Server Liberty Buildpack
# Copyright 2014 the original author or authors.
# Copyright 2014-2015 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@
require 'liberty_buildpack/util/xml_utils'
require 'rexml/document'
require 'rexml/xpath'
require 'set'

module LibertyBuildpack::Container

Expand Down Expand Up @@ -70,7 +71,7 @@ def download_and_install_features(server_xml, liberty_home)
features = get_features(server_xml)
jvm_args = get_jvm_args
cmd = File.join(liberty_home, 'bin', 'installUtility')
script_string = "JAVA_HOME=\"#{@app_dir}/#{@java_home}\" JVM_ARGS=#{jvm_args} #{cmd} install --acceptLicense #{features}"
script_string = "JAVA_HOME=\"#{@app_dir}/#{@java_home}\" JVM_ARGS=#{jvm_args} #{cmd} install --acceptLicense #{features.join(' ')}"

@logger.debug("script invocation string is #{script_string}")
output = `#{script_string} 2>&1`
Expand Down Expand Up @@ -102,7 +103,7 @@ def self.use_liberty_repository?(configuration)
liberty_repository_properties = configuration['liberty_repository_properties']
unless liberty_repository_properties.nil?
use_liberty_repository = liberty_repository_properties['useRepository']
use_liberty_repository = false unless use_liberty_repository == true
use_liberty_repository = (use_liberty_repository == true)
end
use_liberty_repository
end
Expand Down Expand Up @@ -155,16 +156,39 @@ def use_liberty_repository_with_properties_file?
use_liberty_repository_with_properties_file
end

# parse the given server.xml to find all features required and return a
# comma-separated list of these. User features are excluded by looking for
# features that do not contain a colon (user features specify a "product
# extension" location before the colon that indicates the location of the
# feature on disk, the default is to specify "usr").
# collect list of feature names from the server.xml and configDropins/
# directories.
#
# @return a String array of feature names.
def get_features(server_xml)
@logger.debug('entry')
features = Set.new(read_features(server_xml))

# Check for any configuration files under configDrops/overrides and
# configDropins/defaults. Since featureManager's feature elements
# have multiple cardinality, the values will always be merged together.
# Reading or processing order does not matter.
server_dir = File.dirname(server_xml)
%w{defaults overrides}.each do | type |
Dir.glob("#{server_dir}/configDropins/#{type}/*.xml").each do |file|
features.merge(read_features(file))
end
end

features.to_a
end

# parse the given server.xml to find all features required. User features
# are excluded by looking for features that do not contain a colon
# (user features specify a "product extension" location before the colon
# that indicates the location of the feature on disk, the default is to s
# pecify "usr").
#
# @return a String array of feature names.
def read_features(server_xml)
@logger.debug('entry (#{server_xml})')
server_xml_doc = LibertyBuildpack::Util::XmlUtils.read_xml_file(server_xml)
features = REXML::XPath.match(server_xml_doc, '/server/featureManager/feature/text()[not(contains(., ":"))]')
features = features.join(' ')
features = features.map { | feature | feature.to_s }
@logger.debug("exit (#{features})")
features
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<server>

<featureManager>
<feature>mongodb-2.0</feature>
<feature>jsp-2.2</feature>
</featureManager>

</server>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<server>

<featureManager>
<feature>couchdb-1.0</feature>
</featureManager>

</server>
104 changes: 41 additions & 63 deletions spec/liberty_buildpack/container/feature_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def set_up_feature_manager_script(root_dir, test_feature_manager_script_file)
feature_manager_script_file = File.join(liberty_bin_dir, 'installUtility')
FileUtils.copy(test_feature_manager_script_file, feature_manager_script_file)
system "chmod +x #{feature_manager_script_file}"
return app_dir, liberty_dir, liberty_bin_dir # rubocop:disable RedundantReturn
[app_dir, liberty_dir]
end

# invoke the buildpack feature manager code that eventually calls the
Expand All @@ -52,70 +52,54 @@ def call_feature_manager(app_dir, liberty_dir, configuration_file, server_xml_fi
feature_manager.download_and_install_features(server_xml_file, liberty_dir)
end

def run(root_dir, repo_yml = nil, script_file = nil, server_xml_file = nil)
# fixture files to be used.
configuration_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, repo_yml || 'use_default_repo.yml')
feature_manager_script_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, script_file || 'test_feature_manager_good_script')
server_xml_file ||= File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'test_server.xml')

# call feature manager using desired configuration and server.xml files.
app_dir, liberty_dir = set_up_feature_manager_script(root_dir, feature_manager_script_file)
call_feature_manager(app_dir, liberty_dir, configuration_file, server_xml_file)
[app_dir, liberty_dir]
end

it 'should not use the liberty feature repository if not configured' do
Dir.mktmpdir do |root_dir|
# fixture files to be used.
configuration_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'no_repo.yml')
feature_manager_script_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'test_feature_manager_good_script')
server_xml_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'test_server.xml')

# call feature manager using desired configuration and server.xml files.
app_dir, liberty_dir, liberty_bin_dir = set_up_feature_manager_script(root_dir, feature_manager_script_file)
call_feature_manager(app_dir, liberty_dir, configuration_file, server_xml_file)
liberty_dir = run(root_dir, 'no_repo.yml')[1]

# check liberty's featureManager was not called.
feature_manager_command_file = File.join(liberty_bin_dir, 'installUtility.txt')
feature_manager_command_file = File.join(liberty_dir, 'bin', 'installUtility.txt')
expect(File.exists?(feature_manager_command_file)).to eq(false)
end
end

it 'should not use the liberty feature repository if configured with false' do
Dir.mktmpdir do |root_dir|
# fixture files to be used.
configuration_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'dont_use_repo.yml')
feature_manager_script_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'test_feature_manager_good_script')
server_xml_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'test_server.xml')

# call feature manager using desired configuration and server.xml files.
app_dir, liberty_dir, liberty_bin_dir = set_up_feature_manager_script(root_dir, feature_manager_script_file)
call_feature_manager(app_dir, liberty_dir, configuration_file, server_xml_file)
liberty_dir = run(root_dir, 'dont_use_repo.yml')[1]

# check liberty's featureManager was not called.
feature_manager_command_file = File.join(liberty_bin_dir, 'installUtility.txt')
feature_manager_command_file = File.join(liberty_dir, 'bin', 'installUtility.txt')
expect(File.exists?(feature_manager_command_file)).to eq(false)
end
end

it 'should not use the liberty feature repository if configured with invalid value' do
Dir.mktmpdir do |root_dir|
# fixture files to be used.
configuration_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'junk_in_use_repo.yml')
feature_manager_script_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'test_feature_manager_good_script')
server_xml_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'test_server.xml')

# call feature manager using desired configuration and server.xml files.
app_dir, liberty_dir, liberty_bin_dir = set_up_feature_manager_script(root_dir, feature_manager_script_file)
call_feature_manager(app_dir, liberty_dir, configuration_file, server_xml_file)
liberty_dir = run(root_dir, 'junk_in_use_repo.yml')[1]

# check liberty's featureManager was not called.
feature_manager_command_file = File.join(liberty_bin_dir, 'installUtility.txt')
feature_manager_command_file = File.join(liberty_dir, 'bin', 'installUtility.txt')
expect(File.exists?(feature_manager_command_file)).to eq(false)
end
end

it 'should use the liberty feature repository if configured true' do
Dir.mktmpdir do |root_dir|
# fixture files to be used.
configuration_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'use_default_repo.yml')
feature_manager_script_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'test_feature_manager_good_script')
server_xml_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'test_server.xml')

# call feature manager using desired configuration and server.xml files.
app_dir, liberty_dir, liberty_bin_dir = set_up_feature_manager_script(root_dir, feature_manager_script_file)
call_feature_manager(app_dir, liberty_dir, configuration_file, server_xml_file)
app_dir, liberty_dir = run(root_dir)

# check liberty's featureManager was called, with expected parameters.
feature_manager_command_file = File.join(liberty_bin_dir, 'installUtility.txt')
feature_manager_command_file = File.join(liberty_dir, 'bin', 'installUtility.txt')
expect(File.exists?(feature_manager_command_file)).to eq(true)
feature_manager_command = File.read feature_manager_command_file
expect(feature_manager_command).to match(/jsp-2.2/)
Expand All @@ -135,17 +119,10 @@ def call_feature_manager(app_dir, liberty_dir, configuration_file, server_xml_fi

it 'should use the liberty feature repository with properties file if configured true with properties' do
Dir.mktmpdir do |root_dir|
# fixture files to be used.
configuration_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'use_specified_repo.yml')
feature_manager_script_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'test_feature_manager_good_script')
server_xml_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'test_server.xml')

# call feature manager using desired configuration and server.xml files.
app_dir, liberty_dir, liberty_bin_dir = set_up_feature_manager_script(root_dir, feature_manager_script_file)
call_feature_manager(app_dir, liberty_dir, configuration_file, server_xml_file)
app_dir, liberty_dir = run(root_dir, 'use_specified_repo.yml')

# check liberty's featureManager was called, with expected parameters.
feature_manager_command_file = File.join(liberty_bin_dir, 'installUtility.txt')
feature_manager_command_file = File.join(liberty_dir, 'bin', 'installUtility.txt')
expect(File.exists?(feature_manager_command_file)).to eq(true)
feature_manager_command = File.read feature_manager_command_file
expect(feature_manager_command).to match(/jsp-2.2/)
Expand Down Expand Up @@ -180,6 +157,21 @@ def call_feature_manager(app_dir, liberty_dir, configuration_file, server_xml_fi
end
end

it 'should install features from configDropins' do
Dir.mktmpdir do |root_dir|
liberty_dir = run(root_dir, nil, nil, 'spec/fixtures/container_liberty_single_server/server.xml')[1]

# check liberty's featureManager was called, with expected parameters.
feature_manager_command_file = File.join(liberty_dir, 'bin', 'installUtility.txt')
expect(File.exists?(feature_manager_command_file)).to eq(true)

feature_manager_command = File.read feature_manager_command_file
expect(feature_manager_command).to match(/jsp-2.2/)
expect(feature_manager_command).to match(/couchdb-1.0/)
expect(feature_manager_command).to match(/mongodb-2.0/)
end
end

context 'when JVM_ARGS is set by the user' do

JVM_ARGS_KEY = 'JVM_ARGS'.freeze
Expand All @@ -198,17 +190,10 @@ def call_feature_manager(app_dir, liberty_dir, configuration_file, server_xml_fi
ENV[JVM_ARGS_KEY] = prev_jvm_args

Dir.mktmpdir do |root_dir|
# fixture files to be used.
configuration_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'use_default_repo.yml')
feature_manager_script_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'test_feature_manager_good_script')
server_xml_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'test_server.xml')

# call feature manager using desired configuration and server.xml files.
app_dir, liberty_dir, liberty_bin_dir = set_up_feature_manager_script(root_dir, feature_manager_script_file)
call_feature_manager(app_dir, liberty_dir, configuration_file, server_xml_file)
liberty_dir = run(root_dir)[1]

# check liberty's featureManager was called, with expected parameters.
feature_manager_command_file = File.join(liberty_bin_dir, 'installUtility.txt')
feature_manager_command_file = File.join(liberty_dir, 'bin', 'installUtility.txt')
expect(File.exists?(feature_manager_command_file)).to eq(true)
feature_manager_command = File.read feature_manager_command_file
expect(feature_manager_command).to match(/jvm args is \(\)/)
Expand All @@ -219,14 +204,7 @@ def call_feature_manager(app_dir, liberty_dir, configuration_file, server_xml_fi
ENV[JVM_ARGS_KEY] = prev_jvm_args

Dir.mktmpdir do |root_dir|
# fixture files to be used.
configuration_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'use_default_repo.yml')
feature_manager_script_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'test_feature_manager_good_script')
server_xml_file = File.join(FEATURE_REPOSITORY_FIXTURE_DIR, 'test_server.xml')

# call feature manager using desired configuration and server.xml files.
app_dir, liberty_dir = set_up_feature_manager_script(root_dir, feature_manager_script_file)
call_feature_manager(app_dir, liberty_dir, configuration_file, server_xml_file)
run(root_dir)

# check that the JVM_ARGS has the same value from prior to running feature manager
expect(ENV['JVM_ARGS']).to match(prev_jvm_args)
Expand Down

0 comments on commit ebe1761

Please sign in to comment.