rspec-terraspace 0.0.0 → 0.2.3

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: dbabbd26046c3eaf54725c3aa8f4855d937c37069be0dc128df91639ef096f77
4
- data.tar.gz: 139e4c4245bf1263613345a44b74f97a652f75ad04a761dcac9a235225522b13
3
+ metadata.gz: 42c4017e3ddd5cd3cfd9cdf4d6e59582ecaadbe6f1cf34df7d2c56c8180a9e8f
4
+ data.tar.gz: f9fba8a8ffa4d70071a04f487fadf519d174dff1665572d6dbf86652048d7eca
5
5
  SHA512:
6
- metadata.gz: 90a4b91ba86c9fadaa11f50a2a00f3c287c59092560afcae8a4e4e7b543ecacabeccbdcfaf1c145284e4408ac951eb070b4059a39741239afdaca5ee2996c3c9
7
- data.tar.gz: d64afaf6fe488205f6e1dcc9ac72541af75af33140db8164e85f1ee42e326563c55f8015b58f86c55a703eefa6601f45a6c35679fcc15551344cbaead77b61ae
6
+ metadata.gz: e8296689a8b23250cd37e2e9823b761a3529063adddffc45d7a76f685769702eafbb4d38d6df1911565c2c6a4a457b297bc8417d1de273afa67382a321df6c7d
7
+ data.tar.gz: 1e835b72584ba6a459555645b5791f8f3f63782c48d01639caf3487c31129da149463ae69f3c3f352bafe27f054c0c5e2795126390e5bbd3dc92f05ee9c89da7
data/.gitignore CHANGED
@@ -6,6 +6,7 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /Gemfile.lock
9
10
 
10
11
  # rspec failure tracking
11
12
  .rspec_status
