Ask Your Question
0

"" is not a Hash. It looks to be a String

asked 2016-02-04 06:38:20 -0500

karthikeayan gravatar image

updated 2016-02-09 09:14:32 -0500

I have init.pp as below:

class mymodule {

  include mymodule::users
  include mymodule::install
  include mymodule::config
  include mymodule::service

  Class[mymodule::users] -> Class[mymodule::install] -> Class[mymodule::config] -> Class[mymodule::service]

}

I have install.pp as below:

class mymodule::install (
  $artifacts = undef,
) inherits mymodule::params {

  validate_hash($artifacts)

  create_resources('different_module::artifact', $artifacts)
}

And I am creating specs for these classes as below:

init_spec.pp

require 'spec_helper'
require 'hiera'
describe 'mymodule', :type => :class do
  let(:pre_condition) { '
    include mymodule::install
    include mymodule::users
    include mymodule::service
    include mymodule::config
  ' }

  it { should contain_class('mymodule') }

end

install_spec.pp

require 'spec_helper'
require 'hiera'

describe 'mymodule::install', :type => :class do

        let(:pre_condition) {'include different_module'
        }

        let(:hiera_config) { 'spec/fixtures/hiera/hiera.yaml' }
        hiera = Hiera.new(:config => 'spec/fixtures/hiera/hiera.yaml')
        artifacts = hiera.lookup('mymodule::install::artifacts', nil, nil)
        let(:params) { { :artifacts => artifacts } }

        it { should contain_class('mymodule::install') }
        it { should contain_different_module__artifact('mymodule') }
}

spec/fixtures/hiera/hiera.yaml

---
:backends:
  - yaml
:yaml:
  :datadir: spec/fixtures/hieradata
:hierarchy:
  - common

spec/fixtures/hieradata/common.yaml

mymodule::install::artifacts:
  mymodule:
    groupid:     'au.com.org.app'
    artifactid:  'mymodule'
    version:     'r1.0'
    type:        'tgz'
    destination: '/tmp/mymodule_r1.0.tar.gz'
    timeout: '0'

When I run rspec I am getting the below error:

"" is not a Hash.  It looks to be a String at /tmp/mymodule/spec/fixtures/modules/mymodule/manifests/install.pp:6

EDIT: When I add Alex's fail statement before validate_hash all the specs have been failed. Interestingly, I can see the below, only for should contain_class, it is empty:

  1) mymodule should contain Class[mymodule]
     Failure/Error: it { should contain_class('mymodule') }

     Puppet::Error:
       I got  for artifacts at /tmp/mymodule/spec/fixtures/modules/mymodule/manifests/install.pp:6 on node debian-vm.localdomain

And for other specs I can see the value of artifacts as below:

  3) mymodule::install should contain Class[mymodule::install]
     Failure/Error: it { should contain_class('mymodule::install')  }

     Puppet::Error:
       I got {"mymodule"=>{"groupid"=>"au.com.env.app", "artifactid"=>"mymodule", "version"=>"r4", "type"=>"tgz", "destination"=>"/tmp/mymodule_r49.tar.gz", "timeout"=>"0"}} for artifacts at /tmp/mymodule_r4/spec/fixtures/modules/mymodule_r4/manifests/install.pp:6 on node debian-vm.localdomain
edit retag flag offensive close merge delete

Comments

Can you see what's different about your init_spec and the ones that are working, with respect to Hiera?

Alex Harvey gravatar imageAlex Harvey ( 2016-02-09 09:15:15 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2016-02-04 08:09:48 -0500

Firstly, do you really have spaces in your class names like

  include mymodule ::users

I think you've done some search & replace to redact class names, so could you fix that?

Secondly, could you insert a line before the validate_hash line in your manifest:

fail("I got $artifacts for artifacts")

Then run it again, and update your post with this info.

edit flag offensive delete link more

Comments

Sorry for replying late, Alex.

karthikeayan gravatar imagekarthikeayan ( 2016-02-09 03:15:56 -0500 )edit
1

I waited 5 days for you! Just kidding. :)

Alex Harvey gravatar imageAlex Harvey ( 2016-02-09 09:21:48 -0500 )edit

Thanks Alex, with your help am able to resolve it.

karthikeayan gravatar imagekarthikeayan ( 2016-02-11 07:09:27 -0500 )edit
1

answered 2016-02-11 07:08:56 -0500

karthikeayan gravatar image

updated 2016-02-11 09:19:13 -0500

Moving the let(:hiera_config) to spec_helper solves this issue.

spec_helper.rb

RSpec.configure do |c|
  c.module_path = File.join(fixture_path, 'modules')
  c.manifest_dir = File.join(fixture_path, 'manifests')
  c.hiera_config = 'spec/fixtures/hiera/hiera.yaml'
end

As hiera_config was in inside each spec rb, for init spec it is not able to find the value for artifacts.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-02-04 06:38:20 -0500

Seen: 385 times

Last updated: Feb 11 '16