-
Notifications
You must be signed in to change notification settings - Fork 13
/
exec_d_test.go
130 lines (107 loc) · 3.87 KB
/
exec_d_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright 2018-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package libcnb_test
import (
"fmt"
"testing"
. "github.com/onsi/gomega"
"github.com/sclevine/spec"
"github.com/stretchr/testify/mock"
"github.com/buildpacks/libcnb/v2"
"github.com/buildpacks/libcnb/v2/mocks"
)
func testExecD(t *testing.T, _ spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
exitHandler *mocks.ExitHandler
execdWriter *mocks.ExecDWriter
)
it.Before(func() {
execdWriter = &mocks.ExecDWriter{}
execdWriter.On("Write", mock.Anything).Return(nil)
exitHandler = &mocks.ExitHandler{}
exitHandler.On("Error", mock.Anything)
exitHandler.On("Pass", mock.Anything)
exitHandler.On("Fail", mock.Anything)
})
it("encounters the wrong number of arguments", func() {
libcnb.RunExecD(map[string]libcnb.ExecD{},
libcnb.WithArguments([]string{}),
libcnb.WithExitHandler(exitHandler),
)
Expect(exitHandler.Calls[0].Arguments.Get(0)).To(MatchError("expected command name"))
})
it("encounters an unsupported execd binary name", func() {
libcnb.RunExecD(map[string]libcnb.ExecD{},
libcnb.WithArguments([]string{"/dne"}),
libcnb.WithExitHandler(exitHandler),
)
Expect(exitHandler.Calls[0].Arguments.Get(0)).To(MatchError("unsupported command dne"))
})
it("calls the appropriate execd for a given execd invoker binary", func() {
execd1 := &mocks.ExecD{}
execd2 := &mocks.ExecD{}
execd1.On("Execute", mock.Anything).Return(map[string]string{}, nil)
libcnb.RunExecD(map[string]libcnb.ExecD{"execd1": execd1, "execd2": execd2},
libcnb.WithArguments([]string{"execd1"}),
libcnb.WithExitHandler(exitHandler),
libcnb.WithExecDWriter(execdWriter),
)
Expect(execd1.Calls).To(HaveLen(1))
Expect(execd2.Calls).To(BeEmpty())
})
it("calls exitHandler with the error from the execd", func() {
e := &mocks.ExecD{}
err := fmt.Errorf("example error")
e.On("Execute", mock.Anything).Return(nil, err)
libcnb.RunExecD(map[string]libcnb.ExecD{"e": e},
libcnb.WithArguments([]string{"/bin/e"}),
libcnb.WithExitHandler(exitHandler),
libcnb.WithExecDWriter(execdWriter),
)
Expect(e.Calls).To(HaveLen(1))
Expect(execdWriter.Calls).To(HaveLen(0))
Expect(exitHandler.Calls[0].Arguments.Get(0)).To(MatchError(err))
})
it("calls execdWriter.write with the appropriate input", func() {
e := &mocks.ExecD{}
o := map[string]string{"test": "test"}
e.On("Execute", mock.Anything).Return(o, nil)
libcnb.RunExecD(map[string]libcnb.ExecD{"e": e},
libcnb.WithArguments([]string{"/bin/e"}),
libcnb.WithExitHandler(exitHandler),
libcnb.WithExecDWriter(execdWriter),
)
Expect(e.Calls).To(HaveLen(1))
Expect(execdWriter.Calls).To(HaveLen(1))
Expect(execdWriter.Calls[0].Method).To(BeIdenticalTo("Write"))
Expect(execdWriter.Calls[0].Arguments).To(HaveLen(1))
Expect(execdWriter.Calls[0].Arguments[0]).To(Equal(o))
})
it("calls exitHandler with the error from the execd", func() {
e := &mocks.ExecD{}
err := fmt.Errorf("example error")
e.On("Execute", mock.Anything).Return(nil, err)
libcnb.RunExecD(map[string]libcnb.ExecD{"e": e},
libcnb.WithArguments([]string{"/bin/e"}),
libcnb.WithExitHandler(exitHandler),
libcnb.WithExecDWriter(execdWriter),
)
Expect(e.Calls).To(HaveLen(1))
Expect(execdWriter.Calls).To(HaveLen(0))
Expect(exitHandler.Calls[0].Arguments.Get(0)).To(MatchError(err))
})
}