From eb86ba780bc7e666338d767945e1f3acee7f8f2b Mon Sep 17 00:00:00 2001 From: Nathan Sutton Date: Wed, 9 Nov 2011 11:30:33 -0800 Subject: [PATCH 1/2] Adding a method to unmock Fog. Addresses issue #594 --- lib/fog/core/mock.rb | 4 +++ tests/core/mocking_tests.rb | 60 +++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 tests/core/mocking_tests.rb diff --git a/lib/fog/core/mock.rb b/lib/fog/core/mock.rb index 6847d56b5..6524bad28 100644 --- a/lib/fog/core/mock.rb +++ b/lib/fog/core/mock.rb @@ -6,6 +6,10 @@ module Fog @mocking = true end + def self.unmock! + @mocking = false + end + def self.mock? @mocking end diff --git a/tests/core/mocking_tests.rb b/tests/core/mocking_tests.rb new file mode 100644 index 000000000..9d3dcf661 --- /dev/null +++ b/tests/core/mocking_tests.rb @@ -0,0 +1,60 @@ +Shindo.tests('Fog mocking', 'core') do + before do + @fog_was_mocked = Fog.mock? + Fog.unmock! if @fog_was_mocked + end + + after do + Fog.mock! if @fog_was_mocked + end + + tests('Fog.mock!') do + tests('Fog.mock!').returns(true) do + Fog.mock! + end + + tests('Fog.mock? without Fog.mock!').returns(false) do + Fog.mock? + end + + tests('Fog.mock? with Fog.mock!').returns(true) do + Fog.mock! + Fog.mock? + end + + tests('Fog.mocking? without Fog.mock!').returns(false) do + Fog.mocking? + end + + tests('Fog.mocking? with Fog.mock!').returns(true) do + Fog.mock! + Fog.mocking? + end + end + + tests('Fog::Mock.delay') do + tests('Fog::Mock.delay').returns(1, "defaults to 1") do + Fog::Mock.delay + end + + tests('Fog::Mock.delay = 2').returns(2, "changes Fog::Mock.delay to 2") do + Fog::Mock.delay = 2 + Fog::Mock.delay + end + + tests('Fog::Mock.delay = 0').returns(0, "changes Fog::Mock.delay to 0") do + Fog::Mock.delay = 0 + Fog::Mock.delay + end + + tests('Fog::Mock.delay = -1').raises(ArgumentError) do + Fog::Mock.delay = -1 + end + end + + tests('Fog::Mock.not_implemented').raises(Fog::Errors::MockNotImplemented) do + Fog::Mock.not_implemented + end + + +end From b7d5f5181b731f111d685457d0daebf41cd6a2c2 Mon Sep 17 00:00:00 2001 From: Nathan Sutton Date: Wed, 9 Nov 2011 11:33:51 -0800 Subject: [PATCH 2/2] Adding documentation for Fog.unmock! and Fog::Mock.reset --- docs/about/getting_started.markdown | 31 ++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/docs/about/getting_started.markdown b/docs/about/getting_started.markdown index 0e0b769cb..2335e2539 100644 --- a/docs/about/getting_started.markdown +++ b/docs/about/getting_started.markdown @@ -65,7 +65,7 @@ After that you should be able to check your directory list in fog or your filesy ## Next Steps -Using the same interface you can also practice working against a real provider (such as Amazon S3). Rather than worrying about signing up for an account right away though, we can use mocks to simulate S3 while we practice. +Using the same interface you can also practice working against a real provider (such as Amazon S3). Rather than worrying about signing up for an account right away though, we can use mocks to simulate S3 while we practice. This time we will turn on mocking and then, just like before, we will need to make a connection. @@ -76,8 +76,33 @@ This time we will turn on mocking and then, just like before, we will need to ma :provider => 'AWS' }) -You may notice that we used bogus credentials, this is fine since we are just simulating things. To use real S3 you can simply omit `Fog.mock!` and swap in your real credentials. +You may notice that we used bogus credentials, this is fine since we are just simulating things. To use real S3 you can simply omit Fog.mock! and swap in your real credentials. -Once you have your connection you can go through all the steps you did before, only now you will be working against a real cloud service (or at least a simulated one). +If you'd like to turn off mocking after turning it on, you can do it at any time and every subsequent connection will be a real connection. + + # Turn on mocking + Fog.mock! + + # Create a mock connection to S3 + storage = Fog::Storage.new({ + :aws_access_key_id => "asdf", + :aws_secret_access_key => "asdf", + :provider => "AWS" + }) + + # Turn off mocking + Fog.unmock! + + # Create a real connection to S3 + storage = Fog::Storage.new({ + :aws_access_key_id => "asdf", + :aws_secret_access_key => "asdf", + :provider => "AWS" + }) + +Don't worry about your losing mock data, it stays around until you reset it or until your process exits. + + # Reset all mock data + Fog::Mock.reset Congratulations and welcome to the cloud! Continue your journey at [fog.io](http://fog.io)