github.com/sleungcy-sap/cli@v7.1.0+incompatible/integration/v7/isolated/network_policies_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"regexp"
     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("network-policies command", func() {
    14  	Describe("help", func() {
    15  		When("--help flag is set", func() {
    16  			It("Displays command usage to output", func() {
    17  				session := helpers.CF("network-policies", "--help")
    18  				Eventually(session).Should(Say("NAME:"))
    19  				Eventually(session).Should(Say("network-policies - List direct network traffic policies"))
    20  				Eventually(session).Should(Say("USAGE:"))
    21  				Eventually(session).Should(Say(regexp.QuoteMeta("cf network-policies [--source SOURCE_APP]")))
    22  				Eventually(session).Should(Say("OPTIONS:"))
    23  				Eventually(session).Should(Say("   --source      Source app to filter results by"))
    24  				Eventually(session).Should(Say("SEE ALSO:"))
    25  				Eventually(session).Should(Say("   add-network-policy, apps, remove-network-policy"))
    26  				Eventually(session).Should(Exit(0))
    27  			})
    28  		})
    29  	})
    30  
    31  	When("the environment is not setup correctly", func() {
    32  		It("fails with the appropriate errors", func() {
    33  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "network-policies")
    34  		})
    35  	})
    36  
    37  	When("the org and space are properly targeted", func() {
    38  		var (
    39  			orgName   string
    40  			spaceName string
    41  			appName   string
    42  		)
    43  
    44  		BeforeEach(func() {
    45  			orgName = helpers.NewOrgName()
    46  			spaceName = helpers.NewSpaceName()
    47  			appName = helpers.PrefixedRandomName("app")
    48  
    49  			helpers.SetupCF(orgName, spaceName)
    50  
    51  			helpers.WithHelloWorldApp(func(appDir string) {
    52  				Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack", "--no-start")).Should(Exit(0))
    53  			})
    54  
    55  			session := helpers.CF("add-network-policy", appName, appName)
    56  			Eventually(session).Should(Exit(0))
    57  		})
    58  
    59  		AfterEach(func() {
    60  			helpers.QuickDeleteOrg(orgName)
    61  		})
    62  
    63  		When("policies exists", func() {
    64  			It("lists all the policies", func() {
    65  				session := helpers.CF("network-policies")
    66  
    67  				username, _ := helpers.GetCredentials()
    68  				Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
    69  				Consistently(session).ShouldNot(Say("OK"))
    70  				Eventually(session).Should(Say(`source\s+destination\s+protocol\s+ports\s+destination space\s+destination org`))
    71  				Eventually(session).Should(Say(`%s\s+%s\s+tcp\s+8080\s+%s\s+%s`, appName, appName, spaceName, orgName))
    72  				Eventually(session).Should(Exit(0))
    73  			})
    74  		})
    75  
    76  		When("policy has a destination in another org and space", func() {
    77  			var (
    78  				destOrg   string
    79  				destSpace string
    80  				destApp   string
    81  			)
    82  
    83  			BeforeEach(func() {
    84  				destOrg = helpers.NewOrgName()
    85  				destSpace = helpers.NewSpaceName()
    86  				destApp = helpers.PrefixedRandomName("destapp")
    87  
    88  				helpers.SetupCF(destOrg, destSpace)
    89  
    90  				helpers.WithHelloWorldApp(func(appDir string) {
    91  					Eventually(helpers.CF("push", destApp, "-p", appDir, "-b", "staticfile_buildpack", "--no-start")).Should(Exit(0))
    92  				})
    93  
    94  				helpers.SetupCF(orgName, spaceName)
    95  
    96  				session := helpers.CF("add-network-policy", appName, destApp, "-o", destOrg, "-s", destSpace)
    97  				username, _ := helpers.GetCredentials()
    98  				Eventually(session).Should(Say(`Adding network policy from app %s in org %s / space %s to app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, destApp, destOrg, destSpace, username))
    99  				Eventually(session).Should(Say("OK"))
   100  				Eventually(session).Should(Exit(0))
   101  			})
   102  
   103  			It("lists the policy", func() {
   104  				session := helpers.CF("network-policies")
   105  
   106  				username, _ := helpers.GetCredentials()
   107  				Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
   108  				Eventually(session).Should(Say(`source\s+destination\s+protocol\s+ports\s+destination space\s+destination org`))
   109  				Eventually(session).Should(Say(`%s\s+%s\s+tcp\s+8080\s+%s\s+%s`, appName, destApp, destSpace, destOrg))
   110  				Eventually(session).Should(Exit(0))
   111  			})
   112  		})
   113  
   114  		When("policies are filtered by a source app", func() {
   115  			var srcAppName string
   116  			BeforeEach(func() {
   117  				srcAppName = helpers.PrefixedRandomName("app")
   118  				helpers.WithHelloWorldApp(func(appDir string) {
   119  					Eventually(helpers.CF("push", srcAppName, "-p", appDir, "-b", "staticfile_buildpack", "--no-start")).Should(Exit(0))
   120  				})
   121  
   122  				session := helpers.CF("add-network-policy", srcAppName, appName)
   123  				Eventually(session).Should(Exit(0))
   124  			})
   125  
   126  			It("lists only policies for which the app is a source", func() {
   127  				session := helpers.CF("network-policies", "--source", srcAppName)
   128  
   129  				username, _ := helpers.GetCredentials()
   130  				Eventually(session).Should(Say(`Listing network policies of app %s in org %s / space %s as %s\.\.\.`, srcAppName, orgName, spaceName, username))
   131  				Eventually(session).Should(Say(`source\s+destination\s+protocol\s+ports\s+destination space\s+destination org`))
   132  				Eventually(session).ShouldNot(Say(`%s\s+%s\s+tcp\s+8080\s+%s\s+%s`, appName, appName, spaceName, orgName))
   133  				Eventually(session).Should(Say(`%s\s+%s\s+tcp\s+8080\s+%s\s+%s`, srcAppName, appName, spaceName, orgName))
   134  				Eventually(session).Should(Exit(0))
   135  			})
   136  
   137  			When("policy has a destination in another space", func() {
   138  				var (
   139  					destOrg   string
   140  					destSpace string
   141  					destApp   string
   142  				)
   143  
   144  				BeforeEach(func() {
   145  					destOrg = helpers.NewOrgName()
   146  					destSpace = helpers.NewSpaceName()
   147  					destApp = helpers.PrefixedRandomName("destapp")
   148  
   149  					helpers.SetupCF(destOrg, destSpace)
   150  
   151  					helpers.WithHelloWorldApp(func(appDir string) {
   152  						Eventually(helpers.CF("push", destApp, "-p", appDir, "-b", "staticfile_buildpack", "--no-start")).Should(Exit(0))
   153  					})
   154  
   155  					helpers.SetupCF(orgName, spaceName)
   156  
   157  					session := helpers.CF("add-network-policy", appName, destApp, "-o", destOrg, "-s", destSpace)
   158  					username, _ := helpers.GetCredentials()
   159  					Eventually(session).Should(Say(`Adding network policy from app %s in org %s / space %s to app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, destApp, destOrg, destSpace, username))
   160  					Eventually(session).Should(Say("OK"))
   161  					Eventually(session).Should(Exit(0))
   162  				})
   163  
   164  				It("lists only policies for which the app is a source", func() {
   165  					session := helpers.CF("network-policies", "--source", appName)
   166  
   167  					username, _ := helpers.GetCredentials()
   168  					Eventually(session).Should(Say(`Listing network policies of app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
   169  					Eventually(session).Should(Say(`source\s+destination\s+protocol\s+ports\s+destination space\s+destination org`))
   170  					Eventually(session).Should(Say(`%s\s+%s\s+tcp\s+8080\s+%s\s+%s`, appName, appName, spaceName, orgName))
   171  					Eventually(session).Should(Say(`%s\s+%s\s+tcp\s+8080\s+%s\s+%s`, appName, destApp, destSpace, destOrg))
   172  					Eventually(session).ShouldNot(Say(`%s\s+%s\s+tcp\s+8080\s+%s\s+%s`, srcAppName, appName, spaceName, orgName))
   173  					Eventually(session).Should(Exit(0))
   174  				})
   175  			})
   176  		})
   177  
   178  		When("policies are filtered by a non-existent source app", func() {
   179  			It("returns an error", func() {
   180  				session := helpers.CF("network-policies", "--source", "pineapple")
   181  
   182  				username, _ := helpers.GetCredentials()
   183  				Eventually(session).Should(Say(`Listing network policies of app pineapple in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
   184  				Eventually(session.Err).Should(Say("App 'pineapple' not found"))
   185  				Eventually(session).Should(Say("FAILED"))
   186  				Eventually(session).Should(Exit(1))
   187  			})
   188  		})
   189  	})
   190  })