github.com/sleungcy-sap/cli@v7.1.0+incompatible/integration/v6/isolated/update_security_group_command_test.go (about) 1 package isolated 2 3 import ( 4 "encoding/json" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 9 "code.cloudfoundry.org/cli/integration/helpers" 10 "code.cloudfoundry.org/cli/resources" 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 . "github.com/onsi/gomega/gbytes" 14 . "github.com/onsi/gomega/gexec" 15 ) 16 17 var _ = Describe("update-security-group command", func() { 18 When("--help flag is set", func() { 19 It("Displays command usage to output", func() { 20 session := helpers.CF("update-security-group", "--help") 21 displaysUpdateSecurityGroupHelpText(session) 22 Eventually(session).Should(Exit(0)) 23 }) 24 }) 25 26 var ( 27 securityGroup resources.SecurityGroup 28 securityGroupName string 29 updatedSecurityGroupRules []resources.Rule 30 tmpRulesDir string 31 updatedRulesPath string 32 username string 33 err error 34 ) 35 36 BeforeEach(func() { 37 username = helpers.LoginCF() 38 securityGroupName = helpers.NewSecurityGroupName() 39 securityGroup = resources.SecurityGroup{ 40 Name: securityGroupName, 41 Rules: []resources.Rule{}, 42 } 43 helpers.CreateSecurityGroup(securityGroup) 44 }) 45 46 BeforeEach(func() { 47 // create the JSON rules file 48 ports := "25,465,587" 49 description := "Give me a place to stand on, and I shall spam the world!" 50 updatedSecurityGroupRules = []resources.Rule{ 51 { 52 Protocol: "tcp", 53 Destination: "0.0.0.0/0", 54 Ports: &ports, 55 Description: &description, 56 }, 57 } 58 59 tmpRulesDir, err = ioutil.TempDir("", "spam-security-group") 60 Expect(err).ToNot(HaveOccurred()) 61 62 updatedRulesPath = filepath.Join(tmpRulesDir, "spam-security-group.json") 63 64 securityGroup, err := json.Marshal(updatedSecurityGroupRules) 65 Expect(err).ToNot(HaveOccurred()) 66 67 err = ioutil.WriteFile(updatedRulesPath, securityGroup, 0666) 68 Expect(err).ToNot(HaveOccurred()) 69 }) 70 71 AfterEach(func() { 72 helpers.DeleteSecurityGroup(securityGroup) 73 err = os.RemoveAll(tmpRulesDir) 74 Expect(err).ToNot(HaveOccurred()) 75 }) 76 77 It("updates the security group", func() { 78 session := helpers.CF("update-security-group", securityGroupName, updatedRulesPath) 79 Eventually(session).Should(Say(`Updating security group %s as %s`, securityGroupName, username)) 80 Eventually(session).Should(Exit(0)) 81 82 session = helpers.CF("security-group", securityGroupName) 83 Eventually(session).Should(Say(`Getting info for security group %s as %s\.\.\.`, securityGroupName, username)) 84 Eventually(session).Should(Say(`Name\s+%s`, securityGroupName)) 85 Eventually(session).Should(Say("Rules")) 86 Eventually(session).Should(Say("\\[")) 87 Eventually(session).Should(Say("{")) 88 Eventually(session).Should(Say(`"description": "Give me a place to stand on, and I shall spam the world!",`)) 89 Eventually(session).Should(Say(`"destination": "0.0.0.0/0",`)) 90 Eventually(session).Should(Say(`"ports": "25,465,587",`)) 91 Eventually(session).Should(Say(`"protocol": "tcp"`)) 92 Eventually(session).Should(Say("}")) 93 Eventually(session).Should(Say("]")) 94 Eventually(session).Should(Say("No spaces assigned")) 95 96 Eventually(session).Should(Exit(0)) 97 }) 98 99 When("the security group does not exist or is not visible to the user", func() { 100 BeforeEach(func() { 101 helpers.DeleteSecurityGroup(securityGroup) 102 }) 103 It("displays a 'not-found' security group error message and fails", func() { 104 session := helpers.CF("update-security-group", securityGroupName, updatedRulesPath) 105 Eventually(session).Should(Say("FAILED")) 106 Eventually(session).Should(Say("security group %s not found", securityGroupName)) 107 Eventually(session).Should(Exit(1)) 108 }) 109 }) 110 111 When("the JSON file with the security group's rules does not exist", func() { 112 It("displays a does-not-exist error message and displays help text", func() { 113 session := helpers.CF("update-security-group", securityGroupName, "/non/existent/path") 114 Eventually(session.Err).Should(Say("Incorrect Usage: The specified path '/non/existent/path' does not exist.")) 115 displaysUpdateSecurityGroupHelpText(session) 116 Eventually(session).Should(Exit(1)) 117 }) 118 }) 119 120 When("the JSON file with the security group's rules is not JSON", func() { 121 BeforeEach(func() { 122 err = ioutil.WriteFile(updatedRulesPath, []byte("I'm definitely not JSON"), 0666) 123 Expect(err).ToNot(HaveOccurred()) 124 }) 125 It("displays a an incorrect JSON format message and fails", func() { 126 session := helpers.CF("update-security-group", securityGroupName, updatedRulesPath) 127 Eventually(session).Should(Say("FAILED")) 128 Eventually(session).Should(Say("Incorrect json format: invalid character 'I' looking for beginning of value")) 129 Eventually(session).Should(Exit(1)) 130 }) 131 }) 132 133 When("the JSON file with the security group's rules doesn't have the proper keys", func() { 134 BeforeEach(func() { 135 err = ioutil.WriteFile(updatedRulesPath, []byte(`[{"invalid-key":"invalid-value"}]`), 0666) 136 Expect(err).ToNot(HaveOccurred()) 137 }) 138 It("returns the server error and fails", func() { 139 session := helpers.CF("update-security-group", securityGroupName, updatedRulesPath) 140 Eventually(session).Should(Say(`Updating security group %s as %s`, securityGroupName, username)) 141 Eventually(session).Should(Say("FAILED")) 142 Eventually(session).Should(Say("Server error, status code: 400, error code: 300001")) 143 Eventually(session).Should(Exit(1)) 144 }) 145 }) 146 147 When("there are unexpected arguments", func() { 148 It("complains that there are unexpected arguments, fails, and prints the help text", func() { 149 session := helpers.CF("update-security-group", securityGroupName, updatedRulesPath, "unexpected-argument") 150 Eventually(session).Should(Say("FAILED")) 151 Eventually(session).Should(Say(`Incorrect Usage. Requires SECURITY_GROUP and PATH_TO_JSON_RULES_FILE as arguments`)) 152 Eventually(session).Should(Exit(1)) 153 }) 154 }) 155 }) 156 157 func displaysUpdateSecurityGroupHelpText(session *Session) { 158 Eventually(session).Should(Say("NAME:")) 159 Eventually(session).Should(Say("update-security-group - Update a security group")) 160 Eventually(session).Should(Say("USAGE:")) 161 Eventually(session).Should(Say("cf update-security-group SECURITY_GROUP PATH_TO_JSON_RULES_FILE")) 162 Eventually(session).Should(Say("The provided path can be an absolute or relative path to a file.")) 163 Eventually(session).Should(Say("It should have a single array with JSON objects inside describing the rules.")) 164 Eventually(session).Should(Say("Valid json file example:")) 165 Eventually(session).Should(Say("\\[")) 166 Eventually(session).Should(Say("{")) 167 Eventually(session).Should(Say(`\s+"protocol": "tcp",`)) 168 Eventually(session).Should(Say(`"destination": "10.0.11.0/24",`)) 169 Eventually(session).Should(Say(`"ports": "80,443",`)) 170 Eventually(session).Should(Say(`"description": "Allow http and https traffic from ZoneA"`)) 171 Eventually(session).Should(Say("}")) 172 Eventually(session).Should(Say("]")) 173 Eventually(session).Should(Say("TIP: Changes will not apply to existing running applications until they are restarted.")) 174 Eventually(session).Should(Say("SEE ALSO:")) 175 Eventually(session).Should(Say("restage, security-groups")) 176 }