~ubuntu-branches/ubuntu/wily/puppet/wily-proposed

« back to all changes in this revision

Viewing changes to spec/unit/settings_spec.rb

  • Committer: Package Import Robot
  • Author(s): Stig Sandbeck Mathisen
  • Date: 2014-10-24 13:47:15 UTC
  • mfrom: (3.1.64 sid)
  • Revision ID: package-import@ubuntu.com-20141024134715-6ig54u0c4gar36ss
Tags: 3.7.2-1
* Imported upstream release 3.7.2
* Declare compliance with Debian Policy 3.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
require 'spec_helper'
3
3
require 'ostruct'
4
4
require 'puppet/settings/errors'
 
5
require 'puppet_spec/files'
 
6
require 'matchers/resource'
5
7
 
6
8
describe Puppet::Settings do
7
9
  include PuppetSpec::Files
 
10
  include Matchers::Resource
8
11
 
9
12
  let(:main_config_file_default_location) do
10
13
    File.join(Puppet::Util::RunMode[:master].conf_dir, "puppet.conf")
674
677
        @settings.send(:parse_config_files)
675
678
        expect(@settings.value(:manifestdir)).to eq("/somewhere/production/manifests")
676
679
      end
677
 
 
 
680
 
678
681
      it "interpolates the set environment when no environment specified" do
679
682
        text = <<-EOF
680
683
[main]
1003
1006
    end
1004
1007
 
1005
1008
    describe "deprecations" do
1006
 
      context "in puppet.conf" do
1007
 
 
1008
 
        def assert_puppet_conf_deprecation(setting, matches)
1009
 
          Puppet.expects(:deprecation_warning).with(regexp_matches(matches), anything)
1010
 
 
1011
 
          val = "/you/can/set/this/but/will/get/warning"
1012
 
          text = "[main]
1013
 
          #{setting}=#{val}
1014
 
          "
1015
 
          Puppet.settings.parse_config(text)
1016
 
        end
1017
 
 
1018
 
        it "warns when manifest is set" do
1019
 
          assert_puppet_conf_deprecation('manifest', /manifest.*puppet.conf/)
1020
 
        end
1021
 
 
1022
 
        it "warns when modulepath is set" do
1023
 
          assert_puppet_conf_deprecation('modulepath', /modulepath.*puppet.conf/)
1024
 
        end
1025
 
 
1026
 
        it "warns when config_version is set" do
1027
 
          assert_puppet_conf_deprecation('config_version', /config_version.*puppet.conf/)
1028
 
        end
1029
 
 
1030
 
        it "warns when manifestdir is set" do
1031
 
          assert_puppet_conf_deprecation('manifestdir', /Setting manifestdir.*is.*deprecated/)
1032
 
        end
1033
 
 
1034
 
        it "warns when templatedir is set" do
1035
 
          assert_puppet_conf_deprecation('templatedir', /Setting templatedir.*is.*deprecated/)
1036
 
        end
1037
 
      end
1038
 
 
1039
 
      context "on the command line" do
1040
 
        def assert_command_line_deprecation(setting, message)
1041
 
          Puppet.expects(:deprecation_warning).with(message, anything)
1042
 
 
1043
 
          args = ["--#{setting}", "/some/value"]
1044
 
          Puppet.settings.send(:parse_global_options, args)
1045
 
        end
1046
 
 
1047
 
        def assert_command_line_not_deprecated(setting)
 
1009
      let(:settings) { Puppet::Settings.new }
 
1010
      let(:app_defaults) {
 
1011
        {
 
1012
          :logdir     => "/dev/null",
 
1013
          :confdir    => "/dev/null",
 
1014
          :vardir     => "/dev/null",
 
1015
        }
 
1016
      }
 
1017
 
 
1018
      def assert_accessing_setting_is_deprecated(settings, setting)
 
1019
        Puppet.expects(:deprecation_warning).with("Accessing '#{setting}' as a setting is deprecated. See http://links.puppetlabs.com/env-settings-deprecations")
 
