-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathtargetsfile_test.go
More file actions
120 lines (101 loc) · 3.96 KB
/
targetsfile_test.go
File metadata and controls
120 lines (101 loc) · 3.96 KB
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
package apply
import (
"encoding/json"
"os"
"github.com/rancher/fleet/integrationtests/cli"
"github.com/rancher/fleet/internal/cmd/cli/apply"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Fleet apply targets", func() {
var (
dirs []string
options apply.Options
)
JustBeforeEach(func() {
err := fleetApply("targets", dirs, options)
Expect(err).NotTo(HaveOccurred())
})
When("Targets file is empty, and overrideTargets is not provided", func() {
BeforeEach(func() {
dirs = []string{cli.AssetsPath + "targets/simple"}
})
It("Bundle contains the default target", func() {
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
Expect(bundle.Spec.Targets).To(HaveLen(1))
Expect(bundle.Spec.Targets[0].ClusterGroup).To(Equal("default"))
Expect(bundle.Spec.TargetRestrictions).To(BeEmpty())
})
})
When("Targets file is empty, and overrideTargets is provided", func() {
BeforeEach(func() {
dirs = []string{cli.AssetsPath + "targets/override"}
})
It("Bundle contains targets and targetRestrictions from override", func() {
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
Expect(bundle.Spec.Targets).To(HaveLen(1))
Expect(bundle.Spec.Targets[0].ClusterName).To(Equal("overridden"))
Expect(bundle.Spec.TargetRestrictions).To(HaveLen(1))
Expect(bundle.Spec.TargetRestrictions[0].ClusterName).To(Equal("overridden"))
})
})
When("Targets file contains one target, and overrideTargets is not provided", func() {
var (
targets []fleet.BundleTarget
targetRestrictions []fleet.BundleTargetRestriction
)
BeforeEach(func() {
targets = []fleet.BundleTarget{{Name: "target1", ClusterName: "test1", Source: "gitrepo"}}
targetRestrictions = []fleet.BundleTargetRestriction{{Name: "target1", ClusterName: "test1"}}
file := createTargetsFile(targets, targetRestrictions)
options = apply.Options{TargetsFile: file.Name()}
dirs = []string{cli.AssetsPath + "targets/simple"}
})
It("Bundle contains targets and targetRestrictions from the targets file", func() {
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
Expect(bundle.Spec.Targets).To(Equal(targets))
Expect(bundle.Spec.TargetRestrictions).To(Equal(targetRestrictions))
})
})
When("Targets file contains one target, and overrideTargets is provided", func() {
var (
targets []fleet.BundleTarget
targetRestrictions []fleet.BundleTargetRestriction
)
BeforeEach(func() {
targets = []fleet.BundleTarget{{Name: "target1", ClusterName: "test1", Source: "gitrepo"}}
targetRestrictions = []fleet.BundleTargetRestriction{{Name: "target1", ClusterName: "test1"}}
file := createTargetsFile(targets, targetRestrictions)
options = apply.Options{TargetsFile: file.Name()}
dirs = []string{cli.AssetsPath + "targets/override"}
})
It("Bundle contains targets and targetRestrictions from override", func() {
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
Expect(bundle.Spec.Targets).To(Not(Equal(targets)))
Expect(bundle.Spec.TargetRestrictions).To(Not(Equal(targetRestrictions)))
Expect(bundle.Spec.Targets).To(HaveLen(1))
Expect(bundle.Spec.Targets[0].ClusterName).To(Equal("overridden"))
Expect(bundle.Spec.TargetRestrictions).To(HaveLen(1))
Expect(bundle.Spec.TargetRestrictions[0].ClusterName).To(Equal("overridden"))
})
})
})
func createTargetsFile(targets []fleet.BundleTarget, targetRestrictions []fleet.BundleTargetRestriction) *os.File {
tmpDir := GinkgoT().TempDir()
file, err := os.CreateTemp(tmpDir, "targets")
Expect(err).NotTo(HaveOccurred())
spec := &fleet.BundleSpec{
Targets: targets,
TargetRestrictions: targetRestrictions,
}
data, err := json.Marshal(spec)
Expect(err).NotTo(HaveOccurred())
_, err = file.Write(data)
Expect(err).NotTo(HaveOccurred())
return file
}