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

import (
	"errors"
	"testing"
)

func TestMultiString(t *testing.T) {
	m := Multi{
		Resource{Type: "File", Name: "/tmp/a"},
		Resource{Type: "File", Name: "/tmp/b"},
	}

	want := "File[/tmp/a], File[/tmp/b]"
	if got := m.String(); got != want {
		t.Errorf("Multi.String() = %q, want %q", got, want)
	}
}

func TestMultiID(t *testing.T) {
	m := Multi{
		Resource{Type: "File", Name: "/tmp/a"},
		Resource{Type: "File", Name: "/tmp/b"},
	}

	want := "File[/tmp/a]+File[/tmp/b]"
	if got := m.ID(); got != want {
		t.Errorf("Multi.ID() = %q, want %q", got, want)
	}
}

func TestMultiApply(t *testing.T) {
	tests := []struct {
		name     string
		appliers []Applier
		wantErr  bool
	}{
		{
			name: "all success",
			appliers: []Applier{
				ApplierFunc(func() error { return nil }),
				ApplierFunc(func() error { return nil }),
			},
			wantErr: false,
		},
		{
			name: "one failure",
			appliers: []Applier{
				ApplierFunc(func() error { return nil }),
				ApplierFunc(func() error { return errors.New("fail 1") }),
			},
			wantErr: true,
		},
		{
			name: "multiple failures",
			appliers: []Applier{
				ApplierFunc(func() error { return errors.New("fail 1") }),
				ApplierFunc(func() error { return errors.New("fail 2") }),
			},
			wantErr: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			var resources []Resource
			for _, app := range tt.appliers {
				resources = append(resources, Resource{
					applier: app,
				})
			}

			m := Multi(resources)
			err := m.Apply()

			if (err != nil) != tt.wantErr {
				t.Errorf("Multi.Apply() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}