Skip to content

Commit

Permalink
Ansible test refactor (preparing for custom tests) (#2274)
Browse files Browse the repository at this point in the history
Merged PR #2274.
  • Loading branch information
rambleraptor authored and modular-magician committed Sep 10, 2019
1 parent e705535 commit 37b9255
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 23 deletions.
2 changes: 1 addition & 1 deletion build/ansible
10 changes: 6 additions & 4 deletions overrides/ansible/resource_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,8 +40,6 @@ def self.attributes
access_api_results
collection
custom_code
custom_tests
has_tests
hidden
imports
notes
Expand All @@ -49,6 +48,8 @@ def self.attributes
transport
unwrap_resource

tests

facts
]
end
Expand All @@ -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
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions products/cloudbuild/ansible.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion products/compute/ansible.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion products/iam/ansible.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)) -%>
17 changes: 12 additions & 5 deletions products/kms/ansible.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,39 @@ 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:
- products/kms/helpers/python/provider_key_ring.py
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:
- products/kms/helpers/python/provider_key_ring.py
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: |
Immutable purpose of CryptoKey. See
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)) -%>
3 changes: 2 additions & 1 deletion products/storage/ansible.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 14 additions & 8 deletions provider/ansible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
49 changes: 49 additions & 0 deletions provider/ansible/tests.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions templates/ansible/tests_main.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
<%= ansible_style_yaml(object.tests.tests.map { |t| { "include_tasks" => "#{t.name}.yml" } }) -%>

0 comments on commit 37b9255

Please sign in to comment.