Module: Linecook::Test

Extended by:
ModuleMethods
Includes:
ShellTest
Defined in:
lib/linecook/test.rb

Defined Under Namespace

Modules: ClassMethods, ModuleMethods

Constant Summary collapse

LINECOOK_DIR =
File.expand_path('../../..', __FILE__)
LINECOOK =
File.join(LINECOOK_DIR, 'bin/linecook')

Instance Method Summary collapse

Methods included from ModuleMethods

included

Instance Method Details

#assert_recipe(expected, recipe = setup_recipe, &block) ⇒ Object



113
114
115
116
117
# File 'lib/linecook/test.rb', line 113

def assert_recipe(expected, recipe=setup_recipe, &block)
  recipe.instance_eval(&block) if block_given?
  assert_str_equal expected, recipe.to_s
  recipe
end

#assert_recipe_matches(expected, recipe = setup_recipe, &block) ⇒ Object



119
120
121
122
123
# File 'lib/linecook/test.rb', line 119

def assert_recipe_matches(expected, recipe=setup_recipe, &block)
  recipe.instance_eval(&block) if block_given?
  assert_str_match expected, recipe.to_s
  recipe
end

#build_project(options = {}) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/linecook/test.rb', line 137

def build_project(options={})
  options = {
    'L' => 'helpers',
    'C' => method_dir,
    'i' => path('packages'),
    'o' => path('packages'),
    :max_run_time => 3
  }.merge(options)

  Dir.chdir method_dir do
    linecook('build', options, *glob('recipes/*.rb'))
  end
end

#cookbookObject



59
60
61
# File 'lib/linecook/test.rb', line 59

def cookbook
  @cookbook ||= setup_cookbook
end

#export_package(host = self.host) ⇒ Object



125
126
127
128
129
130
# File 'lib/linecook/test.rb', line 125

def export_package(host=self.host)
  resources.each {|resource| resource.register_to(package) }
  package_dir = path("packages/#{host}")
  package.export package_dir
  package_dir
end

#helpersObject



75
76
77
# File 'lib/linecook/test.rb', line 75

def helpers
  @helpers ||= []
end

#hostObject



83
84
85
# File 'lib/linecook/test.rb', line 83

def host
  @host ||= self.class.host
end

#linecook(cmd, options = {}, *args, &block) ⇒ Object



181
182
183
184
185
186
187
188
189
190
# File 'lib/linecook/test.rb', line 181

def linecook(cmd, options={}, *args, &block)
  command = linecook_cmd(cmd, options, *args)
  session = Session.new(options)
  session.on :PS1, "#{command}\n"
  session.on :PS1, "exit\n"
  session.run

  log = session.log
  [log[2].chomp(session.ps1), log.join]
end

#linecook_cmd(cmd, options = {}, *args) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/linecook/test.rb', line 192

def linecook_cmd(cmd, options={}, *args)
  opts = []
  options.each_pair do |key, value|
    next unless key.kind_of?(String)

    key = key.gsub('_', '-')
    key = key.length == 1 ? "-#{key}" : "--#{key}"

    case value
    when true
      opts << key
    when nil, false
    else
      opts << "#{key} '#{value}'"
    end
  end

  args = args.collect! {|arg| "'#{arg}'" }

  cmd = [LINECOOK, cmd] + opts.sort + args
  cmd.join(' ')
end

#method_dirObject



36
37
38
39
40
41
42
43
44
# File 'lib/linecook/test.rb', line 36

def method_dir
  @host_method_dir ||= begin
    if test_host = ENV['LINECOOK_TEST_HOST']
      File.join(super, test_host)
    else
      super
    end
  end
end

#packageObject



67
68
69
# File 'lib/linecook/test.rb', line 67

def package
  @package ||= setup_package
end

#recipeObject



87
88
89
# File 'lib/linecook/test.rb', line 87

def recipe
  @recipe ||= setup_recipe
end

#recipesObject



91
92
93
# File 'lib/linecook/test.rb', line 91

def recipes
  @recipes ||= []
end

#remote_dirObject



46
47
48
# File 'lib/linecook/test.rb', line 46

def remote_dir
  method_dir[(user_dir.length + 1)..-1]
end

#resourcesObject



95
96
97
# File 'lib/linecook/test.rb', line 95

def resources
  recipes
end

#run_package(options = {}, host = self.host, &block) ⇒ Object



132
133
134
135
# File 'lib/linecook/test.rb', line 132

def run_package(options={}, host=self.host, &block)
  options['S'] ||= runlist
  run_project options, export_package(host), &block
end

#run_project(options = {}, *package_dirs, &block) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/linecook/test.rb', line 151

def run_project(options={}, *package_dirs, &block)
  if package_dirs.empty?
    package_dirs = glob('packages/*').select {|path| File.directory?(path) }
  end

  options = {
    'F' => ssh_config_file,
    'D' => remote_dir,
    'q' => true,
    :max_run_time => 3
  }.merge(options)

  linecook('run', options, *package_dirs, &block)
end

#run_project_cmd(options = {}, *package_dirs, &block) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/linecook/test.rb', line 166

def run_project_cmd(options={}, *package_dirs, &block)
  if package_dirs.empty?
    package_dirs = glob('packages/*').select {|path| File.directory?(path) }
  end

  options = {
    'F' => ssh_config_file,
    'D' => remote_dir,
    'q' => true,
    :max_run_time => 3
  }.merge(options)

  linecook_cmd('run', options, *package_dirs, &block)
end

#runlistObject



99
100
101
# File 'lib/linecook/test.rb', line 99

def runlist
  recipes.map {|recipe| recipe._package_path_ }.join(',')
end

#setup_cookbook(project_dir = method_dir, *additional_project_dirs) ⇒ Object



55
56
57
# File 'lib/linecook/test.rb', line 55

def setup_cookbook(project_dir=method_dir, *additional_project_dirs)
  @cookbook = Cookbook.new(project_dir, *additional_project_dirs)
end

#setup_package(env = {}) ⇒ Object



63
64
65
# File 'lib/linecook/test.rb', line 63

def setup_package(env={})
  @package = Package.new(env)
end

#setup_recipe(package_path = package.next_path('recipe'), options = {:mode => 0744}, &block) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/linecook/test.rb', line 103

def setup_recipe(package_path=package.next_path('recipe'), options={:mode => 0744}, &block)
  recipe = Recipe.new(package, cookbook)
  recipe.helpers(*helpers)
  recipe.instance_eval(&block) if block_given?
  recipe.register_as(package_path, options)

  recipes << recipe
  @recipe = recipe
end

#ssh_config_fileObject



50
51
52
53
# File 'lib/linecook/test.rb', line 50

def ssh_config_file
  method_ssh_config_file = path('config/ssh')
  File.file?(method_ssh_config_file) ? method_ssh_config_file : 'config/ssh'
end

#use_helpers(*helpers) ⇒ Object



71
72
73
# File 'lib/linecook/test.rb', line 71

def use_helpers(*helpers)
  @helpers = helpers
end

#use_host(host) ⇒ Object



79
80
81
# File 'lib/linecook/test.rb', line 79

def use_host(host)
  @host = host
end