GitOps for k8s
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.

repo.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package gittest
  2. import (
  3. "context"
  4. "io/ioutil"
  5. "os/exec"
  6. "path/filepath"
  7. "testing"
  8. "github.com/fluxcd/flux/cluster/kubernetes/testfiles"
  9. "github.com/fluxcd/flux/git"
  10. "github.com/fluxcd/flux/resource"
  11. )
  12. // Repo creates a new clone-able git repo, pre-populated with some kubernetes
  13. // files and a few commits. Also returns a cleanup func to clean up after.
  14. func Repo(t *testing.T) (*git.Repo, func()) {
  15. newDir, cleanup := testfiles.TempDir(t)
  16. filesDir := filepath.Join(newDir, "files")
  17. gitDir := filepath.Join(newDir, "git")
  18. if err := execCommand("mkdir", filesDir); err != nil {
  19. t.Fatal(err)
  20. }
  21. var err error
  22. if err = execCommand("git", "-C", filesDir, "init"); err != nil {
  23. cleanup()
  24. t.Fatal(err)
  25. }
  26. if err = execCommand("git", "-C", filesDir, "config", "--local", "user.email", "example@example.com"); err != nil {
  27. cleanup()
  28. t.Fatal(err)
  29. }
  30. if err = execCommand("git", "-C", filesDir, "config", "--local", "user.name", "example"); err != nil {
  31. cleanup()
  32. t.Fatal(err)
  33. }
  34. if err = testfiles.WriteTestFiles(filesDir); err != nil {
  35. cleanup()
  36. t.Fatal(err)
  37. }
  38. if err = execCommand("git", "-C", filesDir, "add", "--all"); err != nil {
  39. cleanup()
  40. t.Fatal(err)
  41. }
  42. if err = execCommand("git", "-C", filesDir, "commit", "-m", "'Initial revision'"); err != nil {
  43. cleanup()
  44. t.Fatal(err)
  45. }
  46. if err = execCommand("git", "clone", "--bare", filesDir, gitDir); err != nil {
  47. t.Fatal(err)
  48. }
  49. mirror := git.NewRepo(git.Remote{
  50. URL: "file://" + gitDir,
  51. }, git.Branch("master"))
  52. return mirror, func() {
  53. mirror.Clean()
  54. cleanup()
  55. }
  56. }
  57. // Workloads is a shortcut to getting the names of the workloads (NB
  58. // not all resources, just the workloads) represented in the test
  59. // files.
  60. func Workloads() (res []resource.ID) {
  61. for k, _ := range testfiles.WorkloadMap("") {
  62. res = append(res, k)
  63. }
  64. return res
  65. }
  66. // CheckoutWithConfig makes a standard repo, clones it, and returns
  67. // the clone, the original repo, and a cleanup function.
  68. func CheckoutWithConfig(t *testing.T, config git.Config, syncTag string) (*git.Checkout, *git.Repo, func()) {
  69. // Add files to the repo with the same name as the git branch and the sync tag.
  70. // This is to make sure that git commands don't have ambiguity problems between revisions and files.
  71. testfiles.Files[config.Branch] = "Filename doctored to create a conflict with the git branch name"
  72. testfiles.Files[syncTag] = "Filename doctored to create a conflict with the git sync tag"
  73. repo, cleanup := Repo(t)
  74. if err := repo.Ready(context.Background()); err != nil {
  75. cleanup()
  76. t.Fatal(err)
  77. }
  78. co, err := repo.Clone(context.Background(), config)
  79. if err != nil {
  80. cleanup()
  81. t.Fatal(err)
  82. }
  83. return co, repo, func() {
  84. co.Clean()
  85. cleanup()
  86. delete(testfiles.Files, config.Branch)
  87. delete(testfiles.Files, syncTag)
  88. }
  89. }
  90. var TestConfig git.Config = git.Config{
  91. Branch: "master",
  92. UserName: "example",
  93. UserEmail: "example@example.com",
  94. NotesRef: "fluxtest",
  95. }
  96. var testSyncTag = "sync"
  97. // Checkout makes a standard repo, clones it, and returns the clone
  98. // with a cleanup function.
  99. func Checkout(t *testing.T) (*git.Checkout, func()) {
  100. checkout, _, cleanup := CheckoutWithConfig(t, TestConfig, testSyncTag)
  101. return checkout, cleanup
  102. }
  103. func execCommand(cmd string, args ...string) error {
  104. c := exec.Command(cmd, args...)
  105. c.Stderr = ioutil.Discard
  106. c.Stdout = ioutil.Discard
  107. return c.Run()
  108. }