registry_handler_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 updateRegistryTests = []*regTest{
  146. &regTest{
  147. initializers: []func(t *tester){
  148. initUserDefault,
  149. initProject,
  150. initRegistry,
  151. },
  152. msg: "Update registry name",
  153. method: "POST",
  154. endpoint: "/api/projects/1/registries/1",
  155. body: `{"name":"registry-new-name"}`,
  156. expStatus: http.StatusOK,
  157. expBody: `{"id":1,"name":"registry-new-name","project_id":1,"service":"ecr"}`,
  158. useCookie: true,
  159. validators: []func(c *regTest, tester *tester, t *testing.T){
  160. regBodyValidator,
  161. },
  162. },
  163. }
  164. func TestHandleUpdateRegistry(t *testing.T) {
  165. testRegistryRequests(t, updateRegistryTests, true)
  166. }
  167. var deleteRegTests = []*regTest{
  168. &regTest{
  169. initializers: []func(t *tester){
  170. initUserDefault,
  171. initProject,
  172. initRegistry,
  173. },
  174. msg: "Delete registry",
  175. method: "DELETE",
  176. endpoint: "/api/projects/1/registries/1",
  177. body: ``,
  178. expStatus: http.StatusOK,
  179. expBody: ``,
  180. useCookie: true,
  181. validators: []func(c *regTest, tester *tester, t *testing.T){
  182. func(c *regTest, tester *tester, t *testing.T) {
  183. req, err := http.NewRequest(
  184. "GET",
  185. "/api/projects/1/registries",
  186. strings.NewReader(""),
  187. )
  188. req.AddCookie(tester.cookie)
  189. if err != nil {
  190. t.Fatal(err)
  191. }
  192. rr2 := httptest.NewRecorder()
  193. tester.router.ServeHTTP(rr2, req)
  194. if status := rr2.Code; status != 200 {
  195. t.Errorf("DELETE registry validation, handler returned wrong status code: got %v want %v",
  196. status, 200)
  197. }
  198. gotBody := make([]*models.RegistryExternal, 0)
  199. expBody := make([]*models.RegistryExternal, 0)
  200. json.Unmarshal(rr2.Body.Bytes(), &gotBody)
  201. if diff := deep.Equal(gotBody, expBody); diff != nil {
  202. t.Errorf("handler returned wrong body:\n")
  203. t.Error(diff)
  204. }
  205. },
  206. },
  207. },
  208. }
  209. func TestHandleDeleteRegistry(t *testing.T) {
  210. testRegistryRequests(t, deleteRegTests, true)
  211. }
  212. // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  213. func initRegistry(tester *tester) {
  214. proj, _ := tester.repo.Project.ReadProject(1)
  215. reg := &models.Registry{
  216. Name: "registry-test",
  217. ProjectID: proj.Model.ID,
  218. AWSIntegrationID: 1,
  219. }
  220. tester.repo.Registry.CreateRegistry(reg)
  221. }
  222. func regBodyValidator(c *regTest, tester *tester, t *testing.T) {
  223. gotBody := &models.RegistryExternal{}
  224. expBody := &models.RegistryExternal{}
  225. json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  226. json.Unmarshal([]byte(c.expBody), &expBody)
  227. if diff := deep.Equal(gotBody, expBody); diff != nil {
  228. t.Errorf("handler returned wrong body:\n")
  229. t.Error(diff)
  230. }
  231. }
  232. func regsBodyValidator(c *regTest, tester *tester, t *testing.T) {
  233. gotBody := make([]*models.RegistryExternal, 0)
  234. expBody := make([]*models.RegistryExternal, 0)
  235. json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  236. json.Unmarshal([]byte(c.expBody), &expBody)
  237. if diff := deep.Equal(gotBody, expBody); diff != nil {
  238. t.Errorf("handler returned wrong body:\n")
  239. t.Error(diff)
  240. }
  241. }
  242. func initDefaultImages(tester *tester) {
  243. initUserDefault(tester)
  244. agent := kubernetes.GetAgentTesting(defaultObjects...)
  245. // overwrite the test agent with new resources
  246. tester.app.TestAgents.K8sAgent = agent
  247. }
  248. func imagesListValidator(c *imagesTest, tester *tester, t *testing.T) {
  249. var gotBody map[string]interface{}
  250. var expBody map[string]interface{}
  251. json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  252. json.Unmarshal([]byte(c.expBody), &expBody)
  253. if diff := deep.Equal(gotBody, expBody); diff != nil {
  254. t.Errorf("handler returned wrong body:\n")
  255. t.Error(diff)
  256. }
  257. }