rspec-terraspace 0.0.0 → 0.1.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: dbabbd26046c3eaf54725c3aa8f4855d937c37069be0dc128df91639ef096f77
4
- data.tar.gz: 139e4c4245bf1263613345a44b74f97a652f75ad04a761dcac9a235225522b13
3
+ metadata.gz: 35a489c43cf46b2b9fef6abe99450214d03c06e967c24ab2f01dac34dd101521
4
+ data.tar.gz: d5b2b8dc3df0e0c5afb2c950872518cc060b899a06b5f2323218344161a41582
5
5
  SHA512:
6
- metadata.gz: 90a4b91ba86c9fadaa11f50a2a00f3c287c59092560afcae8a4e4e7b543ecacabeccbdcfaf1c145284e4408ac951eb070b4059a39741239afdaca5ee2996c3c9
7
- data.tar.gz: d64afaf6fe488205f6e1dcc9ac72541af75af33140db8164e85f1ee42e326563c55f8015b58f86c55a703eefa6601f45a6c35679fcc15551344cbaead77b61ae
6
+ metadata.gz: 8c5a2ca7bcd5a8c303775d33662669ac206f9b2863a18e444c68dc31bbca5970ce5d52747d58d828957eea73892217fdbd43364ca2b1be2360da1da6201afe87
7
+ data.tar.gz: 4a30979a383439cda48ea216519dce5d38442a655062bd93ab2925b4a7f6cf4628547ed0f526931295fd3b60c5fbf1dc0bf7444a1385e0e5cb0b11b4a450fbfb
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
@@ -0,0 +1,7 @@
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.1.0]
7
+ - 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
+ )
@@ -5,9 +5,12 @@ 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
@@ -15,31 +18,91 @@ module RSpec::Terraspace
15
18
  def create
16
19
  clean
17
20
  build_project
21
+ build_config
18
22
  build_modules
19
23
  build_stacks
24
+ build_tfvars
25
+ build_folders
20
26
  puts "Test harness built: #{build_dir}"
21
27
  build_dir
22
28
  end
23
29
 
30
+ # folders at any-level, including top-level can be copied with the folders option
31
+ def build_folders
32
+ return unless @folders
33
+
34
+ @folders.each do |folder|
35
+ dest = "#{build_dir}/#{folder}"
36
+ FileUtils.mkdir_p(File.dirname(dest))
37
+ FileUtils.cp_r(folder, dest)
38
+ end
39
+ end
40
+
24
41
  def build_project
25
42
  parent_dir = File.dirname(build_dir)
26
43
  FileUtils.mkdir_p(parent_dir)
27
44
  Dir.chdir(parent_dir) do
28
45
  project_name = File.basename(build_dir)
29
- ::Terraspace::CLI::New::Project.start([project_name])
46
+ ::Terraspace::CLI::New::Project.start([project_name, "--no-config"])
30
47
  end
48
+ end
31
49
 
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")
50
+ def build_config
51
+ return unless @config
52
+
53
+ config_folder = "#{build_dir}/config"
54
+ FileUtils.rm_rf(config_folder) # wipe current config folder
55
+ FileUtils.mkdir_p(File.dirname(config_folder))
56
+ src = @config
57
+ FileUtils.cp_r(src, config_folder)
35
58
  end
36
59
 
37
60
  def build_modules
38
- build_app_subfolder(@modules, "modules")
61
+ return unless @modules
62
+ build_type_folder("modules", @modules)
39
63
  end
40
64
 
41
65
  def build_stacks
