|
| 1 | +package argocdapp |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + . "github.com/onsi/ginkgo/v2" |
| 9 | + . "github.com/onsi/gomega" |
| 10 | + "github.com/onsi/gomega/ghttp" |
| 11 | + |
| 12 | + "github.com/devstream-io/devstream/pkg/util/downloader" |
| 13 | + "github.com/devstream-io/devstream/pkg/util/scm" |
| 14 | + "github.com/devstream-io/devstream/pkg/util/scm/git" |
| 15 | +) |
| 16 | + |
| 17 | +var _ = Describe("source struct", func() { |
| 18 | + var ( |
| 19 | + scmClient scm.ClientOperation |
| 20 | + errMsg string |
| 21 | + s *source |
| 22 | + ) |
| 23 | + BeforeEach(func() { |
| 24 | + s = &source{ |
| 25 | + Path: "test_path", |
| 26 | + } |
| 27 | + }) |
| 28 | + Context("checkPathExist method", func() { |
| 29 | + When("scm client error", func() { |
| 30 | + BeforeEach(func() { |
| 31 | + errMsg = "scm get path error" |
| 32 | + scmClient = &scm.MockScmClient{ |
| 33 | + GetPathInfoError: errors.New(errMsg), |
| 34 | + } |
| 35 | + }) |
| 36 | + It("should return error", func() { |
| 37 | + _, err := s.checkPathExist(scmClient) |
| 38 | + Expect(err).Should(HaveOccurred()) |
| 39 | + Expect(err.Error()).Should(Equal(errMsg)) |
| 40 | + }) |
| 41 | + }) |
| 42 | + When("path list is empty", func() { |
| 43 | + BeforeEach(func() { |
| 44 | + scmClient = &scm.MockScmClient{ |
| 45 | + GetPathInfoReturnValue: []*git.RepoFileStatus{}, |
| 46 | + } |
| 47 | + }) |
| 48 | + It("should return false", func() { |
| 49 | + exist, err := s.checkPathExist(scmClient) |
| 50 | + Expect(err).ShouldNot(HaveOccurred()) |
| 51 | + Expect(exist).Should(BeFalse()) |
| 52 | + }) |
| 53 | + }) |
| 54 | + When("path list is not empty", func() { |
| 55 | + BeforeEach(func() { |
| 56 | + scmClient = &scm.MockScmClient{ |
| 57 | + GetPathInfoReturnValue: []*git.RepoFileStatus{ |
| 58 | + {Path: "gg"}, |
| 59 | + }, |
| 60 | + } |
| 61 | + }) |
| 62 | + It("should return true", func() { |
| 63 | + exist, err := s.checkPathExist(scmClient) |
| 64 | + Expect(err).ShouldNot(HaveOccurred()) |
| 65 | + Expect(exist).Should(BeTrue()) |
| 66 | + }) |
| 67 | + }) |
| 68 | + }) |
| 69 | +}) |
| 70 | + |
| 71 | +var _ = Describe("options struct", func() { |
| 72 | + var ( |
| 73 | + configLocation downloader.ResourceLocation |
| 74 | + o *options |
| 75 | + s *ghttp.Server |
| 76 | + pathName, reqPath string |
| 77 | + ) |
| 78 | + BeforeEach(func() { |
| 79 | + s = ghttp.NewServer() |
| 80 | + o = &options{ |
| 81 | + App: &app{ |
| 82 | + Name: "test", |
| 83 | + }, |
| 84 | + Source: &source{ |
| 85 | + Path: "test_path", |
| 86 | + RepoURL: "https://test.com/owner/repo", |
| 87 | + }, |
| 88 | + } |
| 89 | + pathName = "/configLoc" |
| 90 | + reqPath = fmt.Sprintf("%s%s", s.URL(), pathName) |
| 91 | + s.RouteToHandler("HEAD", pathName, func(w http.ResponseWriter, r *http.Request) { |
| 92 | + fmt.Fprint(w, "ok") |
| 93 | + }) |
| 94 | + }) |
| 95 | + Context("getArgocdDefaultConfigFiles method", func() { |
| 96 | + When("can't get location", func() { |
| 97 | + BeforeEach(func() { |
| 98 | + s.RouteToHandler("GET", pathName, ghttp.CombineHandlers( |
| 99 | + ghttp.VerifyRequest("GET", pathName), |
| 100 | + ghttp.RespondWith(http.StatusNotFound, ""), |
| 101 | + )) |
| 102 | + configLocation = downloader.ResourceLocation(reqPath) |
| 103 | + }) |
| 104 | + It("should return error", func() { |
| 105 | + _, err := o.getArgocdDefaultConfigFiles(configLocation) |
| 106 | + Expect(err).Should(HaveOccurred()) |
| 107 | + Expect(err.Error()).Should(ContainSubstring("bad response code: 404")) |
| 108 | + }) |
| 109 | + }) |
| 110 | + When("all valid", func() { |
| 111 | + BeforeEach(func() { |
| 112 | + configFileContent := ` |
| 113 | +app: |
| 114 | + name: [[ .App.Name ]]` |
| 115 | + s.RouteToHandler("GET", pathName, ghttp.CombineHandlers( |
| 116 | + ghttp.VerifyRequest("GET", pathName), |
| 117 | + ghttp.RespondWith(http.StatusOK, configFileContent), |
| 118 | + )) |
| 119 | + configLocation = downloader.ResourceLocation(reqPath) |
| 120 | + }) |
| 121 | + It("should return valid data", func() { |
| 122 | + data, err := o.getArgocdDefaultConfigFiles(configLocation) |
| 123 | + Expect(err).ShouldNot(HaveOccurred()) |
| 124 | + Expect(data).ShouldNot(BeEmpty()) |
| 125 | + }) |
| 126 | + }) |
| 127 | + }) |
| 128 | + AfterEach(func() { |
| 129 | + s.Close() |
| 130 | + }) |
| 131 | +}) |
0 commit comments