k8s_handler_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. "context": []string{"context-test"},
  69. }.Encode(),
  70. body: "",
  71. expStatus: http.StatusOK,
  72. expBody: objectsToJSON(defaultObjects),
  73. useCookie: true,
  74. validators: []func(c *k8sTest, tester *tester, t *testing.T){
  75. k8sNamespaceListValidator,
  76. },
  77. },
  78. }
  79. func TestHandleListNamespaces(t *testing.T) {
  80. testK8sRequests(t, listNamespacesTests, true)
  81. }
  82. // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  83. var defaultObjects = []runtime.Object{
  84. &v1.Namespace{
  85. ObjectMeta: metav1.ObjectMeta{
  86. Name: "test-namespace-0",
  87. },
  88. },
  89. &v1.Namespace{
  90. ObjectMeta: metav1.ObjectMeta{
  91. Name: "test-namespace-1",
  92. },
  93. },
  94. }
  95. func initDefaultK8s(tester *tester) {
  96. initUserDefault(tester)
  97. agent := kubernetes.GetAgentTesting(defaultObjects...)
  98. // overwrite the test agent with new resources
  99. tester.app.TestAgents.K8sAgent = agent
  100. }
  101. func objectsToJSON(objs []runtime.Object) string {
  102. str, _ := json.Marshal(objs)
  103. return string(str)
  104. }
  105. func k8sNamespaceListValidator(c *k8sTest, tester *tester, t *testing.T) {
  106. gotBody := &v1.NamespaceList{}
  107. expBody := &[]v1.Namespace{}
  108. json.Unmarshal(tester.rr.Body.Bytes(), gotBody)
  109. json.Unmarshal([]byte(c.expBody), expBody)
  110. if !reflect.DeepEqual(gotBody.Items, *expBody) {
  111. t.Errorf("%s, handler returned wrong body: got %v want %v",
  112. c.msg, gotBody.Items, expBody)
  113. }
  114. }