registry_handler_test.go 6.5 KB

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