You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
2.5 KiB

  1. package config_test
  2. import (
  3. "github.com/jaedle/mirror-to-gitea/internal/config"
  4. . "github.com/onsi/ginkgo"
  5. . "github.com/onsi/gomega"
  6. "os"
  7. )
  8. const mirrorMode = "MIRROR_MODE"
  9. const giteaUrl = "GITEA_URL"
  10. const giteaToken = "GITEA_TOKEN"
  11. const publicMirrorMode = "PUBLIC"
  12. const privateAndPublicMirrorMode = "PRIVATE_AND_PUBLIC"
  13. const unknownMirrorMode = "UNKNOWN"
  14. var _ = Describe("Read", func() {
  15. BeforeEach(func() {
  16. os.Clearenv()
  17. })
  18. It("parses valid configuration", func() {
  19. aValidEnv()
  20. c, err := config.Read()
  21. Expect(err).NotTo(HaveOccurred())
  22. Expect(c).ToNot(BeNil())
  23. })
  24. Context("Gitea", func() {
  25. It("parses configuration", func() {
  26. aValidEnv()
  27. setEnv(giteaUrl, "https://gitea.url/api")
  28. setEnv(giteaToken, "a-gitea-token")
  29. c, err := config.Read()
  30. Expect(err).NotTo(HaveOccurred())
  31. Expect(c.Gitea.GiteaUrl).To(Equal("https://gitea.url/api"))
  32. Expect(c.Gitea.GiteaToken).To(Equal("a-gitea-token"))
  33. })
  34. It("fails on missing gitea url", func() {
  35. aValidEnv()
  36. unsetEnv(giteaUrl)
  37. c, err := config.Read()
  38. Expect(err).To(HaveOccurred())
  39. Expect(c).To(BeNil())
  40. })
  41. It("fails on missing gitea token", func() {
  42. aValidEnv()
  43. unsetEnv(giteaToken)
  44. c, err := config.Read()
  45. Expect(err).To(HaveOccurred())
  46. Expect(c).To(BeNil())
  47. })
  48. })
  49. Context("mirror mode", func() {
  50. It("sets default mirror mode", func() {
  51. aValidEnv()
  52. unsetEnv(mirrorMode)
  53. c, err := config.Read()
  54. Expect(err).NotTo(HaveOccurred())
  55. Expect(c.MirrorMode).To(Equal(config.MirrorModePublic))
  56. })
  57. It("allows public mirror mode PUBLIC", func() {
  58. aValidEnv()
  59. setEnv(mirrorMode, publicMirrorMode)
  60. c, err := config.Read()
  61. Expect(err).NotTo(HaveOccurred())
  62. Expect(c.MirrorMode).To(Equal(config.MirrorModePublic))
  63. })
  64. It("allows mirror mode PRIVATE_AND_PUBLIC", func() {
  65. aValidEnv()
  66. setEnv(mirrorMode, privateAndPublicMirrorMode)
  67. c, err := config.Read()
  68. Expect(err).NotTo(HaveOccurred())
  69. Expect(c.MirrorMode).To(Equal(config.MirrorModePrivateAndPublic))
  70. })
  71. It("fails on unknown mirror mode", func() {
  72. aValidEnv()
  73. setEnv(mirrorMode, unknownMirrorMode)
  74. c, err := config.Read()
  75. Expect(err).To(HaveOccurred())
  76. Expect(c).To(BeNil())
  77. })
  78. })
  79. })
  80. func setEnv(k string, v string) {
  81. err := os.Setenv(k, v)
  82. Expect(err).NotTo(HaveOccurred())
  83. }
  84. func unsetEnv(k string) {
  85. err := os.Unsetenv(k)
  86. Expect(err).NotTo(HaveOccurred())
  87. }
  88. func aValidEnv() {
  89. setEnv(mirrorMode, "PUBLIC")
  90. setEnv(giteaUrl, "https://gitea.url")
  91. setEnv(giteaToken, "valid")
  92. }