From b026e42275a15f9b97b74f05b41fd178bf9949b9 Mon Sep 17 00:00:00 2001 From: Nick Muerdter Date: Wed, 7 Dec 2016 18:27:04 -0700 Subject: [PATCH] Fix timezone dependent tests. Randomize timezone for all tests. We had a few tests that would break if they happened to be run on a computer in the Denver timezone. Fix those tests so they will work regardless of the host machine's timezone. Also add time zone randomization to our test suite to better ensure our tests aren't timezone dependent. --- Gemfile | 3 +++ Gemfile.lock | 2 ++ test/admin_ui/test_legacy_redirects.rb | 27 ++++++++++++++++++-------- test/test_helper.rb | 7 +++++++ 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index abcbb8eee..41bc6e161 100644 --- a/Gemfile +++ b/Gemfile @@ -74,6 +74,9 @@ gem "faker", "~> 1.6.6" # Concurrency helpers. gem "concurrent-ruby", "~> 1.0.2" +# Time zone randomization for tests. +gem "zonebie", "~> 0.6.1" + # Color output gem "rainbow", "~> 2.1.0" diff --git a/Gemfile.lock b/Gemfile.lock index 733999fa8..e40522bcd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -142,6 +142,7 @@ GEM websocket-extensions (0.1.2) xpath (2.0.0) nokogiri (~> 1.3) + zonebie (0.6.1) PLATFORMS ruby @@ -176,6 +177,7 @@ DEPENDENCIES rake (~> 11.3.0) rubocop (~> 0.46.0) typhoeus (~> 1.1.2) + zonebie (~> 0.6.1) BUNDLED WITH 1.13.6 diff --git a/test/admin_ui/test_legacy_redirects.rb b/test/admin_ui/test_legacy_redirects.rb index 5241760d2..ef2e86e25 100644 --- a/test/admin_ui/test_legacy_redirects.rb +++ b/test/admin_ui/test_legacy_redirects.rb @@ -29,8 +29,7 @@ def test_drilldown "end_at" => "2015-01-18", "interval" => "hour", "query" => "{\"condition\":\"AND\",\"rules\":[{\"id\":\"gatekeeper_denied_code\",\"field\":\"gatekeeper_denied_code\",\"type\":\"string\",\"input\":\"select\",\"operator\":\"is_null\",\"value\":null},{\"id\":\"request_host\",\"field\":\"request_host\",\"type\":\"string\",\"input\":\"text\",\"operator\":\"begins_with\",\"value\":\"example.com\"}]}", - "tz" => "America/Denver", - }, fragment_uri.query_values) + }.merge(expected_tz_param), fragment_uri.query_values) end def test_logs @@ -49,8 +48,7 @@ def test_logs "end_at" => "2015-01-18", "interval" => "hour", "query" => "{\"condition\":\"AND\",\"rules\":[{\"id\":\"gatekeeper_denied_code\",\"field\":\"gatekeeper_denied_code\",\"type\":\"string\",\"input\":\"select\",\"operator\":\"is_null\",\"value\":null},{\"id\":\"request_host\",\"field\":\"request_host\",\"type\":\"string\",\"input\":\"text\",\"operator\":\"begins_with\",\"value\":\"example.com\"}]}", - "tz" => "America/Denver", - }, fragment_uri.query_values) + }.merge(expected_tz_param), fragment_uri.query_values) end def test_users @@ -68,8 +66,7 @@ def test_users "start_at" => "2015-01-15", "end_at" => "2015-01-18", "query" => "{\"condition\":\"AND\",\"rules\":[{\"id\":\"gatekeeper_denied_code\",\"field\":\"gatekeeper_denied_code\",\"type\":\"string\",\"input\":\"select\",\"operator\":\"is_null\",\"value\":null},{\"id\":\"request_host\",\"field\":\"request_host\",\"type\":\"string\",\"input\":\"text\",\"operator\":\"begins_with\",\"value\":\"example.com\"}]}", - "tz" => "America/Denver", - }, fragment_uri.query_values) + }.merge(expected_tz_param), fragment_uri.query_values) end def test_map @@ -88,7 +85,21 @@ def test_map "end_at" => "2015-01-18", "query" => "{\"condition\":\"AND\",\"rules\":[{\"id\":\"gatekeeper_denied_code\",\"field\":\"gatekeeper_denied_code\",\"type\":\"string\",\"input\":\"select\",\"operator\":\"is_null\",\"value\":null},{\"id\":\"request_host\",\"field\":\"request_host\",\"type\":\"string\",\"input\":\"text\",\"operator\":\"begins_with\",\"value\":\"example.com\"}]}", "region" => "US", - "tz" => "America/Denver", - }, fragment_uri.query_values) + }.merge(expected_tz_param), fragment_uri.query_values) + end + + private + + def expected_tz_param + # In all our redirect tests, we pass in "America/Denver" for the timezone + # parameter. If the user's current timezone happens to be America/Denver, + # then the "tz" parameter won't be in the redirect (since Ember doesn't add + # default parameters into the URL). However, if we're running in any other + # timezone, this non-default "tz" parameter should be part of the redirect. + if(ENV["TZ"] == "America/Denver") + {} + else + { "tz" => "America/Denver" } + end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 961fefcb7..abe49e917 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -28,6 +28,13 @@ ENV["PATH"], ].join(":") +# Set a random time zone to ensure tests aren't time zone specific. +Zonebie.set_random_timezone + +# Set the TZ environment variable to ensure other processes (like the Capybara +# browser tests) are run in the same random time zone. +ENV["TZ"] = ::Time.zone.tzinfo.identifier + # Load all the support files. Load models first, so they're defined for other # helpers. Dir[File.join(API_UMBRELLA_SRC_ROOT, "test/support/models/*.rb")].each { |f| require f }