registry_handler_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package api_test
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strings"
  6. "testing"
  7. "github.com/go-test/deep"
  8. "github.com/porter-dev/porter/internal/kubernetes"
  9. "github.com/porter-dev/porter/internal/models"
  10. )
  11. // ------------------------- TEST TYPES AND MAIN LOOP ------------------------- //
  12. type regTest struct {
  13. initializers []func(t *tester)
  14. msg string
  15. method string
  16. endpoint string
  17. body string
  18. expStatus int
  19. expBody string
  20. useCookie bool
  21. validators []func(c *regTest, tester *tester, t *testing.T)
  22. }
  23. type imagesTest struct {
  24. initializers []func(tester *tester)
  25. msg string
  26. method string
  27. endpoint string
  28. body string
  29. expStatus int
  30. expBody string
  31. useCookie bool
  32. validators []func(c *imagesTest, tester *tester, t *testing.T)
  33. }
  34. func testRegistryRequests(t *testing.T, tests []*regTest, canQuery bool) {
  35. for _, c := range tests {
  36. // create a new tester
  37. tester := newTester(canQuery)
  38. // if there's an initializer, call it
  39. for _, init := range c.initializers {
  40. init(tester)
  41. }
  42. req, err := http.NewRequest(
  43. c.method,
  44. c.endpoint,
  45. strings.NewReader(c.body),
  46. )
  47. tester.req = req
  48. if c.useCookie {
  49. req.AddCookie(tester.cookie)
  50. }
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. tester.execute()
  55. rr := tester.rr
  56. // first, check that the status matches
  57. if status := rr.Code; status != c.expStatus {
  58. t.Errorf("%s, handler returned wrong status code: got %v want %v",
  59. c.msg, status, c.expStatus)
  60. }
  61. // if there's a validator, call it
  62. for _, validate := range c.validators {
  63. validate(c, tester, t)
  64. }
  65. }
  66. }
  67. func testImagesRequests(t *testing.T, tests []*imagesTest, canQuery bool) {
  68. for _, c := range tests {
  69. // create a new tester
  70. tester := newTester(canQuery)
  71. // if there's an initializer, call it
  72. for _, init := range c.initializers {
  73. init(tester)
  74. }
  75. req, err := http.NewRequest(
  76. c.method,
  77. c.endpoint,
  78. strings.NewReader(c.body),
  79. )
  80. tester.req = req
  81. if c.useCookie {
  82. req.AddCookie(tester.cookie)
  83. }
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. tester.execute()
  88. rr := tester.rr
  89. // first, check that the status matches
  90. if status := rr.Code; status != c.expStatus {
  91. t.Errorf("%s, handler returned wrong status code: got %v want %v",
  92. c.msg, status, c.expStatus)
  93. }
  94. // if there's a validator, call it
  95. for _, validate := range c.validators {
  96. validate(c, tester, t)
  97. }
  98. }
  99. }
  100. // ------------------------- TEST FIXTURES AND FUNCTIONS ------------------------- //
  101. var createRegistryTests = []*regTest{
  102. &regTest{
  103. initializers: []func(t *tester){
  104. initUserDefault,
  105. initProject,
  106. },
  107. msg: "Create registry",
  108. method: "POST",
  109. endpoint: "/api/projects/1/registries",
  110. body: `{"name":"registry-test","aws_integration_id":1}`,
  111. expStatus: http.StatusCreated,
  112. expBody: `{"id":1,"name":"registry-test","project_id":1,"service":"ecr"}`,
  113. useCookie: true,
  114. validators: []func(c *regTest, tester *tester, t *testing.T){
  115. regBodyValidator,
  116. },
  117. },
  118. }
  119. func TestHandleCreateRegistry(t *testing.T) {
  120. testRegistryRequests(t, createRegistryTests, true)
  121. }
  122. var listRegistryTests = []*regTest{
  123. &regTest{
  124. initializers: []func(t *tester){
  125. initUserDefault,
  126. initProject,
  127. initRegistry,
  128. },
  129. msg: "List registries",
  130. method: "GET",
  131. endpoint: "/api/projects/1/registries",
  132. body: ``,
  133. expStatus: http.StatusOK,
  134. expBody: `[{"id":1,"name":"registry-test","project_id":1,"service":"ecr"}]`,
  135. useCookie: true,
  136. validators: []func(c *regTest, tester *tester, t *testing.T){
  137. regsBodyValidator,
  138. },
  139. },
  140. }
  141. func TestHandleListRegistries(t *testing.T) {
  142. testRegistryRequests(t, listRegistryTests, true)
  143. }
  144. var listImagesTests = []*imagesTest{
  145. &imagesTest{
  146. initializers: []func(tester *tester){
  147. initDefaultImages,
  148. },
  149. msg: "List images",
  150. method: "GET",
  151. endpoint: "/api/projects/1/images",
  152. body: "",
  153. expStatus: http.StatusOK,
  154. expBody: "unimplemented",
  155. useCookie: true,
  156. validators: []func(c *imagesTest, tester *tester, t *testing.T){
  157. imagesListValidator,
  158. },
  159. },
  160. }
  161. func TestHandleListImages(t *testing.T) {
  162. testImagesRequests(t, listImagesTests, true)
  163. }
  164. // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  165. func initRegistry(tester *tester) {
  166. proj, _ := tester.repo.Project.ReadProject(1)
  167. reg := &models.Registry{
  168. Name: "registry-test",
  169. ProjectID: proj.Model.ID,
  170. AWSIntegrationID: 1,
  171. }
  172. tester.repo.Registry.CreateRegistry(reg)
  173. }
  174. func regBodyValidator(c *regTest, tester *tester, t *testing.T) {
  175. gotBody := &models.RegistryExternal{}
  176. expBody := &models.RegistryExternal{}
  177. json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  178. json.Unmarshal([]byte(c.expBody), &expBody)
  179. if diff := deep.Equal(gotBody, expBody); diff != nil {
  180. t.Errorf("handler returned wrong body:\n")
  181. t.Error(diff)
  182. }
  183. }
  184. func regsBodyValidator(c *regTest, tester *tester, t *testing.T) {
  185. gotBody := make([]*models.RegistryExternal, 0)
  186. expBody := make([]*models.RegistryExternal, 0)
  187. json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  188. json.Unmarshal([]byte(c.expBody), &expBody)
  189. if diff := deep.Equal(gotBody, expBody); diff != nil {
  190. t.Errorf("handler returned wrong body:\n")
  191. t.Error(diff)
  192. }
  193. }
  194. func initDefaultImages(tester *tester) {
  195. initUserDefault(tester)
  196. agent := kubernetes.GetAgentTesting(defaultObjects...)
  197. // overwrite the test agent with new resources
  198. tester.app.TestAgents.K8sAgent = agent
  199. }
  200. func imagesListValidator(c *imagesTest, tester *tester, t *testing.T) {
  201. var gotBody map[string]interface{}
  202. var expBody map[string]interface{}
  203. json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  204. json.Unmarshal([]byte(c.expBody), &expBody)
  205. if diff := deep.Equal(gotBody, expBody); diff != nil {
  206. t.Errorf("handler returned wrong body:\n")
  207. t.Error(diff)
  208. }
  209. }