github.com/sleungcy-sap/cli@v7.1.0+incompatible/plugin/plugin_shim_test.go (about)

     1  package plugin_test
     2  
     3  import (
     4  	"os/exec"
     5  	"path/filepath"
     6  
     7  	"code.cloudfoundry.org/cli/cf/util/testhelpers/rpcserver"
     8  	"code.cloudfoundry.org/cli/cf/util/testhelpers/rpcserver/rpcserverfakes"
     9  	"code.cloudfoundry.org/cli/plugin"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	"github.com/onsi/gomega/gbytes"
    13  	. "github.com/onsi/gomega/gexec"
    14  )
    15  
    16  var _ = Describe("Command", func() {
    17  	var (
    18  		validPluginPath = filepath.Join("..", "fixtures", "plugins", "test_1.exe")
    19  	)
    20  
    21  	Describe(".Start", func() {
    22  		It("Exits with status 1 and error message if no arguments are passed", func() {
    23  			args := []string{}
    24  			session, err := Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
    25  			Expect(err).ToNot(HaveOccurred())
    26  			Eventually(session, 2).Should(Exit(1))
    27  			Expect(session).To(gbytes.Say("This cf CLI plugin is not intended to be run on its own"))
    28  		})
    29  
    30  		It("Exits with status 1 if it cannot ping the host port passed as an argument", func() {
    31  			args := []string{"0", "0"}
    32  			session, err := Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
    33  			Expect(err).ToNot(HaveOccurred())
    34  			Eventually(session, 2).Should(Exit(1))
    35  		})
    36  
    37  		Context("Executing plugins with '.Start()'", func() {
    38  			var (
    39  				rpcHandlers *rpcserverfakes.FakeHandlers
    40  				ts          *rpcserver.TestServer
    41  				err         error
    42  			)
    43  
    44  			BeforeEach(func() {
    45  				rpcHandlers = new(rpcserverfakes.FakeHandlers)
    46  				ts, err = rpcserver.NewTestRPCServer(rpcHandlers)
    47  				Expect(err).NotTo(HaveOccurred())
    48  			})
    49  
    50  			JustBeforeEach(func() {
    51  				err = ts.Start()
    52  				Expect(err).NotTo(HaveOccurred())
    53  			})
    54  
    55  			AfterEach(func() {
    56  				ts.Stop()
    57  			})
    58  
    59  			Context("checking MinCliVersion", func() {
    60  				It("it calls rpc cmd 'IsMinCliVersion' if plugin metadata 'MinCliVersion' is set", func() {
    61  					args := []string{ts.Port(), "0"}
    62  					session, err := Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
    63  					Expect(err).ToNot(HaveOccurred())
    64  
    65  					session.Wait()
    66  
    67  					Expect(rpcHandlers.IsMinCliVersionCallCount()).To(Equal(1))
    68  				})
    69  
    70  				When("the min cli version is not met", func() {
    71  					BeforeEach(func() {
    72  						rpcHandlers.IsMinCliVersionStub = func(_ string, result *bool) error {
    73  							*result = false
    74  							return nil
    75  						}
    76  					})
    77  
    78  					It("notifies the user", func() {
    79  
    80  						args := []string{ts.Port(), "0"}
    81  						session, err := Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
    82  						Expect(err).ToNot(HaveOccurred())
    83  
    84  						session.Wait()
    85  
    86  						Expect(session).To(gbytes.Say("Minimum CLI version 5.0.0 is required to run this plugin command"))
    87  
    88  					})
    89  				})
    90  			})
    91  		})
    92  	})
    93  
    94  	Describe("MinCliVersionStr", func() {
    95  		It("returns a string representation of VersionType{}", func() {
    96  			version := plugin.VersionType{
    97  				Major: 1,
    98  				Minor: 2,
    99  				Build: 3,
   100  			}
   101  
   102  			str := plugin.MinCliVersionStr(version)
   103  			Expect(str).To(Equal("1.2.3"))
   104  		})
   105  
   106  		It("returns a empty string if no field in VersionType is set", func() {
   107  			version := plugin.VersionType{}
   108  
   109  			str := plugin.MinCliVersionStr(version)
   110  			Expect(str).To(Equal(""))
   111  		})
   112  
   113  		It("uses '0' as return value for field that is not set", func() {
   114  			version := plugin.VersionType{
   115  				Build: 5,
   116  			}
   117  
   118  			str := plugin.MinCliVersionStr(version)
   119  			Expect(str).To(Equal("0.0.5"))
   120  		})
   121  
   122  	})
   123  })