Permalink
Browse files

Add specs for selector terminuses of file_{content,metadata}

  • Loading branch information...
1 parent 828c16a commit 8eb0cd8cc560116b58ae8d2908dcc9846a219bfa @nicklewis nicklewis committed Jun 25, 2012
@@ -75,8 +75,6 @@ def parseonly
def main
require 'etc'
- require 'puppet/file_serving/content'
- require 'puppet/file_serving/metadata'
xmlrpc_handlers = [:Status, :FileServer, :Master, :Report, :Filebucket]
@@ -117,7 +115,7 @@ def main
end
end
- def setup
+ def setup_logs
# Handle the logging settings.
if options[:debug] or options[:verbose]
if options[:debug]
@@ -133,10 +131,11 @@ def setup
end
Puppet::Util::Log.newdestination(:syslog) unless options[:setdest]
+ end
- exit(Puppet.settings.print_configs ? 0 : 1) if Puppet.settings.print_configs?
-
- Puppet.settings.use :main, :master, :ssl, :metrics
+ def setup_terminuses
+ require 'puppet/file_serving/content'
+ require 'puppet/file_serving/metadata'
# Cache our nodes in yaml. Currently not configurable.
Puppet::Node.cache_class = :yaml
@@ -145,7 +144,9 @@ def setup
Puppet::FileServing::Metadata.indirection.terminus_class = :file_server
Puppet::FileBucket::File.indirection.terminus_class = :file
+ end
+ def setup_ssl
# Configure all of the SSL stuff.
if Puppet::SSL::CertificateAuthority.ca?
Puppet::SSL::Host.ca_location = :local
@@ -155,4 +156,18 @@ def setup
Puppet::SSL::Host.ca_location = :none
end
end
+
+ def setup
+ raise Puppet::Error.new("Puppet master is not supported on Microsoft Windows") if Puppet.features.microsoft_windows?
+
+ setup_logs
+
+ exit(Puppet.settings.print_configs ? 0 : 1) if Puppet.settings.print_configs?
+
+ Puppet.settings.use :main, :master, :ssl, :metrics
+
+ setup_terminuses
+
+ setup_ssl
+ end
end
@@ -1,3 +1,4 @@
+require 'puppet/file_serving/content'
require 'puppet/indirector/file_content'
require 'puppet/indirector/code'
require 'puppet/file_serving/terminus_selector'
@@ -1,3 +1,4 @@
+require 'puppet/file_serving/metadata'
require 'puppet/indirector/file_metadata'
require 'puppet/indirector/code'
require 'puppet/file_serving/terminus_selector'
@@ -9,12 +9,5 @@
require 'shared_behaviours/file_serving'
describe Puppet::FileServing::Content, " when finding files" do
- it_should_behave_like "Puppet::FileServing::Files"
-
- before do
- @test_class = Puppet::FileServing::Content
- @indirection = Puppet::FileServing::Content.indirection
- end
-
- after { Puppet::Util::Cacher.expire }
+ it_should_behave_like "a file_serving model"
end
@@ -10,12 +10,5 @@
require 'puppet/indirector/file_metadata/file_server'
describe Puppet::FileServing::Metadata, " when finding files" do
- it_should_behave_like "Puppet::FileServing::Files"
-
- before do
- @test_class = Puppet::FileServing::Metadata
- @indirection = Puppet::FileServing::Metadata.indirection
- end
-
- after { Puppet::Util::Cacher.expire }
+ it_should_behave_like "a file_serving model"
end
View
@@ -1,69 +1,80 @@
-#!/usr/bin/env ruby
-#
-# Created by Luke Kanies on 2007-10-18.
-# Copyright (c) 2007. All rights reserved.
-
-shared_examples_for "Puppet::FileServing::Files" do
- it "should use the rest terminus when the 'puppet' URI scheme is used and a host name is present" do
- uri = "puppet://myhost/fakemod/my/file"
-
- # It appears that the mocking somehow interferes with the caching subsystem.
- # This mock somehow causes another terminus to get generated.
- @indirection.terminus(:rest).expects(:find)
- @test_class.find(uri)
- end
+#!/usr/bin/env rspec
- it "should use the rest terminus when the 'puppet' URI scheme is used, no host name is present, and the process name is not 'puppet' or 'apply'" do
- uri = "puppet:///fakemod/my/file"
- Puppet.settings.stubs(:value).returns "foo"
- Puppet.settings.stubs(:value).with(:name).returns("puppetd")
- Puppet.settings.stubs(:value).with(:modulepath).returns("")
- @indirection.terminus(:rest).expects(:find)
- @test_class.find(uri)
- end
+shared_examples_for "Puppet::FileServing::Files" do |indirection|
+ %w[find search].each do |method|
+ let(:request) { Puppet::Indirector::Request.new(indirection, method, 'foo') }
- it "should use the file_server terminus when the 'puppet' URI scheme is used, no host name is present, and the process name is 'puppet'" do
- uri = "puppet:///fakemod/my/file"
- Puppet::Node::Environment.stubs(:new).returns(stub("env", :name => "testing", :module => nil, :modulepath => []))
- Puppet.settings.stubs(:value).returns ""
- Puppet.settings.stubs(:value).with(:name).returns("puppet")
- Puppet.settings.stubs(:value).with(:fileserverconfig).returns("/whatever")
- @indirection.terminus(:file_server).expects(:find)
- @indirection.terminus(:file_server).stubs(:authorized?).returns(true)
- @test_class.find(uri)
- end
+ before :each do
+ # Stub this so we can set the :name setting
+ Puppet::Util::Settings::ReadOnly.stubs(:include?)
+ end
- it "should use the file_server terminus when the 'puppet' URI scheme is used, no host name is present, and the process name is 'apply'" do
- uri = "puppet:///fakemod/my/file"
- Puppet::Node::Environment.stubs(:new).returns(stub("env", :name => "testing", :module => nil, :modulepath => []))
- Puppet.settings.stubs(:value).returns ""
- Puppet.settings.stubs(:value).with(:name).returns("apply")
- Puppet.settings.stubs(:value).with(:fileserverconfig).returns("/whatever")
- @indirection.terminus(:file_server).expects(:find)
- @indirection.terminus(:file_server).stubs(:authorized?).returns(true)
- @test_class.find(uri)
- end
+ describe "##{method}" do
+ it "should proxy to file terminus if the path is absolute" do
+ request.key = '/tmp/foo'
- it "should use the file terminus when the 'file' URI scheme is used" do
- uri = "file:///fakemod/my/file"
- @indirection.terminus(:file).expects(:find)
- @test_class.find(uri)
- end
+ described_class.indirection.terminus(:file).class.any_instance.expects(method).with(request)
- it "should use the file terminus when a fully qualified path is provided" do
- uri = "/fakemod/my/file"
- @indirection.terminus(:file).expects(:find)
- @test_class.find(uri)
- end
+ subject.send(method, request)
+ end
+
+ it "should proxy to file terminus if the protocol is file" do
+ request.protocol = 'file'
+
+ described_class.indirection.terminus(:file).class.any_instance.expects(method).with(request)
+
+ subject.send(method, request)
+ end
+
+ describe "when the protocol is puppet" do
+ before :each do
+ request.protocol = 'puppet'
+ end
+
+ describe "and a server is specified" do
+ before :each do
+ request.server = 'puppet_server'
+ end
+
+ it "should proxy to rest terminus if we're 'apply'" do
+ Puppet[:name] = 'apply'
+
+ described_class.indirection.terminus(:rest).class.any_instance.expects(method).with(request)
+
+ subject.send(method, request)
+ end
+
+ it "should proxy to rest terminus if we aren't 'apply'" do
+ Puppet[:name] = 'not_apply'
+
+ described_class.indirection.terminus(:rest).class.any_instance.expects(method).with(request)
+
+ subject.send(method, request)
+ end
+ end
+
+ describe "and no server is specified" do
+ before :each do
+ request.server = nil
+ end
+
+ it "should proxy to file_server if we're 'apply'" do
+ Puppet[:name] = 'apply'
+
+ described_class.indirection.terminus(:file_server).class.any_instance.expects(method).with(request)
+
+ subject.send(method, request)
+ end
+
+ it "should proxy to rest if we're not 'apply'" do
+ Puppet[:name] = 'not_apply'
- it "should use the configuration to test whether the request is allowed" do
- uri = "fakemod/my/file"
- mount = mock 'mount'
- config = stub 'configuration', :split_path => [mount, "eh"]
- @indirection.terminus(:file_server).stubs(:configuration).returns config
+ described_class.indirection.terminus(:rest).class.any_instance.expects(method).with(request)
- @indirection.terminus(:file_server).expects(:find)
- mount.expects(:allowed?).returns(true)
- @test_class.find(uri, :node => "foo", :ip => "bar")
+ subject.send(method, request)
+ end
+ end
+ end
+ end
end
end
@@ -0,0 +1,73 @@
+#!/usr/bin/env rspec
+
+shared_examples_for "a file_serving model" do
+ include PuppetSpec::Files
+
+ describe "#indirection" do
+ before :each do
+ # Never connect to the network, no matter what
+ described_class.indirection.terminus(:rest).class.any_instance.stubs(:find)
+ end
+
+ describe "when running the master application" do
+ before :each do
+ Puppet::Application[:master].setup_terminuses
+ end
+
+ {
+ "/etc/sudoers" => :file_server,
+ "file:///etc/sudoers" => :file_server,
+ "puppet:///modules/foo/bar" => :file_server,
+ "puppet://server/modules/foo/bar" => :file_server,
+ }.each do |key, terminus|
+ it "should use the #{terminus} terminus when requesting #{key.inspect}" do
+ described_class.indirection.terminus(terminus).class.any_instance.expects(:find)
+
+ described_class.indirection.find(key)
+ end
+ end
+ end
+
+ describe "when running the apply application" do
+ before :each do
+ # Stub this so we can set the 'name' setting
+ Puppet::Util::Settings::ReadOnly.stubs(:include?)
+ Puppet[:name] = 'apply'
+ end
+
+ {
+ "/etc/sudoers" => :file,
+ "file:///etc/sudoers" => :file,
+ "puppet:///modules/foo/bar" => :file_server,
+ "puppet://server/modules/foo/bar" => :rest,
+ }.each do |key, terminus|
+ it "should use the #{terminus} terminus when requesting #{key.inspect}" do
+ described_class.indirection.terminus(terminus).class.any_instance.expects(:find)
+
+ described_class.indirection.find(key)
+ end
+ end
+ end
+
+ describe "when running another application" do
+ before :each do
+ # Stub this so we can set the 'name' setting
+ Puppet::Util::Settings::ReadOnly.stubs(:include?)
+ Puppet[:name] = 'agent'
+ end
+
+ {
+ "/etc/sudoers" => :file,
+ "file:///etc/sudoers" => :file,
+ "puppet:///modules/foo/bar" => :rest,
+ "puppet://server/modules/foo/bar" => :rest,
+ }.each do |key, terminus|
+ it "should use the #{terminus} terminus when requesting #{key.inspect}" do
+ described_class.indirection.terminus(terminus).class.any_instance.expects(:find)
+
+ described_class.indirection.find(key)
+ end
+ end
+ end
+ end
+end
View
@@ -65,6 +65,15 @@ module PuppetTest
@logs.clear
Puppet::Util::Log.close_all
+
+ # Restore the terminuses to the defaults in case they were changed
+ indirections = Puppet::Indirector::Indirection.send(:class_variable_get, :@@indirections)
+ indirections.each do |indirector|
+ $saved_indirection_state.fetch(indirector.name, {}).each do |variable, value|
+ indirector.instance_variable_set(variable, value)
+ end
+ end
+ $saved_indirection_state = nil
end
config.before :each do
@@ -73,6 +82,17 @@ module PuppetTest
$puppet_application_name = nil
Signal.stubs(:trap)
+ # Save off the current terminuses so we can restore them afterward
+ $saved_indirection_state = {}
+ indirections = Puppet::Indirector::Indirection.send(:class_variable_get, :@@indirections)
+ indirections.each do |indirector|
+ $saved_indirection_state[indirector.name] = {
+ :@terminus_class => indirector.instance_variable_get(:@terminus_class),
+ :@cache_class => indirector.instance_variable_get(:@cache_class)
+ }
+ end
+
+
# Set the confdir and vardir to gibberish so that tests
# have to be correctly mocked.
Puppet[:confdir] = "/dev/null"
@@ -0,0 +1,10 @@
+#!/usr/bin/env rspec
+require 'spec_helper'
+
+require 'puppet/indirector/file_content/selector'
+
+describe Puppet::Indirector::FileContent::Selector do
+ include PuppetSpec::Files
+
+ it_should_behave_like "Puppet::FileServing::Files", :file_content
+end
@@ -0,0 +1,11 @@
+#!/usr/bin/env rspec
+require 'spec_helper'
+
+require 'puppet/indirector/file_metadata/selector'
+
+describe Puppet::Indirector::FileMetadata::Selector do
+ include PuppetSpec::Files
+
+ it_should_behave_like "Puppet::FileServing::Files", :file_metadata
+end
+

0 comments on commit 8eb0cd8

Please sign in to comment.