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.

main_test.go 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Shared main test code
  2. package main
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. "testing"
  11. "time"
  12. "github.com/gorilla/mux"
  13. "github.com/fluxcd/flux/http/client"
  14. transport "github.com/fluxcd/flux/http"
  15. "github.com/fluxcd/flux/job"
  16. )
  17. func mockServiceOpts(trip *genericMockRoundTripper) *rootOpts {
  18. c := http.Client{
  19. Transport: trip,
  20. }
  21. mockAPI := client.New(&c, transport.NewAPIRouter(), "", "")
  22. return &rootOpts{
  23. API: mockAPI,
  24. Timeout: 10*time.Second,
  25. }
  26. }
  27. type genericMockRoundTripper struct {
  28. mockResponses map[*mux.Route]interface{}
  29. requestHistory map[string]*http.Request
  30. }
  31. func (t *genericMockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
  32. var matched mux.RouteMatch
  33. var b []byte
  34. status := 404
  35. for k, v := range t.mockResponses {
  36. if k.Match(req, &matched) {
  37. queryParamsWithArrays := make(map[string]string, len(req.URL.Query()))
  38. for x, y := range req.URL.Query() {
  39. queryParamsWithArrays[x] = strings.Join(y, ",")
  40. }
  41. t.requestHistory[matched.Route.GetName()] = req
  42. b, _ = json.Marshal(v)
  43. status = 200
  44. break
  45. }
  46. }
  47. return &http.Response{
  48. StatusCode: status,
  49. Body: ioutil.NopCloser(bytes.NewReader(b)),
  50. }, nil
  51. }
  52. func (t *genericMockRoundTripper) calledRequest(method string) *http.Request {
  53. return t.requestHistory[method]
  54. }
  55. func (t *genericMockRoundTripper) calledURL(method string) (u *url.URL) {
  56. return t.calledRequest(method).URL
  57. }
  58. func testArgs(t *testing.T, args []string, shouldErr bool, errMsg string) *genericMockRoundTripper {
  59. svc := newMockService()
  60. releaseClient := newWorkloadRelease(mockServiceOpts(svc))
  61. getKubeConfigContextNamespace = func(s string) string { return s }
  62. // Run fluxctl release
  63. cmd := releaseClient.Command()
  64. cmd.SetOutput(ioutil.Discard)
  65. cmd.SetArgs(args)
  66. if err := cmd.Execute(); (err == nil) == shouldErr {
  67. if errMsg != "" {
  68. t.Fatalf("%s: %s", args, errMsg)
  69. } else {
  70. t.Fatalf("%s: %v", args, err)
  71. }
  72. }
  73. return svc
  74. }
  75. // The mocked service is actually a mocked http.RoundTripper
  76. func newMockService() *genericMockRoundTripper {
  77. return &genericMockRoundTripper{
  78. mockResponses: map[*mux.Route]interface{}{
  79. transport.NewAPIRouter().Get("UpdateManifests"): job.ID("here-is-a-job-id"),
  80. transport.NewAPIRouter().Get("JobStatus"): job.Status{
  81. StatusString: job.StatusSucceeded,
  82. },
  83. },
  84. requestHistory: make(map[string]*http.Request),
  85. }
  86. }