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.

167 lines
3.9 KiB

  1. package config_test
  2. import (
  3. "fmt"
  4. "github.com/jaedle/mirror-to-gitea/internal/config"
  5. . "github.com/onsi/ginkgo"
  6. . "github.com/onsi/ginkgo/extensions/table"
  7. . "github.com/onsi/gomega"
  8. "os"
  9. )
  10. const mirrorMode = "MIRROR_MODE"
  11. const giteaUrl = "GITEA_URL"
  12. const giteaToken = "GITEA_TOKEN"
  13. const githubUsername = "GITHUB_USERNAME"
  14. const githubToken = "GITHUB_TOKEN"
  15. const publicMirrorMode = "PUBLIC"
  16. const privateAndPublicMirrorMode = "PRIVATE_AND_PUBLIC"
  17. const unknownMirrorMode = "UNKNOWN"
  18. const aGithubUsername = "a-github-user"
  19. const aGithubToken = "a-github-token"
  20. var _ = Describe("Read", func() {
  21. BeforeEach(func() {
  22. os.Clearenv()
  23. })
  24. It("parses valid configuration", func() {
  25. aValidEnv()
  26. c, err := config.Read()
  27. Expect(err).NotTo(HaveOccurred())
  28. Expect(c).ToNot(BeNil())
  29. })
  30. Context("github", func() {
  31. It("parses configuration", func() {
  32. aValidEnv()
  33. setEnv(githubUsername, aGithubUsername)
  34. unsetEnv(githubToken)
  35. c, err := config.Read()
  36. Expect(err).NotTo(HaveOccurred())
  37. Expect(c.Github.Username).To(Equal(aGithubUsername))
  38. Expect(c.Github.Token).To(BeNil())
  39. })
  40. It("parses configuration with token", func() {
  41. aValidEnv()
  42. setEnv(githubUsername, aGithubUsername)
  43. setEnv(githubToken, aGithubToken)
  44. c, err := config.Read()
  45. Expect(err).NotTo(HaveOccurred())
  46. Expect(c.Github.Username).To(Equal(aGithubUsername))
  47. Expect(*c.Github.Token).To(Equal(aGithubToken))
  48. })
  49. It("fails on missing username", func() {
  50. aValidEnv()
  51. unsetEnv(githubUsername)
  52. c, err := config.Read()
  53. Expect(err).To(HaveOccurred())
  54. Expect(c).To(BeNil())
  55. })
  56. })
  57. Context("Gitea", func() {
  58. It("parses configuration", func() {
  59. aValidEnv()
  60. setEnv(giteaUrl, "https://gitea.url/api")
  61. setEnv(giteaToken, "a-gitea-token")
  62. c, err := config.Read()
  63. Expect(err).NotTo(HaveOccurred())
  64. Expect(c.Gitea.GiteaUrl).To(Equal("https://gitea.url/api"))
  65. Expect(c.Gitea.GiteaToken).To(Equal("a-gitea-token"))
  66. })
  67. It("fails on missing gitea url", func() {
  68. aValidEnv()
  69. unsetEnv(giteaUrl)
  70. c, err := config.Read()
  71. Expect(err).To(HaveOccurred())
  72. Expect(c).To(BeNil())
  73. Expect(err.Error()).To(Equal("missing mandatory parameter GITEA_URL, please specify your target gitea instance"))
  74. })
  75. It("fails on missing gitea token", func() {
  76. aValidEnv()
  77. unsetEnv(giteaToken)
  78. c, err := config.Read()
  79. Expect(err).To(HaveOccurred())
  80. Expect(c).To(BeNil())
  81. Expect(err.Error()).To(Equal("missing mandatory parameter GITEA_TOKEN, please specify your gitea application token"))
  82. })
  83. })
  84. Context("mirror mode", func() {
  85. It("sets default mirror mode", func() {
  86. aValidEnv()
  87. unsetEnv(mirrorMode)
  88. c, err := config.Read()
  89. Expect(err).NotTo(HaveOccurred())
  90. Expect(c.MirrorMode).To(Equal(config.MirrorModePublic))
  91. })
  92. DescribeTable("parses mirror mode: ", func(in string, exp string) {
  93. aValidEnv()
  94. setEnv(mirrorMode, in)
  95. c, err := config.Read()
  96. Expect(err).NotTo(HaveOccurred())
  97. Expect(c.MirrorMode).To(Equal(exp))
  98. },
  99. Entry("public mirror mode", publicMirrorMode, config.MirrorModePublic),
  100. Entry("private mirror mode", privateAndPublicMirrorMode, config.MirrorModePrivateAndPublic),
  101. )
  102. It("fails on unknown mirror mode", func() {
  103. aValidEnv()
  104. setEnv(mirrorMode, unknownMirrorMode)
  105. c, err := config.Read()
  106. Expect(err).To(HaveOccurred())
  107. Expect(c).To(BeNil())
  108. expected := "unknown mirror mode %s, please specify a valid mirror mode: PUBLIC, PRIVATE_AND_PUBLIC"
  109. Expect(err.Error()).To(Equal(fmt.Sprintf(expected, unknownMirrorMode)))
  110. })
  111. })
  112. })
  113. func setEnv(k string, v string) {
  114. err := os.Setenv(k, v)
  115. Expect(err).NotTo(HaveOccurred())
  116. }
  117. func unsetEnv(k string) {
  118. err := os.Unsetenv(k)
  119. Expect(err).NotTo(HaveOccurred())
  120. }
  121. func aValidEnv() {
  122. setEnv(mirrorMode, "PUBLIC")
  123. setEnv(giteaUrl, "https://gitea.url")
  124. setEnv(giteaToken, "valid")
  125. setEnv(githubUsername, "a-github-username")
  126. }