k8s_handler_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package api_test
  2. // // ------------------------- TEST TYPES AND MAIN LOOP ------------------------- //
  3. // type k8sTest struct {
  4. // initializers []func(tester *tester)
  5. // msg string
  6. // method string
  7. // endpoint string
  8. // body string
  9. // expStatus int
  10. // expBody string
  11. // useCookie bool
  12. // validators []func(c *k8sTest, tester *tester, t *testing.T)
  13. // }
  14. // func testK8sRequests(t *testing.T, tests []*k8sTest, canQuery bool) {
  15. // for _, c := range tests {
  16. // // create a new tester
  17. // tester := newTester(canQuery)
  18. // // if there's an initializer, call it
  19. // for _, init := range c.initializers {
  20. // init(tester)
  21. // }
  22. // req, err := http.NewRequest(
  23. // c.method,
  24. // c.endpoint,
  25. // strings.NewReader(c.body),
  26. // )
  27. // tester.req = req
  28. // if c.useCookie {
  29. // req.AddCookie(tester.cookie)
  30. // }
  31. // if err != nil {
  32. // t.Fatal(err)
  33. // }
  34. // tester.execute()
  35. // rr := tester.rr
  36. // // first, check that the status matches
  37. // if status := rr.Code; status != c.expStatus {
  38. // t.Errorf("%s, handler returned wrong status code: got %v want %v",
  39. // c.msg, status, c.expStatus)
  40. // }
  41. // // if there's a validator, call it
  42. // for _, validate := range c.validators {
  43. // validate(c, tester, t)
  44. // }
  45. // }
  46. // }
  47. // // ------------------------- TEST FIXTURES AND FUNCTIONS ------------------------- //
  48. // var listNamespacesTests = []*k8sTest{
  49. // {
  50. // initializers: []func(tester *tester){
  51. // initDefaultK8s,
  52. // },
  53. // msg: "List namespaces",
  54. // method: "GET",
  55. // endpoint: "/api/projects/1/k8s/namespaces?" + url.Values{
  56. // "cluster_id": []string{"1"},
  57. // }.Encode(),
  58. // body: "",
  59. // expStatus: http.StatusOK,
  60. // expBody: objectsToJSON(defaultObjects),
  61. // useCookie: true,
  62. // validators: []func(c *k8sTest, tester *tester, t *testing.T){
  63. // k8sNamespaceListValidator,
  64. // },
  65. // },
  66. // }
  67. // func TestHandleListNamespaces(t *testing.T) {
  68. // testK8sRequests(t, listNamespacesTests, true)
  69. // }
  70. // // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  71. // var defaultObjects = []runtime.Object{
  72. // &v1.Namespace{
  73. // ObjectMeta: metav1.ObjectMeta{
  74. // Name: "test-namespace-0",
  75. // },
  76. // },
  77. // &v1.Namespace{
  78. // ObjectMeta: metav1.ObjectMeta{
  79. // Name: "test-namespace-1",
  80. // },
  81. // },
  82. // }
  83. // func initDefaultK8s(tester *tester) {
  84. // initUserDefault(tester)
  85. // initProject(tester)
  86. // initProjectClusterDefault(tester)
  87. // agent := kubernetes.GetAgentTesting(defaultObjects...)
  88. // // overwrite the test agent with new resources
  89. // tester.app.TestAgents.K8sAgent = agent
  90. // }
  91. // func objectsToJSON(objs []runtime.Object) string {
  92. // str, _ := json.Marshal(objs)
  93. // return string(str)
  94. // }
  95. // func k8sNamespaceListValidator(c *k8sTest, tester *tester, t *testing.T) {
  96. // gotBody := &v1.NamespaceList{}
  97. // expBody := &[]v1.Namespace{}
  98. // json.Unmarshal(tester.rr.Body.Bytes(), gotBody)
  99. // json.Unmarshal([]byte(c.expBody), expBody)
  100. // if !reflect.DeepEqual(gotBody.Items, *expBody) {
  101. // t.Errorf("%s, handler returned wrong body: got %v want %v",
  102. // c.msg, gotBody.Items, expBody)
  103. // }
  104. // }