cluster_handler_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. package api_test
  2. import (
  3. "github.com/porter-dev/porter/internal/forms"
  4. "github.com/porter-dev/porter/internal/kubernetes/fixtures"
  5. "github.com/porter-dev/porter/internal/models"
  6. )
  7. // import (
  8. // "encoding/json"
  9. // "net/http"
  10. // "net/http/httptest"
  11. // "strings"
  12. // "testing"
  13. // "github.com/porter-dev/porter/internal/kubernetes/fixtures"
  14. // "github.com/porter-dev/porter/internal/models/integrations"
  15. // "gorm.io/gorm"
  16. // "github.com/go-test/deep"
  17. // "github.com/porter-dev/porter/internal/forms"
  18. // "github.com/porter-dev/porter/internal/models"
  19. // )
  20. // // ------------------------- TEST TYPES AND MAIN LOOP ------------------------- //
  21. // type clusterTest struct {
  22. // initializers []func(t *tester)
  23. // msg string
  24. // method string
  25. // endpoint string
  26. // body string
  27. // expStatus int
  28. // expBody string
  29. // useCookie bool
  30. // validators []func(c *clusterTest, tester *tester, t *testing.T)
  31. // }
  32. // func testClusterRequests(t *testing.T, tests []*clusterTest, canQuery bool) {
  33. // for _, c := range tests {
  34. // // create a new tester
  35. // tester := newTester(canQuery)
  36. // // if there's an initializer, call it
  37. // for _, init := range c.initializers {
  38. // init(tester)
  39. // }
  40. // req, err := http.NewRequest(
  41. // c.method,
  42. // c.endpoint,
  43. // strings.NewReader(c.body),
  44. // )
  45. // tester.req = req
  46. // if c.useCookie {
  47. // req.AddCookie(tester.cookie)
  48. // }
  49. // if err != nil {
  50. // t.Fatal(err)
  51. // }
  52. // tester.execute()
  53. // rr := tester.rr
  54. // // first, check that the status matches
  55. // if status := rr.Code; status != c.expStatus {
  56. // t.Errorf("%s, handler returned wrong status code: got %v want %v",
  57. // c.msg, status, c.expStatus)
  58. // }
  59. // // if there's a validator, call it
  60. // for _, validate := range c.validators {
  61. // validate(c, tester, t)
  62. // }
  63. // }
  64. // }
  65. // // ------------------------- TEST FIXTURES AND FUNCTIONS ------------------------- //
  66. // var createClusterTests = []*clusterTest{
  67. // {
  68. // initializers: []func(t *tester){
  69. // initUserDefault,
  70. // initProject,
  71. // initAWSIntegration,
  72. // },
  73. // msg: "Create cluster",
  74. // method: "POST",
  75. // endpoint: "/api/projects/1/clusters",
  76. // body: `{"name":"cluster-test","server":"https://10.10.10.10:6443","aws_integration_id":1}`,
  77. // expStatus: http.StatusCreated,
  78. // expBody: `{"id":1,"project_id":1,"name":"cluster-test","server":"https://10.10.10.10:6443","service":"eks"}`,
  79. // useCookie: true,
  80. // validators: []func(c *clusterTest, tester *tester, t *testing.T){
  81. // projectClusterBodyValidator,
  82. // },
  83. // },
  84. // }
  85. // func TestHandleCreateCluster(t *testing.T) {
  86. // testClusterRequests(t, createClusterTests, true)
  87. // }
  88. // var readProjectClusterTest = []*clusterTest{
  89. // {
  90. // initializers: []func(t *tester){
  91. // initUserDefault,
  92. // initProject,
  93. // initProjectClusterDefault,
  94. // },
  95. // msg: "Read project cluster",
  96. // method: "GET",
  97. // endpoint: "/api/projects/1/clusters/1",
  98. // body: ``,
  99. // expStatus: http.StatusOK,
  100. // expBody: `{"id":1,"project_id":1,"name":"cluster-test","server":"https://10.10.10.10","service":"kube"}`,
  101. // useCookie: true,
  102. // validators: []func(c *clusterTest, tester *tester, t *testing.T){
  103. // projectClusterBodyValidator,
  104. // },
  105. // },
  106. // }
  107. // func TestHandleReadProjectCluster(t *testing.T) {
  108. // testClusterRequests(t, readProjectClusterTest, true)
  109. // }
  110. // var listProjectClustersTest = []*clusterTest{
  111. // {
  112. // initializers: []func(t *tester){
  113. // initUserDefault,
  114. // initProject,
  115. // initProjectClusterDefault,
  116. // },
  117. // msg: "List project clusters",
  118. // method: "GET",
  119. // endpoint: "/api/projects/1/clusters",
  120. // body: ``,
  121. // expStatus: http.StatusOK,
  122. // expBody: `[{"id":1,"project_id":1,"name":"cluster-test","server":"https://10.10.10.10","service":"kube"}]`,
  123. // useCookie: true,
  124. // validators: []func(c *clusterTest, tester *tester, t *testing.T){
  125. // projectClustersBodyValidator,
  126. // },
  127. // },
  128. // }
  129. // func TestHandleListProjectClusters(t *testing.T) {
  130. // testClusterRequests(t, listProjectClustersTest, true)
  131. // }
  132. // var updateClusterTests = []*clusterTest{
  133. // {
  134. // initializers: []func(t *tester){
  135. // initUserDefault,
  136. // initProject,
  137. // initProjectClusterDefault,
  138. // },
  139. // msg: "Update cluster name",
  140. // method: "POST",
  141. // endpoint: "/api/projects/1/clusters/1",
  142. // body: `{"name":"cluster-new-name"}`,
  143. // expStatus: http.StatusOK,
  144. // expBody: `{"id":1,"project_id":1,"name":"cluster-new-name","server":"https://10.10.10.10","service":"kube"}`,
  145. // useCookie: true,
  146. // validators: []func(c *clusterTest, tester *tester, t *testing.T){
  147. // projectClusterBodyValidator,
  148. // },
  149. // },
  150. // }
  151. // func TestHandleUpdateCluster(t *testing.T) {
  152. // testClusterRequests(t, updateClusterTests, true)
  153. // }
  154. // var deleteClusterTests = []*clusterTest{
  155. // {
  156. // initializers: []func(t *tester){
  157. // initUserDefault,
  158. // initProject,
  159. // initProjectClusterDefault,
  160. // },
  161. // msg: "Delete cluster",
  162. // method: "DELETE",
  163. // endpoint: "/api/projects/1/clusters/1",
  164. // body: ``,
  165. // expStatus: http.StatusOK,
  166. // expBody: ``,
  167. // useCookie: true,
  168. // validators: []func(c *clusterTest, tester *tester, t *testing.T){
  169. // func(c *clusterTest, tester *tester, t *testing.T) {
  170. // req, err := http.NewRequest(
  171. // "GET",
  172. // "/api/projects/1/clusters/1",
  173. // strings.NewReader(""),
  174. // )
  175. // req.AddCookie(tester.cookie)
  176. // if err != nil {
  177. // t.Fatal(err)
  178. // }
  179. // rr2 := httptest.NewRecorder()
  180. // tester.router.ServeHTTP(rr2, req)
  181. // if status := rr2.Code; status != 403 {
  182. // t.Errorf("DELETE cluster validation, handler returned wrong status code: got %v want %v",
  183. // status, 403)
  184. // }
  185. // },
  186. // },
  187. // },
  188. // }
  189. // func TestHandleDeleteCluster(t *testing.T) {
  190. // testClusterRequests(t, deleteClusterTests, true)
  191. // }
  192. // var createProjectClusterCandidatesTests = []*clusterTest{
  193. // {
  194. // initializers: []func(t *tester){
  195. // initUserDefault,
  196. // initProject,
  197. // },
  198. // msg: "Create project cluster candidate w/ no actions -- should create SA by default",
  199. // method: "POST",
  200. // endpoint: "/api/projects/1/clusters/candidates",
  201. // body: `{"kubeconfig":"` + OIDCAuthWithDataForJSON + `"}`,
  202. // expStatus: http.StatusCreated,
  203. // expBody: `[{"id":1,"resolvers":[],"created_cluster_id":1,"project_id":1,"context_name":"context-test","name":"cluster-test","server":"https://10.10.10.10"}]`,
  204. // useCookie: true,
  205. // validators: []func(c *clusterTest, tester *tester, t *testing.T){
  206. // projectClusterCandidateBodyValidator,
  207. // // check that Cluster was created by default
  208. // func(c *clusterTest, tester *tester, t *testing.T) {
  209. // clusters, err := tester.repo.Cluster().ListClustersByProjectID(1)
  210. // if err != nil {
  211. // t.Fatalf("%v\n", err)
  212. // }
  213. // if len(clusters) != 1 {
  214. // t.Fatal("Expected cluster to be created by default, but does not exist\n")
  215. // }
  216. // gotCluster := clusters[0]
  217. // gotCluster.Model = gorm.Model{}
  218. // expCluster := &models.Cluster{
  219. // AuthMechanism: models.OIDC,
  220. // ProjectID: 1,
  221. // Name: "cluster-test",
  222. // Server: "https://10.10.10.10",
  223. // OIDCIntegrationID: 1,
  224. // TokenCache: integrations.ClusterTokenCache{},
  225. // CertificateAuthorityData: []byte("-----BEGIN CER"),
  226. // }
  227. // if diff := deep.Equal(gotCluster, expCluster); diff != nil {
  228. // t.Errorf("handler returned wrong body:\n")
  229. // t.Error(diff)
  230. // }
  231. // },
  232. // },
  233. // },
  234. // {
  235. // initializers: []func(t *tester){
  236. // initUserDefault,
  237. // initProject,
  238. // },
  239. // msg: "Create project SA candidate",
  240. // method: "POST",
  241. // endpoint: "/api/projects/1/clusters/candidates",
  242. // body: `{"kubeconfig":"` + OIDCAuthWithoutDataForJSON + `"}`,
  243. // expStatus: http.StatusCreated,
  244. // expBody: `[{"id":1,"resolvers":[{"name":"upload-oidc-idp-issuer-ca-data","data":{"filename":"/fake/path/to/ca.pem"},"docs":"https://github.com/porter-dev/porter","resolved":false,"fields":"oidc_idp_issuer_ca_data"}],"created_cluster_id":0,"project_id":1,"context_name":"context-test","name":"cluster-test","server":"https://10.10.10.10"}]`,
  245. // useCookie: true,
  246. // validators: []func(c *clusterTest, tester *tester, t *testing.T){
  247. // projectClusterCandidateBodyValidator,
  248. // },
  249. // },
  250. // }
  251. // func TestHandleCreateProjectClusterCandidate(t *testing.T) {
  252. // testClusterRequests(t, createProjectClusterCandidatesTests, true)
  253. // }
  254. // var listProjectClusterCandidatesTests = []*clusterTest{
  255. // {
  256. // initializers: []func(t *tester){
  257. // initUserDefault,
  258. // initProject,
  259. // initProjectClusterCandidate,
  260. // },
  261. // msg: "List project cluster candidates",
  262. // method: "GET",
  263. // endpoint: "/api/projects/1/clusters/candidates",
  264. // body: ``,
  265. // expStatus: http.StatusOK,
  266. // expBody: `[{"id":1,"resolvers":[{"name":"upload-oidc-idp-issuer-ca-data","data":{"filename":"/fake/path/to/ca.pem"},"docs":"https://github.com/porter-dev/porter","resolved":false,"fields":"oidc_idp_issuer_ca_data"}],"created_cluster_id":0,"project_id":1,"context_name":"context-test","name":"cluster-test","server":"https://10.10.10.10"}]`,
  267. // useCookie: true,
  268. // validators: []func(c *clusterTest, tester *tester, t *testing.T){
  269. // projectClusterCandidateBodyValidator,
  270. // },
  271. // },
  272. // }
  273. // func TestHandleListProjectClusterCandidates(t *testing.T) {
  274. // testClusterRequests(t, listProjectClusterCandidatesTests, true)
  275. // }
  276. // var resolveProjectClusterCandidatesTests = []*clusterTest{
  277. // {
  278. // initializers: []func(t *tester){
  279. // initUserDefault,
  280. // initProject,
  281. // initProjectClusterCandidate,
  282. // },
  283. // msg: "Resolve project cluster candidate",
  284. // method: "POST",
  285. // endpoint: "/api/projects/1/clusters/candidates/1/resolve",
  286. // body: `{"oidc_idp_issuer_ca_data": "LS0tLS1CRUdJTiBDRVJ="}`,
  287. // expStatus: http.StatusCreated,
  288. // expBody: `{"id":1,"project_id":1,"name":"cluster-test","server":"https://10.10.10.10","service":"kube"}`,
  289. // useCookie: true,
  290. // validators: []func(c *clusterTest, tester *tester, t *testing.T){
  291. // projectClusterBodyValidator,
  292. // },
  293. // },
  294. // }
  295. // func TestHandleResolveProjectClusterCandidate(t *testing.T) {
  296. // testClusterRequests(t, resolveProjectClusterCandidatesTests, true)
  297. // }
  298. // // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  299. // func initProjectClusterCandidate(tester *tester) {
  300. // proj, _ := tester.repo.Project().ReadProject(1)
  301. // form := &forms.CreateClusterCandidatesForm{
  302. // ProjectID: proj.ID,
  303. // Kubeconfig: fixtures.OIDCAuthWithoutData,
  304. // }
  305. // // convert the form to a ServiceAccountCandidate
  306. // ccs, _ := form.ToClusterCandidates(false)
  307. // for _, cc := range ccs {
  308. // tester.repo.Cluster().CreateClusterCandidate(cc)
  309. // }
  310. // }
  311. func initProjectClusterDefault(tester *tester) {
  312. proj, _ := tester.repo.Project().ReadProject(1)
  313. form := &forms.CreateClusterCandidatesForm{
  314. ProjectID: proj.ID,
  315. Kubeconfig: fixtures.OIDCAuthWithData,
  316. }
  317. // convert the form to a ServiceAccountCandidate
  318. ccs, _ := form.ToClusterCandidates(false)
  319. for _, cc := range ccs {
  320. tester.repo.Cluster().CreateClusterCandidate(cc)
  321. }
  322. clusterForm := forms.ResolveClusterForm{
  323. Resolver: &models.ClusterResolverAll{},
  324. ClusterCandidateID: 1,
  325. ProjectID: 1,
  326. UserID: 1,
  327. }
  328. clusterForm.ResolveIntegration(tester.repo)
  329. clusterForm.ResolveCluster(tester.repo)
  330. }
  331. // func projectClusterCandidateBodyValidator(c *clusterTest, tester *tester, t *testing.T) {
  332. // gotBody := make([]*models.ClusterCandidateExternal, 0)
  333. // expBody := make([]*models.ClusterCandidateExternal, 0)
  334. // json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  335. // json.Unmarshal([]byte(c.expBody), &expBody)
  336. // if diff := deep.Equal(gotBody, expBody); diff != nil {
  337. // t.Errorf("handler returned wrong body:\n")
  338. // t.Error(diff)
  339. // }
  340. // }
  341. // func projectClusterBodyValidator(c *clusterTest, tester *tester, t *testing.T) {
  342. // gotBody := &models.ClusterExternal{}
  343. // expBody := &models.ClusterExternal{}
  344. // json.Unmarshal(tester.rr.Body.Bytes(), gotBody)
  345. // json.Unmarshal([]byte(c.expBody), expBody)
  346. // if diff := deep.Equal(gotBody, expBody); diff != nil {
  347. // t.Errorf("handler returned wrong body:\n")
  348. // t.Error(diff)
  349. // }
  350. // }
  351. // func projectClustersBodyValidator(c *clusterTest, tester *tester, t *testing.T) {
  352. // gotBody := make([]*models.ClusterExternal, 0)
  353. // expBody := make([]*models.ClusterExternal, 0)
  354. // json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  355. // json.Unmarshal([]byte(c.expBody), &expBody)
  356. // if diff := deep.Equal(gotBody, expBody); diff != nil {
  357. // t.Errorf("handler returned wrong body:\n")
  358. // t.Error(diff)
  359. // }
  360. // }
  361. // const OIDCAuthWithDataForJSON string = `apiVersion: v1\nclusters:\n- cluster:\n server: https://10.10.10.10\n certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=\n name: cluster-test\ncontexts:\n- context:\n cluster: cluster-test\n user: test-admin\n name: context-test\ncurrent-context: context-test\nkind: Config\npreferences: {}\nusers:\n- name: test-admin\n user:\n auth-provider:\n config:\n client-id: porter-api\n id-token: token\n idp-issuer-url: https://10.10.10.10\n idp-certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=\n name: oidc`
  362. // const OIDCAuthWithoutDataForJSON string = `apiVersion: v1\nclusters:\n- cluster:\n server: https://10.10.10.10\n certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=\n name: cluster-test\ncontexts:\n- context:\n cluster: cluster-test\n user: test-admin\n name: context-test\ncurrent-context: context-test\nkind: Config\npreferences: {}\nusers:\n- name: test-admin\n user:\n auth-provider:\n config:\n client-id: porter-api\n id-token: token\n idp-issuer-url: https://10.10.10.10\n idp-certificate-authority: /fake/path/to/ca.pem\n name: oidc`
  363. // const OIDCAuthWithoutData string = `
  364. // apiVersion: v1
  365. // clusters:
  366. // - cluster:
  367. // server: https://10.10.10.10
  368. // certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  369. // name: cluster-test
  370. // contexts:
  371. // - context:
  372. // cluster: cluster-test
  373. // user: test-admin
  374. // name: context-test
  375. // current-context: context-test
  376. // kind: Config
  377. // preferences: {}
  378. // users:
  379. // - name: test-admin
  380. // user:
  381. // auth-provider:
  382. // config:
  383. // client-id: porter-api
  384. // id-token: token
  385. // idp-issuer-url: https://10.10.10.10
  386. // idp-certificate-authority: /fake/path/to/ca.pem
  387. // name: oidc
  388. // `
  389. // const OIDCAuthWithData string = `
  390. // apiVersion: v1
  391. // clusters:
  392. // - cluster:
  393. // server: https://10.10.10.10
  394. // certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  395. // name: cluster-test
  396. // contexts:
  397. // - context:
  398. // cluster: cluster-test
  399. // user: test-admin
  400. // name: context-test
  401. // current-context: context-test
  402. // kind: Config
  403. // preferences: {}
  404. // users:
  405. // - name: test-admin
  406. // user:
  407. // auth-provider:
  408. // config:
  409. // client-id: porter-api
  410. // id-token: token
  411. // idp-issuer-url: https://10.10.10.10
  412. // idp-certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  413. // name: oidc
  414. // `