rspec-terraspace 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 54ad8cf08fa4c6c27cae1858bf26c3519e3687a6c5c2e29a142ccea6ffce66f9
4
- data.tar.gz: ad84fe683514e979be0a0b528ced6c93998553183f39aec960133b143fa69620
3
+ metadata.gz: c09973fe7ff5551af56434b0cdc5bc97194e4695fcdd061c3c7cfaebbd7d468f
4
+ data.tar.gz: eadb2ce4620283dfd666afff0993aee8769217c6a36848ed8ca98bf1d3db8d27
5
5
  SHA512:
6
- metadata.gz: 1615ec5212492fafc800243e546c0755f99b77ac86ac92f911f2d277ebd613b18f7dafe5d6edebdd5181031ad8fb4639d4127186454fa56eb871684b1aba3a64
7
- data.tar.gz: 4201be145491631a431a65e5916a9ac92928ec584911d269552a6fff815fc18abe314b6a5d816afd14b69f9b53f875970173cc207c76fe4242a99c410b345634
6
+ metadata.gz: 63571680195d695c14d2bf3633f165f904cadc3145e3f802610ecc6c33386fe57f99a4acb5c9234c05d27b6870137192eb37c759612ff83e27d1dc6eb1202958
7
+ data.tar.gz: c524ce214a25bc5d9a54be71a74b1b30130433ed79c8b63f7d716da420c7e161a900ea8b2a18e7b96d5d50084cedcf9cc0cc4619e852cec137329a1c0ced6f07
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.0] - 2021-07-26
7
+ - [#5](https://github.com/boltops-tools/rspec-terraspace/pull/5) Improvements
8
+ - detect module or stack type so module specs can be written
9
+ - better organize internal ts helper methods to concerns
10
+ - improve config folder copy so it overwrites existing files instead of blowing away folder
11
+ - improve starter boilerplate templates
12
+ - clean up reconfigure logging and auto call it
13
+
14
+ ## [0.2.3] - 2021-05-31
15
+ - [#4](https://github.com/boltops-tools/rspec-terraspace/pull/4) fix module test generator
16
+
17
+ ## [0.2.2] - 2020-11-17
18
+ - fix outputs helper and templates
19
+
20
+ ## [0.2.1] - 2020-11-16
21
+ - [#3](https://github.com/boltops-tools/rspec-terraspace/pull/3) add config terraform provider template for azure test
22
+
6
23
  ## [0.2.0] - 2020-11-15
7
24
  - [#2](https://github.com/boltops-tools/rspec-terraspace/pull/2) stack test like module test template, reconfigure logger
8
25
  - stack test like module test template, reconfigure logger
@@ -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
@@ -52,10 +52,10 @@ module RSpec::Terraspace
52
52
  return unless @config
53
53
 
54
54
  config_folder = "#{build_dir}/config"
55
- FileUtils.rm_rf(config_folder) # wipe current config folder
56
55
  FileUtils.mkdir_p(File.dirname(config_folder))
57
- src = @config
58
- FileUtils.cp_r(src, config_folder)
56
+ Dir.glob("#{@config}/*").each do |src|
57
+ FileUtils.cp_r(src, config_folder)
58
+ end
59
59
  end
60
60
 
61
61
  def build_modules
@@ -92,7 +92,8 @@ module RSpec::Terraspace
92
92
  def build_tfvars
93
93
  return unless @tfvars
94
94
  @tfvars.each do |stack, src|
95
- tfvars_folder = "#{build_dir}/app/stacks/#{stack}/tfvars"
95
+ type = detected_type
96
+ tfvars_folder = "#{build_dir}/app/#{type}/#{stack}/tfvars"
96
97
  FileUtils.rm_rf(tfvars_folder) # wipe current tfvars folder. dont use any of the live values
97
98
 
98
99
  if File.directory?(src)
@@ -106,6 +107,13 @@ module RSpec::Terraspace
106
107
  end
107
108
  end
108
109
 
110
+ # Returns: modules or stacks
111
+ def detected_type
112
+ dir = Dir.pwd
113
+ md = dir.match(%r{app/(stacks|modules)/(.*)?/})
114
+ md[1]
115
+ end
116
+
109
117
  # Inputs:
110
118
  #
111
119
  # list: options[:modules] or options[:stacks]
@@ -3,6 +3,7 @@ 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
 
@@ -16,7 +17,6 @@ module RSpec::Terraspace
16
17
  run("up #{args} -y")
17
18
  mod = args.split(' ').first
18
19
  @mod = ::Terraspace::Mod.new(mod)
19
- save_output
20
20
  end
21
21
 
22
22
  def down(args)
@@ -24,27 +24,9 @@ module RSpec::Terraspace
24
24
  end
25
25
 
26
26
  def run(command)
27
- puts "=> terraspace #{command}".color(:green)
27
+ puts "=> TS_ENV=#{Terraspace.env} terraspace #{command}".color(:green)
28
28
  args = command.split(' ')
29
29
  CLI.start(args)
30
30
  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
31
  end
50
32
  end
@@ -1,5 +1,5 @@
1
1
  module Rspec
2
2
  module Terraspace
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -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
@@ -0,0 +1,4 @@
1
+ Terraspace.configure do |config|
2
+ config.logger.level = :info
3
+ config.test_framework = "rspec"
4
+ end
@@ -0,0 +1,5 @@
1
+ <% if autodetect_provider == "azurerm" -%>
2
+ provider "azurerm" {
3
+ features {} # required
4
+ }
5
+ <% 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,9 +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
- # create config if needed. The folder will be copied over
15
- # config: "#{stack_path}/spec/fixtures/config",
12
+ config: "#{stack_path}/test/spec/fixtures/config",
13
+ # tfvars: {demo: "#{stack_path}/test/spec/fixtures/tfvars/test.tfvars"},
16
14
  )
17
15
  terraspace.up("<%= name %>")
18
16
  end
@@ -24,8 +22,12 @@ describe "main" do
24
22
  # Replace with your own test
25
23
  expect(true).to be true
26
24
  # Example
27
- # pp terraspace.outputs("<%= name %>")
28
- # output_value = terraspace.output("<%= name %>", "some-output")
29
- # 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')
30
32
  end
31
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.0
4
+ version: 0.3.0
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-15 00:00:00.000000000 Z
11
+ date: 2021-07-26 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,14 +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
- - lib/templates/stack/test/spec/fixtures/tfvars/%name%.tfvars
117
+ - lib/templates/stack/test/spec/fixtures/config/app.rb
118
+ - lib/templates/stack/test/spec/fixtures/config/terraform/provider.tf.tt
119
+ - lib/templates/stack/test/spec/fixtures/tfvars/test.tfvars
117
120
  - lib/templates/stack/test/spec/main_spec.rb.tt
118
121
  - lib/templates/stack/test/spec/spec_helper.rb
119
122
  - rspec-terraspace.gemspec
@@ -136,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
139
  - !ruby/object:Gem::Version
137
140
  version: '0'
138
141
  requirements: []
139
- rubygems_version: 3.1.4
142
+ rubygems_version: 3.2.5
140
143
  signing_key:
141
144
  specification_version: 4
142
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