Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ group :test do
gem 'shoulda'
gem 'shoulda-matchers'
gem 'shoulda-context'
gem 'timecop'
end
11 changes: 11 additions & 0 deletions lib/instance_agent/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

module InstanceAgent
class Config < ProcessManager::Config
VALID_TIME_ZONES = ['local', 'utc']

def self.init
@config = Config.new
ProcessManager::Config.instance_variable_set("@config", @config)
Expand All @@ -11,6 +13,7 @@ def self.init
def validate
errors = super
validate_children(errors)
validate_time_zone(errors)
errors
end

Expand All @@ -31,6 +34,7 @@ def initialize
:instance_service_port => nil,
:wait_between_runs => 30,
:wait_after_error => 30,
:time_zone => 'local',
:codedeploy_test_profile => 'prod',
:kill_agent_max_wait_time_seconds => 7200,
:on_premises_config_file => '/etc/codedeploy-agent/conf/codedeploy.onpremises.yml',
Expand All @@ -39,10 +43,17 @@ def initialize
})
end

private

def validate_children(errors = [])
errors << 'children can only be set to 1' unless config[:children] == 1
errors
end

def validate_time_zone(errors = [])
errors << 'time_zone can only be set to [local|utc]' unless VALID_TIME_ZONES.include?(config[:time_zone])
errors
end

end
end
16 changes: 14 additions & 2 deletions lib/instance_agent/log.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'process_manager/log'
require 'singleton'
require 'time'

InstanceAgent::Log = ProcessManager::Log

Expand All @@ -11,11 +12,22 @@ def initialize
FileUtils.mkdir_p(deployment_logs_dir) unless File.exists? deployment_logs_dir
@deployment_log ||= Logger.new(File.join(deployment_logs_dir, "#{InstanceAgent::Config.config[:program_name]}-deployments.log"), 8, 64 * 1024 * 1024)
@deployment_log.formatter = proc do |severity, datetime, progname, msg|
"[#{datetime.strftime('%Y-%m-%d %H:%M:%S.%L')}] #{msg}\n"
"[#{format_datetime(datetime)}] #{msg}\n"
end
end

def log(message)
@deployment_log.info(message)
end
end

private

def format_datetime(datetime)
case InstanceAgent::Config.config[:time_zone]
when 'utc'
datetime.utc.iso8601(3)
else
datetime.strftime('%Y-%m-%d %H:%M:%S.%L')
end
end
end
13 changes: 13 additions & 0 deletions test/instance_agent/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class InstanceAgentConfigTest < InstanceAgentTestCase
:instance_service_port => nil,
:wait_between_runs => 30,
:wait_after_error => 30,
:time_zone => 'local',
:codedeploy_test_profile => 'prod',
:on_premises_config_file => '/etc/codedeploy-agent/conf/codedeploy.onpremises.yml',
:ongoing_deployment_tracking => 'ongoing-deployment',
Expand Down Expand Up @@ -64,6 +65,18 @@ class InstanceAgentConfigTest < InstanceAgentTestCase
InstanceAgent::Config.config[:children] = 1
assert InstanceAgent::Config.validate_config.empty?, InstanceAgent::Config.validate_config.inspect
end

should 'validate the time_zone setting' do
InstanceAgent::Config.config[:time_zone] = nil
puts InstanceAgent::Config.config.inspect
assert_equal 'time_zone can only be set to [local|utc]', InstanceAgent::Config.validate_config.pop
InstanceAgent::Config.config[:time_zone] = 'invalid_time_zone'
assert_equal 'time_zone can only be set to [local|utc]', InstanceAgent::Config.validate_config.pop
InstanceAgent::Config.config[:time_zone] = 'local'
assert InstanceAgent::Config.validate_config.empty?, InstanceAgent::Config.validate_config.inspect
InstanceAgent::Config.config[:time_zone] = 'utc'
assert InstanceAgent::Config.validate_config.empty?, InstanceAgent::Config.validate_config.inspect
end
end
end
end
44 changes: 44 additions & 0 deletions test/instance_agent/deployment_log_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'test_helper'

class InstanceAgentDeploymentLogTest < InstanceAgentTestCase
setup do
InstanceAgent::Config.config[:root_dir] = @dir
InstanceAgent::Config.config[:program_name] = 'app'
@log_file = File.join(@dir, 'deployment-logs', "app-deployments.log")
end

context 'The instance agent deployment log when no explicit :time_zone config option is given' do
should 'prints log output with local time' do
Timecop.freeze(Time.local(2008, 9, 1, 12, 0, 0)) do
InstanceAgent::DeploymentLog.instance.log("Use local time")
assert_equal("[2008-09-01 12:00:00.000] Use local time\n", `tail -n 1 #{@log_file}`)
end
end
end

context 'The instance agent deployment log when :time_zone config option is local' do
setup do
InstanceAgent::Config.config[:time_zone] = 'local'
end

should 'prints log output with local time' do
Timecop.freeze(Time.local(2018, 9, 1, 15, 0, 0)) do
InstanceAgent::DeploymentLog.instance.log("Use local time")
assert_equal("[2018-09-01 15:00:00.000] Use local time\n", `tail -n 1 #{@log_file}`)
end
end
end

context 'The instance agent deployment log when :time_zone config option is utc' do
setup do
InstanceAgent::Config.config[:time_zone] = 'utc'
end

should 'prints log output with UTC ISO8601 time' do
Timecop.freeze(Time.new(2018, 9, 1, 14, 0, 0, "+10:00")) do
InstanceAgent::DeploymentLog.instance.log("Use UTC ISO8601 formatted time")
assert_equal("[2018-09-01T04:00:00.000Z] Use UTC ISO8601 formatted time\n", `tail -n 1 #{@log_file}`)
end
end
end
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@

# require local test helpers. If you need a helper write,
# keep this pattern or you'll be punished hard
require 'instance_agent_helper'
require_relative './helpers/instance_agent_helper'