1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
|
require 'spec_helper'
require 'tmpdir'
require 'puppet/node/environment'
require 'puppet/util/execution'
require 'puppet_spec/modules'
require 'puppet/parser/parser_factory'
describe Puppet::Node::Environment do
let(:env) { Puppet::Node::Environment.create("testing", []) }
include PuppetSpec::Files
context 'the environment' do
it "converts an environment to string when converting to YAML" do
expect(env.to_yaml).to match(/--- testing/)
end
describe ".create" do
it "creates equivalent environments whether specifying name as a symbol or a string" do
expect(Puppet::Node::Environment.create(:one, [])).to eq(Puppet::Node::Environment.create("one", []))
end
it "interns name" do
expect(Puppet::Node::Environment.create("one", []).name).to equal(:one)
end
it "does not produce environment singletons" do
expect(Puppet::Node::Environment.create("one", [])).to_not equal(Puppet::Node::Environment.create("one", []))
end
end
it "returns its name when converted to a string" do
expect(env.to_s).to eq("testing")
end
it "has an inspect method for debugging" do
e = Puppet::Node::Environment.create(:test, ['/modules/path', '/other/modules'], '/manifests/path')
expect("a #{e} env").to eq("a test env")
expect(e.inspect).to match(%r{<Puppet::Node::Environment:\w* @name="test" @manifest="#{File.expand_path('/manifests/path')}" @modulepath="#{File.expand_path('/modules/path')}:#{File.expand_path('/other/modules')}" >})
end
describe "equality" do
it "works as a hash key" do
base = Puppet::Node::Environment.create(:first, ["modules"], "manifests")
same = Puppet::Node::Environment.create(:first, ["modules"], "manifests")
different = Puppet::Node::Environment.create(:first, ["different"], "manifests")
hash = {}
hash[base] = "base env"
hash[same] = "same env"
hash[different] = "different env"
expect(hash[base]).to eq("same env")
expect(hash[different]).to eq("different env")
expect(hash).to have(2).item
end
it "is equal when name, modules, and manifests are the same" do
base = Puppet::Node::Environment.create(:base, ["modules"], "manifests")
different_name = Puppet::Node::Environment.create(:different, base.full_modulepath, base.manifest)
expect(base).to_not eq("not an environment")
expect(base).to eq(base)
expect(base.hash).to eq(base.hash)
expect(base.override_with(:modulepath => ["different"])).to_not eq(base)
expect(base.override_with(:modulepath => ["different"]).hash).to_not eq(base.hash)
expect(base.override_with(:manifest => "different")).to_not eq(base)
expect(base.override_with(:manifest => "different").hash).to_not eq(base.hash)
expect(different_name).to_not eq(base)
expect(different_name.hash).to_not eq(base.hash)
end
end
describe "overriding an existing environment" do
let(:original_path) { [tmpdir('original')] }
let(:new_path) { [tmpdir('new')] }
let(:environment) { Puppet::Node::Environment.create(:overridden, original_path, 'orig.pp', '/config/script') }
it "overrides modulepath" do
overridden = environment.override_with(:modulepath => new_path)
expect(overridden).to_not be_equal(environment)
expect(overridden.name).to eq(:overridden)
expect(overridden.manifest).to eq(File.expand_path('orig.pp'))
expect(overridden.modulepath).to eq(new_path)
expect(overridden.config_version).to eq('/config/script')
end
it "overrides manifest" do
overridden = environment.override_with(:manifest => 'new.pp')
expect(overridden).to_not be_equal(environment)
expect(overridden.name).to eq(:overridden)
expect(overridden.manifest).to eq(File.expand_path('new.pp'))
expect(overridden.modulepath).to eq(original_path)
expect(overridden.config_version).to eq('/config/script')
end
it "overrides config_version" do
overridden = environment.override_with(:config_version => '/new/script')
expect(overridden).to_not be_equal(environment)
expect(overridden.name).to eq(:overridden)
expect(overridden.manifest).to eq(File.expand_path('orig.pp'))
expect(overridden.modulepath).to eq(original_path)
expect(overridden.config_version).to eq('/new/script')
end
end
describe "when managing known resource types" do
before do
allow(env).to receive(:perform_initial_import).and_return(Puppet::Parser::AST::Hostclass.new(''))
end
it "creates a resource type collection if none exists" do
expect(env.known_resource_types).to be_kind_of(Puppet::Resource::TypeCollection)
end
it "memoizes resource type collection" do
expect(env.known_resource_types).to equal(env.known_resource_types)
end
it "performs the initial import when creating a new collection" do
expect(env).to receive(:perform_initial_import).and_return(Puppet::Parser::AST::Hostclass.new(''))
env.known_resource_types
end
it "generates a new TypeCollection if the current one requires reparsing" do
old_type_collection = env.known_resource_types
allow(old_type_collection).to receive(:parse_failed?).and_return(true)
env.check_for_reparse
new_type_collection = env.known_resource_types
expect(new_type_collection).to be_a Puppet::Resource::TypeCollection
expect(new_type_collection).to_not equal(old_type_collection)
end
end
it "validates the modulepath directories" do
real_file = tmpdir('moduledir')
path = ['/one', '/two', real_file]
env = Puppet::Node::Environment.create(:test, path)
expect(env.modulepath).to eq([real_file])
end
it "prefixes the value of the 'PUPPETLIB' environment variable to the module path if present" do
first_puppetlib = tmpdir('puppetlib1')
second_puppetlib = tmpdir('puppetlib2')
first_moduledir = tmpdir('moduledir1')
second_moduledir = tmpdir('moduledir2')
Puppet::Util.withenv("PUPPETLIB" => [first_puppetlib, second_puppetlib].join(File::PATH_SEPARATOR)) do
env = Puppet::Node::Environment.create(:testing, [first_moduledir, second_moduledir])
expect(env.modulepath).to eq([first_puppetlib, second_puppetlib, first_moduledir, second_moduledir])
end
end
describe "validating manifest settings" do
before(:each) do
Puppet[:default_manifest] = "/default/manifests/site.pp"
end
it "has no validation errors when disable_per_environment_manifest is false" do
expect(Puppet::Node::Environment.create(:directory, [], '/some/non/default/manifest.pp').validation_errors).to be_empty
end
context "when disable_per_environment_manifest is true" do
let(:config) { double('config') }
let(:global_modulepath) { ["/global/modulepath"] }
let(:envconf) { Puppet::Settings::EnvironmentConf.new("/some/direnv", config, global_modulepath) }
before(:each) do
Puppet[:disable_per_environment_manifest] = true
end
def assert_manifest_conflict(expectation, envconf_manifest_value)
expect(config).to receive(:setting).with(:manifest).and_return(
double('setting', :value => envconf_manifest_value)
)
environment = Puppet::Node::Environment.create(:directory, [], '/default/manifests/site.pp')
loader = Puppet::Environments::Static.new(environment)
allow(loader).to receive(:get_conf).and_return(envconf)
Puppet.override(:environments => loader) do
if expectation
expect(environment.validation_errors).to have_matching_element(/The 'disable_per_environment_manifest' setting is true.*and the.*environment.*conflicts/)
else
expect(environment.validation_errors).to be_empty
end
end
end
it "has conflicting_manifest_settings when environment.conf manifest was set" do
assert_manifest_conflict(true, '/some/envconf/manifest/site.pp')
end
it "does not have conflicting_manifest_settings when environment.conf manifest is empty" do
assert_manifest_conflict(false, '')
end
it "does not have conflicting_manifest_settings when environment.conf manifest is nil" do
assert_manifest_conflict(false, nil)
end
it "does not have conflicting_manifest_settings when environment.conf manifest is an exact, uninterpolated match of default_manifest" do
assert_manifest_conflict(false, '/default/manifests/site.pp')
end
end
end
describe "when modeling a specific environment" do
let(:first_modulepath) { tmpdir('firstmodules') }
let(:second_modulepath) { tmpdir('secondmodules') }
let(:env) { Puppet::Node::Environment.create(:modules_test, [first_modulepath, second_modulepath]) }
let(:module_options) {
{
:environment => env,
:metadata => {
:author => 'puppetlabs',
},
}
}
describe "module data" do
describe ".module" do
it "returns an individual module that exists in its module path" do
one = PuppetSpec::Modules.create('one', first_modulepath, module_options)
expect(env.module('one')).to eq(one)
end
it "returns nil if asked for a module that does not exist in its path" do
expect(env.module("doesnotexist")).to be_nil
end
end
describe "#modules_by_path" do
it "returns an empty list if there are no modules" do
expect(env.modules_by_path).to eq({
first_modulepath => [],
second_modulepath => []
})
end
it "includes modules even if they exist in multiple dirs in the modulepath" do
one = PuppetSpec::Modules.create('one', first_modulepath, module_options)
two = PuppetSpec::Modules.create('two', second_modulepath, module_options)
expect(env.modules_by_path).to eq({
first_modulepath => [one],
second_modulepath => [two],
})
end
it "ignores modules with invalid names" do
PuppetSpec::Modules.generate_files('foo', first_modulepath)
PuppetSpec::Modules.generate_files('.foo', first_modulepath)
PuppetSpec::Modules.generate_files('foo2', first_modulepath)
PuppetSpec::Modules.generate_files('foo-bar', first_modulepath)
PuppetSpec::Modules.generate_files('foo_bar', first_modulepath)
PuppetSpec::Modules.generate_files('foo=bar', first_modulepath)
PuppetSpec::Modules.generate_files('foo bar', first_modulepath)
PuppetSpec::Modules.generate_files('foo.bar', first_modulepath)
PuppetSpec::Modules.generate_files('-foo', first_modulepath)
PuppetSpec::Modules.generate_files('foo-', first_modulepath)
PuppetSpec::Modules.generate_files('foo--bar', first_modulepath)
expect(env.modules_by_path[first_modulepath].collect{|mod| mod.name}.sort).to eq(%w{foo foo2 foo_bar})
end
end
describe "#module_requirements" do
it "returns a list of what modules depend on other modules" do
PuppetSpec::Modules.create(
'foo',
first_modulepath,
:metadata => {
:author => 'puppetlabs',
:dependencies => [{ 'name' => 'puppetlabs/bar', "version_requirement" => ">= 1.0.0" }]
}
)
PuppetSpec::Modules.create(
'bar',
second_modulepath,
:metadata => {
:author => 'puppetlabs',
:dependencies => [{ 'name' => 'puppetlabs/foo', "version_requirement" => "<= 2.0.0" }]
}
)
PuppetSpec::Modules.create(
'baz',
first_modulepath,
:metadata => {
:author => 'puppetlabs',
:dependencies => [{ 'name' => 'puppetlabs-bar', "version_requirement" => "3.0.0" }]
}
)
PuppetSpec::Modules.create(
'alpha',
first_modulepath,
:metadata => {
:author => 'puppetlabs',
:dependencies => [{ 'name' => 'puppetlabs/bar', "version_requirement" => "~3.0.0" }]
}
)
expect(env.module_requirements).to eq({
'puppetlabs/alpha' => [],
'puppetlabs/foo' => [
{
"name" => "puppetlabs/bar",
"version" => "9.9.9",
"version_requirement" => "<= 2.0.0"
}
],
'puppetlabs/bar' => [
{
"name" => "puppetlabs/alpha",
"version" => "9.9.9",
"version_requirement" => "~3.0.0"
},
{
"name" => "puppetlabs/baz",
"version" => "9.9.9",
"version_requirement" => "3.0.0"
},
{
"name" => "puppetlabs/foo",
"version" => "9.9.9",
"version_requirement" => ">= 1.0.0"
}
],
'puppetlabs/baz' => []
})
end
end
describe ".module_by_forge_name" do
it "finds modules by forge_name" do
mod = PuppetSpec::Modules.create(
'baz',
first_modulepath,
module_options
)
expect(env.module_by_forge_name('puppetlabs/baz')).to eq(mod)
end
it "does not find modules with same name by the wrong author" do
PuppetSpec::Modules.create(
'baz',
first_modulepath,
:metadata => {:author => 'sneakylabs'},
:environment => env
)
expect(env.module_by_forge_name('puppetlabs/baz')).to eq(nil)
end
it "returns nil when the module can't be found" do
expect(env.module_by_forge_name('ima/nothere')).to be_nil
end
end
describe ".modules" do
it "returns an empty list if there are no modules" do
expect(env.modules).to eq([])
end
it "returns a module named for every directory in each module path" do
%w{foo bar}.each do |mod_name|
PuppetSpec::Modules.generate_files(mod_name, first_modulepath)
end
%w{bee baz}.each do |mod_name|
PuppetSpec::Modules.generate_files(mod_name, second_modulepath)
end
expect(env.modules.collect{|mod| mod.name}.sort).to eq(%w{foo bar bee baz}.sort)
end
it "removes duplicates" do
PuppetSpec::Modules.generate_files('foo', first_modulepath)
PuppetSpec::Modules.generate_files('foo', second_modulepath)
expect(env.modules.collect{|mod| mod.name}.sort).to eq(%w{foo})
end
it "ignores modules with invalid names" do
PuppetSpec::Modules.generate_files('foo', first_modulepath)
PuppetSpec::Modules.generate_files('.foo', first_modulepath)
PuppetSpec::Modules.generate_files('foo2', first_modulepath)
PuppetSpec::Modules.generate_files('foo-bar', first_modulepath)
PuppetSpec::Modules.generate_files('foo_bar', first_modulepath)
PuppetSpec::Modules.generate_files('foo=bar', first_modulepath)
PuppetSpec::Modules.generate_files('foo bar', first_modulepath)
expect(env.modules.collect{|mod| mod.name}.sort).to eq(%w{foo foo2 foo_bar})
end
it "creates modules with the correct environment" do
PuppetSpec::Modules.generate_files('foo', first_modulepath)
env.modules.each do |mod|
expect(mod.environment).to eq(env)
end
end
it "logs an exception if a module contains invalid metadata" do
PuppetSpec::Modules.generate_files(
'foo',
first_modulepath,
:metadata => {
:author => 'puppetlabs'
# missing source, version, etc
}
)
expect(Puppet).to receive(:log_exception).with(be_a(Puppet::Module::MissingMetadata))
env.modules
end
end
end
end
describe "when performing initial import" do
let(:loaders) { Puppet::Pops::Loaders.new(env) }
before(:each) do
allow_any_instance_of(Puppet::Parser::Compiler).to receive(:loaders).and_return(loaders)
end
around :each do |example|
Puppet.override(:loaders => loaders, :current_environment => env) do
example.run
Puppet::Pops::Loaders.clear
end
end
it "loads from Puppet[:code]" do
Puppet[:code] = "define foo {}"
krt = env.known_resource_types
expect(krt.find_definition('foo')).to be_kind_of(Puppet::Resource::Type)
end
it "parses from the the environment's manifests if Puppet[:code] is not set" do
filename = tmpfile('a_manifest.pp')
File.open(filename, 'w') do |f|
f.puts("define from_manifest {}")
end
env = Puppet::Node::Environment.create(:testing, [], filename)
krt = env.known_resource_types
expect(krt.find_definition('from_manifest')).to be_kind_of(Puppet::Resource::Type)
end
it "prefers Puppet[:code] over manifest files" do
Puppet[:code] = "define from_code_setting {}"
filename = tmpfile('a_manifest.pp')
File.open(filename, 'w') do |f|
f.puts("define from_manifest {}")
end
env = Puppet::Node::Environment.create(:testing, [], filename)
krt = env.known_resource_types
expect(krt.find_definition('from_code_setting')).to be_kind_of(Puppet::Resource::Type)
end
it "initial import proceeds even if manifest file does not exist on disk" do
filename = tmpfile('a_manifest.pp')
env = Puppet::Node::Environment.create(:testing, [], filename)
expect(env.known_resource_types).to be_kind_of(Puppet::Resource::TypeCollection)
end
it "returns an empty TypeCollection if neither code nor manifests is present" do
expect(env.known_resource_types).to be_kind_of(Puppet::Resource::TypeCollection)
end
it "fails helpfully if there is an error importing" do
Puppet[:code] = "oops {"
expect do
env.known_resource_types
end.to raise_error(Puppet::Error, /Could not parse for environment #{env.name}/)
end
it "should mark the type collection as needing a reparse when there is an error parsing" do
Puppet[:code] = "oops {"
expect do
env.known_resource_types
end.to raise_error(Puppet::Error, /Syntax error at .../)
expect(env.known_resource_types.parse_failed?).to be_truthy
end
end
end
describe "managing module translations" do
it "creates a new text domain the first time we try to use the text domain" do
expect(Puppet::GettextConfig).to receive(:reset_text_domain).with(env.name)
expect(Puppet::ModuleTranslations).to receive(:load_from_modulepath)
expect(Puppet::GettextConfig).to receive(:clear_text_domain)
env.with_text_domain do; end
end
it "uses the existing text domain once it has been created" do
env.with_text_domain do; end
expect(Puppet::GettextConfig).to receive(:use_text_domain).with(env.name)
env.with_text_domain do; end
end
it "yields block results" do
ran = false
expect(env.with_text_domain { ran = true; :result }).to eq(:result)
expect(ran).to eq(true)
end
it "yields block results when i18n is disabled" do
Puppet[:disable_i18n] = true
ran = false
expect(env.with_text_domain { ran = true; :result }).to eq(:result)
expect(ran).to eq(true)
end
end
end
|