Skip to content

Commit

Permalink
Fix e2e test that depends on env
Browse files Browse the repository at this point in the history
  • Loading branch information
mortent committed Dec 17, 2020
1 parent 149e5d6 commit 03be6c5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
15 changes: 10 additions & 5 deletions commands/applycmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func GetApplyRunner(provider provider.Provider, loader manifestreader.ManifestLo
}
// Set the wrapper run to be the RunE function for the wrapped command.
applyRunner.Command.RunE = w.RunE
applyRunner.Command.PreRunE = w.PreRunE
return w
}

Expand All @@ -43,16 +44,20 @@ func (w *ApplyRunnerWrapper) Command() *cobra.Command {
return w.applyRunner.Command
}

// RunE runs the ResourceGroup CRD installation as a pre-step if an
// environment variable exists. Then the wrapped ApplyRunner is
// invoked. Returns an error if one happened. Swallows the
// "AlreadyExists" error for CRD installation.
func (w *ApplyRunnerWrapper) RunE(cmd *cobra.Command, args []string) error {
func (w *ApplyRunnerWrapper) PreRunE(_ *cobra.Command, args []string) error {
if len(args) > 0 {
if err := setters.CheckForRequiredSetters(args[0]); err != nil {
return err
}
}
return nil
}

// RunE runs the ResourceGroup CRD installation as a pre-step if an
// environment variable exists. Then the wrapped ApplyRunner is
// invoked. Returns an error if one happened. Swallows the
// "AlreadyExists" error for CRD installation.
func (w *ApplyRunnerWrapper) RunE(cmd *cobra.Command, args []string) error {
if _, exists := os.LookupEnv(resourceGroupEnv); exists {
klog.V(4).Infoln("wrapper applyRunner detected environment variable")
err := live.ApplyResourceGroupCRD(w.factory)
Expand Down
13 changes: 9 additions & 4 deletions commands/previewcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func GetPreviewRunner(provider provider.Provider, loader manifestreader.Manifest
}
// Set the wrapper run to be the RunE function for the wrapped command.
previewRunner.Command.RunE = w.RunE
previewRunner.Command.PreRunE = w.PreRunE
return w
}

Expand All @@ -35,14 +36,18 @@ func (w *PreviewRunnerWrapper) Command() *cobra.Command {
return w.previewRunner.Command
}

// RunE checks if required setters are set as a pre-step if Kptfile
// exists in the package path. Then the wrapped PreviewRunner is
// invoked. Returns an error if one happened.
func (w *PreviewRunnerWrapper) RunE(cmd *cobra.Command, args []string) error {
func (w *PreviewRunnerWrapper) PreRunE(_ *cobra.Command, args []string) error {
if len(args) > 0 {
if err := setters.CheckForRequiredSetters(args[0]); err != nil {
return err
}
}
return nil
}

// RunE checks if required setters are set as a pre-step if Kptfile
// exists in the package path. Then the wrapped PreviewRunner is
// invoked. Returns an error if one happened.
func (w *PreviewRunnerWrapper) RunE(cmd *cobra.Command, args []string) error {
return w.previewRunner.RunE(cmd, args)
}
16 changes: 12 additions & 4 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/GoogleContainerTools/kpt/internal/testutil"
"github.com/GoogleContainerTools/kpt/pkg/kptfile"
"github.com/GoogleContainerTools/kpt/run"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"sigs.k8s.io/kustomize/cmd/config/ext"
"sigs.k8s.io/kustomize/kyaml/openapi"
Expand Down Expand Up @@ -522,7 +523,7 @@ openAPI:
errMsg: `setter replicas is required but not set, please set it to new value and try again`,
},
{
name: "preview command setters pre-check pass, fail with no cluster",
name: "preview command setters pre-check pass",
command: "preview",
inputOpenAPI: `
apiVersion: v1alpha1
Expand All @@ -539,10 +540,9 @@ openAPI:
required: true
isSet: true
`,
errMsg: `Package uninitialized`,
},
{
name: "apply command setters pre-check pass, fail with no cluster",
name: "apply command setters pre-check pass",
command: "apply",
inputOpenAPI: `
apiVersion: v1alpha1
Expand All @@ -559,7 +559,6 @@ openAPI:
required: true
isSet: true
`,
errMsg: `Package uninitialized`,
},
}
for i := range tests {
Expand All @@ -581,6 +580,15 @@ openAPI:
}

cmd := run.GetMain()

subCommand, _, err := cmd.Find([]string{"live", test.command})
if !assert.NoError(t, err) {
t.FailNow()
}
subCommand.RunE = func(_ *cobra.Command, _ []string) error {
return nil
}

args := []string{"live", test.command, dir}
args = append(args, test.args...)
cmd.SetArgs(args)
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -686,20 +686,14 @@ k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89 h1:d4vVOjXm687F1iLSP2q3lyPPuyvTU
k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89/go.mod h1:sZAwmy6armz5eXlNoLmJcl4F1QuKu7sr+mFQ0byX7Ew=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.7/go.mod h1:PHgbrJT7lCHcxMU+mDHEm+nx46H4zuuHZkDP6icnhu0=
sigs.k8s.io/cli-utils v0.22.1-0.20201117031003-fd39030f0508 h1:uty/Bj8/fa2xX6CUpeehTy0JeOj0CykbWB7asovZ1PU=
sigs.k8s.io/cli-utils v0.22.1-0.20201117031003-fd39030f0508/go.mod h1:Mt1gLc/Nfa7Z3Lhbfk72uT2Kc4GNyuX4oMqEN9FbPMs=
sigs.k8s.io/cli-utils v0.22.2-0.20201210231122-103e4dc4231a h1:dRyYEIosdhURWTxk0LUZ64byfP/oDLBZZSd8HIvWXyc=
sigs.k8s.io/cli-utils v0.22.2-0.20201210231122-103e4dc4231a/go.mod h1:URzYOsy8WRWqD6dAcvuM7LSks8Nr0ccNDXkgQgQnEL8=
sigs.k8s.io/controller-runtime v0.6.0 h1:Fzna3DY7c4BIP6KwfSlrfnj20DJ+SeMBK8HSFvOk9NM=
sigs.k8s.io/controller-runtime v0.6.0/go.mod h1:CpYf5pdNY/B352A1TFLAS2JVSlnGQ5O2cftPHndTroo=
sigs.k8s.io/kustomize v2.0.3+incompatible h1:JUufWFNlI44MdtnjUqVnvh29rR37PQFzPbLXqhyOyX0=
sigs.k8s.io/kustomize v2.0.3+incompatible/go.mod h1:MkjgH3RdOWrievjo6c9T245dYlB5QeXV4WCbnt/PEpU=
sigs.k8s.io/kustomize/cmd/config v0.8.5 h1:e8uMA7LjAUss3cMefVoOY2tFr92brCTUAA6CmWgq57M=
sigs.k8s.io/kustomize/cmd/config v0.8.5/go.mod h1:PlZTWxL7Xi75mY64HBC9IVf2xwrEbzy3L7r7+19kON4=
sigs.k8s.io/kustomize/cmd/config v0.8.7-0.20201211170716-cc43a2d732d1 h1:hn0F38XEFVoa38ExOT6tA4HMptyKX1h0CtgqhSUvL9g=
sigs.k8s.io/kustomize/cmd/config v0.8.7-0.20201211170716-cc43a2d732d1/go.mod h1:e4PgdLUNnkf+Iapvjyb6gTG9DZQkDZIR6uS1Bv4YA6s=
sigs.k8s.io/kustomize/kyaml v0.9.4 h1:DDuzZtjIzFqp2IPy4DTyCI69Cl3bDgcJODjI6sjF9NY=
sigs.k8s.io/kustomize/kyaml v0.9.4/go.mod h1:UTm64bSWVdBUA8EQoYCxVOaBQxUdIOr5LKWxA4GNbkw=
sigs.k8s.io/kustomize/kyaml v0.10.3 h1:ARSJUMN/c3k31DYxRfZ+vp/UepUQjg9zCwny7Oj908I=
sigs.k8s.io/kustomize/kyaml v0.10.3/go.mod h1:RA+iCHA2wPCOfv6uG6TfXXWhYsHpgErq/AljxWKuxtg=
sigs.k8s.io/kustomize/kyaml v0.10.4-0.20201211170716-cc43a2d732d1 h1:Y/e0BZo4s0d7k0B7JNyUes3ADYG4csk7aBLlyhgSwG4=
Expand Down

0 comments on commit 03be6c5

Please sign in to comment.