1020
        Puppet.expects(:deprecation_warning).with("Modifying '#{setting}' as a setting is deprecated. See http://links.puppetlabs.com/env-settings-deprecations")
 
1021
        settings[setting.intern] = apath = File.expand_path('foo')
 
1022
        expect(settings[setting.intern]).to eq(apath)
 
1023
      end
 
1024
 
 
1025
      before(:each) do
 
1026
        settings.define_settings(:main, {
 
1027
          :logdir => { :default => 'a', :desc => 'a' },
 
1028
          :confdir => { :default => 'b', :desc => 'b' },
 
1029
          :vardir => { :default => 'c', :desc => 'c' },
 
1030
        })
 
1031
      end
 
1032
 
 
1033
      context "complete" do
 
1034
        let(:completely_deprecated_settings) do
 
1035
          settings.define_settings(:main, {
 
1036
            :manifestdir => {
 
1037
              :default => 'foo',
 
1038
              :desc    => 'a deprecated setting',
 
1039
              :deprecated => :completely,
 
1040
            }
 
1041
          })
 
1042
          settings
 
1043
        end
 
1044
 
 
1045
        it "warns when set in puppet.conf" do
 
1046
          Puppet.expects(:deprecation_warning).with(regexp_matches(/manifestdir is deprecated\./), 'setting-manifestdir')
 
1047
 
 
1048
          completely_deprecated_settings.parse_config(<<-CONF)
 
1049
            manifestdir='should warn'
 
1050
          CONF
 
1051
          completely_deprecated_settings.initialize_app_defaults(app_defaults)
 
1052
        end
 
1053
 
 
1054
        it "warns when set on the commandline" do
 
1055
          Puppet.expects(:deprecation_warning).with(regexp_matches(/manifestdir is deprecated\./), 'setting-manifestdir')
 
1056
 
 
1057
          args = ["--manifestdir", "/some/value"]
 
1058
          completely_deprecated_settings.send(:parse_global_options, args)
 
1059
          completely_deprecated_settings.initialize_app_defaults(app_defaults)
 
1060
        end
 
1061
 
 
1062
        it "warns when set in code" do
 
1063
          assert_accessing_setting_is_deprecated(completely_deprecated_settings, 'manifestdir')
 
1064
        end
 
1065
      end
 
1066
 
 
1067
      context "partial" do
 
1068
        let(:partially_deprecated_settings) do
 
1069
          settings.define_settings(:main, {
 
1070
            :modulepath => {
 
1071
              :default => 'foo',
 
1072
              :desc    => 'a partially deprecated setting',
 
1073
              :deprecated => :allowed_on_commandline,
 
1074
            }
 
1075
          })
 
1076
          settings
 
1077
        end
 
1078
 
 
1079
        it "warns for a deprecated setting allowed on the command line set in puppet.conf" do
 
1080
          Puppet.expects(:deprecation_warning).with(regexp_matches(/modulepath is deprecated in puppet\.conf/), 'puppet-conf-setting-modulepath')
 
1081
          partially_deprecated_settings.parse_config(<<-CONF)
 
1082
            modulepath='should warn'
 
1083
          CONF
 
1084
          partially_deprecated_settings.initialize_app_defaults(app_defaults)
 
1085
        end
 
1086
 
 
1087
        it "does not warn when manifest is set on command line" do
1048
1088
          Puppet.expects(:deprecation_warning).never
1049
1089
 
1050
 
          args = ["--#{setting}", "/some/value"]
1051
 
          Puppet.settings.send(:parse_global_options, args)
1052
 
        end
1053
 
 
1054
 
        it "does not warn when manifest is set on command line" do
1055
 
          assert_command_line_not_deprecated('manifest')
1056
 
        end
1057
 
 
1058
 
        it "does not warn when modulepath is set on command line" do
1059
 
          assert_command_line_not_deprecated('modulepath')
1060
 
        end
1061
 
 
1062
 
        it "does not warn when config_version is set on command line" do
1063
 
          assert_command_line_not_deprecated('config_version')
1064
 
        end
1065
 
 
1066
 
        it "warns when manifestdir is set on command line" do
1067
 
          assert_command_line_deprecation('manifestdir', "Setting manifestdir is deprecated. See http://links.puppetlabs.com/env-settings-deprecations")
1068
 
        end
1069
 
 
1070
 
        it "warns when templatedir is set on command line" do
1071
 
          assert_command_line_deprecation('templatedir', "Setting templatedir is deprecated. See http://links.puppetlabs.com/env-settings-deprecations")
1072
 
        end
1073
 
      end
1074
 
 
1075
 
      context "as settings in the code base" do
1076
 
        def assert_accessing_setting_is_deprecated(setting)
1077
 
          Puppet.expects(:deprecation_warning).with("Accessing '#{setting}' as a setting is deprecated. See http://links.puppetlabs.com/env-settings-deprecations")
1078
 
          Puppet.expects(:deprecation_warning).with("Modifying '#{setting}' as a setting is deprecated. See http://links.puppetlabs.com/env-settings-deprecations")
1079
 
          Puppet[setting.intern] = apath = File.expand_path('foo')
1080
 
          expect(Puppet[setting.intern]).to eq(apath)
1081
 
        end
1082
 
 
1083
 
        it "warns when attempt to access a 'manifest' setting" do
1084
 
          assert_accessing_setting_is_deprecated('manifest')
1085
 
        end
1086
 
 
1087
 
        it "warns when attempt to access a 'modulepath' setting" do
1088
 
          assert_accessing_setting_is_deprecated('modulepath')
1089
 
        end
1090
 
        it "warns when attempt to access a 'config_version' setting" do
1091
 
          assert_accessing_setting_is_deprecated('config_version')
1092
 
        end
1093
 
 
1094
 
        it "warns when attempt to access a 'manifestdir' setting" do
1095
 
          assert_accessing_setting_is_deprecated('manifestdir')
1096
 
        end
1097
 
 
1098
 
        it "warns when attempt to access a 'templatedir' setting" do
1099
 
          assert_accessing_setting_is_deprecated('templatedir')
1100
 
        end
1101
 
 
 
1090
          args = ["--modulepath", "/some/value"]
 
1091
          partially_deprecated_settings.send(:parse_global_options, args)
 
1092
          partially_deprecated_settings.initialize_app_defaults(app_defaults)
 
1093
        end
 
1094
 
 
1095
        it "warns when set in code" do
 
1096
          assert_accessing_setting_is_deprecated(partially_deprecated_settings, 'modulepath')
 
1097
        end
1102
1098
      end
1103
1099
    end
1104
1100
  end
1345
1341
      @settings.to_catalog
1346
1342
    end
1347
1343
 
 
1344
    it "should ignore manifestdir if environmentpath is set" do
 
1345
      @settings.define_settings :main,
 
1346
        :manifestdir => { :type => :directory, :default => @prefix+"/manifestdir", :desc => "a" },
 
1347
        :environmentpath => { :type => :path, :default => @prefix+"/envs", :desc => "a" }
 
1348
 
 
1349
      catalog = @settings.to_catalog(:main)
 
1350
 
 
1351
      expect(catalog).to_not have_resource("File[#{@prefix}/manifestdir]")
 
1352
    end
 
1353
 
1348
1354
    describe "on Microsoft Windows" do
1349
1355
      before :each do
1350
1356
        Puppet.features.stubs(:root?).returns true
1366
1372
      end
1367
1373
    end
1368
1374
 
 
1375
    describe "adding default directory environment to the catalog" do
 
1376
      let(:tmpenv) { tmpdir("envs") }
 
1377
      let(:default_path) { "#{tmpenv}/environments" }
 
1378
      before(:each) do
 
1379
        @settings.define_settings :main,
 
1380
          :environment     => { :default => "production", :desc => "env"},
 
1381
          :environmentpath => { :type => :path, :default => default_path, :desc => "envpath"}
 
1382
      end
 
1383
 
 
1384
      it "adds if environmentpath exists" do
 
1385
        envpath = "#{tmpenv}/custom_envpath"
 
1386
        @settings[:environmentpath] = envpath
 
1387
        Dir.mkdir(envpath)
 
1388
        catalog = @settings.to_catalog
 
1389
        expect(catalog.resource_keys).to include(["File", "#{envpath}/production"])
 
1390
      end
 
1391
 
 
1392
      it "adds the first directory of environmentpath" do
 
1393
        envdir = "#{tmpenv}/custom_envpath"
 
1394
        envpath = "#{envdir}#{File::PATH_SEPARATOR}/some/other/envdir"
 
1395
        @settings[:environmentpath] = envpath
 
1396
        Dir.mkdir(envdir)
 
1397
        catalog = @settings.to_catalog
 
1398
        expect(catalog.resource_keys).to include(["File", "#{envdir}/production"])
 
1399
      end
 
1400
 
 
1401
      it "handles a non-existent environmentpath" do
 
1402
        catalog = @settings.to_catalog
 
1403
        expect(catalog.resource_keys).to be_empty
 
1404
      end
 
1405
 
 
1406
      it "handles a default environmentpath" do
 
1407
        Dir.mkdir(default_path)
 
1408
        catalog = @settings.to_catalog
 
1409
        expect(catalog.resource_keys).to include(["File", "#{default_path}/production"])
 
1410
      end
 
1411
 
 
1412
      it "does not add if the path to the default directory environment exists as a symlink", :if => Puppet.features.manages_symlinks? do
 
1413
        Dir.mkdir(default_path)
 
1414
        Puppet::FileSystem.symlink("#{tmpenv}/nowhere", File.join(default_path, 'production'))
 
1415
        catalog = @settings.to_catalog
 
1416
        expect(catalog.resource_keys).to_not include(["File", "#{default_path}/production"])
 
1417
      end
 
1418
    end
 
1419
 
1369
1420
    describe "when adding users and groups to the catalog" do
1370
1421
      before do
1371
1422
        Puppet.features.stubs(:root?).returns true
1475
1526
          :maindir => { :type => :directory, :default => make_absolute("/maindir"), :desc => "a" },
1476
1527
          :seconddir => { :type => :directory, :default => make_absolute("/seconddir"), :desc => "a"}
1477
1528
      @settings.define_settings :main, :user => { :default => "suser", :desc => "doc" }, :group => { :default => "sgroup", :desc => "doc" }
1478
 
      @settings.define_settings :other, :otherdir => {:type => :directory, :default => make_absolute("/otherdir"), :desc => "a", :owner => "service", :group => "service", :mode => 0755}
 
1529
      @settings.define_settings :other, :otherdir => {:type => :directory, :default => make_absolute("/otherdir"), :desc => "a", :owner => "service", :group => "service", :mode => '0755'}
1479
1530
      @settings.define_settings :third, :thirddir => { :type => :directory, :default => make_absolute("/thirddir"), :desc => "b"}
1480
 
      @settings.define_settings :files, :myfile => {:type => :file, :default => make_absolute("/myfile"), :desc => "a", :mode => 0755}
 
1531
      @settings.define_settings :files, :myfile => {:type => :file, :default => make_absolute("/myfile"), :desc => "a", :mode => '0755'}
1481
1532
    end
1482
1533
 
1483
1534
    it "should provide a method that creates directories with the correct modes" do
1484
1535
      Puppet::Util::SUIDManager.expects(:asuser).with("suser", "sgroup").yields
1485
 
      Dir.expects(:mkdir).with(make_absolute("/otherdir"), 0755)
 
1536
      Dir.expects(:mkdir).with(make_absolute("/otherdir"), '0755')
1486
1537
      @settings.mkdir(:otherdir)
1487
1538
    end
1488
1539