Skip to content

Commit

Permalink
fix(ssg): RHICOMPL-2030 do not override default SSGs file, use two
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Kofink <[email protected]>
  • Loading branch information
skateman authored and akofink committed Jul 14, 2021
1 parent 4d41245 commit 8943ab6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 25 deletions.
15 changes: 10 additions & 5 deletions app/services/ssg_config_downloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

# Service for dowloading SSG configuration files
class SsgConfigDownloader
class << self
SSG_DS_FILE = File.new('config/supported_ssg.yaml')
FILE_PATH = 'config/supported_ssg.yaml'
FALLBACK_PATH = 'config/supported_ssg.default.yaml'

class << self
def ssg_ds
File.read(SSG_DS_FILE)
File.read(ssg_ds_file)
end

def ssg_ds_checksum
Digest::MD5.file(SSG_DS_FILE).hexdigest
Digest::MD5.file(ssg_ds_file).hexdigest
end

def update_ssg_ds
ssg_content = download(ssg_datastream_config_url)&.read
ssg_content && File.open(SSG_DS_FILE, 'w') do |f|
ssg_content && File.open(FILE_PATH, 'w') do |f|
f.write(ssg_content)
end
end
Expand All @@ -29,6 +30,10 @@ def ssg_datastream_config_url

private

def ssg_ds_file
File.new(File.exist?(FILE_PATH) ? FILE_PATH : FALLBACK_PATH)
end

def download(url)
file = SafeDownloader.download(url)
Rails.logger.audit_success("Downloaded config from #{url}")
Expand Down
File renamed without changes.
72 changes: 52 additions & 20 deletions test/services/ssg_config_downloader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,66 @@

# A class to test the SsgConfigDownloader service
class ConfigDownloaderTest < ActiveSupport::TestCase
setup do
@ds_config_file = File.new('config/supported_ssg.yaml')
end
context 'fallback file' do
setup do
if File.exist?(SsgConfigDownloader::FILE_PATH)
File.delete(SsgConfigDownloader::FILE_PATH)
end

test 'ssg_ds returns SSG datastream config file from disk' do
assert_equal @ds_config_file.read, SsgConfigDownloader.ssg_ds
end
@ds_config_file = File.new(SsgConfigDownloader::FALLBACK_PATH)
end

should 'ssg_ds returns SSG datastream fallback file from disk' do
assert_equal @ds_config_file.read, SsgConfigDownloader.ssg_ds
end

test 'ssg_ds_checksum' do
assert_equal Digest::MD5.file(@ds_config_file).hexdigest,
SsgConfigDownloader.ssg_ds_checksum
should 'ssg_ds_checksum' do
assert_equal Digest::MD5.file(@ds_config_file).hexdigest,
SsgConfigDownloader.ssg_ds_checksum
end
end

test 'update_ssg_ds success' do
SafeDownloader.expects(:download)
context 'downloaded file' do
setup do
FileUtils.cp(
SsgConfigDownloader::FALLBACK_PATH,
SsgConfigDownloader::FILE_PATH
)

SsgConfigDownloader.update_ssg_ds
assert_audited 'Downloaded config'
@ds_config_file = File.new(SsgConfigDownloader::FILE_PATH)
end

assert_equal @ds_config_file.read, SsgConfigDownloader.ssg_ds
end
should 'ssg_ds returns SSG datastream file from disk' do
assert_equal @ds_config_file.read, SsgConfigDownloader.ssg_ds
end

should 'ssg_ds_checksum' do
assert_equal Digest::MD5.file(@ds_config_file).hexdigest,
SsgConfigDownloader.ssg_ds_checksum
end

should 'update_ssg_ds success' do
SafeDownloader.expects(:download)

SsgConfigDownloader.update_ssg_ds
assert_audited 'Downloaded config'

assert_equal @ds_config_file.read, SsgConfigDownloader.ssg_ds
end

should 'update_ssg_ds handles failure gracefully' do
SafeDownloader.expects(:download).raises(StandardError)

test 'update_ssg_ds handles failure gracefully' do
SafeDownloader.expects(:download).raises(StandardError)
SsgConfigDownloader.update_ssg_ds
assert_audited 'Failed to download config'

SsgConfigDownloader.update_ssg_ds
assert_audited 'Failed to download config'
assert_equal @ds_config_file.read, SsgConfigDownloader.ssg_ds
end

assert_equal @ds_config_file.read, SsgConfigDownloader.ssg_ds
teardown do
if File.exist?(SsgConfigDownloader::FILE_PATH)
File.delete(SsgConfigDownloader::FILE_PATH)
end
end
end
end

0 comments on commit 8943ab6

Please sign in to comment.