42
- build_app_subfolder(@stacks, "stacks")
66
+ return unless @stacks
67
+ build_type_folder("stacks", @stacks)
68
+ end
69
+
70
+ # If a file has been supplied, then it gets copied over.
71
+ #
72
+ # # File
73
+ # terraspace.build_test_harness(
74
+ # tfvars: {demo: "spec/fixtures/tfvars/demo.tfvars"},
75
+ # end
76
+ #
77
+ # # Results in:
78
+ # app/stacks/#{stack}/tfvars/test.tfvars
79
+ #
80
+ # If a directory has been supplied, then the folder fully gets copied over.
81
+ #
82
+ # # Directory
83
+ # terraspace.build_test_harness(
84
+ # tfvars: {demo: "spec/fixtures/tfvars/demo"},
85
+ # end
86
+ #
87
+ # # Results in (whatever is in the folder):
88
+ # app/stacks/#{stack}/tfvars/base.tfvars
89
+ # app/stacks/#{stack}/tfvars/test.tfvars
90
+ #
91
+ def build_tfvars
92
+ return unless @tfvars
93
+ @tfvars.each do |stack, src|
94
+ tfvars_folder = "#{build_dir}/app/stacks/#{stack}/tfvars"
95
+ FileUtils.rm_rf(tfvars_folder) # wipe current tfvars folder. dont use any of the live values
96
+
97
+ if File.directory?(src)
98
+ FileUtils.mkdir_p(File.dirname(tfvars_folder))
99
+ FileUtils.cp_r(src, tfvars_folder)
100
+ else # if only a single file, then generate a test.tfvars since this runs under TS_ENV=test
101
+ dest = "#{tfvars_folder}/test.tfvars"
102
+ FileUtils.mkdir_p(File.dirname(dest))
103
+ FileUtils.cp(src, dest)
104
+ end
105
+ end
43
106
  end
44
107
 
45
108
  # Inputs:
@@ -58,7 +121,7 @@ module RSpec::Terraspace
58
121
  # If provide a String, it should be a path to folder containing all modules or stacks.
59
122
  # This provides less fine-grain control but is easier to use and shorter.
60
123
  #
61
- def build_app_subfolder(list, type_dir)
124
+ def build_type_folder(type_dir, list)
62
125
  case list
63
126
  when Hash
64
127
  list.each do |name, src|
@@ -89,11 +152,15 @@ module RSpec::Terraspace
89
152
  end
90
153
 
91
154
  def build_dir
92
- "#{build_root}/#{@name}"
155
+ "#{tmp_root}/#{@name}"
156
+ end
157
+
158
+ def tmp_root
159
+ self.class.tmp_root
93
160
  end
94
161
 
95
- def build_root
96
- ENV['TS_RSPEC_BUILD_ROOT'] || "/tmp/terraspace/test-harnesses"
162
+ def self.tmp_root
163
+ "#{Terraspace.tmp_root}/test-harnesses"
97
164
  end
98
165
  end
99
166
  end
@@ -9,12 +9,15 @@ module RSpec::Terraspace
9
9
  def build_test_harness(options={})
10
10
  puts "Building test harness..."
11
11
  project = Project.new(options)
12
- @ts_root = project.create
13
- ENV['TS_ROOT'] = @ts_root # switch root to the generated test harness
12
+ root = project.create
13
+ Terraspace.root = root # switch root to the generated test harness
14
14
  end
15
15
 
16
16
  def up(args)
17
17
  run("up #{args} -y")
18
+ mod = args.split(' ').first
19
+ @mod = ::Terraspace::Mod.new(mod)
20
+ save_output
18
21
  end
19
22
 
20
23
  def down(args)
@@ -22,23 +25,24 @@ module RSpec::Terraspace
22
25
  end
23
26
 
24
27
  def run(command)
28
+ puts "=> terraspace #{command}".color(:green)
25
29
  args = command.split(' ')
26
30
  CLI.start(args)
27
31
  end
28
32
 
29
- def save_output(mod)
30
- run("output #{mod} --json --save-to #{saved_output_path}")
33
+ # Note: a terraspace.down will remove the output.json since it does a clean
34
+ def save_output
35
+ FileUtils.mkdir_p(File.dirname(out_path))
36
+ run("output #{@mod.name} --format json --out #{out_path}")
31
37
  end
