forked from cloudfoundry/guardian
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbundler_test.go
110 lines (90 loc) · 3.27 KB
/
bundler_test.go
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
package rundmc_test
import (
"errors"
spec "code.cloudfoundry.org/guardian/gardener/container-spec"
"code.cloudfoundry.org/guardian/rundmc"
"code.cloudfoundry.org/guardian/rundmc/goci"
fakes "code.cloudfoundry.org/guardian/rundmc/rundmcfakes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/opencontainers/runtime-spec/specs-go"
)
var _ = Describe("BundleTemplate", func() {
var (
bundler rundmc.BundleTemplate
containerDir = "some-container-dir"
)
Context("when there is only one rule", func() {
var rule *fakes.FakeBundlerRule
BeforeEach(func() {
rule = new(fakes.FakeBundlerRule)
bundler = rundmc.BundleTemplate{
Rules: []rundmc.BundlerRule{rule},
}
})
It("returns the bundle from the first rule", func() {
returnedSpec := goci.Bndl{}.WithRootFS("something")
rule.ApplyStub = func(bndle goci.Bndl, spec spec.DesiredContainerSpec, containerDir string) (goci.Bndl, error) {
Expect(spec.BaseConfig.Root.Path).To(Equal("the-rootfs"))
return returnedSpec, nil
}
result, err := bundler.Generate(spec.DesiredContainerSpec{BaseConfig: specs.Spec{Root: &specs.Root{Path: "the-rootfs"}}}, containerDir)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(returnedSpec))
})
It("returns the error from the first failing rule", func() {
rule.ApplyReturns(goci.Bndl{}, errors.New("didn't work"))
_, err := bundler.Generate(spec.DesiredContainerSpec{BaseConfig: specs.Spec{Root: &specs.Root{Path: "the-rootfs"}}}, containerDir)
Expect(err).To(MatchError(ContainSubstring("didn't work")))
})
It("passes an empty bundle, the desired spec, and container dir to the first rule", func() {
spec := spec.DesiredContainerSpec{Handle: "some-handle"}
bundler.Generate(spec, containerDir)
Expect(rule.ApplyCallCount()).To(Equal(1))
bndl, actualSpec, actualContainerDir := rule.ApplyArgsForCall(0)
Expect(bndl).To(Equal(goci.Bndl{}))
Expect(actualSpec).To(Equal(spec))
Expect(actualContainerDir).To(Equal(containerDir))
})
})
Context("with multiple rules", func() {
var (
ruleA, ruleB *fakes.FakeBundlerRule
)
BeforeEach(func() {
ruleA = new(fakes.FakeBundlerRule)
ruleB = new(fakes.FakeBundlerRule)
bundler = rundmc.BundleTemplate{
Rules: []rundmc.BundlerRule{
ruleA, ruleB,
},
}
})
It("calls all the rules", func() {
bundler.Generate(spec.DesiredContainerSpec{}, containerDir)
Expect(ruleA.ApplyCallCount()).To(Equal(1))
Expect(ruleB.ApplyCallCount()).To(Equal(1))
})
It("passes the bundle from the first rule to the subsequent rules", func() {
bndl := goci.Bndl{}.WithMounts(
specs.Mount{Destination: "test_a"},
specs.Mount{Destination: "test_b"},
)
ruleA.ApplyReturns(bndl, nil)
bundler.Generate(spec.DesiredContainerSpec{}, containerDir)
Expect(ruleB.ApplyCallCount()).To(Equal(1))
recBndl, _, _ := ruleB.ApplyArgsForCall(0)
Expect(recBndl).To(Equal(bndl))
})
It("returns the results of the last rule", func() {
bndl := goci.Bndl{}.WithMounts(
specs.Mount{Destination: "test_a"},
specs.Mount{Destination: "test_b"},
)
ruleB.ApplyReturns(bndl, nil)
recBndl, err := bundler.Generate(spec.DesiredContainerSpec{}, containerDir)
Expect(err).NotTo(HaveOccurred())
Expect(recBndl).To(Equal(bndl))
})
})
})