github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/actor/pluginaction/plugin_info_test.go (about)

     1  package pluginaction_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	. "code.cloudfoundry.org/cli/actor/pluginaction"
     8  	"code.cloudfoundry.org/cli/actor/pluginaction/pluginactionfakes"
     9  	"code.cloudfoundry.org/cli/api/plugin"
    10  	"code.cloudfoundry.org/cli/util/configv3"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("plugin info actions", func() {
    16  	var (
    17  		actor      *Actor
    18  		fakeClient *pluginactionfakes.FakePluginClient
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		fakeClient = new(pluginactionfakes.FakePluginClient)
    23  		actor = NewActor(nil, fakeClient)
    24  	})
    25  
    26  	Describe("GetPluginInfoFromRepositoriesForPlatform", func() {
    27  		When("there is a single repository", func() {
    28  			When("getting the plugin repository errors", func() {
    29  				BeforeEach(func() {
    30  					fakeClient.GetPluginRepositoryReturns(plugin.PluginRepository{}, errors.New("some-error"))
    31  				})
    32  
    33  				It("returns a FetchingPluginInfoFromRepositoryError", func() {
    34  					_, _, err := actor.GetPluginInfoFromRepositoriesForPlatform("some-plugin", []configv3.PluginRepository{{Name: "some-repository", URL: "some-url"}}, "some-platform")
    35  					Expect(err).To(MatchError(actionerror.FetchingPluginInfoFromRepositoryError{
    36  						RepositoryName: "some-repository",
    37  						Err:            errors.New("some-error"),
    38  					}))
    39  				})
    40  			})
    41  
    42  			When("getting the plugin repository succeeds", func() {
    43  				BeforeEach(func() {
    44  					fakeClient.GetPluginRepositoryReturns(plugin.PluginRepository{
    45  						Plugins: []plugin.Plugin{
    46  							{
    47  								Name:    "some-plugin",
    48  								Version: "1.2.3",
    49  								Binaries: []plugin.PluginBinary{
    50  									{Platform: "osx", URL: "http://some-darwin-url", Checksum: "somechecksum"},
    51  									{Platform: "win64", URL: "http://some-windows-url", Checksum: "anotherchecksum"},
    52  									{Platform: "linux64", URL: "http://some-linux-url", Checksum: "lastchecksum"},
    53  								},
    54  							},
    55  							{
    56  								Name:    "linux-plugin",
    57  								Version: "1.5.0",
    58  								Binaries: []plugin.PluginBinary{
    59  									{Platform: "osx", URL: "http://some-url", Checksum: "somechecksum"},
    60  									{Platform: "win64", URL: "http://another-url", Checksum: "anotherchecksum"},
    61  									{Platform: "linux64", URL: "http://last-url", Checksum: "lastchecksum"},
    62  								},
    63  							},
    64  							{
    65  								Name:    "osx-plugin",
    66  								Version: "3.0.0",
    67  								Binaries: []plugin.PluginBinary{
    68  									{Platform: "osx", URL: "http://some-url", Checksum: "somechecksum"},
    69  									{Platform: "win64", URL: "http://another-url", Checksum: "anotherchecksum"},
    70  									{Platform: "linux64", URL: "http://last-url", Checksum: "lastchecksum"},
    71  								},
    72  							},
    73  						},
    74  					}, nil)
    75  				})
    76  
    77  				When("the specified plugin does not exist in the repository", func() {
    78  					It("returns a PluginNotFoundInRepositoryError", func() {
    79  						_, _, err := actor.GetPluginInfoFromRepositoriesForPlatform("plugin-i-dont-exist", []configv3.PluginRepository{{Name: "some-repo", URL: "some-url"}}, "platform-i-dont-exist")
    80  						Expect(err).To(MatchError(actionerror.PluginNotFoundInAnyRepositoryError{
    81  							PluginName: "plugin-i-dont-exist",
    82  						}))
    83  					})
    84  				})
    85  
    86  				When("the specified plugin for the provided platform does not exist in the repository", func() {
    87  					It("returns a NoCompatibleBinaryError", func() {
    88  						_, _, err := actor.GetPluginInfoFromRepositoriesForPlatform("linux-plugin", []configv3.PluginRepository{{Name: "some-repo", URL: "some-url"}}, "platform-i-dont-exist")
    89  						Expect(err).To(MatchError(actionerror.NoCompatibleBinaryError{}))
    90  					})
    91  				})
    92  
    93  				When("the specified plugin exists", func() {
    94  					It("returns the plugin info", func() {
    95  						pluginInfo, repos, err := actor.GetPluginInfoFromRepositoriesForPlatform("some-plugin", []configv3.PluginRepository{{Name: "some-repo", URL: "some-url"}}, "osx")
    96  						Expect(err).ToNot(HaveOccurred())
    97  						Expect(pluginInfo.Name).To(Equal("some-plugin"))
    98  						Expect(pluginInfo.Version).To(Equal("1.2.3"))
    99  						Expect(pluginInfo.URL).To(Equal("http://some-darwin-url"))
   100  						Expect(repos).To(ConsistOf("some-repo"))
   101  					})
   102  				})
   103  			})
   104  		})
   105  
   106  		When("there are multiple repositories", func() {
   107  			var pluginRepositories []configv3.PluginRepository
   108  
   109  			BeforeEach(func() {
   110  				pluginRepositories = []configv3.PluginRepository{
   111  					{Name: "repo1", URL: "url1"},
   112  					{Name: "repo2", URL: "url2"},
   113  					{Name: "repo3", URL: "url3"},
   114  				}
   115  			})
   116  
   117  			When("getting a plugin repository errors", func() {
   118  				BeforeEach(func() {
   119  					fakeClient.GetPluginRepositoryReturnsOnCall(0, plugin.PluginRepository{
   120  						Plugins: []plugin.Plugin{
   121  							{
   122  								Name:    "some-plugin",
   123  								Version: "1.2.3",
   124  								Binaries: []plugin.PluginBinary{
   125  									{Platform: "osx", URL: "http://some-darwin-url", Checksum: "somechecksum"},
   126  									{Platform: "win64", URL: "http://some-windows-url", Checksum: "anotherchecksum"},
   127  									{Platform: "linux64", URL: "http://some-linux-url", Checksum: "lastchecksum"},
   128  								},
   129  							},
   130  						},
   131  					}, nil)
   132  					fakeClient.GetPluginRepositoryReturnsOnCall(1, plugin.PluginRepository{}, errors.New("some-error"))
   133  				})
   134  
   135  				It("returns a FetchingPluginInfoFromRepositoryError", func() {
   136  					_, _, err := actor.GetPluginInfoFromRepositoriesForPlatform("some-plugin", pluginRepositories, "some-platform")
   137  					Expect(err).To(MatchError(actionerror.FetchingPluginInfoFromRepositoryError{
   138  						RepositoryName: "repo2",
   139  						Err:            errors.New("some-error")}))
   140  				})
   141  			})
   142  
   143  			When("the plugin isn't found", func() {
   144  				It("returns the PluginNotFoundInAnyRepositoryError", func() {
   145  					_, _, err := actor.GetPluginInfoFromRepositoriesForPlatform("some-plugin", pluginRepositories, "some-platform")
   146  					Expect(err).To(Equal(actionerror.PluginNotFoundInAnyRepositoryError{PluginName: "some-plugin"}))
   147  				})
   148  			})
   149  
   150  			When("no compatible binaries are found for the plugin", func() {
   151  				BeforeEach(func() {
   152  					fakeClient.GetPluginRepositoryStub = func(repoURL string) (plugin.PluginRepository, error) {
   153  						return plugin.PluginRepository{Plugins: []plugin.Plugin{
   154  							{Name: "some-plugin", Version: "1.2.3", Binaries: []plugin.PluginBinary{
   155  								{Platform: "incompatible-platform", URL: "some-url", Checksum: "some-checksum"},
   156  							}},
   157  						}}, nil
   158  					}
   159  				})
   160  
   161  				It("returns the NoCompatibleBinaryError", func() {
   162  					_, _, err := actor.GetPluginInfoFromRepositoriesForPlatform("some-plugin", pluginRepositories, "some-platform")
   163  					Expect(err).To(MatchError(actionerror.NoCompatibleBinaryError{}))
   164  				})
   165  			})
   166  
   167  			When("some binaries are compatible and some are not", func() {
   168  				BeforeEach(func() {
   169  					fakeClient.GetPluginRepositoryStub = func(repoURL string) (plugin.PluginRepository, error) {
   170  						if repoURL == "url1" {
   171  							return plugin.PluginRepository{Plugins: []plugin.Plugin{
   172  								{Name: "some-plugin", Version: "1.2.3", Binaries: []plugin.PluginBinary{
   173  									{Platform: "incompatible-platform", URL: "some-url", Checksum: "some-checksum"},
   174  								}},
   175  							}}, nil
   176  						} else {
   177  							return plugin.PluginRepository{Plugins: []plugin.Plugin{
   178  								{Name: "some-plugin", Version: "1.2.3", Binaries: []plugin.PluginBinary{
   179  									{Platform: "some-platform", URL: "some-url", Checksum: "some-checksum"},
   180  								}},
   181  							}}, nil
   182  						}
   183  					}
   184  				})
   185  
   186  				It("returns the compatible plugin info and a list of the repositories it was found in", func() {
   187  					pluginInfo, repos, err := actor.GetPluginInfoFromRepositoriesForPlatform("some-plugin", pluginRepositories, "some-platform")
   188  
   189  					Expect(err).ToNot(HaveOccurred())
   190  					Expect(pluginInfo).To(Equal(PluginInfo{
   191  						Name:     "some-plugin",
   192  						Version:  "1.2.3",
   193  						URL:      "some-url",
   194  						Checksum: "some-checksum",
   195  					}))
   196  					Expect(repos).To(ConsistOf("repo2", "repo3"))
   197  				})
   198  			})
   199  
   200  			When("the plugin is found in one repository", func() {
   201  				BeforeEach(func() {
   202  					fakeClient.GetPluginRepositoryStub = func(repoURL string) (plugin.PluginRepository, error) {
   203  						if repoURL == "url1" {
   204  							return plugin.PluginRepository{Plugins: []plugin.Plugin{
   205  								{Name: "some-plugin", Version: "1.2.3", Binaries: []plugin.PluginBinary{
   206  									{Platform: "some-platform", URL: "some-url", Checksum: "some-checksum"},
   207  								}},
   208  							}}, nil
   209  						} else {
   210  							return plugin.PluginRepository{}, nil
   211  						}
   212  					}
   213  				})
   214  
   215  				It("returns the plugin info and a list of the repositories it was found in", func() {
   216  					pluginInfo, repos, err := actor.GetPluginInfoFromRepositoriesForPlatform("some-plugin", pluginRepositories, "some-platform")
   217  
   218  					Expect(err).ToNot(HaveOccurred())
   219  					Expect(pluginInfo).To(Equal(PluginInfo{
   220  						Name:     "some-plugin",
   221  						Version:  "1.2.3",
   222  						URL:      "some-url",
   223  						Checksum: "some-checksum",
   224  					}))
   225  					Expect(repos).To(ConsistOf("repo1"))
   226  				})
   227  			})
   228  
   229  			When("the plugin is found in many repositories", func() {
   230  				BeforeEach(func() {
   231  					fakeClient.GetPluginRepositoryStub = func(repoURL string) (plugin.PluginRepository, error) {
   232  						return plugin.PluginRepository{Plugins: []plugin.Plugin{
   233  							{Name: "some-plugin", Version: "1.2.3", Binaries: []plugin.PluginBinary{
   234  								{Platform: "some-platform", URL: "some-url", Checksum: "some-checksum"},
   235  							}},
   236  						}}, nil
   237  					}
   238  				})
   239  
   240  				It("returns the plugin info and a list of the repositories it was found in", func() {
   241  					pluginInfo, repos, err := actor.GetPluginInfoFromRepositoriesForPlatform("some-plugin", pluginRepositories, "some-platform")
   242  
   243  					Expect(err).ToNot(HaveOccurred())
   244  					Expect(pluginInfo).To(Equal(PluginInfo{
   245  						Name:     "some-plugin",
   246  						Version:  "1.2.3",
   247  						URL:      "some-url",
   248  						Checksum: "some-checksum",
   249  					}))
   250  					Expect(repos).To(ConsistOf("repo1", "repo2", "repo3"))
   251  				})
   252  			})
   253  
   254  			When("different versions of the plugin are found in all the repositories", func() {
   255  				BeforeEach(func() {
   256  					fakeClient.GetPluginRepositoryStub = func(repoURL string) (plugin.PluginRepository, error) {
   257  						switch repoURL {
   258  						case "url1":
   259  							return plugin.PluginRepository{Plugins: []plugin.Plugin{
   260  								{Name: "some-plugin", Version: "1.2.3", Binaries: []plugin.PluginBinary{
   261  									{Platform: "some-platform", URL: "some-url", Checksum: "some-checksum"},
   262  								}},
   263  							}}, nil
   264  						case "url2":
   265  							return plugin.PluginRepository{Plugins: []plugin.Plugin{
   266  								{Name: "some-plugin", Version: "2.2.3", Binaries: []plugin.PluginBinary{
   267  									{Platform: "some-platform", URL: "some-url", Checksum: "some-checksum"},
   268  								}},
   269  							}}, nil
   270  						default:
   271  							return plugin.PluginRepository{Plugins: []plugin.Plugin{
   272  								{Name: "some-plugin", Version: "0.2.3", Binaries: []plugin.PluginBinary{
   273  									{Platform: "some-platform", URL: "some-url", Checksum: "some-checksum"},
   274  								}},
   275  							}}, nil
   276  						}
   277  					}
   278  				})
   279  
   280  				It("returns the newest plugin info and only the repository it was found in", func() {
   281  					pluginInfo, repos, err := actor.GetPluginInfoFromRepositoriesForPlatform("some-plugin", pluginRepositories, "some-platform")
   282  
   283  					Expect(err).ToNot(HaveOccurred())
   284  					Expect(pluginInfo).To(Equal(PluginInfo{
   285  						Name:     "some-plugin",
   286  						Version:  "2.2.3",
   287  						URL:      "some-url",
   288  						Checksum: "some-checksum",
   289  					}))
   290  					Expect(repos).To(ConsistOf("repo2"))
   291  				})
   292  			})
   293  
   294  			When("some repositories contain a newer version of the plugin than others", func() {
   295  				BeforeEach(func() {
   296  					fakeClient.GetPluginRepositoryStub = func(repoURL string) (plugin.PluginRepository, error) {
   297  						switch repoURL {
   298  						case "url1", "url2":
   299  							return plugin.PluginRepository{Plugins: []plugin.Plugin{
   300  								{Name: "some-plugin", Version: "1.2.3", Binaries: []plugin.PluginBinary{
   301  									{Platform: "some-platform", URL: "some-url", Checksum: "some-checksum"},
   302  								}},
   303  							}}, nil
   304  						default:
   305  							return plugin.PluginRepository{Plugins: []plugin.Plugin{
   306  								{Name: "some-plugin", Version: "0.2.3", Binaries: []plugin.PluginBinary{
   307  									{Platform: "some-platform", URL: "some-url", Checksum: "some-checksum"},
   308  								}},
   309  							}}, nil
   310  						}
   311  					}
   312  				})
   313  
   314  				It("returns only the newest plugin info and the list of repositories it's contained in", func() {
   315  					pluginInfo, repos, err := actor.GetPluginInfoFromRepositoriesForPlatform("some-plugin", pluginRepositories, "some-platform")
   316  
   317  					Expect(err).ToNot(HaveOccurred())
   318  					Expect(pluginInfo).To(Equal(PluginInfo{
   319  						Name:     "some-plugin",
   320  						Version:  "1.2.3",
   321  						URL:      "some-url",
   322  						Checksum: "some-checksum",
   323  					}))
   324  					Expect(repos).To(ConsistOf("repo1", "repo2"))
   325  				})
   326  			})
   327  		})
   328  	})
   329  })