/in/in_command_test.go

https://gitlab.com/JamesClonk/s3-resource · Go · 317 lines · 248 code · 69 blank · 0 comment · 0 complexity · 291ae5476f4c5031e2e223c2417edd23 MD5 · raw file

  1. package in_test
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. . "github.com/onsi/ginkgo"
  7. . "github.com/onsi/gomega"
  8. "github.com/concourse/s3-resource"
  9. . "github.com/concourse/s3-resource/in"
  10. "github.com/concourse/s3-resource/fakes"
  11. )
  12. var _ = Describe("In Command", func() {
  13. Describe("running the command", func() {
  14. var (
  15. tmpPath string
  16. destDir string
  17. request InRequest
  18. s3client *fakes.FakeS3Client
  19. command *InCommand
  20. )
  21. BeforeEach(func() {
  22. var err error
  23. tmpPath, err = ioutil.TempDir("", "in_command")
  24. Ω(err).ShouldNot(HaveOccurred())
  25. destDir = filepath.Join(tmpPath, "destination")
  26. request = InRequest{
  27. Source: s3resource.Source{
  28. Bucket: "bucket-name",
  29. Regexp: "files/a-file-(.*).tgz",
  30. },
  31. Version: s3resource.Version{
  32. Path: "files/a-file-1.3.tgz",
  33. },
  34. }
  35. s3client = &fakes.FakeS3Client{}
  36. command = NewInCommand(s3client)
  37. s3client.URLReturns("http://google.com")
  38. })
  39. AfterEach(func() {
  40. err := os.RemoveAll(tmpPath)
  41. Ω(err).ShouldNot(HaveOccurred())
  42. })
  43. It("creates the destination directory", func() {
  44. Ω(destDir).ShouldNot(ExistOnFilesystem())
  45. _, err := command.Run(destDir, request)
  46. Ω(err).ShouldNot(HaveOccurred())
  47. Ω(destDir).Should(ExistOnFilesystem())
  48. })
  49. Context("when there is no existing version in the request", func() {
  50. BeforeEach(func() {
  51. request.Version.Path = ""
  52. s3client.BucketFilesReturns([]string{
  53. "files/a-file-0.0.1.tgz",
  54. "files/a-file-3.53.tgz",
  55. "files/a-file-2.33.333.tgz",
  56. "files/a-file-2.4.3.tgz",
  57. }, nil)
  58. })
  59. It("scans the bucket for the latest file to download", func() {
  60. _, err := command.Run(destDir, request)
  61. Ω(err).ShouldNot(HaveOccurred())
  62. Ω(s3client.DownloadFileCallCount()).Should(Equal(1))
  63. bucketName, remotePath, localPath := s3client.DownloadFileArgsForCall(0)
  64. Ω(bucketName).Should(Equal("bucket-name"))
  65. Ω(remotePath).Should(Equal("files/a-file-3.53.tgz"))
  66. Ω(localPath).Should(Equal(filepath.Join(destDir, "a-file-3.53.tgz")))
  67. })
  68. Context("when using a CloudFront domain", func() {
  69. BeforeEach(func() {
  70. request.Source.CloudfrontURL = "https://1234567890.cloudfront.net"
  71. })
  72. It("creates a 'url' file that contains the URL including the CloudFront domain", func() {
  73. urlPath := filepath.Join(destDir, "url")
  74. Ω(urlPath).ShouldNot(ExistOnFilesystem())
  75. _, err := command.Run(destDir, request)
  76. Ω(err).ShouldNot(HaveOccurred())
  77. Ω(urlPath).Should(ExistOnFilesystem())
  78. contents, err := ioutil.ReadFile(urlPath)
  79. Ω(err).ShouldNot(HaveOccurred())
  80. Ω(string(contents)).Should(Equal("https://1234567890.cloudfront.net/files/a-file-3.53.tgz"))
  81. })
  82. })
  83. It("creates a 'url' file that contains the URL", func() {
  84. urlPath := filepath.Join(destDir, "url")
  85. Ω(urlPath).ShouldNot(ExistOnFilesystem())
  86. _, err := command.Run(destDir, request)
  87. Ω(err).ShouldNot(HaveOccurred())
  88. Ω(urlPath).Should(ExistOnFilesystem())
  89. contents, err := ioutil.ReadFile(urlPath)
  90. Ω(err).ShouldNot(HaveOccurred())
  91. Ω(string(contents)).Should(Equal("http://google.com"))
  92. bucketName, remotePath, private := s3client.URLArgsForCall(0)
  93. Ω(bucketName).Should(Equal("bucket-name"))
  94. Ω(remotePath).Should(Equal("files/a-file-3.53.tgz"))
  95. Ω(private).Should(Equal(false))
  96. })
  97. Context("when configured with private URLs", func() {
  98. BeforeEach(func() {
  99. request.Source.Private = true
  100. })
  101. It("creates a 'url' file that contains the private URL if told to do that", func() {
  102. urlPath := filepath.Join(destDir, "url")
  103. Ω(urlPath).ShouldNot(ExistOnFilesystem())
  104. _, err := command.Run(destDir, request)
  105. Ω(err).ShouldNot(HaveOccurred())
  106. Ω(urlPath).Should(ExistOnFilesystem())
  107. contents, err := ioutil.ReadFile(urlPath)
  108. Ω(err).ShouldNot(HaveOccurred())
  109. Ω(string(contents)).Should(Equal("http://google.com"))
  110. Ω(s3client.URLCallCount()).Should(Equal(1))
  111. bucketName, remotePath, private := s3client.URLArgsForCall(0)
  112. Ω(bucketName).Should(Equal("bucket-name"))
  113. Ω(remotePath).Should(Equal("files/a-file-3.53.tgz"))
  114. Ω(private).Should(Equal(true))
  115. })
  116. })
  117. It("creates a 'version' file that contains the latest version", func() {
  118. versionFile := filepath.Join(destDir, "version")
  119. Ω(versionFile).ShouldNot(ExistOnFilesystem())
  120. _, err := command.Run(destDir, request)
  121. Ω(err).ShouldNot(HaveOccurred())
  122. Ω(versionFile).Should(ExistOnFilesystem())
  123. contents, err := ioutil.ReadFile(versionFile)
  124. Ω(err).ShouldNot(HaveOccurred())
  125. Ω(string(contents)).Should(Equal("3.53"))
  126. })
  127. Context("when the regexp has no groups", func() {
  128. BeforeEach(func() {
  129. request.Source.Regexp = "files/a-file-.*.tgz"
  130. })
  131. It("returns an error when the regexp has no groups", func() {
  132. _, err := command.Run(destDir, request)
  133. Ω(err).Should(HaveOccurred())
  134. })
  135. })
  136. Describe("the response", func() {
  137. It("has a version that is the remote file path", func() {
  138. response, err := command.Run(destDir, request)
  139. Ω(err).ShouldNot(HaveOccurred())
  140. Ω(response.Version.Path).Should(Equal("files/a-file-3.53.tgz"))
  141. })
  142. It("has metadata about the file", func() {
  143. response, err := command.Run(destDir, request)
  144. Ω(err).ShouldNot(HaveOccurred())
  145. Ω(response.Metadata[0].Name).Should(Equal("filename"))
  146. Ω(response.Metadata[0].Value).Should(Equal("a-file-3.53.tgz"))
  147. Ω(response.Metadata[1].Name).Should(Equal("url"))
  148. Ω(response.Metadata[1].Value).Should(Equal("http://google.com"))
  149. })
  150. Context("when the output is private", func() {
  151. BeforeEach(func() {
  152. request.Source.Private = true
  153. })
  154. It("doesn't include the URL in the metadata", func() {
  155. response, err := command.Run(destDir, request)
  156. Ω(err).ShouldNot(HaveOccurred())
  157. Ω(response.Metadata).Should(HaveLen(1))
  158. Ω(response.Metadata[0].Name).ShouldNot(Equal("url"))
  159. })
  160. })
  161. })
  162. })
  163. Context("when there is an existing version in the request", func() {
  164. BeforeEach(func() {
  165. request.Version.Path = "files/a-file-1.3.tgz"
  166. })
  167. It("downloads the existing version of the file", func() {
  168. _, err := command.Run(destDir, request)
  169. Ω(err).ShouldNot(HaveOccurred())
  170. Ω(s3client.DownloadFileCallCount()).Should(Equal(1))
  171. bucketName, remotePath, localPath := s3client.DownloadFileArgsForCall(0)
  172. Ω(bucketName).Should(Equal("bucket-name"))
  173. Ω(remotePath).Should(Equal("files/a-file-1.3.tgz"))
  174. Ω(localPath).Should(Equal(filepath.Join(destDir, "a-file-1.3.tgz")))
  175. })
  176. It("creates a 'url' file that contains the URL", func() {
  177. urlPath := filepath.Join(destDir, "url")
  178. Ω(urlPath).ShouldNot(ExistOnFilesystem())
  179. _, err := command.Run(destDir, request)
  180. Ω(err).ShouldNot(HaveOccurred())
  181. Ω(urlPath).Should(ExistOnFilesystem())
  182. contents, err := ioutil.ReadFile(urlPath)
  183. Ω(err).ShouldNot(HaveOccurred())
  184. Ω(string(contents)).Should(Equal("http://google.com"))
  185. bucketName, remotePath, private := s3client.URLArgsForCall(0)
  186. Ω(bucketName).Should(Equal("bucket-name"))
  187. Ω(remotePath).Should(Equal("files/a-file-1.3.tgz"))
  188. Ω(private).Should(Equal(false))
  189. })
  190. Context("when configured with private URLs", func() {
  191. BeforeEach(func() {
  192. request.Source.Private = true
  193. })
  194. It("creates a 'url' file that contains the private URL if told to do that", func() {
  195. urlPath := filepath.Join(destDir, "url")
  196. Ω(urlPath).ShouldNot(ExistOnFilesystem())
  197. _, err := command.Run(destDir, request)
  198. Ω(err).ShouldNot(HaveOccurred())
  199. Ω(urlPath).Should(ExistOnFilesystem())
  200. contents, err := ioutil.ReadFile(urlPath)
  201. Ω(err).ShouldNot(HaveOccurred())
  202. Ω(string(contents)).Should(Equal("http://google.com"))
  203. Ω(s3client.URLCallCount()).Should(Equal(1))
  204. bucketName, remotePath, private := s3client.URLArgsForCall(0)
  205. Ω(bucketName).Should(Equal("bucket-name"))
  206. Ω(remotePath).Should(Equal("files/a-file-1.3.tgz"))
  207. Ω(private).Should(Equal(true))
  208. })
  209. })
  210. It("creates a 'version' file that contains the matched version", func() {
  211. versionFile := filepath.Join(destDir, "version")
  212. Ω(versionFile).ShouldNot(ExistOnFilesystem())
  213. _, err := command.Run(destDir, request)
  214. Ω(err).ShouldNot(HaveOccurred())
  215. Ω(versionFile).Should(ExistOnFilesystem())
  216. contents, err := ioutil.ReadFile(versionFile)
  217. Ω(err).ShouldNot(HaveOccurred())
  218. Ω(string(contents)).Should(Equal("1.3"))
  219. })
  220. Describe("the response", func() {
  221. It("has a version that is the remote file path", func() {
  222. response, err := command.Run(destDir, request)
  223. Ω(err).ShouldNot(HaveOccurred())
  224. Ω(response.Version.Path).Should(Equal("files/a-file-1.3.tgz"))
  225. })
  226. It("has metadata about the file", func() {
  227. response, err := command.Run(destDir, request)
  228. Ω(err).ShouldNot(HaveOccurred())
  229. Ω(response.Metadata[0].Name).Should(Equal("filename"))
  230. Ω(response.Metadata[0].Value).Should(Equal("a-file-1.3.tgz"))
  231. Ω(response.Metadata[1].Name).Should(Equal("url"))
  232. Ω(response.Metadata[1].Value).Should(Equal("http://google.com"))
  233. })
  234. Context("when the output is private", func() {
  235. BeforeEach(func() {
  236. request.Source.Private = true
  237. })
  238. It("doesn't include the URL in the metadata", func() {
  239. response, err := command.Run(destDir, request)
  240. Ω(err).ShouldNot(HaveOccurred())
  241. Ω(response.Metadata).Should(HaveLen(1))
  242. Ω(response.Metadata[0].Name).ShouldNot(Equal("url"))
  243. })
  244. })
  245. })
  246. })
  247. })
  248. })