32
- memoize :save_output
33
38
 
34
39
  def output(mod, name)
35
- save_output(mod)
36
- data = JSON.load(IO.read(saved_output_path))
40
+ data = JSON.load(IO.read(out_path))
37
41
  data.dig(name, "value")
38
42
  end
39
43
 
40
- def saved_output_path
41
- "#{@ts_root}/output.json"
44
+ def out_path
45
+ "#{Terraspace.tmp_root}/rspec/output.json"
42
46
  end
43
47
  end
44
48
  end
@@ -1,5 +1,5 @@
1
1
  module Rspec
2
2
  module Terraspace
3
- VERSION = "0.0.0"
3
+ VERSION = "0.1.0"
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,6 @@
1
+ # This is where you call your module. Example:
2
+ module "<%= name %>" {
3
+ source = "../../modules/<%= name %>"
4
+ # Input variables
5
+ # var1 = "val1"
6
+ }
@@ -0,0 +1 @@
1
+ # This is where you put your output declarations
@@ -0,0 +1,24 @@
1
+ describe "main" do
2
+ before(:all) do
3
+ mod_path = File.expand_path("../..", __dir__) # the source of the module to test is 2 levels up
4
+ # Build terraspace project to use as a test harness
5
+ # Will be located at: /tmp/terraspace/test-harnesses/<%= name %>-harness
6
+ terraspace.build_test_harness(
7
+ name: "<%= name %>-harness",
8
+ modules: {<%= name %>: mod_path},
9
+ stacks: {<%= name %>: "#{mod_path}/test/spec/fixtures/stack"}, # folder with the stack module files
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
+ # output_value = terraspace.output("<%= name %>", "some-output")
22
+ # expect(output_value).to include("some-value")
23
+ end
24
+ 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 @@
1
+ # This is where you put your tfvars
@@ -0,0 +1,28 @@
1
+ describe "main" do
2
+ before(:all) do
3
+ # Build terraspace project to use as a test harness
4
+ # Will be located at: /tmp/terraspace/test-harnesses/<%= name %>-harness
5
+ terraspace.build_test_harness(
6
+ name: "<%= name %>-harness",
7
+ modules: "app/modules", # include all modules in this folder
8
+ stacks: "app/stacks", # include all stacks in this folder
9
+ # override demo stack tfvars for testing
10
+ # copied over to test harness' app/stacks/demo/tfvars/test.tfvars
11
+ tfvars: {demo: "spec/fixtures/tfvars/demo.tfvars"},
12
+ # create config if needed. The folder will be copied over
13
+ # config: "spec/fixtures/config",
14
+ )
15
+ terraspace.up("<%= name %>") # provision real resources
16
+ end
17
+ after(:all) do
18
+ terraspace.down("<%= name %>") # destroy real resources
19
+ end
20
+
21
+ it "successful deploy" do
22
+ # Replace with your actual test
23
+ expect(true).to be true
24
+ # Example
25
+ # output_value = terraspace.output("<%= name %>", "some-output")
26
+ # expect(output_value).to include("some-value")
27
+ end
28
+ 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.1.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-05-22 00:00:00.000000000 Z
11
+ date: 2020-07-23 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,16 @@ 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/fixtures/stack/main.tf.tt
110
+ - lib/templates/module/test/spec/fixtures/stack/outputs.tf
111
+ - lib/templates/module/test/spec/main_spec.rb.tt
112
+ - lib/templates/module/test/spec/spec_helper.rb
113
+ - lib/templates/project/spec/fixtures/tfvars/%name%.tfvars
114
+ - lib/templates/project/spec/stacks/%name%/main_spec.rb.tt
105
115
  - rspec-terraspace.gemspec
106
116
  homepage: https://github.com/boltops-tools/rspec-terraspace
107
117
  licenses:
@@ -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