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

     1  package global
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    10  	"code.cloudfoundry.org/cli/integration/helpers"
    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("create-security-group command", func() {
    18  	var (
    19  		dir               string
    20  		securityGroupName string
    21  		tempPath          string
    22  		userName          string
    23  	)
    24  
    25  	BeforeEach(func() {
    26  		userName = helpers.LoginCF()
    27  		securityGroupName = helpers.NewSecurityGroupName()
    28  		dir = helpers.TempDirAbsolutePath("", "security-group")
    29  
    30  		tempPath = filepath.Join(dir, "tmpfile.json")
    31  		err := ioutil.WriteFile(tempPath, []byte(`[{
    32  			"protocol": "tcp",
    33  			"destination": "10.0.11.0/24",
    34  			"ports": "80,443",
    35  			"description": "Allow http and https traffic from ZoneA"
    36  		}]`), 0666)
    37  		Expect(err).ToNot(HaveOccurred())
    38  	})
    39  
    40  	AfterEach(func() {
    41  		err := os.RemoveAll(dir)
    42  		Expect(err).NotTo(HaveOccurred())
    43  	})
    44  
    45  	Describe("help", func() {
    46  		When("--help flag is set", func() {
    47  			It("appears in cf help -a", func() {
    48  				session := helpers.CF("help", "-a")
    49  				Eventually(session).Should(Exit(0))
    50  				Expect(session).To(HaveCommandInCategoryWithDescription("create-security-group", "SECURITY GROUP", "Create a security group"))
    51  			})
    52  
    53  			It("Displays command usage to output", func() {
    54  				session := helpers.CF("create-security-group", "--help")
    55  				Eventually(session).Should(Say("NAME:"))
    56  				Eventually(session).Should(Say("create-security-group - Create a security group"))
    57  				Eventually(session).Should(Say("USAGE:"))
    58  				Eventually(session).Should(Say(`cf create-security-group SECURITY_GROUP PATH_TO_JSON_RULES_FILE`))
    59  				Eventually(session).Should(Say(`The provided path can be an absolute or relative path to a file. The file should have`))
    60  				Eventually(session).Should(Say(`a single array with JSON objects inside describing the rules. The JSON Base Object is`))
    61  				Eventually(session).Should(Say(`omitted and only the square brackets and associated child object are required in the file.`))
    62  
    63  				Eventually(session).Should(Say(`Valid json file example:`))
    64  				Eventually(session).Should(Say(`\s+\[`))
    65  				Eventually(session).Should(Say(`\s+{`))
    66  				Eventually(session).Should(Say(`\s+"protocol": "tcp",`))
    67  				Eventually(session).Should(Say(`\s+"destination": "10.0.11.0/24",`))
    68  				Eventually(session).Should(Say(`\s+"ports": "80,443",`))
    69  				Eventually(session).Should(Say(`\s+"description": "Allow http and https traffic from ZoneA"`))
    70  				Eventually(session).Should(Say(`\s+}`))
    71  				Eventually(session).Should(Say(`\s+\]`))
    72  				Eventually(session).Should(Say("SEE ALSO:"))
    73  				Eventually(session).Should(Say("bind-running-security-group, bind-security-group, bind-staging-security-group, security-groups"))
    74  				Eventually(session).Should(Exit(0))
    75  			})
    76  		})
    77  	})
    78  
    79  	When("not logged in", func() {
    80  		It("fails with the appropriate errors", func() {
    81  			helpers.CheckEnvironmentTargetedCorrectly(false, false, "", "create-security-group", "some-group", tempPath)
    82  		})
    83  	})
    84  
    85  	When("the environment is set up correctly", func() {
    86  		When("the security group name is not provided", func() {
    87  			It("tells the user that the security group name is required, prints help text, and exits 1", func() {
    88  				session := helpers.CF("create-security-group")
    89  
    90  				Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `SECURITY_GROUP` and `PATH_TO_JSON_RULES_FILE` were not provided"))
    91  				Eventually(session).Should(Say("NAME:"))
    92  				Eventually(session).Should(Exit(1))
    93  			})
    94  		})
    95  
    96  		When("the security group path is not valid", func() {
    97  			It("tells the user that the security group path does not exist, prints help text, and exits 1", func() {
    98  				session := helpers.CF("create-security-group", securityGroupName, "/invalid/path")
    99  
   100  				Eventually(session.Err).Should(Say("Incorrect Usage: The specified path '/invalid/path' does not exist."))
   101  				Eventually(session).Should(Say("NAME:"))
   102  				Eventually(session).Should(Exit(1))
   103  			})
   104  		})
   105  
   106  		When("the security group path has invalid JSON", func() {
   107  			BeforeEach(func() {
   108  				err := ioutil.WriteFile(tempPath, []byte("Invalid JSON!"), 0666)
   109  				Expect(err).ToNot(HaveOccurred())
   110  			})
   111  
   112  			It("tells the user that the security group path is required, prints help text, and exits 1", func() {
   113  				session := helpers.CF("create-security-group", securityGroupName, tempPath)
   114  				Eventually(session).Should(Say("Creating security group %s as %s...", securityGroupName, userName))
   115  				Eventually(session.Err).Should(Say("Incorrect json format: %s", strings.ReplaceAll(tempPath, "\\", "\\\\")))
   116  
   117  				Eventually(session.Err).Should(Say("Valid json file example:"))
   118  				Eventually(session.Err).Should(Say(`\[`))
   119  				Eventually(session.Err).Should(Say(`\s+{`))
   120  				Eventually(session.Err).Should(Say(`\s+"protocol": "tcp",`))
   121  				Eventually(session.Err).Should(Say(`\s+"destination": "10.244.1.18",`))
   122  				Eventually(session.Err).Should(Say(`\s+"ports": "3306"`))
   123  				Eventually(session.Err).Should(Say(`\s+}`))
   124  				Eventually(session.Err).Should(Say(`\]`))
   125  				Eventually(session).Should(Exit(1))
   126  			})
   127  		})
   128  
   129  		When("the security group does not exist", func() {
   130  			It("creates the security group", func() {
   131  				session := helpers.CF("create-security-group", securityGroupName, tempPath)
   132  				Eventually(session).Should(Say("Creating security group %s as %s...", securityGroupName, userName))
   133  				Eventually(session).Should(Say("OK"))
   134  				Eventually(session).Should(Exit(0))
   135  
   136  				session = helpers.CF("security-group", securityGroupName)
   137  				Eventually(session).Should(Say(`name:\s+%s`, securityGroupName))
   138  			})
   139  		})
   140  
   141  		When("the security group already exists", func() {
   142  			BeforeEach(func() {
   143  				Eventually(helpers.CF("create-security-group", securityGroupName, tempPath)).Should(Exit(0))
   144  			})
   145  
   146  			It("notifies the user that the security group already exists and exits 0", func() {
   147  				session := helpers.CF("create-security-group", securityGroupName, tempPath)
   148  				Eventually(session).Should(Say("Creating security group %s as %s...", securityGroupName, userName))
   149  				Eventually(session.Err).Should(Say(`Security group with name '%s' already exists\.`, securityGroupName))
   150  				Eventually(session).Should(Say("OK"))
   151  				Eventually(session).Should(Exit(0))
   152  			})
   153  		})
   154  	})
   155  })