summaryrefslogtreecommitdiff
path: root/internal/resource/dependencies_test.go
blob: e2987401618631c2c523534024fdf3bbba48e822 (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
package resource

import (
	"reflect"
	"testing"
)

func TestResourceDependencies(t *testing.T) {
	r := Resource{Type: "File", Name: "/tmp/a"}

	got := r.Dependencies()
	want := []string{"File[/tmp/a]"}
	if !reflect.DeepEqual(got, want) {
		t.Errorf("Resource.Dependencies() = %v, want %v", got, want)
	}
}

// TestMultiDependencies ensures a Multi flattens into each member's individual
// ID, so depending on a Multi records a dependency on every member.
func TestMultiDependencies(t *testing.T) {
	m := Multi{
		Resource{Type: "File", Name: "/tmp/a"},
		Resource{Type: "File", Name: "/tmp/b"},
	}

	got := m.Dependencies()
	want := []string{"File[/tmp/a]", "File[/tmp/b]"}
	if !reflect.DeepEqual(got, want) {
		t.Errorf("Multi.Dependencies() = %v, want %v", got, want)
	}
}

// TestRegisterWithDeps ensures Register seeds dependsOn from the variadic deps
// and de-duplicates repeated IDs.
func TestRegisterWithDeps(t *testing.T) {
	ResetRepository()

	res := Register("File", "/tmp/a", &mockApplier{}, "File[b]", "File[c]", "File[b]")

	if _, ok := res.dependsOn["File[b]"]; !ok {
		t.Error("expected dependency File[b]")
	}
	if _, ok := res.dependsOn["File[c]"]; !ok {
		t.Error("expected dependency File[c]")
	}
	if len(res.dependsOn) != 2 {
		t.Errorf("expected 2 unique dependencies, got %d: %v", len(res.dependsOn), res.dependsOn)
	}
}

func TestRegisterNoDeps(t *testing.T) {
	ResetRepository()

	res := Register("File", "/tmp/a", &mockApplier{})
	if res.dependsOn == nil {
		t.Error("expected dependsOn to be initialized, got nil")
	}
	if len(res.dependsOn) != 0 {
		t.Errorf("expected no dependencies, got %v", res.dependsOn)
	}
}

// TestSortedDependsOn ensures dependency IDs are returned sorted for stable log
// output regardless of insertion/map order.
func TestSortedDependsOn(t *testing.T) {
	ResetRepository()

	res := Register("File", "/tmp/a", &mockApplier{}, "File[c]", "File[a]", "File[b]")

	got := res.sortedDependsOn()
	want := []string{"File[a]", "File[b]", "File[c]"}
	if !reflect.DeepEqual(got, want) {
		t.Errorf("sortedDependsOn() = %v, want %v", got, want)
	}
}