1package backup
2
3import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 "github.com/stretchr/testify/require"
8
9 "github.com/bnema/gordon/internal/domain"
10)
11
12func TestSelectVolumeBackupTargets(t *testing.T) {
13 containers := []*domain.Container{
14 {
15 ID: "unmanaged",
16 Name: "unmanaged",
17 Labels: map[string]string{
18 domain.LabelDomain: "app.example.com",
19 },
20 VolumeMounts: []domain.ContainerVolumeMount{{Name: "gordon-app-data", Type: "volume", Destination: "/data"}},
21 },
22 {
23 ID: "app",
24 Name: "app",
25 Labels: map[string]string{
26 domain.LabelManaged: "true",
27 domain.LabelDomain: "app.example.com",
28 },
29 VolumeMounts: []domain.ContainerVolumeMount{
30 {Name: "gordon-app-example-com-data", Type: "volume", Destination: "/data"},
31 {Name: "gordon-app-example-com-cache", Type: "bind", Destination: "/cache"},
32 {Name: "random-anonymous", Type: "volume", Destination: "/anon"},
33 {Name: "", Type: "volume", Destination: "/empty"},
34 },
35 },
36 {
37 ID: "postgres",
38 Name: "postgres",
39 Labels: map[string]string{
40 domain.LabelManaged: "true",
41 domain.LabelAttachment: "true",
42 domain.LabelAttachedTo: "app.example.com",
43 },
44 VolumeMounts: []domain.ContainerVolumeMount{
45 {Name: "gordon-postgres-data", Type: "volume", Destination: "/var/lib/postgresql/data"},
46 {Name: "gordon-app-example-com-data", Type: "volume", Destination: "/shared"},
47 },
48 },
49 }
50
51 targets := SelectVolumeBackupTargets(containers, "gordon")
52
53 assert.Equal(t, []domain.VolumeBackupTarget{
54 {
55 Domain: "app.example.com",
56 ContainerName: "app",
57 ContainerID: "app",
58 VolumeName: "gordon-app-example-com-data",
59 MountPath: "/data",
60 },
61 {
62 Domain: "app.example.com",
63 ContainerName: "postgres",
64 ContainerID: "postgres",
65 VolumeName: "gordon-postgres-data",
66 MountPath: "/var/lib/postgresql/data",
67 },
68 }, targets)
69}
70
71func TestSelectVolumeBackupTargetsForScopeDedupesAfterFiltering(t *testing.T) {
72 containers := []*domain.Container{
73 {
74 ID: "alpha",
75 Name: "alpha",
76 Labels: map[string]string{
77 domain.LabelManaged: "true",
78 domain.LabelDomain: "alpha.example.com",
79 },
80 VolumeMounts: []domain.ContainerVolumeMount{{Name: "gordon-shared", Type: "volume", Destination: "/data"}},
81 },
82 {
83 ID: "beta",
84 Name: "beta",
85 Labels: map[string]string{
86 domain.LabelManaged: "true",
87 domain.LabelDomain: "beta.example.com",
88 },
89 VolumeMounts: []domain.ContainerVolumeMount{{Name: "gordon-shared", Type: "volume", Destination: "/data"}},
90 },
91 }
92
93 targets := SelectVolumeBackupTargetsForScope(containers, "gordon", "beta.example.com", "")
94
95 require.Len(t, targets, 1)
96 assert.Equal(t, "beta.example.com", targets[0].Domain)
97 assert.Equal(t, "gordon-shared", targets[0].VolumeName)
98}
99
100func TestSelectVolumeBackupTargetsRequiresDomain(t *testing.T) {
101 containers := []*domain.Container{
102 {
103 ID: "missing-domain",
104 Name: "missing-domain",
105 Labels: map[string]string{
106 domain.LabelManaged: "true",
107 },
108 VolumeMounts: []domain.ContainerVolumeMount{{Name: "gordon-data", Type: "volume", Destination: "/data"}},
109 },
110 }
111
112 targets := SelectVolumeBackupTargets(containers, "gordon")
113
114 assert.Empty(t, targets)
115}