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.

171 lines
3.9 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  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. var reader *config.Reader
  22. BeforeEach(func() {
  23. os.Clearenv()
  24. reader = config.NewReader()
  25. })
  26. It("parses valid configuration", func() {
  27. aValidEnv()
  28. c, err := reader.Read()
  29. Expect(err).NotTo(HaveOccurred())
  30. Expect(c).ToNot(BeNil())
  31. })
  32. Context("github", func() {
  33. It("parses configuration", func() {
  34. aValidEnv()
  35. setEnv(githubUsername, aGithubUsername)
  36. unsetEnv(githubToken)
  37. c, err := reader.Read()
  38. Expect(err).NotTo(HaveOccurred())
  39. Expect(c.Github.Username).To(Equal(aGithubUsername))
  40. Expect(c.Github.Token).To(BeNil())
  41. })
  42. It("parses configuration with token", func() {
  43. aValidEnv()
  44. setEnv(githubUsername, aGithubUsername)
  45. setEnv(githubToken, aGithubToken)
  46. c, err := reader.Read()
  47. Expect(err).NotTo(HaveOccurred())
  48. Expect(c.Github.Username).To(Equal(aGithubUsername))
  49. Expect(*c.Github.Token).To(Equal(aGithubToken))
  50. })
  51. It("fails on missing username", func() {
  52. aValidEnv()
  53. unsetEnv(githubUsername)
  54. c, err := reader.Read()
  55. Expect(err).To(HaveOccurred())
  56. Expect(c).To(BeNil())
  57. })
  58. })
  59. Context("Gitea", func() {
  60. It("parses configuration", func() {
  61. aValidEnv()
  62. setEnv(giteaUrl, "https://gitea.url/api")
  63. setEnv(giteaToken, "a-gitea-token")
  64. c, err := reader.Read()
  65. Expect(err).NotTo(HaveOccurred())
  66. Expect(c.Gitea.Url).To(Equal("https://gitea.url/api"))
  67. Expect(c.Gitea.Token).To(Equal("a-gitea-token"))
  68. })
  69. It("fails on missing gitea url", func() {
  70. aValidEnv()
  71. unsetEnv(giteaUrl)
  72. c, err := reader.Read()
  73. Expect(err).To(HaveOccurred())
  74. Expect(c).To(BeNil())
  75. Expect(err.Error()).To(Equal("missing mandatory parameter GITEA_URL, please specify your target gitea instance"))
  76. })
  77. It("fails on missing gitea token", func() {
  78. aValidEnv()
  79. unsetEnv(giteaToken)
  80. c, err := reader.Read()
  81. Expect(err).To(HaveOccurred())
  82. Expect(c).To(BeNil())
  83. Expect(err.Error()).To(Equal("missing mandatory parameter GITEA_TOKEN, please specify your gitea application token"))
  84. })
  85. })
  86. Context("mirror mode", func() {
  87. It("sets default mirror mode", func() {
  88. aValidEnv()
  89. unsetEnv(mirrorMode)
  90. c, err := reader.Read()
  91. Expect(err).NotTo(HaveOccurred())
  92. Expect(c.MirrorMode).To(Equal(config.MirrorModePublic))
  93. })
  94. DescribeTable("parses mirror mode: ", func(in string, exp string) {
  95. aValidEnv()
  96. setEnv(mirrorMode, in)
  97. c, err := reader.Read()
  98. Expect(err).NotTo(HaveOccurred())
  99. Expect(c.MirrorMode).To(Equal(exp))
  100. },
  101. Entry("public mirror mode", publicMirrorMode, config.MirrorModePublic),
  102. Entry("private mirror mode", privateAndPublicMirrorMode, config.MirrorModePrivateAndPublic),
  103. )
  104. It("fails on unknown mirror mode", func() {
  105. aValidEnv()
  106. setEnv(mirrorMode, unknownMirrorMode)
  107. c, err := reader.Read()
  108. Expect(err).To(HaveOccurred())
  109. Expect(c).To(BeNil())
  110. expected := "unknown mirror mode %s, please specify a valid mirror mode: PUBLIC, PRIVATE_AND_PUBLIC"
  111. Expect(err.Error()).To(Equal(fmt.Sprintf(expected, unknownMirrorMode)))
  112. })
  113. })
  114. })
  115. func setEnv(k string, v string) {
  116. err := os.Setenv(k, v)
  117. Expect(err).NotTo(HaveOccurred())
  118. }
  119. func unsetEnv(k string) {
  120. err := os.Unsetenv(k)
  121. Expect(err).NotTo(HaveOccurred())
  122. }
  123. func aValidEnv() {
  124. setEnv(mirrorMode, "PUBLIC")
  125. setEnv(giteaUrl, "https://gitea.url")
  126. setEnv(giteaToken, "valid")
  127. setEnv(githubUsername, "a-github-username")
  128. }