github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/integration/v7/global/set_running_environment_variable_group_command_test.go (about)

     1  package global
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/integration/helpers"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  	. "github.com/onsi/gomega/gbytes"
    10  	. "github.com/onsi/gomega/gexec"
    11  )
    12  
    13  var _ = Describe("set-running-environment-variable-group command", func() {
    14  	var (
    15  		key1 string
    16  		key2 string
    17  		val1 string
    18  		val2 string
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		helpers.LoginCF()
    23  
    24  		key1 = helpers.PrefixedRandomName("key1")
    25  		key2 = helpers.PrefixedRandomName("key2")
    26  		val1 = helpers.PrefixedRandomName("val1")
    27  		val2 = helpers.PrefixedRandomName("val2")
    28  	})
    29  
    30  	AfterEach(func() {
    31  		session := helpers.CF("set-running-environment-variable-group", "{}")
    32  		Eventually(session).Should(Exit(0))
    33  	})
    34  
    35  	Describe("help", func() {
    36  		When("--help flag is set", func() {
    37  			It("Displays command usage to output", func() {
    38  				session := helpers.CF("set-running-environment-variable-group", "--help")
    39  				Eventually(session).Should(Say("NAME:"))
    40  				Eventually(session).Should(Say("set-running-environment-variable-group - Pass parameters as JSON to create a running environment variable group"))
    41  				Eventually(session).Should(Say("USAGE:"))
    42  				Eventually(session).Should(Say(`cf set-running-environment-variable-group '{"name":"value","name":"value"}'`))
    43  				Eventually(session).Should(Say("SEE ALSO:"))
    44  				Eventually(session).Should(Say("running-environment-variable-group, set-env"))
    45  				Eventually(session).Should(Exit(0))
    46  			})
    47  		})
    48  	})
    49  
    50  	It("sets running environment variables", func() {
    51  		json := fmt.Sprintf(`{"%s":"%s", "%s":"%s"}`, key1, val1, key2, val2)
    52  		session := helpers.CF("set-running-environment-variable-group", json)
    53  		Eventually(session).Should(Say("Setting the contents of the running environment variable group as"))
    54  		Eventually(session).Should(Say("OK"))
    55  		Eventually(session).Should(Exit(0))
    56  
    57  		session = helpers.CF("running-environment-variable-group")
    58  		Eventually(session).Should(Exit(0))
    59  		// We cannot use `Say()`, for the results are returned in a random order
    60  		Expect(string(session.Out.Contents())).To(MatchRegexp(`%s\s+%s`, key1, val1))
    61  		Expect(string(session.Out.Contents())).To(MatchRegexp(`%s\s+%s`, key2, val2))
    62  	})
    63  
    64  	When("user passes in '{}'", func() {
    65  		BeforeEach(func() {
    66  			json := fmt.Sprintf(`{"%s":"%s", "%s":"%s"}`, key1, val1, key2, val2)
    67  			session := helpers.CF("set-running-environment-variable-group", json)
    68  			Eventually(session).Should(Exit(0))
    69  		})
    70  
    71  		It("clears the environment group", func() {
    72  			json := fmt.Sprintf(`{}`)
    73  			session := helpers.CF("set-running-environment-variable-group", json)
    74  			Eventually(session).Should(Say("Setting the contents of the running environment variable group as"))
    75  			Eventually(session).Should(Say("OK"))
    76  			Eventually(session).Should(Exit(0))
    77  
    78  			session = helpers.CF("running-environment-variable-group")
    79  			Eventually(session).Should(Exit(0))
    80  			Expect(string(session.Out.Contents())).ToNot(MatchRegexp(fmt.Sprintf(`%s\s+%s`, key1, val1)))
    81  			Expect(string(session.Out.Contents())).ToNot(MatchRegexp(fmt.Sprintf(`%s\s+%s`, key2, val2)))
    82  		})
    83  	})
    84  
    85  	When("user unsets some, but not all. variables", func() {
    86  		BeforeEach(func() {
    87  			json := fmt.Sprintf(`{"%s":"%s", "%s":"%s"}`, key1, val1, key2, val2)
    88  			session := helpers.CF("set-running-environment-variable-group", json)
    89  			Eventually(session).Should(Exit(0))
    90  		})
    91  
    92  		It("clears the removed variables", func() {
    93  			json := fmt.Sprintf(`{"%s":"%s"}`, key1, val1)
    94  			session := helpers.CF("set-running-environment-variable-group", json)
    95  			Eventually(session).Should(Say("Setting the contents of the running environment variable group as"))
    96  			Eventually(session).Should(Say("OK"))
    97  			Eventually(session).Should(Exit(0))
    98  
    99  			session = helpers.CF("running-environment-variable-group")
   100  			Eventually(session).Should(Exit(0))
   101  			Expect(string(session.Out.Contents())).To(MatchRegexp(fmt.Sprintf(`%s\s+%s`, key1, val1)))
   102  			Expect(string(session.Out.Contents())).ToNot(MatchRegexp(fmt.Sprintf(`%s\s+%s`, key2, val2)))
   103  		})
   104  	})
   105  
   106  	When("user passes invalid JSON", func() {
   107  		It("fails helpfully", func() {
   108  			session := helpers.CF("set-running-environment-variable-group", `not json...`)
   109  			Eventually(session).Should(Say("Setting the contents of the running environment variable group as"))
   110  			Eventually(session.Err).Should(Say("Invalid environment variable group provided. Please provide a valid JSON object."))
   111  			Eventually(session).Should(Say("FAILED"))
   112  			Eventually(session).Should(Exit(1))
   113  		})
   114  	})
   115  })