diff --git a/build/ansible b/build/ansible index 11fb11c0615d..6d6a995be4be 160000 --- a/build/ansible +++ b/build/ansible @@ -1 +1 @@ -Subproject commit 11fb11c0615de6cb25f72304754acf5547cf1448 +Subproject commit 6d6a995be4be0b7f7851728732de450a90d1f07b diff --git a/overrides/ansible/resource_override.rb b/overrides/ansible/resource_override.rb index 7291fa97f602..83abf5f473dd 100644 --- a/overrides/ansible/resource_override.rb +++ b/overrides/ansible/resource_override.rb @@ -14,6 +14,7 @@ require 'overrides/resources' require 'provider/ansible/facts_override' require 'provider/ansible/custom_code' +require 'provider/ansible/tests' module Overrides module Ansible @@ -39,8 +40,6 @@ def self.attributes access_api_results collection custom_code - custom_tests - has_tests hidden imports notes @@ -49,6 +48,8 @@ def self.attributes transport unwrap_resource + tests + facts ] end @@ -64,8 +65,6 @@ def validate check :collection, type: ::String check :custom_code, type: Provider::Ansible::CustomCode, default: Provider::Ansible::CustomCode.new - check :custom_tests, type: :boolean, default: false - check :has_tests, type: :boolean, default: true check :hidden, type: ::Array, item_type: String, default: [] check :imports, type: ::Array, default: [], item_type: String check :notes, type: ::Array, item_type: String @@ -75,6 +74,9 @@ def validate check :update, type: ::String check :unwrap_resource, type: :boolean, default: false + check :tests, type: Provider::Ansible::Tests, + default: Provider::Ansible::Tests.new + check :facts, type: Provider::Ansible::FactsOverride, default: Provider::Ansible::FactsOverride.new end diff --git a/products/cloudbuild/ansible.yaml b/products/cloudbuild/ansible.yaml index e26faef66453..58dbf67e410e 100644 --- a/products/cloudbuild/ansible.yaml +++ b/products/cloudbuild/ansible.yaml @@ -23,8 +23,9 @@ overrides: !ruby/object:Overrides::ResourceOverrides - The id for this resource is created by the API after you create the resource the first time. If you want to manage this resource after creation, you'll have to copy the generated id into the playbook. If you do not, new triggers will be created on subsequent runs. - # TODO(alexstephen): Create tests for resources without names. - has_tests: false + tests: !ruby/object:Provider::Ansible::Tests + # TODO(alexstephen): Create tests for resources without names. + has_autogenerated_test: false properties: id: !ruby/object:Overrides::Ansible::PropertyOverride output: false diff --git a/products/compute/ansible.yaml b/products/compute/ansible.yaml index 7e10b008773f..68ccfdde65d7 100644 --- a/products/compute/ansible.yaml +++ b/products/compute/ansible.yaml @@ -237,7 +237,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides - 'products/compute/helpers/python/provider_instance_template.py' - 'products/compute/helpers/python/instance_metadata.py' InterconnectAttachment: !ruby/object:Overrides::Ansible::ResourceOverride - has_tests: false + tests: !ruby/object:Provider::Ansible::Tests + has_autogenerated_test: false RegionDisk: !ruby/object:Overrides::Ansible::ResourceOverride properties: sourceSnapshot: !ruby/object:Overrides::Ansible::PropertyOverride diff --git a/products/iam/ansible.yaml b/products/iam/ansible.yaml index 29aef06c3c59..a1d07ac47268 100644 --- a/products/iam/ansible.yaml +++ b/products/iam/ansible.yaml @@ -40,7 +40,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides transport: !ruby/object:Overrides::Ansible::Transport decoder: decode_response template: 'products/iam/helpers/ansible/service_account_key_template.erb' - has_tests: false + tests: !ruby/object:Provider::Ansible::Tests + has_autogenerated_test: false files: !ruby/object:Provider::Config::Files resource: <%= lines(indent(compile('provider/ansible/resource~compile.yaml'), 4)) -%> diff --git a/products/kms/ansible.yaml b/products/kms/ansible.yaml index 859c21f551fb..aa974ba78b6b 100644 --- a/products/kms/ansible.yaml +++ b/products/kms/ansible.yaml @@ -27,7 +27,6 @@ datasources: !ruby/object:Overrides::ResourceOverrides for inputs. overrides: !ruby/object:Overrides::ResourceOverrides KeyRing: !ruby/object:Overrides::Ansible::ResourceOverride - custom_tests: true transport: !ruby/object:Overrides::Ansible::Transport decoder: decode_response provider_helpers: @@ -35,8 +34,13 @@ overrides: !ruby/object:Overrides::ResourceOverrides custom_code: !ruby/object:Provider::Ansible::CustomCode delete: | module.fail_json(msg="KeyRings cannot be deleted") + tests: !ruby/object:Provider::Ansible::Tests + has_autogenerated_test: false + tests: + - !ruby/object:Provider::Ansible::Test + name: 'autogen' + path: products/kms/helpers/python/key_ring_test.yaml CryptoKey: !ruby/object:Overrides::Ansible::ResourceOverride - custom_tests: true transport: !ruby/object:Overrides::Ansible::Transport decoder: decode_response provider_helpers: @@ -44,6 +48,12 @@ overrides: !ruby/object:Overrides::ResourceOverrides custom_code: !ruby/object:Provider::Ansible::CustomCode delete: | module.fail_json(msg="KeyRings cannot be deleted") + tests: !ruby/object:Provider::Ansible::Tests + has_autogenerated_test: false + tests: + - !ruby/object:Provider::Ansible::Test + name: 'autogen' + path: products/kms/helpers/python/crypto_key_test.yaml properties: purpose: !ruby/object:Overrides::Ansible::PropertyOverride description: | @@ -51,8 +61,5 @@ overrides: !ruby/object:Overrides::ResourceOverrides https://cloud.google.com/kms/docs/reference/rest/v1/projects.locations.keyRings.cryptoKeys#CryptoKeyPurpose for inputs. files: !ruby/object:Provider::Config::Files - copy: - 'tests/integration/targets/gcp_kms_key_ring/tasks/main.yml': products/kms/helpers/python/key_ring_test.yaml - 'tests/integration/targets/gcp_kms_crypto_key/tasks/main.yml': products/kms/helpers/python/crypto_key_test.yaml resource: <%= lines(indent(compile('provider/ansible/resource~compile.yaml'), 4)) -%> diff --git a/products/storage/ansible.yaml b/products/storage/ansible.yaml index 9b1280c7a03a..7e5bb62f28d9 100644 --- a/products/storage/ansible.yaml +++ b/products/storage/ansible.yaml @@ -27,7 +27,8 @@ datasources: !ruby/object:Overrides::ResourceOverrides overrides: !ruby/object:Overrides::ResourceOverrides Object: !ruby/object:Overrides::Ansible::ResourceOverride template: 'products/storage/helpers/ansible/object_template.erb' - has_tests: false + tests: !ruby/object:Provider::Ansible::Tests + has_autogenerated_test: false ObjectAccessControl: !ruby/object:Overrides::Ansible::ResourceOverride exclude: true DefaultObjectACL: !ruby/object:Overrides::Ansible::ResourceOverride diff --git a/provider/ansible.rb b/provider/ansible.rb index 77d43fc44d65..2d8959bbc4f7 100644 --- a/provider/ansible.rb +++ b/provider/ansible.rb @@ -246,21 +246,27 @@ def generate_resource_tests(data) path = ["products/#{data.product.api_name}", "examples/ansible/#{prod_name}.yaml"].join('/') - return unless data.object.has_tests - # Unlike other providers, all resources will not be built at once or - # in close timing to each other (due to external PRs). - # This means that examples might not be built out for every resource - # in a GCP product. - return unless File.file?(path) + return if data.object.tests.tests.empty? target_folder = data.output_folder name = module_name(data.object) + + # Generate the main file with a list of tests. path = File.join(target_folder, "tests/integration/targets/#{name}/tasks/main.yml") - unless data.object.custom_tests + data.generate( + 'templates/ansible/tests_main.erb', + path, + self + ) + + # Generate each of the tests individually + data.object.tests.tests.each do |t| + path = File.join(target_folder, + "tests/integration/targets/#{name}/tasks/#{t.name}.yml") data.generate( - 'templates/ansible/integration_test.erb', + t.path, path, self ) diff --git a/provider/ansible/tests.rb b/provider/ansible/tests.rb new file mode 100644 index 000000000000..a8480c43c8fd --- /dev/null +++ b/provider/ansible/tests.rb @@ -0,0 +1,49 @@ +# Copyright 2017 Google Inc. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +module Provider + module Ansible + # Contains all information about Ansible tests. + class Tests < Api::Object + attr_reader :has_autogenerated_test + attr_reader :tests + def validate + check :has_autogenerated_test, type: :boolean, default: true + check :tests, type: Array, item_type: Provider::Ansible::Test, default: [] + + return unless @has_autogenerated_test + + tests << Test.new('autogen', 'templates/ansible/integration_test.erb') + end + end + + # Contains information about an individual Ansible test. + class Test < Api:: Object + # The filename of the Ansible test playbook (without extension) + attr_reader :name + + # The path of the test file / test template in MM. + attr_reader :path + + def initialize(name, path) + @name = name + @path = path + end + + def validate + check :name, required: true, type: String + check :path, required: true, type: String + end + end + end +end diff --git a/templates/ansible/tests_main.erb b/templates/ansible/tests_main.erb new file mode 100644 index 000000000000..ff68656c6333 --- /dev/null +++ b/templates/ansible/tests_main.erb @@ -0,0 +1,2 @@ +--- +<%= ansible_style_yaml(object.tests.tests.map { |t| { "include_tasks" => "#{t.name}.yml" } }) -%>