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

import (
	"testing"
)

func TestResourceID(t *testing.T) {
	resetRepository()
	res := Register("File", "/tmp/foo.txt")
	expected := "File[/tmp/foo.txt]"
	if res.ID() != expected {
		t.Errorf("expected ID %s, got %s", expected, res.ID())
	}
}

func TestResourceString(t *testing.T) {
	resetRepository()
	res := Register("File", "/tmp/foo.txt")
	expected := "File[/tmp/foo.txt]"
	if res.String() != expected {
		t.Errorf("expected String %s, got %s", expected, res.String())
	}
}

func TestNew(t *testing.T) {
	resetRepository()
	type_ := "File"
	name := "/tmp/foo.txt"
	res := Register(type_, name)

	if res.Type != type_ {
		t.Errorf("expected type %s, got %s", type_, res.Type)
	}
	if res.Name != name {
		t.Errorf("expected name %s, got %s", name, res.Name)
	}
	if res.dependsOn == nil {
		t.Error("expected dependsOn map to be initialized, got nil")
	}
}

func TestRepositoryRegister(t *testing.T) {
	resetRepository()
	repo := getRepository()
	res := Register("File", "/tmp/foo.txt")

	// First registration already happened in New()
	// But we can try to register again via the repository directly
	if err := repo.register(res); err == nil {
		t.Error("expected error when registering the same resource twice, got nil")
	}

	// Registration of a different resource should succeed
	res2 := Register("File", "/tmp/bar.txt")
	if err := repo.register(res2); err == nil {
		t.Error("expected error when registering the same resource twice, got nil")
	}
}