rspec-terraspace 0.2.1 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e8e1b5f17a60183d80868b81010f3afa769afa017639895276a4b1afd0ebe9cf
4
- data.tar.gz: 44feb0924a0d0a02e1c8e0c6d25bffcdbcdbea47e2b0ffb4acfc34db8e0200de
3
+ metadata.gz: 86b718772c65d61edfd892aab57116ec4414f6302162d59c7369090a2864cd50
4
+ data.tar.gz: d27b219c170bd11a122d71ca07f6d06b3f10a3056524f23a4a9a55104b3c8b48
5
5
  SHA512:
6
- metadata.gz: e2172c1a7f4c5655094a42fa5ce0d36489677c7b92618dcdda9b97318a2dbb1ad3173f54b0d73373edd08cb55223b1bd078b0edf0fc274bcd8128f37e097ba5b
7
- data.tar.gz: 92597f1143ddcc9e4202b240ca089a2d5357665fcc66e038270f6cad0bdc47fdae85b81942e404772e877af6910f1de4fd857083f04391d4d3c003279777ba7e
6
+ metadata.gz: 7cf1300e159a1bd5be2c0ad59236e889b426a83f955829be294b504981290e3da22a5af83835f8bc5b6dafb780e8350835b4acda49a3aa6f349d50a92b1d8002
7
+ data.tar.gz: cd0486c714961d6e4d34e577e2c60b313dcae8112e59affead2a1228588eaf517d5137acbe2cbae9952ded6c9624e0c925fc4f1c221f681ea7c3a91cb5989893
data/CHANGELOG.md CHANGED
@@ -3,6 +3,23 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project *tries* to adhere to [Semantic Versioning](http://semver.org/).
5
5
 
6
+ ## [0.3.1] - 2021-12-29
7
+ - [#8](https://github.com/boltops-tools/rspec-terraspace/pull/8) provider autodetection
8
+
9
+ ## [0.3.0] - 2021-07-26
10
+ - [#5](https://github.com/boltops-tools/rspec-terraspace/pull/5) Improvements
11
+ - detect module or stack type so module specs can be written
12
+ - better organize internal ts helper methods to concerns
13
+ - improve config folder copy so it overwrites existing files instead of blowing away folder
14
+ - improve starter boilerplate templates
15
+ - clean up reconfigure logging and auto call it
16
+
17
+ ## [0.2.3] - 2021-05-31
18
+ - [#4](https://github.com/boltops-tools/rspec-terraspace/pull/4) fix module test generator
19
+
20
+ ## [0.2.2] - 2020-11-17
21
+ - fix outputs helper and templates
22
+
6
23
  ## [0.2.1] - 2020-11-16
7
24
  - [#3](https://github.com/boltops-tools/rspec-terraspace/pull/3) add config terraform provider template for azure test
8
25
 
data/README.md CHANGED
@@ -21,7 +21,7 @@ So if you set it: `export TS_RSPEC_BUILD_ROOT=~/environment/terraspace-test-harn
21
21
 
22
22
  The test helpers support both module-level and project-level tests. See:
23
23
 
24
- * [Terraspace Testing](https://github.com/boltops-tools/terraspace-docs/blob/master/testing.md)
24
+ * [Terraspace Testing](https://terraspace.cloud/docs/testing/)
25
25
 
26
26
  ## Installation
27
27
 
@@ -0,0 +1,50 @@
1
+ module RSpec::Terraspace
2
+ module Concern
3
+ extend Memoist
4
+
5
+ def output(mod, name)
6
+ outputs.dig(name, "value")
7
+ end
8
+
9
+ def outputs
10
+ save_output
11
+ JSON.load(IO.read(out_path))
12
+ end
13
+
14
+ # Note: a terraspace.down will remove the output.json since it does a clean
15
+ def save_output
16
+ FileUtils.mkdir_p(File.dirname(out_path))
17
+ run("output #{@mod.name} --format json --out #{out_path}")
18
+ end
19
+ memoize :save_output
20
+
21
+ def out_path
22
+ "#{Terraspace.tmp_root}/rspec/terraform-output.json"
23
+ end
24
+
25
+ def state
26
+ save_state
27
+ JSON.load(IO.read(state_path))
28
+ end
29
+
30
+ # full_name: random_pet.this
31
+ def state_resource(full_name)
32
+ type, name = full_name.split('.')
33
+ state['resources'].find do |i|
34
+ i['type'] == type && i['name'] == name || # IE: type=random_pet name=this
35
+ i['module'] == full_name # IE: module.bucket
36
+ end
37
+ end
38
+
39
+ def save_state
40
+ FileUtils.mkdir_p(File.dirname(state_path))
41
+ run("state pull #{@mod.name} > #{state_path}")
42
+ end
43
+ memoize :save_state
44
+
45
+ def state_path
46
+ "#{Terraspace.tmp_root}/rspec/terraform-state.json"
47
+ end
48
+
49
+ end
50
+ end
@@ -2,13 +2,6 @@ module RSpec::Terraspace
2
2
  module Helpers
3
3
  extend Memoist
4
4
 
5
- def reconfigure_logging(level="info")
6
- path = "/tmp/terraspace/log/test.log"
7
- FileUtils.mkdir_p(File.dirname(path))
8
- Terraspace.logger = Terraspace::Logger.new(path)
9
- puts "Terraspace.logger has been reconfigured to #{path}"
10
- end
11
-
12
5
  def ts
13
6
  Ts.new
14
7
  end
@@ -11,6 +11,7 @@ module RSpec::Terraspace
11
11
  @stacks = options[:stacks]
12
12
  @tfvars = options[:tfvars]
13
13
  @folders = options[:folders]
14
+ @plugin = options[:plugin]
14
15
 
15
16
  @remove_test_folder = options[:remove_test_folder].nil? ? true : options[:remove_test_folder]
16
17
  end
@@ -44,7 +45,29 @@ module RSpec::Terraspace
44
45
  FileUtils.mkdir_p(parent_dir)
45
46
  Dir.chdir(parent_dir) do
46
47
  project_name = File.basename(build_dir)
47
- ::Terraspace::CLI::New::Project.start([project_name, "--no-config", "--quiet"])
48
+ args = [project_name, "--no-config", "--quiet"] + plugin_option
49
+ ::Terraspace::CLI::New::Project.start(args)
50
+ end
51
+ end
52
+
53
+ def plugin_option
54
+ if @plugin
55
+ ["-p", @plugin]
56
+ else
57
+ provider = autodetect_provider || "none"
58
+ ["-p", provider]
59
+ end
60
+ end
61
+
62
+ def autodetect_provider
63
+ providers = Terraspace::Plugin.meta.keys
64
+ if providers.size == 1
65
+ providers.first
66
+ else
67
+ precedence = %w[aws azurerm google]
68
+ precedence.find do |p|
69
+ providers.include?(p)
70
+ end
48
71
  end
49
72
  end
50
73
 
@@ -52,10 +75,10 @@ module RSpec::Terraspace
52
75
  return unless @config
53
76
 
54
77
  config_folder = "#{build_dir}/config"
55
- FileUtils.rm_rf(config_folder) # wipe current config folder
56
78
  FileUtils.mkdir_p(File.dirname(config_folder))
57
- src = @config
58
- FileUtils.cp_r(src, config_folder)
79
+ Dir.glob("#{@config}/*").each do |src|
80
+ FileUtils.cp_r(src, config_folder)
81
+ end
59
82
  end
60
83
 
61
84
  def build_modules
@@ -92,7 +115,8 @@ module RSpec::Terraspace
92
115
  def build_tfvars
93
116
  return unless @tfvars
94
117
  @tfvars.each do |stack, src|
95
- tfvars_folder = "#{build_dir}/app/stacks/#{stack}/tfvars"
118
+ type = detected_type
119
+ tfvars_folder = "#{build_dir}/app/#{type}/#{stack}/tfvars"
96
120
  FileUtils.rm_rf(tfvars_folder) # wipe current tfvars folder. dont use any of the live values
97
121
 
98
122
  if File.directory?(src)
@@ -106,6 +130,13 @@ module RSpec::Terraspace
106
130
  end
107
131
  end
108
132
 
133
+ # Returns: modules or stacks
134
+ def detected_type
135
+ dir = Dir.pwd
136
+ md = dir.match(%r{app/(stacks|modules)/(.*)?/})
137
+ md[1]
138
+ end
139
+
109
140
  # Inputs:
110
141
  #
111
142
  # list: options[:modules] or options[:stacks]
@@ -3,20 +3,29 @@ require "json"
3
3
  module RSpec::Terraspace
4
4
  class Ts
5
5
  extend Memoist
6
+ include Concern
6
7
 
7
8
  CLI = ::Terraspace::CLI
8
9
 
9
10
  def build_test_harness(options={})
11
+ setup
10
12
  project = Project.new(options)
11
13
  root = project.create
12
14
  Terraspace.root = root # switch root to the generated test harness
13
15
  end
14
16
 
17
+ def setup
18
+ # Require gems in Gemfile so terraspace_plugin_* gets loaded and registered
19
+ # This it test Gemfile. IE: app/stacks/demo/test/Gemfile
20
+ Kernel.require "bundler/setup"
21
+ Bundler.require # Same as Bundler.require(:default)
22
+ Terraspace.check_project = false
23
+ end
24
+
15
25
  def up(args)
16
26
  run("up #{args} -y")
17
27
  mod = args.split(' ').first
18
28
  @mod = ::Terraspace::Mod.new(mod)
19
- save_output
20
29
  end
21
30
 
22
31
  def down(args)
@@ -24,27 +33,9 @@ module RSpec::Terraspace
24
33
  end
25
34
 
26
35
  def run(command)
27
- puts "=> terraspace #{command}".color(:green)
36
+ puts "=> TS_ENV=#{Terraspace.env} terraspace #{command}".color(:green)
28
37
  args = command.split(' ')
29
38
  CLI.start(args)
30
39
  end
31
-
32
- # Note: a terraspace.down will remove the output.json since it does a clean
33
- def save_output
34
- FileUtils.mkdir_p(File.dirname(out_path))
35
- run("output #{@mod.name} --format json --out #{out_path}")
36
- end
37
-
38
- def output(mod, name)
39
- outputs.dig(name, "value")
40
- end
41
-
42
- def outputs(mod)
43
- JSON.load(IO.read(out_path))
44
- end
45
-
46
- def out_path
47
- "#{Terraspace.tmp_root}/rspec/output.json"
48
- end
49
40
  end
50
41
  end
@@ -1,5 +1,5 @@
1
1
  module Rspec
2
2
  module Terraspace
3
- VERSION = "0.2.1"
3
+ VERSION = "0.3.1"
4
4
  end
5
5
  end
@@ -1,5 +1,6 @@
1
1
  require "rspec/terraspace/version"
2
2
 
3
+ require "active_support"
3
4
  require "active_support/core_ext/class"
4
5
  require "active_support/core_ext/hash"
5
6
  require "active_support/core_ext/string"
@@ -1,13 +1,14 @@
1
1
  describe "main" do
2
2
  before(:all) do
3
- reconfigure_logging # reconfigure Terraspace.logger to a file
4
3
  mod_path = File.expand_path("../..", __dir__) # the source of the module to test is 2 levels up
5
4
  # Build terraspace project to use as a test harness
6
5
  # Will be located at: /tmp/terraspace/test-harnesses/<%= name %>-harness
7
6
  terraspace.build_test_harness(
8
7
  name: "<%= name %>-harness",
9
8
  modules: {<%= name %>: mod_path},
10
- stacks: {<%= name %>: "#{mod_path}/test/spec/fixtures/stack"}, # folder with the stack module files
9
+ # See: https://terraspace.cloud/docs/testing/test-harness/
10
+ # config: "spec/fixtures/config",
11
+ # tfvars: {example: "spec/fixtures/tfvars/test.tfvars"},
11
12
  )
12
13
  terraspace.up("<%= name %>")
13
14
  end
@@ -19,8 +20,8 @@ describe "main" do
19
20
  # Replace with your own test
20
21
  expect(true).to be true
21
22
  # Example
22
- # pp terraspace.outputs("<%= name %>")
23
- # output_value = terraspace.output("<%= name %>", "some-output")
23
+ # pp terraspace.outputs
24
+ # output_value = terraspace.output("<%= name %>", "name")
24
25
  # expect(output_value).to include("some-value")
25
26
  end
26
27
  end
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper
@@ -0,0 +1,24 @@
1
+ ENV["TS_ENV"] = "test"
2
+
3
+ require "terraspace"
4
+ require "rspec/terraspace"
5
+
6
+ module Helper
7
+ def execute(cmd)
8
+ puts "Running: #{cmd}" if ENV['SHOW_COMMAND']
9
+ out = `#{cmd}`
10
+ puts out if ENV['SHOW_COMMAND']
11
+ out
12
+ end
13
+ end
14
+
15
+ RSpec.configure do |c|
16
+ c.before(:all) do
17
+ Dir.glob("config/helpers/**/*.rb").each do |path|
18
+ require "./#{path}"
19
+ name = path.sub(%r{config/helpers/},'').sub('.rb','').camelize
20
+ mod = "Terraspace::Project::#{name}"
21
+ c.include mod.constantize
22
+ end
23
+ end
24
+ end
@@ -1,6 +1,5 @@
1
1
  describe "main" do
2
2
  before(:all) do
3
- reconfigure_logging # reconfigure Terraspace.logger to a file
4
3
  stack_path = File.expand_path("../..", __dir__) # the source of the stack to test is 2 levels up
5
4
  ts_root = File.expand_path("../../..", stack_path) # original Terraspace.root
6
5
  # Build terraspace project to use as a test harness
@@ -10,8 +9,8 @@ describe "main" do
10
9
  modules: "#{ts_root}/app/modules", # include all modules in folder
11
10
  stacks: {<%= name %>: stack_path},
12
11
  # override demo stack tfvars for testing
13
- tfvars: {demo: "#{stack_path}/test/spec/fixtures/tfvars/demo.tfvars"},
14
12
  config: "#{stack_path}/test/spec/fixtures/config",
13
+ # tfvars: {demo: "#{stack_path}/test/spec/fixtures/tfvars/test.tfvars"},
15
14
  )
16
15
  terraspace.up("<%= name %>")
17
16
  end
@@ -23,8 +22,12 @@ describe "main" do
23
22
  # Replace with your own test
24
23
  expect(true).to be true
25
24
  # Example
26
- # pp terraspace.outputs("<%= name %>")
27
- # output_value = terraspace.output("<%= name %>", "some-output")
28
- # expect(output_value).to include("some-value")
25
+ # pp terraspace.outputs
26
+ output_value = terraspace.output("<%= name %>", "bucket_name")
27
+ puts "output_value #{output_value}"
28
+ # More useful helpers:
29
+ # pp terraspace.state['resources']
30
+ # pp terraspace.state_resource('random_pet.this')
31
+ # pp terraspace.state_resource('module.bucket')
29
32
  end
30
33
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-terraspace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-16 00:00:00.000000000 Z
11
+ date: 2021-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -98,6 +98,7 @@ files:
98
98
  - bin/setup
99
99
  - lib/rspec/terraspace.rb
100
100
  - lib/rspec/terraspace/autoloader.rb
101
+ - lib/rspec/terraspace/concern.rb
101
102
  - lib/rspec/terraspace/helpers.rb
102
103
  - lib/rspec/terraspace/project.rb
103
104
  - lib/rspec/terraspace/ts.rb
@@ -106,16 +107,16 @@ files:
106
107
  - lib/templates/bootstrap/spec/spec_helper.rb
107
108
  - lib/templates/module/test/.rspec
108
109
  - lib/templates/module/test/Gemfile.tt
109
- - lib/templates/module/test/spec/fixtures/stack/main.tf.tt
110
- - lib/templates/module/test/spec/fixtures/stack/outputs.tf
111
110
  - lib/templates/module/test/spec/main_spec.rb.tt
112
111
  - lib/templates/module/test/spec/spec_helper.rb
112
+ - lib/templates/project/.rspec
113
113
  - lib/templates/project/spec/%test_name%_spec.rb.tt
114
+ - lib/templates/project/spec/spec_helper.rb
114
115
  - lib/templates/stack/test/.rspec
115
116
  - lib/templates/stack/test/Gemfile.tt
116
117
  - lib/templates/stack/test/spec/fixtures/config/app.rb
117
118
  - lib/templates/stack/test/spec/fixtures/config/terraform/provider.tf.tt
118
- - lib/templates/stack/test/spec/fixtures/tfvars/%name%.tfvars
119
+ - lib/templates/stack/test/spec/fixtures/tfvars/test.tfvars
119
120
  - lib/templates/stack/test/spec/main_spec.rb.tt
120
121
  - lib/templates/stack/test/spec/spec_helper.rb
121
122
  - rspec-terraspace.gemspec
@@ -138,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
139
  - !ruby/object:Gem::Version
139
140
  version: '0'
140
141
  requirements: []
141
- rubygems_version: 3.1.4
142
+ rubygems_version: 3.2.32
142
143
  signing_key:
143
144
  specification_version: 4
144
145
  summary: Terraspace RSpec support
@@ -1,6 +0,0 @@
1
- # This is where you call your module. Example:
2
- module "<%= name %>" {
3
- source = "../../modules/<%= name %>"
4
- # Input variables
5
- # var1 = "val1"
6
- }
@@ -1 +0,0 @@
1
- # This is where you put your output declarations