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_test.go 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package gittest
  2. import (
  3. "io/ioutil"
  4. "path/filepath"
  5. "reflect"
  6. "sync"
  7. "testing"
  8. "time"
  9. "context"
  10. "github.com/weaveworks/flux"
  11. "github.com/weaveworks/flux/cluster/kubernetes/testfiles"
  12. "github.com/weaveworks/flux/git"
  13. "github.com/weaveworks/flux/job"
  14. "github.com/weaveworks/flux/update"
  15. )
  16. func TestCommit(t *testing.T) {
  17. config := TestConfig
  18. config.SkipMessage = " **SKIP**"
  19. checkout, repo, cleanup := CheckoutWithConfig(t, config)
  20. defer cleanup()
  21. for file, _ := range testfiles.Files {
  22. path := filepath.Join(checkout.ManifestDir(), file)
  23. if err := ioutil.WriteFile(path, []byte("FIRST CHANGE"), 0666); err != nil {
  24. t.Fatal(err)
  25. }
  26. break
  27. }
  28. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  29. defer cancel()
  30. commitAction := git.CommitAction{Message: "Changed file"}
  31. if err := checkout.CommitAndPush(ctx, commitAction, nil); err != nil {
  32. t.Fatal(err)
  33. }
  34. err := repo.Refresh(ctx)
  35. if err != nil {
  36. t.Error(err)
  37. }
  38. commits, err := repo.CommitsBefore(ctx, "HEAD", "")
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. if len(commits) < 1 {
  43. t.Fatal("expected at least one commit")
  44. }
  45. if msg := commits[0].Message; msg != commitAction.Message+config.SkipMessage {
  46. t.Errorf(`expected commit message to be:
  47. %s
  48. but it was
  49. %s
  50. `, commitAction.Message+config.SkipMessage, msg)
  51. }
  52. }
  53. func TestCheckout(t *testing.T) {
  54. repo, cleanup := Repo(t)
  55. defer cleanup()
  56. sd, sg := make(chan struct{}), &sync.WaitGroup{}
  57. sg.Add(1)
  58. go repo.Start(sd, sg)
  59. WaitForRepoReady(repo, t)
  60. ctx := context.Background()
  61. params := git.Config{
  62. Branch: "master",
  63. UserName: "example",
  64. UserEmail: "example@example.com",
  65. SyncTag: "flux-test",
  66. NotesRef: "fluxtest",
  67. }
  68. checkout, err := repo.Clone(ctx, params)
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. defer checkout.Clean()
  73. // We don't expect any notes in the clone, yet. Make sure we get
  74. // no note, rather than an error.
  75. head, err := checkout.HeadRevision(ctx)
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. note, err := checkout.GetNote(ctx, head)
  80. if err != nil {
  81. t.Error(err)
  82. }
  83. if note != nil {
  84. t.Errorf("Expected no note on head revision; got %#v", note)
  85. }
  86. changedFile := ""
  87. for file, _ := range testfiles.Files {
  88. path := filepath.Join(checkout.ManifestDir(), file)
  89. if err := ioutil.WriteFile(path, []byte("FIRST CHANGE"), 0666); err != nil {
  90. t.Fatal(err)
  91. }
  92. changedFile = file
  93. break
  94. }
  95. commitAction := git.CommitAction{Author: "", Message: "Changed file"}
  96. if err := checkout.CommitAndPush(ctx, commitAction, nil); err != nil {
  97. t.Fatal(err)
  98. }
  99. path := filepath.Join(checkout.ManifestDir(), changedFile)
  100. if err := ioutil.WriteFile(path, []byte("SECOND CHANGE"), 0666); err != nil {
  101. t.Fatal(err)
  102. }
  103. // An example note with some of the fields filled in, so we can test
  104. // serialization a bit.
  105. expectedNote := git.Note{
  106. JobID: job.ID("jobID1234"),
  107. Spec: update.Spec{
  108. Type: update.Images,
  109. Spec: update.ReleaseSpec{},
  110. },
  111. Result: update.Result{
  112. flux.MustParseResourceID("default/service1"): update.ControllerResult{
  113. Status: update.ReleaseStatusFailed,
  114. Error: "failed the frobulator",
  115. },
  116. },
  117. }
  118. commitAction = git.CommitAction{Author: "", Message: "Changed file again"}
  119. if err := checkout.CommitAndPush(ctx, commitAction, &expectedNote); err != nil {
  120. t.Fatal(err)
  121. }
  122. check := func(c *git.Checkout) {
  123. contents, err := ioutil.ReadFile(filepath.Join(c.ManifestDir(), changedFile))
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. if string(contents) != "SECOND CHANGE" {
  128. t.Error("contents in checkout are not what we committed")
  129. }
  130. rev, err := c.HeadRevision(ctx)
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. note, err := c.GetNote(ctx, rev)
  135. if err != nil {
  136. t.Error(err)
  137. }
  138. if !reflect.DeepEqual(*note, expectedNote) {
  139. t.Errorf("note is not what we supplied when committing: %#v", note)
  140. }
  141. }
  142. // Do we see the changes if we make another working checkout?
  143. if err := repo.Refresh(ctx); err != nil {
  144. t.Error(err)
  145. }
  146. another, err := repo.Clone(ctx, params)
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. defer another.Clean()
  151. check(another)
  152. close(sd)
  153. sg.Wait()
  154. }