-
Notifications
You must be signed in to change notification settings - Fork 14
/
command.go
77 lines (61 loc) · 2.36 KB
/
command.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
// Copyright 2020-Present VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package testhelpers
import (
"bytes"
"testing"
kpackfakes "github.com/pivotal/kpack/pkg/client/clientset/versioned/fake"
kpacktesthelpers "github.com/pivotal/kpack/pkg/reconciler/testhelpers"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/runtime"
k8sfakes "k8s.io/client-go/kubernetes/fake"
clientgotesting "k8s.io/client-go/testing"
)
type CommandTest struct {
Objects []runtime.Object
StdIn string
Args []string
ExpectErr bool
ExpectedOutput string
ExpectedErrorOutput string
ExpectUpdates []clientgotesting.UpdateActionImpl
ExpectCreates []runtime.Object
ExpectDeletes []clientgotesting.DeleteActionImpl
ExpectPatches []string
}
func (c CommandTest) TestK8sAndKpack(t *testing.T, cmdFactory func(k8sClientSet *k8sfakes.Clientset, kpackClientSet *kpackfakes.Clientset) *cobra.Command) {
t.Helper()
listers := kpacktesthelpers.NewListers(c.Objects)
k8sClient := k8sfakes.NewSimpleClientset(listers.GetKubeObjects()...)
kpackClient := kpackfakes.NewSimpleClientset(listers.BuildServiceObjects()...)
cmd := cmdFactory(k8sClient, kpackClient)
cmd.SetArgs(c.Args)
inputBuffer := bytes.NewBufferString(c.StdIn)
cmd.SetIn(inputBuffer)
out := &bytes.Buffer{}
cmd.SetOut(out)
errOut := &bytes.Buffer{}
cmd.SetErr(errOut)
err := cmd.Execute()
if !c.ExpectErr {
require.NoError(t, err)
} else {
require.Error(t, err)
}
require.Equal(t, c.ExpectedOutput, out.String(), "Actual output does not match ExpectedOutput")
require.Equal(t, c.ExpectedErrorOutput, errOut.String(), "Actual error output does not match ExpectedErrorOutput")
TestK8sAndKpackActions(t, k8sClient, kpackClient, c.ExpectUpdates, c.ExpectCreates, c.ExpectDeletes, c.ExpectPatches)
}
func (c CommandTest) TestKpack(t *testing.T, cmdFactory func(clientSet *kpackfakes.Clientset) *cobra.Command) {
t.Helper()
c.TestK8sAndKpack(t, func(k8sClientSet *k8sfakes.Clientset, kpackClientSet *kpackfakes.Clientset) *cobra.Command {
return cmdFactory(kpackClientSet)
})
}
func (c CommandTest) TestK8s(t *testing.T, cmdFactory func(clientSet *k8sfakes.Clientset) *cobra.Command) {
t.Helper()
c.TestK8sAndKpack(t, func(k8sClientSet *k8sfakes.Clientset, kpackClientSet *kpackfakes.Clientset) *cobra.Command {
return cmdFactory(k8sClientSet)
})
}