k8s_handler_test.go 3.0 KB

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