github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/add_network_policy_command_test.go (about)

     1  package v3_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/actor/cfnetworkingaction"
     6  	"code.cloudfoundry.org/cli/command/commandfakes"
     7  	"code.cloudfoundry.org/cli/command/flag"
     8  	"code.cloudfoundry.org/cli/command/translatableerror"
     9  	. "code.cloudfoundry.org/cli/command/v3"
    10  	"code.cloudfoundry.org/cli/command/v3/v3fakes"
    11  	"code.cloudfoundry.org/cli/util/configv3"
    12  	"code.cloudfoundry.org/cli/util/ui"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/gbytes"
    16  )
    17  
    18  var _ = Describe("add-network-policy Command", func() {
    19  	var (
    20  		cmd             AddNetworkPolicyCommand
    21  		testUI          *ui.UI
    22  		fakeConfig      *commandfakes.FakeConfig
    23  		fakeSharedActor *commandfakes.FakeSharedActor
    24  		fakeActor       *v3fakes.FakeAddNetworkPolicyActor
    25  		binaryName      string
    26  		executeErr      error
    27  		srcApp          string
    28  		destApp         string
    29  		protocol        string
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		fakeActor = new(v3fakes.FakeAddNetworkPolicyActor)
    37  
    38  		srcApp = "some-app"
    39  		destApp = "some-other-app"
    40  		protocol = "tcp"
    41  
    42  		cmd = AddNetworkPolicyCommand{
    43  			UI:             testUI,
    44  			Config:         fakeConfig,
    45  			SharedActor:    fakeSharedActor,
    46  			Actor:          fakeActor,
    47  			RequiredArgs:   flag.AddNetworkPolicyArgs{SourceApp: srcApp},
    48  			DestinationApp: destApp,
    49  		}
    50  
    51  		binaryName = "faceman"
    52  		fakeConfig.BinaryNameReturns(binaryName)
    53  	})
    54  
    55  	JustBeforeEach(func() {
    56  		executeErr = cmd.Execute(nil)
    57  	})
    58  
    59  	Context("when checking target fails", func() {
    60  		BeforeEach(func() {
    61  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    62  		})
    63  
    64  		It("returns an error", func() {
    65  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    66  
    67  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    68  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    69  			Expect(checkTargetedOrg).To(BeTrue())
    70  			Expect(checkTargetedSpace).To(BeTrue())
    71  		})
    72  	})
    73  
    74  	Context("when the user is logged in, an org is targeted, and a space is targeted", func() {
    75  		BeforeEach(func() {
    76  			fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
    77  			fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"})
    78  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"})
    79  		})
    80  
    81  		Context("when protocol is specified but port is not", func() {
    82  			BeforeEach(func() {
    83  				cmd.Protocol = flag.NetworkProtocol{Protocol: protocol}
    84  			})
    85  
    86  			It("returns an error", func() {
    87  				Expect(executeErr).To(MatchError(translatableerror.NetworkPolicyProtocolOrPortNotProvidedError{}))
    88  				Expect(testUI.Out).NotTo(Say(`Adding network policy`))
    89  			})
    90  		})
    91  
    92  		Context("when port is specified but protocol is not", func() {
    93  			BeforeEach(func() {
    94  				cmd.Port = flag.NetworkPort{StartPort: 8080, EndPort: 8081}
    95  			})
    96  
    97  			It("returns an error", func() {
    98  				Expect(executeErr).To(MatchError(translatableerror.NetworkPolicyProtocolOrPortNotProvidedError{}))
    99  				Expect(testUI.Out).NotTo(Say(`Adding network policy`))
   100  			})
   101  		})
   102  
   103  		Context("when both protocol and port are specificed", func() {
   104  			BeforeEach(func() {
   105  				cmd.Protocol = flag.NetworkProtocol{Protocol: protocol}
   106  				cmd.Port = flag.NetworkPort{StartPort: 8080, EndPort: 8081}
   107  			})
   108  
   109  			Context("when the policy creation is successful", func() {
   110  				BeforeEach(func() {
   111  					fakeActor.AddNetworkPolicyReturns(cfnetworkingaction.Warnings{"some-warning-1", "some-warning-2"}, nil)
   112  				})
   113  
   114  				It("displays OK when no error occurs", func() {
   115  					Expect(executeErr).ToNot(HaveOccurred())
   116  					Expect(fakeActor.AddNetworkPolicyCallCount()).To(Equal(1))
   117  					passedSpaceGuid, passedSrcAppName, passedDestAppName, passedProtocol, passedStartPort, passedEndPort := fakeActor.AddNetworkPolicyArgsForCall(0)
   118  					Expect(passedSpaceGuid).To(Equal("some-space-guid"))
   119  					Expect(passedSrcAppName).To(Equal("some-app"))
   120  					Expect(passedDestAppName).To(Equal("some-other-app"))
   121  					Expect(passedProtocol).To(Equal("tcp"))
   122  					Expect(passedStartPort).To(Equal(8080))
   123  					Expect(passedEndPort).To(Equal(8081))
   124  
   125  					Expect(testUI.Out).To(Say(`Adding network policy to app %s in org some-org / space some-space as some-user\.\.\.`, srcApp))
   126  					Expect(testUI.Err).To(Say("some-warning-1"))
   127  					Expect(testUI.Err).To(Say("some-warning-2"))
   128  					Expect(testUI.Out).To(Say("OK"))
   129  				})
   130  			})
   131  
   132  			Context("when the policy creation is not successful", func() {
   133  				BeforeEach(func() {
   134  					fakeActor.AddNetworkPolicyReturns(cfnetworkingaction.Warnings{"some-warning-1", "some-warning-2"}, actionerror.ApplicationNotFoundError{Name: srcApp})
   135  				})
   136  
   137  				It("does not display OK when an error occurs", func() {
   138  					Expect(executeErr).To(MatchError(actionerror.ApplicationNotFoundError{Name: srcApp}))
   139  
   140  					Expect(testUI.Out).To(Say(`Adding network policy to app %s in org some-org / space some-space as some-user\.\.\.`, srcApp))
   141  					Expect(testUI.Err).To(Say("some-warning-1"))
   142  					Expect(testUI.Err).To(Say("some-warning-2"))
   143  					Expect(testUI.Out).ToNot(Say("OK"))
   144  				})
   145  			})
   146  		})
   147  
   148  		Context("when both protocol and port are not specified", func() {
   149  			It("defaults protocol to 'tcp' and port to '8080'", func() {
   150  				Expect(executeErr).ToNot(HaveOccurred())
   151  
   152  				Expect(fakeActor.AddNetworkPolicyCallCount()).To(Equal(1))
   153  				_, _, _, passedProtocol, passedStartPort, passedEndPort := fakeActor.AddNetworkPolicyArgsForCall(0)
   154  				Expect(passedProtocol).To(Equal("tcp"))
   155  				Expect(passedStartPort).To(Equal(8080))
   156  				Expect(passedEndPort).To(Equal(8080))
   157  			})
   158  		})
   159  	})
   160  })