Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using variable_files var names in Inspec? #390

Open
MattMencel opened this issue Mar 5, 2020 · 4 comments
Open

Using variable_files var names in Inspec? #390

MattMencel opened this issue Mar 5, 2020 · 4 comments

Comments

@MattMencel
Copy link

MattMencel commented Mar 5, 2020

Is this possible? I have my driver in kitchen.yaml configured like this...

driver:
  name: terraform
  root_module_directory: test/fixtures/network
  command_timeout: 1200
  variables:
    resource_group_name: test-rg
    virtual_network_name: test_vnet
  variable_files:
    - test/fixtures/network/terraform.tfvars

For testing purposes, I've defined the resource_group_name as a variable and am sourcing other variables from the variable_files reference.

In my Inspec test file I have a control like this...

control 'network' do
  resource_group = attribute('input_resource_group_name')
  vnet = attribute('input_virtual_network_name')
  location = attribute('input_location')

  describe azurerm_virtual_network(resource_group: resource_group, name: vnet) do
    it               { should exist }
    its('location')  { should eq location }
    its('address_space') { should eq ['10.0.0.0/16'] }
  end
end

The input for resource_group and vnet works fine, but inputs from variables in the variable_files reference do not work. For example when location is in the variables file...

  ×  network: 'test_vnet' Virtual Network (1 failed)
     ✔  'test_vnet' Virtual Network is expected to exist
     ×  'test_vnet' Virtual Network location is expected to eq #<Inspec::Input::NO_VALUE_SET:0x00007fac28b13e98 @name="input_location">
     can't convert Inspec::Input::NO_VALUE_SET to Array (Inspec::Input::NO_VALUE_SET#to_ary gives Inspec::Input::NO_VALUE_SET)
'test_vnet' Virtual Network address_space is expected to eq ["10.0.0.0/16"]

Is there a way to have this work?

@anniehedgpeth
Copy link

If you're using a yaml file as a lookup to serve as a variables file, for example:

# yaml file
resource_group_name: mygroupname
location: centralus
# tf file
locals {
  settings = yamldecode(file("path/to/file.yml"))
}

resource "azurerm_resource_group" "group" {
  name     = local.settings.resource_group_name
  location = local.settings.location
}

...then this is an easy win to call from InSpec in the form of an attributes file like this call from the kitchen.yml in the systems block:

attrs:
  - path/to/file.yml

But if you're just using a regular tfvars file, then it would be cool, if maybe during the kitchen create there was a way that Ruby could dynamically create that attributes yaml file (for InSpec consumption) with erb that was reading the tfvars file of the test suite.

The only problem is that you still have to declare those attributes in the InSpec test files like so:

resource_group_name = attribute('resource_group_name')
location = attribute('location')

Idk, might be cool, though.

@anniehedgpeth
Copy link

Also, @MattMencel , this kind of does what you need it to but you just don't want to explicitly copy it out again, right?:

verifier:
  name: terraform
  systems:
    - name: a system
      backend: local
      attrs_outputs:
        an_attribute_name: an_output_name

@edwardbartholomew
Copy link
Collaborator

@MattMencel Thank you for your interest in kitchen-terraform! I asked this question recently and currently there is only support for input variables inside kitchen.yml and not variables in variable_file. There is some discussion here: https://gitter.im/kitchen-terraform/Lobby?at=5df2504dc6ce6027ebab7478)

I think Annie has an interesting idea as a potential workaround.

@MattMencel
Copy link
Author

Nice @anniehedgpeth! Using the yamldecode method with the attributes works great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants