summaryrefslogtreecommitdiffstats
path: root/test/network/handler/master.rb
blob: 018f6904b2f388b4f71f7a5732d7dd8dca5e41e8 (plain)
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
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../lib/puppettest'

require 'puppettest'
require 'puppet/network/handler/master'

class TestMaster < Test::Unit::TestCase
  include PuppetTest::ServerTest

  def setup
    super
    @master = Puppet::Network::Handler.master.new(:Manifest => tempfile)

    @catalog = stub 'catalog', :extract => ""
    Puppet::Resource::Catalog.indirection.stubs(:find).returns(@catalog)
  end

  def teardown
    super
    Puppet::Util::Cacher.expire
  end

  def test_freshness_is_always_now
    now1 = mock 'now1'
    Time.stubs(:now).returns(now1)

    now1.expects(:to_i).returns 10

    assert_equal(@master.freshness, 10, "Did not return current time as freshness")
  end

  def test_hostname_is_used_if_client_is_missing
    @master.expects(:decode_facts).returns("hostname" => "yay")
    Puppet::Node::Facts.expects(:new).with { |name, facts| name == "yay" }.returns(stub('facts', :save => nil))

    @master.getconfig("facts")
  end

  def test_facts_are_saved
    facts = mock('facts')
    Puppet::Node::Facts.expects(:new).returns(facts)
    facts.expects(:save)

    @master.stubs(:decode_facts)

    @master.getconfig("facts", "yaml", "foo.com")
  end

  def test_catalog_is_used_for_compiling
    facts = stub('facts', :save => nil)
    Puppet::Node::Facts.stubs(:new).returns(facts)

    @master.stubs(:decode_facts)

    Puppet::Resource::Catalog.indirection.expects(:find).with("foo.com").returns(@catalog)

    @master.getconfig("facts", "yaml", "foo.com")
  end
end

class TestMasterFormats < Test::Unit::TestCase
  def setup
    @facts = stub('facts', :save => nil)
    Puppet::Node::Facts.stubs(:new).returns(@facts)

    @master = Puppet::Network::Handler.master.new(:Code => "")
    @master.stubs(:decode_facts)

    @catalog = stub 'catalog', :extract => ""
    Puppet::Resource::Catalog.indirection.stubs(:find).returns(@catalog)
  end

  def test_marshal_can_be_used
    @catalog.expects(:extract).returns "myextract"

    Marshal.expects(:dump).with("myextract").returns "eh"

    @master.getconfig("facts", "marshal", "foo.com")
  end

  def test_yaml_can_be_used
    extract = mock 'extract'
    @catalog.expects(:extract).returns extract

    extract.expects(:to_yaml).returns "myaml"

    @master.getconfig("facts", "yaml", "foo.com")
  end

  def test_failure_when_non_yaml_or_marshal_is_used
    assert_raise(RuntimeError) { @master.getconfig("facts", "blah", "foo.com") }
  end
end