chart_handler_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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/helm"
  10. "helm.sh/helm/v3/pkg/chart"
  11. "helm.sh/helm/v3/pkg/release"
  12. "helm.sh/helm/v3/pkg/storage/driver"
  13. )
  14. type releaseStub struct {
  15. name string
  16. namespace string
  17. version int
  18. chartVersion string
  19. status release.Status
  20. }
  21. // ------------------------- TEST TYPES AND MAIN LOOP ------------------------- //
  22. type chartTest struct {
  23. initializers []func(tester *tester)
  24. msg string
  25. method string
  26. endpoint string
  27. body string
  28. expStatus int
  29. expBody string
  30. useCookie bool
  31. validators []func(c *chartTest, tester *tester, t *testing.T)
  32. }
  33. func testChartRequests(t *testing.T, tests []*chartTest, canQuery bool) {
  34. for _, c := range tests {
  35. // create a new tester
  36. tester := newTester(canQuery)
  37. // if there's an initializer, call it
  38. for _, init := range c.initializers {
  39. init(tester)
  40. }
  41. req, err := http.NewRequest(
  42. c.method,
  43. c.endpoint,
  44. strings.NewReader(c.body),
  45. )
  46. tester.req = req
  47. if c.useCookie {
  48. req.AddCookie(tester.cookie)
  49. }
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. tester.execute()
  54. rr := tester.rr
  55. // first, check that the status matches
  56. if status := rr.Code; status != c.expStatus {
  57. t.Errorf("%s, handler returned wrong status code: got %v want %v",
  58. c.msg, status, c.expStatus)
  59. }
  60. // if there's a validator, call it
  61. for _, validate := range c.validators {
  62. validate(c, tester, t)
  63. }
  64. }
  65. }
  66. // ------------------------- TEST FIXTURES AND FUNCTIONS ------------------------- //
  67. var listChartsTests = []*chartTest{
  68. &chartTest{
  69. initializers: []func(tester *tester){
  70. initDefaultCharts,
  71. },
  72. msg: "List charts",
  73. method: "GET",
  74. endpoint: "/api/charts?" + url.Values{
  75. "namespace": []string{""},
  76. "context": []string{"context-test"},
  77. "storage": []string{"memory"},
  78. "limit": []string{"20"},
  79. "skip": []string{"0"},
  80. "byDate": []string{"false"},
  81. "statusFilter": []string{"deployed"},
  82. }.Encode(),
  83. body: "",
  84. expStatus: http.StatusOK,
  85. expBody: releaseStubsToChartJSON(sampleReleaseStubs),
  86. useCookie: true,
  87. validators: []func(c *chartTest, tester *tester, t *testing.T){
  88. chartReleaseBodyValidator,
  89. },
  90. },
  91. }
  92. func TestHandleListCharts(t *testing.T) {
  93. testChartRequests(t, listChartsTests, true)
  94. }
  95. var getChartTests = []*chartTest{
  96. &chartTest{
  97. initializers: []func(tester *tester){
  98. initDefaultCharts,
  99. },
  100. msg: "Get charts",
  101. method: "GET",
  102. endpoint: "/api/charts/airwatch/1?" + url.Values{
  103. "namespace": []string{""},
  104. "context": []string{"context-test"},
  105. "storage": []string{"memory"},
  106. }.Encode(),
  107. body: "",
  108. expStatus: http.StatusOK,
  109. expBody: releaseStubToChartJSON(sampleReleaseStubs[0]),
  110. useCookie: true,
  111. validators: []func(c *chartTest, tester *tester, t *testing.T){
  112. chartReleaseBodyValidator,
  113. },
  114. },
  115. }
  116. func TestHandleGetChart(t *testing.T) {
  117. testChartRequests(t, getChartTests, true)
  118. }
  119. var listChartHistoryTests = []*chartTest{
  120. &chartTest{
  121. initializers: []func(tester *tester){
  122. initHistoryCharts,
  123. },
  124. msg: "List chart history",
  125. method: "GET",
  126. endpoint: "/api/charts/wordpress/history?" + url.Values{
  127. "namespace": []string{""},
  128. "context": []string{"context-test"},
  129. "storage": []string{"memory"},
  130. }.Encode(),
  131. body: "",
  132. expStatus: http.StatusOK,
  133. expBody: releaseStubsToChartJSON(historyReleaseStubs),
  134. useCookie: true,
  135. validators: []func(c *chartTest, tester *tester, t *testing.T){
  136. chartReleaseBodyValidator,
  137. },
  138. },
  139. }
  140. func TestHandleListChartHistory(t *testing.T) {
  141. testChartRequests(t, listChartHistoryTests, true)
  142. }
  143. var rollbackChartTests = []*chartTest{
  144. &chartTest{
  145. initializers: []func(tester *tester){
  146. initHistoryCharts,
  147. },
  148. msg: "Rollback relase",
  149. method: "POST",
  150. endpoint: "/api/charts/rollback/wordpress/1",
  151. body: `
  152. {
  153. "namespace": "default",
  154. "context": "context-test",
  155. "storage": "memory"
  156. }
  157. `,
  158. expStatus: http.StatusOK,
  159. expBody: releaseStubsToChartJSON(historyReleaseStubs),
  160. useCookie: true,
  161. validators: []func(c *chartTest, tester *tester, t *testing.T){
  162. func(c *chartTest, tester *tester, t *testing.T) {
  163. t.Error("asdlkfjasf")
  164. },
  165. },
  166. },
  167. }
  168. func TestRollbackChart(t *testing.T) {
  169. testChartRequests(t, rollbackChartTests, true)
  170. }
  171. // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  172. func initDefaultCharts(tester *tester) {
  173. initUserDefault(tester)
  174. agent := tester.app.TestAgents.HelmAgent
  175. makeReleases(agent, sampleReleaseStubs)
  176. // calling agent.ActionConfig.Releases.Create in makeReleases will automatically set the
  177. // namespace, so we have to reset the namespace of the storage driver
  178. agent.ActionConfig.Releases.Driver.(*driver.Memory).SetNamespace("")
  179. }
  180. func initHistoryCharts(tester *tester) {
  181. initUserDefault(tester)
  182. agent := tester.app.TestAgents.HelmAgent
  183. makeReleases(agent, historyReleaseStubs)
  184. // calling agent.ActionConfig.Releases.Create in makeReleases will automatically set the
  185. // namespace, so we have to reset the namespace of the storage driver
  186. agent.ActionConfig.Releases.Driver.(*driver.Memory).SetNamespace("")
  187. }
  188. var sampleReleaseStubs = []releaseStub{
  189. releaseStub{"airwatch", "default", 1, "1.0.0", release.StatusDeployed},
  190. releaseStub{"not-in-default-namespace", "other", 1, "1.0.1", release.StatusDeployed},
  191. releaseStub{"wordpress", "default", 1, "1.0.2", release.StatusDeployed},
  192. }
  193. var historyReleaseStubs = []releaseStub{
  194. releaseStub{"wordpress", "default", 1, "1.0.1", release.StatusSuperseded},
  195. releaseStub{"wordpress", "default", 2, "1.0.2", release.StatusDeployed},
  196. }
  197. func releaseStubsToChartJSON(rels []releaseStub) string {
  198. releases := make([]*release.Release, 0)
  199. for _, r := range rels {
  200. rel := releaseStubToRelease(r)
  201. releases = append(releases, rel)
  202. }
  203. str, _ := json.Marshal(releases)
  204. return string(str)
  205. }
  206. func releaseStubToChartJSON(r releaseStub) string {
  207. rel := releaseStubToRelease(r)
  208. str, _ := json.Marshal(rel)
  209. return string(str)
  210. }
  211. func releaseStubToRelease(r releaseStub) *release.Release {
  212. return &release.Release{
  213. Name: r.name,
  214. Namespace: r.namespace,
  215. Version: r.version,
  216. Info: &release.Info{
  217. Status: r.status,
  218. },
  219. Chart: &chart.Chart{
  220. Metadata: &chart.Metadata{
  221. Version: r.chartVersion,
  222. Icon: "https://example.com/icon.png",
  223. },
  224. },
  225. }
  226. }
  227. func makeReleases(agent *helm.Agent, rels []releaseStub) {
  228. storage := agent.ActionConfig.Releases
  229. for _, r := range rels {
  230. rel := releaseStubToRelease(r)
  231. storage.Create(rel)
  232. }
  233. }
  234. func chartReleaseBodyValidator(c *chartTest, tester *tester, t *testing.T) {
  235. gotBody := &[]release.Release{}
  236. expBody := &[]release.Release{}
  237. json.Unmarshal(tester.rr.Body.Bytes(), gotBody)
  238. json.Unmarshal([]byte(c.expBody), expBody)
  239. if !reflect.DeepEqual(gotBody, expBody) {
  240. t.Errorf("%s, handler returned wrong body: got %v want %v",
  241. c.msg, gotBody, expBody)
  242. }
  243. }