[ARVADOS] created: cea92754dfacf2b409d1f5b45dd0775fc44c842d

git at public.curoverse.com git at public.curoverse.com
Thu Jun 5 15:44:30 EDT 2014


        at  cea92754dfacf2b409d1f5b45dd0775fc44c842d (commit)


commit cea92754dfacf2b409d1f5b45dd0775fc44c842d
Author: Tim Pierce <twp at curoverse.com>
Date:   Thu Jun 5 15:42:06 2014 -0400

    2857: fix flaky time-sensitive tests
    
    Allow tests to inject a mock Time object into Blob, in order to generate
    consistent behavior for tests that rely on permission signatures.
    
    Closes #2857.

diff --git a/services/api/app/models/blob.rb b/services/api/app/models/blob.rb
index 5decd77..c4e1881 100644
--- a/services/api/app/models/blob.rb
+++ b/services/api/app/models/blob.rb
@@ -16,6 +16,15 @@ class Blob
   class InvalidSignatureError < StandardError
   end
 
+  # Clock is a wrapper for Time.
+  # It can be replaced with a stub for unit testing (the poor man's mock).
+  # It must support a Clock.now method that returns a Time object.
+  @@Clock = Time
+
+  def self.set_clock c
+    @@Clock = c
+  end
+
   # Blob.sign_locator: return a signed and timestamped blob locator.
   #
   # The 'opts' argument should include:
@@ -35,7 +44,7 @@ class Blob
       end
       timestamp = opts[:expire]
     else
-      timestamp = Time.now.to_i + (opts[:ttl] || 600)
+      timestamp = @@Clock.now.to_i + (opts[:ttl] || 600)
     end
     timestamp_hex = timestamp.to_s(16)
     # => "53163cb4"
@@ -82,7 +91,7 @@ class Blob
     if !timestamp.match /^[\da-f]+$/
       raise Blob::InvalidSignatureError.new 'Timestamp is not a base16 number.'
     end
-    if timestamp.to_i(16) < Time.now.to_i
+    if timestamp.to_i(16) < @@Clock.now.to_i
       raise Blob::InvalidSignatureError.new 'Signature expiry time has passed.'
     end
 
diff --git a/services/api/test/functional/arvados/v1/collections_controller_test.rb b/services/api/test/functional/arvados/v1/collections_controller_test.rb
index 8b2725a..ba6929b 100644
--- a/services/api/test/functional/arvados/v1/collections_controller_test.rb
+++ b/services/api/test/functional/arvados/v1/collections_controller_test.rb
@@ -2,6 +2,25 @@ require 'test_helper'
 
 class Arvados::V1::CollectionsControllerTest < ActionController::TestCase
 
+  # StoppedClock.now always returns the same timestamp.
+  # Set the Blob permission signing clock to ensure that
+  # all permission hints use consistent timestamps for testing.
+
+  class StoppedClock
+    @@cached_timestamp = Time.now
+    def self.now
+      return @@cached_timestamp
+    end
+  end
+
+  def setup
+    Blob.set_clock(StoppedClock)
+  end
+
+  def teardown
+    Blob.set_clock(Time)
+  end
+
   test "should get index" do
     authorize_with :active
     get :index

-----------------------------------------------------------------------


hooks/post-receive
-- 




More information about the arvados-commits mailing list