k8s_handler_test.go 3.1 KB

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