Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rake task to reset proxy config change history #1733

Merged
merged 2 commits into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/tasks/proxy.rake
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,25 @@ namespace :proxy do
task :set_correct_endpoint_hosted => :environment do
Service.includes(:proxy).where(proxies: {endpoint: nil}, deployment_option: 'hosted').find_each(&:deployment_option_changed)
end

desc 'Resets proxy config tracking object'
task :reset_config_change_history, [:account_id] => :environment do |_, args|
account_id = args[:account_id]
collection = account_id ? Account.providers_with_master.find_by(id: account_id)&.proxies : Proxy.all

return unless collection

reset_date = Time.utc(1900, 1, 1).freeze
progress = ProgressCounter.new(collection.count)

collection.find_in_batches do |proxies|
proxies.each do |proxy|
tracking_object = proxy.affecting_change_history
progress.call
next if tracking_object.created_at != tracking_object.updated_at
tracking_object.update_column(:updated_at, reset_date)
end
sleep(0.5) unless Rails.env.test?
end
end
end
73 changes: 73 additions & 0 deletions test/unit/tasks/proxy_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

require 'test_helper'

module Tasks
class ProxyTest < ActiveSupport::TestCase
class ResetProxyConfigChangeHistoryTest < ActiveSupport::TestCase
setup do
@providers = FactoryBot.create_list(:provider_account, 3)
proxies.each(&:affecting_change_history) # so change history exists for the proxies associated with all these accounts

@proxy_with_legit_change = proxies.last
@legit_change_date = 2.minutes.from_now.freeze
proxy_with_legit_change.affecting_change_history.update_column(:updated_at, legit_change_date) # so change history of this proxy has been updated

@providers << FactoryBot.create(:provider_account) # change history for the proxy associated with this account does not exist
@proxy_without_change_history = proxies.last
@reset_date = Time.utc(1900, 1, 1).freeze
end

attr_reader :providers, :proxy_with_legit_change, :legit_change_date, :proxy_without_change_history, :reset_date

test 'creates missing change history' do
refute ProxyConfigAffectingChange.where(proxy: proxy_without_change_history).exists?
execute_rake_task 'proxy.rake', 'proxy:reset_config_change_history'
assert ProxyConfigAffectingChange.where(proxy: proxy_without_change_history).exists?
end

test 'resets change history of all providers whose proxy config is untouched' do
assert_did_not_reset_proxies_without_legit_change
assert_did_not_reset_proxy_with_legit_change
refute ProxyConfigAffectingChange.where(proxy: proxy_without_change_history).exists?

execute_rake_task 'proxy.rake', 'proxy:reset_config_change_history'

ProxyConfigAffectingChange.where(proxy: proxies.to_a.values_at(0, 1, 3)).each { |tracking_object| assert_equal reset_date, tracking_object.updated_at }
assert_did_not_reset_proxy_with_legit_change
end

test 'resets change history of given account id' do
assert_did_not_reset_proxies_without_legit_change
assert_did_not_reset_proxy_with_legit_change
refute ProxyConfigAffectingChange.where(proxy: proxy_without_change_history).exists?

execute_rake_task 'proxy.rake', 'proxy:reset_config_change_history', providers.last.id

assert_did_not_reset_proxies_without_legit_change
assert_did_not_reset_proxy_with_legit_change
assert_equal reset_date, proxies.last.affecting_change_history.updated_at
end

test 'reset change history of legit tracking record is a no-op' do
assert_did_not_reset_proxy_with_legit_change
execute_rake_task 'proxy.rake', 'proxy:reset_config_change_history', providers[2].id
assert_did_not_reset_proxy_with_legit_change
end

protected

def proxies
Proxy.where(service: Service.where(account: providers)).order(:id)
end

def assert_did_not_reset_proxies_without_legit_change
ProxyConfigAffectingChange.where(proxy: proxies[0..1]).each { |tracking_object| assert tracking_object.created_at == tracking_object.updated_at }
end

def assert_did_not_reset_proxy_with_legit_change
assert_equal legit_change_date.to_i, proxy_with_legit_change.affecting_change_history.updated_at.to_i
end
end
end
end