summaryrefslogtreecommitdiffstats
path: root/test/language/node.rb
blob: 791c448744c0964eef74f6278e8cd8e5554fc08b (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
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
require 'puppet'
require 'puppet/parser/parser'
require 'puppettest'

class TestParser < Test::Unit::TestCase
    include PuppetTest::ParserTesting

    def setup
        super
        Puppet[:parseonly] = true
        @parser = Puppet::Parser::Parser.new()
    end

    def test_simple_hostname
        check_parseable "host1"
        check_parseable "'host2'"
        check_parseable [ "'host1'",  "host2" ]
        check_parseable [ "'host1'",  "'host2'" ]
    end

    def test_qualified_hostname
        check_parseable "'host.example.com'"
        check_parseable [ "'host.example.com'", "host1" ]
        check_parseable "'host-1.37examples.example.com'"
        check_parseable "'svn.23.nu'"
        check_parseable "'HOST'"
    end

    def test_reject_hostname
        check_nonparseable "host.example.com"
        check_nonparseable "host@example.com"
        check_nonparseable "\"host\""
        check_nonparseable "'$foo.example.com'"
        check_nonparseable "'host1 host2'"
        check_nonparseable "HOST"
    end

    AST = Puppet::Parser::AST

    def check_parseable(hostnames)
        unless hostnames.is_a?(Array)
            hostnames = [ hostnames ]
        end
        assert_nothing_raised {
            @parser.string = "node #{hostnames.join(", ")} { }"
        }
        # Strip quotes
        hostnames.map! { |s| s.sub(/^'(.*)'$/, "\\1") }
        ast = nil
        assert_nothing_raised {
            ast = @parser.parse
        }
        # Verify that the AST has the expected structure
        # and that the leaves have the right hostnames in them
        assert_kind_of(AST::ASTArray, ast)
        assert_equal(1, ast.children.size)
        nodedef = ast.children[0]
        assert_kind_of(AST::NodeDef, nodedef)
        assert_kind_of(AST::ASTArray, nodedef.names)
        assert_equal(hostnames.size, nodedef.names.children.size)
        hostnames.size.times do |i|
            hostnode = nodedef.names.children[i]
            assert_kind_of(AST::HostName, hostnode)
            assert_equal(hostnames[i], hostnode.value)
        end
    end

    def check_nonparseable(hostname)
        assert_nothing_raised {
            @parser.string = "node #{hostname} { }"
        }

        assert_raise(Puppet::DevError, Puppet::ParseError) {
            @parser.parse
        }
    end

    # Make sure we can find default nodes if there's no other entry
    def test_default_node
        Puppet[:parseonly] = false
        @parser = Puppet::Parser::Parser.new()

        fileA = tempfile()
        fileB = tempfile()
        @parser.string = %{
node mynode {
    file { "#{fileA}": ensure => file }
}

node default {
    file { "#{fileB}": ensure => file }
}
}

        # First make sure it parses
        ast = nil
        assert_nothing_raised {
            ast = @parser.parse
        }

        args = {
            :ast => ast,
            :facts => {},
            :names => ["mynode"]
        }
        # Make sure we get a config for "mynode"

        trans = nil
        assert_nothing_raised {
            trans = Puppet::Parser::Scope.new.evaluate(args)
        }

        assert(trans, "Did not get config for mynode")

        args[:names] = ["othernode"]
        # Now make sure the default node is used
        trans = nil
        assert_nothing_raised {
            trans = Puppet::Parser::Scope.new.evaluate(args)
        }

        assert(trans, "Did not get config for default node")
    end
end