Module: ControlSpecHelper

Defined in:
lib/control_spec_helper/control_spec_helper.rb,
lib/control_spec_helper/version.rb

Overview

rubocop:disable Style/DotPosition, Style/HashSyntax

Defined Under Namespace

Modules: Version

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#basebranchObject



10
11
12
# File 'lib/control_spec_helper/control_spec_helper.rb', line 10

def basebranch
  @basebranch ||= 'master'
end

#basepathObject



6
7
8
# File 'lib/control_spec_helper/control_spec_helper.rb', line 6

def basepath
  @basepath ||= 'site'
end

Instance Method Details

#all_roles_with_changesObject

TODO: this could be much more accurate if we compiled catalogs for all roles and then parsed them for included Classes, but that is very complicated



75
76
77
78
79
# File 'lib/control_spec_helper/control_spec_helper.rb', line 75

def all_roles_with_changes
  (diff_roles + diff_profile.map do |klass|
    roles_that_include(klass)
  end.flatten).uniq
end

#class_from_path(path) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/control_spec_helper/control_spec_helper.rb', line 54

def class_from_path(path)
  return nil unless path =~ /manifests.+\.pp$/

  (path.sub(project_root + '/', '').
    sub(/\.pp$/, '').
    split('/') - %w(site manifests)).
    join('::')
end

#debug(msg) ⇒ Object



14
15
16
# File 'lib/control_spec_helper/control_spec_helper.rb', line 14

def debug(msg)
  puts "DEBUG: #{msg}" if ENV['debug']
end

#diff_from_baseObject



38
39
40
# File 'lib/control_spec_helper/control_spec_helper.rb', line 38

def diff_from_base
  `git diff #{@basebranch} --cached --diff-filter=ACMR --name-only`.split("\n")
end

#diff_profileObject



48
49
50
51
52
# File 'lib/control_spec_helper/control_spec_helper.rb', line 48

def diff_profile
  diff_from_base.
    select { |file| file.match(%r{site/profile/manifests}) }.
    map { |path| class_from_path(path) }
end

#diff_rolesObject



42
43
44
45
46
# File 'lib/control_spec_helper/control_spec_helper.rb', line 42

def diff_roles
  diff_from_base.
    select { |file| file.match(%r{site/role/manifests}) }.
    map { |path| class_from_path(path) }
end

#profile_fixturesObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/control_spec_helper/control_spec_helper.rb', line 103

def profile_fixtures
  Dir.chdir(profile_path) do
    debug("cd to #{profile_path}")
    profile_ln = './spec/fixtures/modules/profile'

    FileUtils.mkpath './spec/fixtures/modules/'
    File.symlink(profile_path, profile_ln) unless File.exists?(profile_ln)

    Dir.glob('../../modules/*').each do |folder|
      next unless File.directory?(folder)
      old_path = File.join(File.dirname(__FILE__), folder)
      new_path = File.join("./spec/fixtures/modules/#{File.basename(folder)}")

      File.symlink(old_path, new_path) unless File.symlink?(new_path)
    end
  end
  debug "cd to #{Dir.pwd}"
end

#profile_pathObject



34
35
36
# File 'lib/control_spec_helper/control_spec_helper.rb', line 34

def profile_path
  File.join(project_root, @basepath, 'profile')
end

#project_rootObject



23
24
25
26
27
28
# File 'lib/control_spec_helper/control_spec_helper.rb', line 23

def project_root
  return @root if @root
  @root = `git rev-parse --show-toplevel`.chomp
  debug("project_root = #{@root}")
  @root
end

#puppet_cmdObject



18
19
20
21
# File 'lib/control_spec_helper/control_spec_helper.rb', line 18

def puppet_cmd
  'puppet apply manifests/site.pp \
    --modulepath $(echo `pwd`/modules:`pwd`/site) --hiera_config hiera.yaml'
end

#r10kObject



94
95
96
97
98
99
100
101
# File 'lib/control_spec_helper/control_spec_helper.rb', line 94

def r10k
  Dir.chdir(project_root) do
    debug("cd to #{project_root}")
    puts 'Installing modules with r10k'
    `r10k puppetfile install`
  end
  debug "cd to #{Dir.pwd}"
end

#role_pathObject



30
31
32
# File 'lib/control_spec_helper/control_spec_helper.rb', line 30

def role_path
  File.join(project_root, @basepath, 'role')
end

#roles_that_include(klass) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/control_spec_helper/control_spec_helper.rb', line 63

def roles_that_include(klass)
  Dir.chdir(role_path) do
    debug("cd to #{role_path}")
    `git grep -l #{klass}`.split("\n").
      map { |path| class_from_path(File.join(role_path, path)) }.
      compact
  end
  debug "cd to #{Dir.pwd}"
end

#spec_cleanObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/control_spec_helper/control_spec_helper.rb', line 122

def spec_clean
  Dir.chdir(project_root) do
    debug("cd to #{project_root}")
    fixtures = File.join(profile_path, 'spec', 'fixtures', 'modules')
    modules  = File.join(project_root, 'modules')

    abort if fixtures == '' || !fixtures
    abort if modules == '' || !modules

    `rm -rf #{fixtures}/*`
    `rm -rf #{modules}/*`
  end
  debug "cd to #{Dir.pwd}"
end

#spec_from_class(klass) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/control_spec_helper/control_spec_helper.rb', line 81

def spec_from_class(klass)
  test = if klass =~ /profile/
           { :path => 'profile', :type => nil }
         elsif klass =~ /role/
           { :path => 'role', :type => 'acceptance' }
         else
           fail ArgumentError
         end

  path = [project_root, @basepath, test[:path], 'spec', test[:type]].compact
  File.join(path << (klass.split('::') - [test[:path]])) + '_spec.rb'
end