github.com/loafoe/cli@v7.1.0+incompatible/integration/v7/global/update_security_group_command_test.go (about)

     1  package global
     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(Say("OK"))
    81  		Eventually(session).Should(Say(`TIP: Changes require an app restart \(for running\) or restage \(for staging\) to apply to existing applications.`))
    82  		Eventually(session).Should(Exit(0))
    83  
    84  		session = helpers.CF("security-group", securityGroupName)
    85  		Eventually(session).Should(Say(`Getting info for security group %s as %s\.\.\.`, securityGroupName, username))
    86  		Eventually(session).Should(Say(`name:\s+%s`, securityGroupName))
    87  		Eventually(session).Should(Say("rules:"))
    88  		Eventually(session).Should(Say("\\["))
    89  		Eventually(session).Should(Say("{"))
    90  		Eventually(session).Should(Say(`"protocol": "tcp",`))
    91  		Eventually(session).Should(Say(`"destination": "0.0.0.0/0",`))
    92  		Eventually(session).Should(Say(`"ports": "25,465,587",`))
    93  		Eventually(session).Should(Say(`"description": "Give me a place to stand on, and I shall spam the world!"`))
    94  		Eventually(session).Should(Say("}"))
    95  		Eventually(session).Should(Say("]"))
    96  		Eventually(session).Should(Say("No spaces assigned"))
    97  
    98  		Eventually(session).Should(Exit(0))
    99  	})
   100  
   101  	When("the security group does not exist or is not visible to the user", func() {
   102  		BeforeEach(func() {
   103  			helpers.DeleteSecurityGroup(securityGroup)
   104  		})
   105  		It("displays a 'not-found' security group error message and fails", func() {
   106  			session := helpers.CF("update-security-group", securityGroupName, updatedRulesPath)
   107  			Eventually(session).Should(Say("FAILED"))
   108  			Eventually(session.Err).Should(Say("Security group '%s' not found.", securityGroupName))
   109  			Eventually(session).Should(Exit(1))
   110  		})
   111  	})
   112  
   113  	When("the JSON file with the security group's rules does not exist", func() {
   114  		It("displays a does-not-exist error message and displays help text", func() {
   115  			session := helpers.CF("update-security-group", securityGroupName, "/non/existent/path")
   116  			Eventually(session.Err).Should(Say("Incorrect Usage: The specified path '/non/existent/path' does not exist."))
   117  			displaysUpdateSecurityGroupHelpText(session)
   118  			Eventually(session).Should(Exit(1))
   119  		})
   120  	})
   121  
   122  	When("the JSON file with the security group's rules is not JSON", func() {
   123  		BeforeEach(func() {
   124  			err = ioutil.WriteFile(updatedRulesPath, []byte("I'm definitely not JSON"), 0666)
   125  			Expect(err).ToNot(HaveOccurred())
   126  		})
   127  		It("displays a an incorrect JSON format message and fails", func() {
   128  			session := helpers.CF("update-security-group", securityGroupName, updatedRulesPath)
   129  			Eventually(session).Should(Say("FAILED"))
   130  			Eventually(session.Err).Should(helpers.SayPath("Incorrect json format: %s", updatedRulesPath))
   131  			Eventually(session).Should(Exit(1))
   132  		})
   133  	})
   134  
   135  	When("there are unexpected arguments", func() {
   136  		It("complains that there are unexpected arguments, fails, and prints the help text", func() {
   137  			session := helpers.CF("update-security-group", securityGroupName, updatedRulesPath, "unexpected-argument")
   138  			Eventually(session.Err).Should(Say(`Incorrect Usage: unexpected argument "unexpected-argument"`))
   139  			Eventually(session).Should(Say("FAILED"))
   140  			displaysUpdateSecurityGroupHelpText(session)
   141  			Eventually(session).Should(Exit(1))
   142  		})
   143  	})
   144  })
   145  
   146  func displaysUpdateSecurityGroupHelpText(session *Session) {
   147  	Eventually(session).Should(Say("NAME:"))
   148  	Eventually(session).Should(Say("update-security-group - Update a security group"))
   149  	Eventually(session).Should(Say("USAGE:"))
   150  	Eventually(session).Should(Say("cf update-security-group SECURITY_GROUP PATH_TO_JSON_RULES_FILE"))
   151  	Eventually(session).Should(Say("The provided path can be an absolute or relative path to a file. The file should have"))
   152  	Eventually(session).Should(Say("a single array with JSON objects inside describing the rules. The JSON Base Object is"))
   153  	Eventually(session).Should(Say("omitted and only the square brackets and associated child object are required in the file."))
   154  	Eventually(session).Should(Say("Valid json file example:"))
   155  	Eventually(session).Should(Say("\\["))
   156  	Eventually(session).Should(Say("{"))
   157  	Eventually(session).Should(Say(`"protocol": "tcp",`))
   158  	Eventually(session).Should(Say(`"destination": "10.0.11.0/24",`))
   159  	Eventually(session).Should(Say(`"ports": "80,443",`))
   160  	Eventually(session).Should(Say(`"description": "Allow http and https traffic from ZoneA"`))
   161  	Eventually(session).Should(Say("}"))
   162  	Eventually(session).Should(Say("]"))
   163  	Eventually(session).Should(Say(`TIP: Changes require an app restart \(for running\) or restage \(for staging\) to apply to existing applications.`))
   164  	Eventually(session).Should(Say("SEE ALSO:"))
   165  	Eventually(session).Should(Say("restage, security-groups"))
   166  }