data/CHANGELOG.md ADDED
@@ -0,0 +1,20 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ This project *tries* to adhere to [Semantic Versioning](http://semver.org/).
5
+
6
+ ## [0.2.3] - 2021-05-31
7
+ - [#4](https://github.com/boltops-tools/rspec-terraspace/pull/4) fix module test generator
8
+
9
+ ## [0.2.2] - 2020-11-17
10
+ - fix outputs helper and templates
11
+
12
+ ## [0.2.1] - 2020-11-16
13
+ - [#3](https://github.com/boltops-tools/rspec-terraspace/pull/3) add config terraform provider template for azure test
14
+
15
+ ## [0.2.0] - 2020-11-15
16
+ - [#2](https://github.com/boltops-tools/rspec-terraspace/pull/2) stack test like module test template, reconfigure logger
17
+ - stack test like module test template, reconfigure logger
18
+
19
+ ## [0.1.0]
20
+ - Initial release
data/Gemfile CHANGED
@@ -5,3 +5,7 @@ gemspec
5
5
 
6
6
  gem "rake", "~> 12.0"
7
7
  gem "rspec", "~> 3.0"
8
+
9
+ group :development, :test do
10
+ gem "terraspace"
11
+ end
@@ -12,6 +12,11 @@ RSpec::Terraspace::Autoloader.setup
12
12
  module Rspec
13
13
  module Terraspace
14
14
  class Error < StandardError; end
15
- # Your code goes here...
16
15
  end
17
16
  end
17
+
18
+ require "terraspace"
19
+
20
+ Terraspace::Tester.register("rspec",
21
+ root: File.expand_path("../..", __dir__)
22
+ )
@@ -2,6 +2,13 @@ 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
+
5
12
  def ts
6
13
  Ts.new
7
14
  end
@@ -5,41 +5,105 @@ module RSpec::Terraspace
5
5
  class Project
6
6
  def initialize(options={})
7
7
  @options = options
8
- @name = options[:name] || "demo"
8
+ @name = options[:name]
9
+ @config = options[:config]
9
10
  @modules = options[:modules]
10
11
  @stacks = options[:stacks]
12
+ @tfvars = options[:tfvars]
13
+ @folders = options[:folders]
11
14
 
12
15
  @remove_test_folder = options[:remove_test_folder].nil? ? true : options[:remove_test_folder]
13
16
  end
14
17
 
15
18
  def create
19
+ puts "Building test harness at: #{build_dir}"
16
20
  clean
17
21
  build_project
22
+ build_config
18
23
  build_modules
19
24
  build_stacks
20
- puts "Test harness built: #{build_dir}"
25
+ build_tfvars
26
+ build_folders
27
+ puts "Test harness built."
21
28
  build_dir
22
29
  end
23
30
 
31
+ # folders at any-level, including top-level can be copied with the folders option
32
+ def build_folders
33
+ return unless @folders
34
+
35
+ @folders.each do |folder|
36
+ dest = "#{build_dir}/#{folder}"
37
+ FileUtils.mkdir_p(File.dirname(dest))
38
+ FileUtils.cp_r(folder, dest)
39
+ end
40
+ end
41
+
24
42
  def build_project
25
43
  parent_dir = File.dirname(build_dir)
26
44
  FileUtils.mkdir_p(parent_dir)
27
45
  Dir.chdir(parent_dir) do
28
46
  project_name = File.basename(build_dir)
29
- ::Terraspace::CLI::New::Project.start([project_name])
47
+ ::Terraspace::CLI::New::Project.start([project_name, "--no-config", "--quiet"])
30
48
  end
49
+ end
31
50
 
32
- # TODO: terraspace new project --no-config option instead
33
- FileUtils.rm_f("#{build_dir}/config/backend.tf")
34
- FileUtils.rm_f("#{build_dir}/config/provider.tf")
51
+ def build_config
52
+ return unless @config
53
+
54
+ config_folder = "#{build_dir}/config"
55
+ FileUtils.rm_rf(config_folder) # wipe current config folder
56
+ FileUtils.mkdir_p(File.dirname(config_folder))
57
+ src = @config
58
+ FileUtils.cp_r(src, config_folder)
35
59
  end
36
60
 
37
61
  def build_modules
38
- build_app_subfolder(@modules, "modules")
62
+ return unless @modules
63
+ build_type_folder("modules", @modules)
39
64
  end
40
65
 
41
66
  def build_stacks
42
- build_app_subfolder(@stacks, "stacks")
67
+ return unless @stacks
68
+ build_type_folder("stacks", @stacks)
69
+ end
70
+
71
+ # If a file has been supplied, then it gets copied over.
72
+ #
73
+ # # File
74
+ # terraspace.build_test_harness(
75
+ # tfvars: {demo: "spec/fixtures/tfvars/demo.tfvars"},
76
+ # end
77
+ #
78
+ # # Results in:
79
+ # app/stacks/#{stack}/tfvars/test.tfvars
80
+ #
81
+ # If a directory has been supplied, then the folder fully gets copied over.
82
+ #
83
+ # # Directory
84
+ # terraspace.build_test_harness(
85
+ # tfvars: {demo: "spec/fixtures/tfvars/demo"},
86
+ # end
87
+ #
88
+ # # Results in (whatever is in the folder):
89
+ # app/stacks/#{stack}/tfvars/base.tfvars
90
+ # app/stacks/#{stack}/tfvars/test.tfvars
91
+ #
92
+ def build_tfvars
93
+ return unless @tfvars
94
+ @tfvars.each do |stack, src|
95
+ tfvars_folder = "#{build_dir}/app/stacks/#{stack}/tfvars"
96
+ FileUtils.rm_rf(tfvars_folder) # wipe current tfvars folder. dont use any of the live values
97
+
98
+ if File.directory?(src)
99
+ FileUtils.mkdir_p(File.dirname(tfvars_folder))
100
+ FileUtils.cp_r(src, tfvars_folder)
101
+ else # if only a single file, then generate a test.tfvars since this runs under TS_ENV=test
102
+ dest = "#{tfvars_folder}/test.tfvars"
103
+ FileUtils.mkdir_p(File.dirname(dest))
104
+ FileUtils.cp(src, dest)
105
+ end
106
+ end
43
107
  end
44
108
 
45
109
  # Inputs:
@@ -58,7 +122,7 @@ module RSpec::Terraspace
58
122
  # If provide a String, it should be a path to folder containing all modules or stacks.
59
123
  # This provides less fine-grain control but is easier to use and shorter.
60
124
  #
61
- def build_app_subfolder(list, type_dir)
125
+ def build_type_folder(type_dir, list)
62
126
  case list
63
127
  when Hash
64
128
  list.each do |name, src|
@@ -89,11 +153,15 @@ module RSpec::Terraspace
89
153
  end
90
154
 
91
155
  def build_dir
92
- "#{build_root}/#{@name}"
156
+ "#{tmp_root}/#{@name}"
157
+ end
158
+
159
+ def tmp_root
160
+ self.class.tmp_root
93
161
  end
94
162
 
95
- def build_root
96
- ENV['TS_RSPEC_BUILD_ROOT'] || "/tmp/terraspace/test-harnesses"
163
+ def self.tmp_root
164
+ "#{Terraspace.tmp_root}/test-harnesses"
97
165
  end
98
166
  end
99
167
  end
@@ -7,14 +7,16 @@ module RSpec::Terraspace
7
7
  CLI = ::Terraspace::CLI
8
8
 
9
9
  def build_test_harness(options={})
10
- puts "Building test harness..."
11
10
  project = Project.new(options)
12
- @ts_root = project.create
13
- ENV['TS_ROOT'] = @ts_root # switch root to the generated test harness
11
+ root = project.create
12
+ Terraspace.root = root # switch root to the generated test harness
14
13
  end
15
14
 
16
15
  def up(args)
17
16
  run("up #{args} -y")
17
+ mod = args.split(' ').first
18
+ @mod = ::Terraspace::Mod.new(mod)
19
+ save_output
18
20
  end
19
21
 
20
22
  def down(args)
@@ -22,23 +24,27 @@ module RSpec::Terraspace
22
24
  end
23
25
 
24
26
  def run(command)
27
+ puts "=> terraspace #{command}".color(:green)
25
28
  args = command.split(' ')
26
29
  CLI.start(args)
27
30
  end
28
31
 
29
- def save_output(mod)
30
- run("output #{mod} --json --save-to #{saved_output_path}")
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}")
31
36
  end
32
- memoize :save_output
33
37
 
34
38
  def output(mod, name)
35
- save_output(mod)
36
- data = JSON.load(IO.read(saved_output_path))
37
- data.dig(name, "value")
39
+ outputs.dig(name, "value")
38
40
  end
39
41
 
40
- def saved_output_path
41
- "#{@ts_root}/output.json"
42
+ def outputs
43
+ JSON.load(IO.read(out_path))
44
+ end
45
+
46
+ def out_path
47
+ "#{Terraspace.tmp_root}/rspec/output.json"
42
48
  end
43
49
  end
44
50
  end
@@ -1,5 +1,5 @@
1
1
  module Rspec
2
2
  module Terraspace
3
- VERSION = "0.0.0"
3
+ VERSION = "0.2.3"
4
4
  end
5
5
  end
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper
@@ -0,0 +1,13 @@
1
+ ENV["TS_ENV"] = "test"
2
+
3
+ require "terraspace"
4
+ require "rspec/terraspace"
5
+
6
+ module Helper
7
+ # Add your helpers here
8
+ end
9
+
10
+ RSpec.configure do |c|
11
+ c.include RSpec::Terraspace::Helpers
12
+ c.include Helper
13
+ end
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ <%=
4
+ build_gemfile(
5
+ "terraspace",
6
+ "rspec-terraspace",
7
+ plugin_gem_name,
8
+ )
9
+ %>
@@ -0,0 +1,25 @@
1
+ describe "main" do
2
+ before(:all) do
3
+ reconfigure_logging # reconfigure Terraspace.logger to a file
4
+ mod_path = File.expand_path("../..", __dir__) # the source of the module to test is 2 levels up
5
+ # Build terraspace project to use as a test harness
6
+ # Will be located at: /tmp/terraspace/test-harnesses/<%= name %>-harness
7
+ terraspace.build_test_harness(
8
+ name: "<%= name %>-harness",
9
+ modules: {<%= name %>: mod_path},
10
+ )
11
+ terraspace.up("<%= name %>")
12
+ end
13
+ after(:all) do
14
+ terraspace.down("<%= name %>")
15
+ end
16
+
17
+ it "successful deploy" do
18
+ # Replace with your own test
19
+ expect(true).to be true
20
+ # Example
21
+ # pp terraspace.outputs
22
+ # output_value = terraspace.output("<%= name %>", "some-output")
23
+ # expect(output_value).to include("some-value")
24
+ end
25
+ end
@@ -0,0 +1,18 @@
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.include Helper
17
+ c.include RSpec::Terraspace::Helpers
18
+ end
@@ -0,0 +1,5 @@
1
+ describe "<%= test_name %>" do
2
+ it "the truth" do
3
+ expect(true).to be true
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ <%=
4
+ build_gemfile(
5
+ "terraspace",
6
+ "rspec-terraspace",
7
+ plugin_gem_name,
8
+ )
9
+ %>
@@ -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 -%>
@@ -0,0 +1 @@
1
+ # This is where you put your tfvars
@@ -0,0 +1,30 @@
1
+ describe "main" do
2
+ before(:all) do
3
+ reconfigure_logging # reconfigure Terraspace.logger to a file
4
+ stack_path = File.expand_path("../..", __dir__) # the source of the stack to test is 2 levels up
5
+ ts_root = File.expand_path("../../..", stack_path) # original Terraspace.root
6
+ # Build terraspace project to use as a test harness
7
+ # Will be located at: /tmp/terraspace/test-harnesses/<%= name %>-harness
8
+ terraspace.build_test_harness(
9
+ name: "<%= name %>-harness",
10
+ modules: "#{ts_root}/app/modules", # include all modules in folder
11
+ stacks: {<%= name %>: stack_path},
12
+ # override demo stack tfvars for testing
13
+ tfvars: {demo: "#{stack_path}/test/spec/fixtures/tfvars/demo.tfvars"},
14
+ config: "#{stack_path}/test/spec/fixtures/config",
15
+ )
16
+ terraspace.up("<%= name %>")
17
+ end
18
+ after(:all) do
19
+ terraspace.down("<%= name %>")
20
+ end
21
+
22
+ it "successful deploy" do
23
+ # Replace with your own test
24
+ expect(true).to be true
25
+ # Example
26
+ # pp terraspace.outputs
27
+ # output_value = terraspace.output("<%= name %>", "some-output")
28
+ # expect(output_value).to include("some-value")
29
+ end
30
+ end
@@ -0,0 +1,18 @@
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.include Helper
17
+ c.include RSpec::Terraspace::Helpers
18
+ 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.0.0
4
+ version: 0.2.3
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-05-22 00:00:00.000000000 Z
11
+ date: 2021-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -89,8 +89,8 @@ extra_rdoc_files: []
89
89
  files:
90
90
  - ".gitignore"
91
91
  - ".rspec"
92
+ - CHANGELOG.md
92
93
  - Gemfile
93
- - Gemfile.lock
94
94
  - LICENSE.txt
95
95
  - README.md
96
96
  - Rakefile
@@ -102,6 +102,20 @@ files:
102
102
  - lib/rspec/terraspace/project.rb
103
103
  - lib/rspec/terraspace/ts.rb
104
104
  - lib/rspec/terraspace/version.rb
105
+ - lib/templates/bootstrap/.rspec
106
+ - lib/templates/bootstrap/spec/spec_helper.rb
107
+ - lib/templates/module/test/.rspec
108
+ - lib/templates/module/test/Gemfile.tt
109
+ - lib/templates/module/test/spec/main_spec.rb.tt
110
+ - lib/templates/module/test/spec/spec_helper.rb
111
+ - lib/templates/project/spec/%test_name%_spec.rb.tt
112
+ - lib/templates/stack/test/.rspec
113
+ - lib/templates/stack/test/Gemfile.tt
114
+ - lib/templates/stack/test/spec/fixtures/config/app.rb
115
+ - lib/templates/stack/test/spec/fixtures/config/terraform/provider.tf.tt
116
+ - lib/templates/stack/test/spec/fixtures/tfvars/%name%.tfvars
117
+ - lib/templates/stack/test/spec/main_spec.rb.tt
118
+ - lib/templates/stack/test/spec/spec_helper.rb
105
119
  - rspec-terraspace.gemspec
106
120
  homepage: https://github.com/boltops-tools/rspec-terraspace
107
121
  licenses:
@@ -122,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
136
  - !ruby/object:Gem::Version
123
137
  version: '0'
124
138
  requirements: []
125
- rubygems_version: 3.1.2
139
+ rubygems_version: 3.2.5
126
140
  signing_key:
127
141
  specification_version: 4
128
142
  summary: Terraspace RSpec support
data/Gemfile.lock DELETED
@@ -1,54 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- rspec-terraspace (0.1.0)
5
- activesupport
6
- memoist
7
- rainbow
8
- zeitwerk
9
-
10
- GEM
11
- remote: https://rubygems.org/
12
- specs:
13
- activesupport (6.0.3.1)
14
- concurrent-ruby (~> 1.0, >= 1.0.2)
15
- i18n (>= 0.7, < 2)
16
- minitest (~> 5.1)
17
- tzinfo (~> 1.1)
18
- zeitwerk (~> 2.2, >= 2.2.2)
19
- concurrent-ruby (1.1.6)
20
- diff-lcs (1.3)
21
- i18n (1.8.2)
22
- concurrent-ruby (~> 1.0)
23
- memoist (0.16.2)
24
- minitest (5.14.1)
25
- rainbow (3.0.0)
26
- rake (12.3.3)
27
- rspec (3.9.0)
28
- rspec-core (~> 3.9.0)
29
- rspec-expectations (~> 3.9.0)
30
- rspec-mocks (~> 3.9.0)
31
- rspec-core (3.9.2)
32
- rspec-support (~> 3.9.3)
33
- rspec-expectations (3.9.2)
34
- diff-lcs (>= 1.2.0, < 2.0)
35
- rspec-support (~> 3.9.0)
36
- rspec-mocks (3.9.1)
37
- diff-lcs (>= 1.2.0, < 2.0)
38
- rspec-support (~> 3.9.0)
39
- rspec-support (3.9.3)
40
- thread_safe (0.3.6)
41
- tzinfo (1.2.7)
42
- thread_safe (~> 0.1)
43
- zeitwerk (2.3.0)
44
-
45
- PLATFORMS
46
- ruby
47
-
48
- DEPENDENCIES
49
- rake (~> 12.0)
50
- rspec (~> 3.0)
51
- rspec-terraspace!
52
-
53
- BUNDLED WITH
54
- 2.1.4