github.com/Thanhphan1147/cloudfoundry-cli@v7.1.0+incompatible/actor/v7action/feature_flag_test.go (about)

     1  package v7action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	. "code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/actor/v7action/v7actionfakes"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("FeatureFlag", func() {
    16  	var (
    17  		actor                     *Actor
    18  		fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		actor, fakeCloudControllerClient, _, _, _, _, _ = NewTestActor()
    23  	})
    24  
    25  	Describe("GetFeatureFlagByName", func() {
    26  		var (
    27  			featureFlagName = "flag1"
    28  			featureFlag     FeatureFlag
    29  			warnings        Warnings
    30  			executeErr      error
    31  		)
    32  
    33  		JustBeforeEach(func() {
    34  			featureFlag, warnings, executeErr = actor.GetFeatureFlagByName(featureFlagName)
    35  		})
    36  
    37  		When("getting feature flag fails", func() {
    38  			BeforeEach(func() {
    39  				fakeCloudControllerClient.GetFeatureFlagReturns(
    40  					ccv3.FeatureFlag{},
    41  					ccv3.Warnings{"this is a warning"},
    42  					errors.New("some-error"))
    43  			})
    44  
    45  			It("returns warnings and error", func() {
    46  				Expect(executeErr).To(MatchError("some-error"))
    47  				Expect(warnings).To(ConsistOf("this is a warning"))
    48  				Expect(fakeCloudControllerClient.GetFeatureFlagCallCount()).To(Equal(1))
    49  				nameArg := fakeCloudControllerClient.GetFeatureFlagArgsForCall(0)
    50  				Expect(nameArg).To(Equal(featureFlagName))
    51  			})
    52  		})
    53  
    54  		When("no feature flag is returned", func() {
    55  			BeforeEach(func() {
    56  				var ccFeatureFlag ccv3.FeatureFlag
    57  
    58  				fakeCloudControllerClient.GetFeatureFlagReturns(
    59  					ccFeatureFlag,
    60  					ccv3.Warnings{"this is a warning"},
    61  					ccerror.FeatureFlagNotFoundError{})
    62  			})
    63  
    64  			It("returns warnings and a FeatureFlagNotFoundError", func() {
    65  				Expect(executeErr).To(MatchError(actionerror.FeatureFlagNotFoundError{FeatureFlagName: featureFlagName}))
    66  				Expect(warnings).To(ConsistOf("this is a warning"))
    67  			})
    68  		})
    69  
    70  		When("getting feature flag is successful", func() {
    71  
    72  			BeforeEach(func() {
    73  				featureFlagName = "flag1"
    74  				ccFeatureFlag := ccv3.FeatureFlag{Name: "flag1"}
    75  				fakeCloudControllerClient.GetFeatureFlagReturns(
    76  					ccFeatureFlag,
    77  					ccv3.Warnings{"this is a warning"},
    78  					nil)
    79  			})
    80  
    81  			It("Returns the proper featureFlag", func() {
    82  				Expect(executeErr).ToNot(HaveOccurred())
    83  				Expect(warnings).To(ConsistOf("this is a warning"))
    84  				Expect(featureFlag).To(Equal(FeatureFlag{Name: "flag1"}))
    85  			})
    86  		})
    87  	})
    88  
    89  	Describe("GetFeatureFlags", func() {
    90  		var (
    91  			featureFlags []FeatureFlag
    92  			warnings     Warnings
    93  			executeErr   error
    94  		)
    95  
    96  		JustBeforeEach(func() {
    97  			featureFlags, warnings, executeErr = actor.GetFeatureFlags()
    98  		})
    99  
   100  		When("The client is successful", func() {
   101  			BeforeEach(func() {
   102  				fakeCloudControllerClient.GetFeatureFlagsReturns(
   103  					[]ccv3.FeatureFlag{
   104  						{
   105  							Name:    "flag1",
   106  							Enabled: false,
   107  						},
   108  						{
   109  							Name:    "flag2",
   110  							Enabled: true,
   111  						},
   112  					},
   113  					ccv3.Warnings{"some-cc-warning"},
   114  					nil,
   115  				)
   116  			})
   117  
   118  			It("Returns the list of feature flags", func() {
   119  				Expect(executeErr).ToNot(HaveOccurred())
   120  				Expect(warnings).To(ConsistOf(Warnings{"some-cc-warning"}))
   121  				Expect(featureFlags).To(ConsistOf(
   122  					FeatureFlag{
   123  						Name:    "flag1",
   124  						Enabled: false,
   125  					},
   126  					FeatureFlag{
   127  						Name:    "flag2",
   128  						Enabled: true,
   129  					},
   130  				))
   131  			})
   132  		})
   133  
   134  		When("The client errors", func() {
   135  			BeforeEach(func() {
   136  				fakeCloudControllerClient.GetFeatureFlagsReturns(nil, ccv3.Warnings{"some-cc-warning"}, errors.New("some-error"))
   137  			})
   138  
   139  			It("Returns the error", func() {
   140  				Expect(executeErr).To(MatchError(errors.New("some-error")))
   141  				Expect(warnings).To(ConsistOf("some-cc-warning"))
   142  			})
   143  		})
   144  	})
   145  
   146  	Describe("EnableFeatureFlag", func() {
   147  		var (
   148  			flagName        string
   149  			ccFlag          ccv3.FeatureFlag
   150  			expectedArgFlag ccv3.FeatureFlag
   151  			warnings        Warnings
   152  			executeErr      error
   153  		)
   154  
   155  		BeforeEach(func() {
   156  			flagName = "flag1"
   157  			ccFlag = ccv3.FeatureFlag{Name: flagName, Enabled: true}
   158  			expectedArgFlag = ccv3.FeatureFlag{Name: flagName, Enabled: true}
   159  		})
   160  
   161  		JustBeforeEach(func() {
   162  			warnings, executeErr = actor.EnableFeatureFlag(flagName)
   163  		})
   164  
   165  		When("The flag exists", func() {
   166  			BeforeEach(func() {
   167  				fakeCloudControllerClient.UpdateFeatureFlagReturns(
   168  					ccFlag,
   169  					ccv3.Warnings{"update-warning"},
   170  					nil,
   171  				)
   172  			})
   173  
   174  			It("returns warnings and no error", func() {
   175  				Expect(executeErr).ToNot(HaveOccurred())
   176  				Expect(warnings).To(Equal(Warnings{"update-warning"}))
   177  				Expect(fakeCloudControllerClient.UpdateFeatureFlagCallCount()).To(Equal(1))
   178  				argFlag := fakeCloudControllerClient.UpdateFeatureFlagArgsForCall(0)
   179  				Expect(argFlag).To(Equal(expectedArgFlag))
   180  			})
   181  		})
   182  
   183  		When("the flag doesn't exist", func() {
   184  			BeforeEach(func() {
   185  				fakeCloudControllerClient.UpdateFeatureFlagReturns(
   186  					ccv3.FeatureFlag{},
   187  					ccv3.Warnings{"update-warning"},
   188  					ccerror.FeatureFlagNotFoundError{},
   189  				)
   190  			})
   191  			It("returns warnings and a FeatureFlagNotFoundError", func() {
   192  				Expect(executeErr).To(MatchError(actionerror.FeatureFlagNotFoundError{FeatureFlagName: flagName}))
   193  				Expect(warnings).To(Equal(Warnings{"update-warning"}))
   194  				Expect(fakeCloudControllerClient.UpdateFeatureFlagCallCount()).To(Equal(1))
   195  				argFlag := fakeCloudControllerClient.UpdateFeatureFlagArgsForCall(0)
   196  				Expect(argFlag).To(Equal(expectedArgFlag))
   197  			})
   198  		})
   199  
   200  		When("the client errors", func() {
   201  			BeforeEach(func() {
   202  				fakeCloudControllerClient.UpdateFeatureFlagReturns(
   203  					ccv3.FeatureFlag{},
   204  					ccv3.Warnings{"update-warning"},
   205  					errors.New("some-random-error"),
   206  				)
   207  			})
   208  			It("returns warnings and a FeatureFlagNotFoundError", func() {
   209  				Expect(executeErr).To(MatchError(errors.New("some-random-error")))
   210  				Expect(warnings).To(Equal(Warnings{"update-warning"}))
   211  				Expect(fakeCloudControllerClient.UpdateFeatureFlagCallCount()).To(Equal(1))
   212  				argFlag := fakeCloudControllerClient.UpdateFeatureFlagArgsForCall(0)
   213  				Expect(argFlag).To(Equal(expectedArgFlag))
   214  			})
   215  		})
   216  	})
   217  
   218  	Describe("EnableFeatureFlag", func() {
   219  		var (
   220  			flagName        string
   221  			ccFlag          ccv3.FeatureFlag
   222  			expectedArgFlag ccv3.FeatureFlag
   223  			warnings        Warnings
   224  			executeErr      error
   225  		)
   226  
   227  		BeforeEach(func() {
   228  			flagName = "flag1"
   229  			ccFlag = ccv3.FeatureFlag{Name: flagName, Enabled: true}
   230  			expectedArgFlag = ccv3.FeatureFlag{Name: flagName, Enabled: false}
   231  		})
   232  
   233  		JustBeforeEach(func() {
   234  			warnings, executeErr = actor.DisableFeatureFlag(flagName)
   235  		})
   236  
   237  		When("The flag exists", func() {
   238  			BeforeEach(func() {
   239  				fakeCloudControllerClient.UpdateFeatureFlagReturns(
   240  					ccFlag,
   241  					ccv3.Warnings{"update-warning"},
   242  					nil,
   243  				)
   244  			})
   245  
   246  			It("returns warnings and no error", func() {
   247  				Expect(executeErr).ToNot(HaveOccurred())
   248  				Expect(warnings).To(Equal(Warnings{"update-warning"}))
   249  				Expect(fakeCloudControllerClient.UpdateFeatureFlagCallCount()).To(Equal(1))
   250  				argFlag := fakeCloudControllerClient.UpdateFeatureFlagArgsForCall(0)
   251  				Expect(argFlag).To(Equal(expectedArgFlag))
   252  			})
   253  		})
   254  
   255  		When("the flag doesn't exist", func() {
   256  			BeforeEach(func() {
   257  				fakeCloudControllerClient.UpdateFeatureFlagReturns(
   258  					ccv3.FeatureFlag{},
   259  					ccv3.Warnings{"update-warning"},
   260  					ccerror.FeatureFlagNotFoundError{},
   261  				)
   262  			})
   263  			It("returns warnings and a FeatureFlagNotFoundError", func() {
   264  				Expect(executeErr).To(MatchError(actionerror.FeatureFlagNotFoundError{FeatureFlagName: flagName}))
   265  				Expect(warnings).To(Equal(Warnings{"update-warning"}))
   266  				Expect(fakeCloudControllerClient.UpdateFeatureFlagCallCount()).To(Equal(1))
   267  				argFlag := fakeCloudControllerClient.UpdateFeatureFlagArgsForCall(0)
   268  				Expect(argFlag).To(Equal(expectedArgFlag))
   269  			})
   270  		})
   271  
   272  		When("the client errors", func() {
   273  			BeforeEach(func() {
   274  				fakeCloudControllerClient.UpdateFeatureFlagReturns(
   275  					ccv3.FeatureFlag{},
   276  					ccv3.Warnings{"update-warning"},
   277  					errors.New("some-random-error"),
   278  				)
   279  			})
   280  			It("returns warnings and a FeatureFlagNotFoundError", func() {
   281  				Expect(executeErr).To(MatchError(errors.New("some-random-error")))
   282  				Expect(warnings).To(Equal(Warnings{"update-warning"}))
   283  				Expect(fakeCloudControllerClient.UpdateFeatureFlagCallCount()).To(Equal(1))
   284  				argFlag := fakeCloudControllerClient.UpdateFeatureFlagArgsForCall(0)
   285  				Expect(argFlag).To(Equal(expectedArgFlag))
   286  			})
   287  		})
   288  	})
   289  })