rspec-terraspace 0.2.3 → 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 +4 -4
- data/CHANGELOG.md +8 -0
- data/lib/rspec/terraspace/concern.rb +50 -0
- data/lib/rspec/terraspace/helpers.rb +0 -7
- data/lib/rspec/terraspace/project.rb +12 -4
- data/lib/rspec/terraspace/ts.rb +2 -20
- data/lib/rspec/terraspace/version.rb +1 -1
- data/lib/templates/module/test/spec/main_spec.rb.tt +4 -2
- data/lib/templates/project/.rspec +3 -0
- data/lib/templates/project/spec/spec_helper.rb +24 -0
- data/lib/templates/stack/test/spec/fixtures/tfvars/{%name%.tfvars → test.tfvars} +0 -0
- data/lib/templates/stack/test/spec/main_spec.rb.tt +7 -4
- metadata +6 -3
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: c09973fe7ff5551af56434b0cdc5bc97194e4695fcdd061c3c7cfaebbd7d468f
         | 
| 4 | 
            +
              data.tar.gz: eadb2ce4620283dfd666afff0993aee8769217c6a36848ed8ca98bf1d3db8d27
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 63571680195d695c14d2bf3633f165f904cadc3145e3f802610ecc6c33386fe57f99a4acb5c9234c05d27b6870137192eb37c759612ff83e27d1dc6eb1202958
         | 
| 7 | 
            +
              data.tar.gz: c524ce214a25bc5d9a54be71a74b1b30130433ed79c8b63f7d716da420c7e161a900ea8b2a18e7b96d5d50084cedcf9cc0cc4619e852cec137329a1c0ced6f07
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    | @@ -3,6 +3,14 @@ | |
| 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 | 
            +
             | 
| 6 14 | 
             
            ## [0.2.3] - 2021-05-31
         | 
| 7 15 | 
             
            - [#4](https://github.com/boltops-tools/rspec-terraspace/pull/4) fix module test generator
         | 
| 8 16 |  | 
| @@ -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 | 
            -
                   | 
| 58 | 
            -
             | 
| 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 | 
            -
                     | 
| 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]
         | 
    
        data/lib/rspec/terraspace/ts.rb
    CHANGED
    
    | @@ -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
         | 
| 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,12 +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},
         | 
| 9 | 
            +
                  # See: https://terraspace.cloud/docs/testing/test-harness/
         | 
| 10 | 
            +
                  # config: "spec/fixtures/config",
         | 
| 11 | 
            +
                  # tfvars: {example: "spec/fixtures/tfvars/test.tfvars"},
         | 
| 10 12 | 
             
                )
         | 
| 11 13 | 
             
                terraspace.up("<%= name %>")
         | 
| 12 14 | 
             
              end
         | 
| @@ -19,7 +21,7 @@ describe "main" do | |
| 19 21 | 
             
                expect(true).to be true
         | 
| 20 22 | 
             
                # Example
         | 
| 21 23 | 
             
                # pp terraspace.outputs
         | 
| 22 | 
            -
                # output_value = terraspace.output("<%= name %>", " | 
| 24 | 
            +
                # output_value = terraspace.output("<%= name %>", "name")
         | 
| 23 25 | 
             
                # expect(output_value).to include("some-value")
         | 
| 24 26 | 
             
              end
         | 
| 25 27 | 
             
            end
         | 
| @@ -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
         | 
| 
            File without changes
         | 
| @@ -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
         | 
| @@ -24,7 +23,11 @@ describe "main" do | |
| 24 23 | 
             
                expect(true).to be true
         | 
| 25 24 | 
             
                # Example
         | 
| 26 25 | 
             
                # pp terraspace.outputs
         | 
| 27 | 
            -
                 | 
| 28 | 
            -
                 | 
| 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. | 
| 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: 2021- | 
| 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
         | 
| @@ -108,12 +109,14 @@ files: | |
| 108 109 | 
             
            - lib/templates/module/test/Gemfile.tt
         | 
| 109 110 | 
             
            - lib/templates/module/test/spec/main_spec.rb.tt
         | 
| 110 111 | 
             
            - lib/templates/module/test/spec/spec_helper.rb
         | 
| 112 | 
            +
            - lib/templates/project/.rspec
         | 
| 111 113 | 
             
            - lib/templates/project/spec/%test_name%_spec.rb.tt
         | 
| 114 | 
            +
            - lib/templates/project/spec/spec_helper.rb
         | 
| 112 115 | 
             
            - lib/templates/stack/test/.rspec
         | 
| 113 116 | 
             
            - lib/templates/stack/test/Gemfile.tt
         | 
| 114 117 | 
             
            - lib/templates/stack/test/spec/fixtures/config/app.rb
         | 
| 115 118 | 
             
            - lib/templates/stack/test/spec/fixtures/config/terraform/provider.tf.tt
         | 
| 116 | 
            -
            - lib/templates/stack/test/spec/fixtures/tfvars | 
| 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
         |