Skip to content

Commit

Permalink
Fail command if EULA terms are not accepted
Browse files Browse the repository at this point in the history
When invoking a command and it is deemed necessary to display the
EULA prompt to solicit acceptance by the user, fail the command unless
acceptance is received, with a message stating that the terms have to
have to be accepted

Also updated unit tests for eula show as well as situations when
the prompt should be auto-triggered.
  • Loading branch information
vuil committed May 16, 2023
1 parent c7df086 commit fce2901
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
59 changes: 58 additions & 1 deletion pkg/command/eula_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,25 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"

"github.com/vmware-tanzu/tanzu-cli/pkg/constants"
"github.com/vmware-tanzu/tanzu-plugin-runtime/config"
)

// Executes the command and verify if prompt is expected to be shown or not
func checkForPromptOnExecute(cmd *cobra.Command, expectPrompt bool) {
err := cmd.Execute()
// Survey prompts when run without attached tty will fail. Use this fact
// to help detect that a prompt is indeed presented.
if expectPrompt {
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring("prompt failed"))
} else {
Expect(err).To(BeNil())
}
}

var _ = Describe("EULA command tests", func() {
Describe("config eula show tests", func() {
var (
Expand All @@ -32,14 +46,17 @@ var _ = Describe("EULA command tests", func() {
featureArray := strings.Split(constants.FeatureContextCommand, ".")
err = config.SetFeature(featureArray[1], featureArray[2], "true")
Expect(err).To(BeNil())

os.Setenv("TANZU_CLI_CEIP_OPT_IN_PROMPT_ANSWER", "No")
})
AfterEach(func() {
os.Unsetenv("TANZU_CONFIG")
os.Unsetenv("TANZU_CONFIG_NEXT_GEN")
os.Unsetenv("TANZU_CLI_CEIP_OPT_IN_PROMPT_ANSWER")
os.RemoveAll(tanzuConfigFile.Name())
os.RemoveAll(tanzuConfigFileNG.Name())
})
// TODO(vuil) : add tests for show command

Context("When invoking the accept command", func() {
It("should return successfully with agreement acceptance registered", func() {
eulaCmd := newEULACmd()
Expand All @@ -52,5 +69,45 @@ var _ = Describe("EULA command tests", func() {
Expect(eulaStatus).To(Equal(config.EULAStatusAccepted))
})
})
Context("When invoking the show command", func() {
It("should invoke the eula prompt", func() {
eulaCmd := newEULACmd()
eulaCmd.SetArgs([]string{"show"})
checkForPromptOnExecute(eulaCmd, true)
})

It("should invoke the eula prompt even if EULA is accepted", func() {
acceptCmd := newEULACmd()
acceptCmd.SetArgs([]string{"accept"})
err = acceptCmd.Execute()
Expect(err).To(BeNil())

eulaCmd := newEULACmd()
eulaCmd.SetArgs([]string{"show"})
checkForPromptOnExecute(eulaCmd, true)
})
})
Context("When invoking an arbitrary command", func() {
It("should invoke the eula prompt if EULA has not been accepted", func() {
os.Setenv("TANZU_CLI_EULA_PROMPT_ANSWER", "No")
cmd, err := NewRootCmd()
Expect(err).To(BeNil())
cmd.SetArgs([]string{"context", "list"})
err = cmd.Execute()
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring("terms not accepted"))
os.Unsetenv("TANZU_CLI_EULA_PROMPT_ANSWER")
})

It("should not invoke the eula prompt if EULA has been accepted", func() {
os.Setenv("TANZU_CLI_EULA_PROMPT_ANSWER", "yes")
cmd, err := NewRootCmd()
Expect(err).To(BeNil())
cmd.SetArgs([]string{"context", "list"})
err = cmd.Execute()
Expect(err).To(BeNil())
os.Unsetenv("TANZU_CLI_EULA_PROMPT_ANSWER")
})
})
})
})
8 changes: 8 additions & 0 deletions pkg/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package command

import (
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -132,6 +133,13 @@ func newRootCmd() *cobra.Command {
if err := cliconfig.ConfigureEULA(false); err != nil {
return err
}

configVal, _ := config.GetEULAStatus()
if configVal != config.EULAStatusAccepted {
fmt.Fprintf(os.Stderr, "The Tanzu CLI is only usable with reduced functionality until the General Terms are agreed to.\nPlease use `tanzu config eula show` to review the terms, or `tanzu config eula accept` to accept them directly\n")
return errors.New("terms not accepted")
}

if err := cliconfig.ConfigureCEIPOptIn(); err != nil {
return err
}
Expand Down

0 comments on commit fce2901

Please sign in to comment.