cwhub_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package cwhub
  2. import (
  3. "io"
  4. "net/http"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. log "github.com/sirupsen/logrus"
  10. "github.com/stretchr/testify/require"
  11. "github.com/crowdsecurity/crowdsec/pkg/csconfig"
  12. )
  13. /*
  14. To test :
  15. - Download 'first' hub index
  16. - Update hub index
  17. - Install collection + list content
  18. - Taint existing parser + list
  19. - Upgrade collection
  20. */
  21. var responseByPath map[string]string
  22. // testHub initializes a temporary hub with an empty json file, optionally updating it
  23. func testHub(t *testing.T, update bool) *Hub {
  24. tmpDir, err := os.MkdirTemp("", "testhub")
  25. require.NoError(t, err)
  26. hubCfg := &csconfig.HubCfg{
  27. HubDir: filepath.Join(tmpDir, "crowdsec", "hub"),
  28. HubIndexFile: filepath.Join(tmpDir, "crowdsec", "hub", ".index.json"),
  29. InstallDir: filepath.Join(tmpDir, "crowdsec"),
  30. InstallDataDir: filepath.Join(tmpDir, "installed-data"),
  31. }
  32. err = os.MkdirAll(hubCfg.HubDir, 0o700)
  33. require.NoError(t, err)
  34. err = os.MkdirAll(hubCfg.InstallDir, 0o700)
  35. require.NoError(t, err)
  36. err = os.MkdirAll(hubCfg.InstallDataDir, 0o700)
  37. require.NoError(t, err)
  38. index, err := os.Create(hubCfg.HubIndexFile)
  39. require.NoError(t, err)
  40. _, err = index.WriteString(`{}`)
  41. require.NoError(t, err)
  42. index.Close()
  43. t.Cleanup(func() {
  44. os.RemoveAll(tmpDir)
  45. })
  46. constructor := InitHub
  47. if update {
  48. constructor = InitHubUpdate
  49. }
  50. hub, err := constructor(hubCfg)
  51. require.NoError(t, err)
  52. return hub
  53. }
  54. // envSetup initializes the temporary hub and mocks the http client
  55. func envSetup(t *testing.T) *Hub {
  56. setResponseByPath()
  57. log.SetLevel(log.DebugLevel)
  58. defaultTransport := http.DefaultClient.Transport
  59. t.Cleanup(func() {
  60. http.DefaultClient.Transport = defaultTransport
  61. })
  62. // Mock the http client
  63. http.DefaultClient.Transport = newMockTransport()
  64. hub := testHub(t, true)
  65. return hub
  66. }
  67. type mockTransport struct{}
  68. func newMockTransport() http.RoundTripper {
  69. return &mockTransport{}
  70. }
  71. // Implement http.RoundTripper
  72. func (t *mockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
  73. // Create mocked http.Response
  74. response := &http.Response{
  75. Header: make(http.Header),
  76. Request: req,
  77. StatusCode: http.StatusOK,
  78. }
  79. response.Header.Set("Content-Type", "application/json")
  80. log.Infof("---> %s", req.URL.Path)
  81. // FAKE PARSER
  82. resp, ok := responseByPath[req.URL.Path]
  83. if !ok {
  84. log.Fatalf("unexpected url :/ %s", req.URL.Path)
  85. }
  86. response.Body = io.NopCloser(strings.NewReader(resp))
  87. return response, nil
  88. }
  89. func fileToStringX(path string) string {
  90. f, err := os.Open(path)
  91. if err != nil {
  92. panic(err)
  93. }
  94. defer f.Close()
  95. data, err := io.ReadAll(f)
  96. if err != nil {
  97. panic(err)
  98. }
  99. return strings.ReplaceAll(string(data), "\r\n", "\n")
  100. }
  101. func setResponseByPath() {
  102. responseByPath = map[string]string{
  103. "/master/parsers/s01-parse/crowdsecurity/foobar_parser.yaml": fileToStringX("./testdata/foobar_parser.yaml"),
  104. "/master/parsers/s01-parse/crowdsecurity/foobar_subparser.yaml": fileToStringX("./testdata/foobar_parser.yaml"),
  105. "/master/collections/crowdsecurity/test_collection.yaml": fileToStringX("./testdata/collection_v1.yaml"),
  106. "/master/.index.json": fileToStringX("./testdata/index1.json"),
  107. "/master/scenarios/crowdsecurity/foobar_scenario.yaml": `filter: true
  108. name: crowdsecurity/foobar_scenario`,
  109. "/master/scenarios/crowdsecurity/barfoo_scenario.yaml": `filter: true
  110. name: crowdsecurity/foobar_scenario`,
  111. "/master/collections/crowdsecurity/foobar_subcollection.yaml": `
  112. blah: blalala
  113. qwe: jejwejejw`,
  114. "/master/collections/crowdsecurity/foobar.yaml": `
  115. blah: blalala
  116. qwe: jejwejejw`,
  117. }
  118. }