From 8943ab6109cc7b97d51c66c3ab9d5cee2d0609a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Hal=C3=A1sz?= Date: Tue, 13 Jul 2021 14:49:58 +0200 Subject: [PATCH] fix(ssg): RHICOMPL-2030 do not override default SSGs file, use two Signed-off-by: Andrew Kofink --- app/services/ssg_config_downloader.rb | 15 ++-- ...ed_ssg.yaml => supported_ssg.default.yaml} | 0 test/services/ssg_config_downloader_test.rb | 72 +++++++++++++------ 3 files changed, 62 insertions(+), 25 deletions(-) rename config/{supported_ssg.yaml => supported_ssg.default.yaml} (100%) diff --git a/app/services/ssg_config_downloader.rb b/app/services/ssg_config_downloader.rb index ac8451b81..cba276ba7 100644 --- a/app/services/ssg_config_downloader.rb +++ b/app/services/ssg_config_downloader.rb @@ -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 @@ -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}") diff --git a/config/supported_ssg.yaml b/config/supported_ssg.default.yaml similarity index 100% rename from config/supported_ssg.yaml rename to config/supported_ssg.default.yaml diff --git a/test/services/ssg_config_downloader_test.rb b/test/services/ssg_config_downloader_test.rb index 9f5bdd807..861e9405a 100644 --- a/test/services/ssg_config_downloader_test.rb +++ b/test/services/ssg_config_downloader_test.rb @@ -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