From d0722e696b3ce3be8d54a21125b58ba9b164645a Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 6 Jun 2018 11:33:26 +1000 Subject: [PATCH 1/4] Add basic collection unit tests This should help us test new model/collection contributions and help keep our collections the same across the board. --- tasks/test.rake | 12 +++++++++++- test/unit/compute/test_collections.rb | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/unit/compute/test_collections.rb diff --git a/tasks/test.rake b/tasks/test.rake index 3f7079cf92..806e4c33e4 100644 --- a/tasks/test.rake +++ b/tasks/test.rake @@ -1,7 +1,7 @@ require "rake/testtask" Rake::TestTask.new do |t| - t.description = "Run all integration tests" + t.description = "Run integration and unit tests" t.libs << "test" t.pattern = File.join("test", "**", "test_*.rb") t.warning = false @@ -19,6 +19,16 @@ namespace :test do "test:pubsub", "test:sql", "test:storage"] + + Rake::TestTask.new do |t| + t.name = "unit" + t.description = "Run Unit tests" + t.libs << "test" + t.pattern = FileList['test/unit/**/test_*.rb'] + t.warning = false + t.verbose = true + end + Rake::TestTask.new do |t| t.name = "compute" t.description = "Run Compute API tests" diff --git a/test/unit/compute/test_collections.rb b/test/unit/compute/test_collections.rb new file mode 100644 index 0000000000..f78327e921 --- /dev/null +++ b/test/unit/compute/test_collections.rb @@ -0,0 +1,23 @@ +require "helpers/test_helper" + +class UnitTestCollections < MiniTest::Test + + def setup + Fog.mock! + @client = Fog::Compute.new(:provider => "Google") + common_ancestors = [Fog::Collection, Fog::Association, Fog::PagedCollection] + descendants = ObjectSpace.each_object(Fog::Collection.singleton_class).to_a + + @collections = descendants - common_ancestors + end + + def test_common_methods + # This tests whether Fog::Compute::Google collections have common lifecycle methods + @collections.each do |klass| + obj = klass.new + assert obj.respond_to?(:all), "#{klass} should respond to .all" + assert obj.respond_to?(:get), "#{klass} should respond to .get" + assert obj.respond_to?(:each), "#{klass} should be enumerable" + end + end +end \ No newline at end of file From e07b2007bc7a5f7ef5ecfe8c513a7ef5735a445c Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 6 Jun 2018 11:33:38 +1000 Subject: [PATCH 2/4] Set travis to run unit tests --- tasks/test.rake | 2 +- test/unit/compute/test_collections.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tasks/test.rake b/tasks/test.rake index 806e4c33e4..cb8a77c39a 100644 --- a/tasks/test.rake +++ b/tasks/test.rake @@ -10,7 +10,7 @@ end namespace :test do mock = ENV["FOG_MOCK"] || "true" task :travis do - sh("export FOG_MOCK=#{mock} && bundle exec shindont") + sh("bundle exec rake test:unit") end desc "Run all integration tests in parallel" diff --git a/test/unit/compute/test_collections.rb b/test/unit/compute/test_collections.rb index f78327e921..c54857090f 100644 --- a/test/unit/compute/test_collections.rb +++ b/test/unit/compute/test_collections.rb @@ -1,7 +1,6 @@ require "helpers/test_helper" class UnitTestCollections < MiniTest::Test - def setup Fog.mock! @client = Fog::Compute.new(:provider => "Google") @@ -20,4 +19,5 @@ def test_common_methods assert obj.respond_to?(:each), "#{klass} should be enumerable" end end -end \ No newline at end of file + +end From a75ba640aee03d8665d211e60979545d3ab757af Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 6 Jun 2018 11:46:25 +1000 Subject: [PATCH 3/4] Add parameters to client mock to work without .fog --- test/unit/compute/test_collections.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/unit/compute/test_collections.rb b/test/unit/compute/test_collections.rb index c54857090f..4f37639da7 100644 --- a/test/unit/compute/test_collections.rb +++ b/test/unit/compute/test_collections.rb @@ -3,7 +3,7 @@ class UnitTestCollections < MiniTest::Test def setup Fog.mock! - @client = Fog::Compute.new(:provider => "Google") + @client = Fog::Compute.new(:provider => "Google", :google_project => "foo") common_ancestors = [Fog::Collection, Fog::Association, Fog::PagedCollection] descendants = ObjectSpace.each_object(Fog::Collection.singleton_class).to_a @@ -19,5 +19,4 @@ def test_common_methods assert obj.respond_to?(:each), "#{klass} should be enumerable" end end - end From ac2ad90af88c3923ede1c9b0b1819796b4573959 Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 6 Jun 2018 19:14:39 +1000 Subject: [PATCH 4/4] Add Model unit tests + light cleanup --- ...lections.rb => test_common_collections.rb} | 17 ++++++--- test/unit/compute/test_common_models.rb | 36 +++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) rename test/unit/compute/{test_collections.rb => test_common_collections.rb} (50%) create mode 100644 test/unit/compute/test_common_models.rb diff --git a/test/unit/compute/test_collections.rb b/test/unit/compute/test_common_collections.rb similarity index 50% rename from test/unit/compute/test_collections.rb rename to test/unit/compute/test_common_collections.rb index 4f37639da7..a07e9e08b6 100644 --- a/test/unit/compute/test_collections.rb +++ b/test/unit/compute/test_common_collections.rb @@ -4,19 +4,28 @@ class UnitTestCollections < MiniTest::Test def setup Fog.mock! @client = Fog::Compute.new(:provider => "Google", :google_project => "foo") + + # Top-level ancestors we do not dest common_ancestors = [Fog::Collection, Fog::Association, Fog::PagedCollection] + # Projects do not have a "list" method in compute API + exceptions = [Fog::Compute::Google::Projects] + # Enumerate all descendants of Fog::Collection descendants = ObjectSpace.each_object(Fog::Collection.singleton_class).to_a - @collections = descendants - common_ancestors + @collections = descendants - common_ancestors - exceptions + end + + def teardown + Fog.unmock! end def test_common_methods # This tests whether Fog::Compute::Google collections have common lifecycle methods @collections.each do |klass| obj = klass.new - assert obj.respond_to?(:all), "#{klass} should respond to .all" - assert obj.respond_to?(:get), "#{klass} should respond to .get" - assert obj.respond_to?(:each), "#{klass} should be enumerable" + assert obj.respond_to?(:all), "#{klass} should have an .all method" + assert obj.respond_to?(:get), "#{klass} should have a .get method" + assert obj.respond_to?(:each), "#{klass} should behave like Enumerable" end end end diff --git a/test/unit/compute/test_common_models.rb b/test/unit/compute/test_common_models.rb new file mode 100644 index 0000000000..d830538475 --- /dev/null +++ b/test/unit/compute/test_common_models.rb @@ -0,0 +1,36 @@ +require "helpers/test_helper" + +class UnitTestModels < MiniTest::Test + def setup + Fog.mock! + @client = Fog::Compute.new(:provider => "Google", :google_project => "foo") + + # Top-level ancestors we do not test + common_ancestors = [Fog::Model, Fog::Compute::Server] + # Do not test models that do not have a create method in API + exceptions = [ Fog::Compute::Google::MachineType, + Fog::Compute::Google::Region, + Fog::Compute::Google::DiskType, + Fog::Compute::Google::Operation, + Fog::Compute::Google::Zone, + Fog::Compute::Google::Snapshot, + Fog::Compute::Google::Project ] + # Enumerate all descendants of Fog::Model + descendants = ObjectSpace.each_object(Fog::Model.singleton_class).to_a + + @models = descendants - common_ancestors - exceptions + end + + def teardown + Fog.unmock! + end + + def test_common_methods + # This tests whether Fog::Compute::Google models have common lifecycle methods + @models.each do |klass| + obj = klass.new + assert obj.respond_to?(:save), "#{klass} should have a .save method" + assert obj.respond_to?(:destroy), "#{klass} should have a .destroy method" + end + end +end