summaryrefslogtreecommitdiff
path: root/spec/unit/graph/relationship_graph_spec.rb
blob: 8bd35de4dab63d904100360bf9d5b89fae1df64c (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
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
#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/graph'

require 'puppet_spec/compiler'
require 'matchers/include_in_order'
require 'matchers/relationship_graph_matchers'

describe Puppet::Graph::RelationshipGraph do
  include PuppetSpec::Files
  include PuppetSpec::Compiler
  include RelationshipGraphMatchers

  let(:graph) { Puppet::Graph::RelationshipGraph.new(Puppet::Graph::SequentialPrioritizer.new) }

  it "allows adding a new vertex with a specific priority" do
    vertex = stub_vertex('something')

    graph.add_vertex(vertex, 2)

    expect(graph.resource_priority(vertex)).to eq(2)
  end

  it "returns resource priority based on the order added" do
    # strings chosen so the old hex digest method would put these in the
    # wrong order
    first = stub_vertex('aa')
    second = stub_vertex('b')

    graph.add_vertex(first)
    graph.add_vertex(second)

    expect(graph.resource_priority(first)).to be < graph.resource_priority(second)
  end

  it "retains the first priority when a resource is added more than once" do
    first = stub_vertex(1)
    second = stub_vertex(2)

    graph.add_vertex(first)
    graph.add_vertex(second)
    graph.add_vertex(first)

    expect(graph.resource_priority(first)).to be < graph.resource_priority(second)
  end

  it "forgets the priority of a removed resource" do
    vertex = stub_vertex(1)

    graph.add_vertex(vertex)
    graph.remove_vertex!(vertex)

    expect(graph.resource_priority(vertex)).to be_nil
  end

  it "does not give two resources the same priority" do
    first = stub_vertex(1)
    second = stub_vertex(2)
    third = stub_vertex(3)

    graph.add_vertex(first)
    graph.add_vertex(second)
    graph.remove_vertex!(first)
    graph.add_vertex(third)

    expect(graph.resource_priority(second)).to be < graph.resource_priority(third)
  end

  context "order of traversal" do
    it "traverses independent resources in the order they are added" do
      relationships = compile_to_relationship_graph(<<-MANIFEST)
        notify { "first": }
        notify { "second": }
        notify { "third": }
        notify { "fourth": }
        notify { "fifth": }
      MANIFEST

      expect(order_resources_traversed_in(relationships)).to(
        include_in_order("Notify[first]",
                         "Notify[second]",
                         "Notify[third]",
                         "Notify[fourth]",
                         "Notify[fifth]"))
    end

    it "traverses resources generated during catalog creation in the order inserted" do
      relationships = compile_to_relationship_graph(<<-MANIFEST)
        create_resources(notify, { "first" => {} })
        create_resources(notify, { "second" => {} })
        notify{ "third": }
        create_resources(notify, { "fourth" => {} })
        create_resources(notify, { "fifth" => {} })
      MANIFEST

      expect(order_resources_traversed_in(relationships)).to(
        include_in_order("Notify[first]",
                         "Notify[second]",
                         "Notify[third]",
                         "Notify[fourth]",
                         "Notify[fifth]"))
    end

    it "traverses all independent resources before traversing dependent ones" do
      relationships = compile_to_relationship_graph(<<-MANIFEST)
        notify { "first": require => Notify[third] }
        notify { "second": }
        notify { "third": }
      MANIFEST

      expect(order_resources_traversed_in(relationships)).to(
        include_in_order("Notify[second]", "Notify[third]", "Notify[first]"))
    end

    it "traverses all independent resources before traversing dependent ones (with a backwards require)" do
      relationships = compile_to_relationship_graph(<<-MANIFEST)
        notify { "first": }
        notify { "second": }
        notify { "third": require => Notify[second] }
        notify { "fourth": }
      MANIFEST

      expect(order_resources_traversed_in(relationships)).to(
        include_in_order("Notify[first]", "Notify[second]", "Notify[third]", "Notify[fourth]"))
    end

    it "traverses resources in classes in the order they are added" do
      relationships = compile_to_relationship_graph(<<-MANIFEST)
        class c1 {
            notify { "a": }
            notify { "b": }
        }
        class c2 {
            notify { "c": require => Notify[b] }
        }
        class c3 {
            notify { "d": }
        }
        include c2
        include c1
        include c3
      MANIFEST

      expect(order_resources_traversed_in(relationships)).to(
        include_in_order("Notify[a]", "Notify[b]", "Notify[c]", "Notify[d]"))
    end

    it "traverses resources in defines in the order they are added" do
      relationships = compile_to_relationship_graph(<<-MANIFEST)
        define d1() {
          notify { "a": }
          notify { "b": }
        }
        define d2() {
          notify { "c": require => Notify[b]}
        }
        define d3() {
            notify { "d": }
        }
        d2 { "c": }
        d1 { "d": }
        d3 { "e": }
      MANIFEST

      expect(order_resources_traversed_in(relationships)).to(
        include_in_order("Notify[a]", "Notify[b]", "Notify[c]", "Notify[d]"))
    end
  end

  describe "when interrupting traversal" do
    def collect_canceled_resources(relationships, trigger_on)
      continue = true
      continue_while = lambda { continue }

      canceled_resources = []
      canceled_resource_handler = lambda { |resource| canceled_resources << resource.ref }

      relationships.traverse(:while => continue_while,
                             :canceled_resource_handler => canceled_resource_handler) do |resource|
        if resource.ref == trigger_on
          continue = false
        end
      end

      canceled_resources
    end

    it "enumerates the remaining resources" do
      relationships = compile_to_relationship_graph(<<-MANIFEST)
      notify { "a": }
      notify { "b": }
      notify { "c": }
    MANIFEST
      resources = collect_canceled_resources(relationships, 'Notify[b]')

      expect(resources).to include('Notify[c]')
    end

    it "enumerates the remaining blocked resources" do
      relationships = compile_to_relationship_graph(<<-MANIFEST)
      notify { "a": }
      notify { "b": }
      notify { "c": }
      notify { "d": require => Notify["c"] }
    MANIFEST
      resources = collect_canceled_resources(relationships, 'Notify[b]')

      expect(resources).to include('Notify[d]')
    end
  end

  describe "when constructing dependencies" do
    let(:child) { make_absolute('/a/b') }
    let(:parent) { make_absolute('/a') }

    it "does not create an automatic relationship that would interfere with a manual relationship" do
      relationship_graph = compile_to_relationship_graph(<<-MANIFEST)
        file { "#{child}": }

        file { "#{parent}": require => File["#{child}"] }
      MANIFEST

      relationship_graph.should enforce_order_with_edge("File[#{child}]", "File[#{parent}]")
    end

    it "creates automatic relationships defined by the type" do
      relationship_graph = compile_to_relationship_graph(<<-MANIFEST)
        file { "#{child}": }

        file { "#{parent}": }
      MANIFEST

      relationship_graph.should enforce_order_with_edge("File[#{parent}]", "File[#{child}]")
    end
  end

  describe "when reconstructing containment relationships" do
    def admissible_sentinel_of(ref)
      "Admissible_#{ref}"
    end

    def completed_sentinel_of(ref)
      "Completed_#{ref}"
    end

    it "an empty container's completed sentinel should depend on its admissible sentinel" do
      relationship_graph = compile_to_relationship_graph(<<-MANIFEST)
        class a { }

        include a
      MANIFEST

      relationship_graph.should enforce_order_with_edge(
        admissible_sentinel_of("class[A]"),
        completed_sentinel_of("class[A]"))
    end

    it "a container with children does not directly connect the completed sentinel to its admissible sentinel" do
      relationship_graph = compile_to_relationship_graph(<<-MANIFEST)
        class a { notify { "a": } }

        include a
      MANIFEST

      relationship_graph.should_not enforce_order_with_edge(
        admissible_sentinel_of("class[A]"),
        completed_sentinel_of("class[A]"))
    end

    it "all contained objects should depend on their container's admissible sentinel" do
      relationship_graph = compile_to_relationship_graph(<<-MANIFEST)
        class a {
          notify { "class a": }
        }

        include a
      MANIFEST

      relationship_graph.should enforce_order_with_edge(
        admissible_sentinel_of("class[A]"),
        "Notify[class a]")
    end

    it "completed sentinels should depend on their container's contents" do
      relationship_graph = compile_to_relationship_graph(<<-MANIFEST)
        class a {
          notify { "class a": }
        }

        include a
      MANIFEST

      relationship_graph.should enforce_order_with_edge(
          "Notify[class a]",
          completed_sentinel_of("class[A]"))
    end

    it "should remove all Component objects from the dependency graph" do
      relationship_graph = compile_to_relationship_graph(<<-MANIFEST)
        class a {
          notify { "class a": }
        }
        define b() {
          notify { "define b": }
        }

        include a
        b { "testing": }
      MANIFEST

      relationship_graph.vertices.find_all { |v| v.is_a?(Puppet::Type.type(:component)) }.should be_empty
    end

    it "should remove all Stage resources from the dependency graph" do
      relationship_graph = compile_to_relationship_graph(<<-MANIFEST)
        notify { "class a": }
      MANIFEST

      relationship_graph.vertices.find_all { |v| v.is_a?(Puppet::Type.type(:stage)) }.should be_empty
    end

    it "should retain labels on non-containment edges" do
      relationship_graph = compile_to_relationship_graph(<<-MANIFEST)
        class a {
          notify { "class a": }
        }
        define b() {
          notify { "define b": }
        }

        include a
        Class[a] ~> b { "testing": }
      MANIFEST

      relationship_graph.edges_between(
        vertex_called(relationship_graph, completed_sentinel_of("class[A]")),
        vertex_called(relationship_graph, admissible_sentinel_of("b[testing]")))[0].label.
        should == {:callback => :refresh, :event => :ALL_EVENTS}
    end

    it "should not add labels to edges that have none" do
      relationship_graph = compile_to_relationship_graph(<<-MANIFEST)
        class a {
          notify { "class a": }
        }
        define b() {
          notify { "define b": }
        }

        include a
        Class[a] -> b { "testing": }
      MANIFEST

      relationship_graph.edges_between(
        vertex_called(relationship_graph, completed_sentinel_of("class[A]")),
        vertex_called(relationship_graph, admissible_sentinel_of("b[testing]")))[0].label.
        should be_empty
    end

    it "should copy notification labels to all created edges" do
      relationship_graph = compile_to_relationship_graph(<<-MANIFEST)
        class a {
          notify { "class a": }
        }
        define b() {
          notify { "define b": }
        }

        include a
        Class[a] ~> b { "testing": }
      MANIFEST

      relationship_graph.edges_between(
        vertex_called(relationship_graph, admissible_sentinel_of("b[testing]")),
        vertex_called(relationship_graph, "Notify[define b]"))[0].label.
        should == {:callback => :refresh, :event => :ALL_EVENTS}
    end
  end

  def vertex_called(graph, name)
    graph.vertices.find { |v| v.ref =~ /#{Regexp.escape(name)}/ }
  end

  def stub_vertex(name)
    stub "vertex #{name}", :ref => name
  end
end