123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- package cwhub
- import (
- "io"
- "net/http"
- "os"
- "path/filepath"
- "strings"
- "testing"
- log "github.com/sirupsen/logrus"
- "github.com/stretchr/testify/require"
- "github.com/crowdsecurity/crowdsec/pkg/csconfig"
- )
- /*
- To test :
- - Download 'first' hub index
- - Update hub index
- - Install collection + list content
- - Taint existing parser + list
- - Upgrade collection
- */
- var responseByPath map[string]string
- // testHub initializes a temporary hub with an empty json file, optionally updating it
- func testHub(t *testing.T, update bool) *Hub {
- tmpDir, err := os.MkdirTemp("", "testhub")
- require.NoError(t, err)
- hubCfg := &csconfig.HubCfg{
- HubDir: filepath.Join(tmpDir, "crowdsec", "hub"),
- HubIndexFile: filepath.Join(tmpDir, "crowdsec", "hub", ".index.json"),
- InstallDir: filepath.Join(tmpDir, "crowdsec"),
- InstallDataDir: filepath.Join(tmpDir, "installed-data"),
- }
- err = os.MkdirAll(hubCfg.HubDir, 0o700)
- require.NoError(t, err)
- err = os.MkdirAll(hubCfg.InstallDir, 0o700)
- require.NoError(t, err)
- err = os.MkdirAll(hubCfg.InstallDataDir, 0o700)
- require.NoError(t, err)
- index, err := os.Create(hubCfg.HubIndexFile)
- require.NoError(t, err)
- _, err = index.WriteString(`{}`)
- require.NoError(t, err)
- index.Close()
- t.Cleanup(func() {
- os.RemoveAll(tmpDir)
- })
- constructor := InitHub
- if update {
- constructor = InitHubUpdate
- }
- hub, err := constructor(hubCfg)
- require.NoError(t, err)
- return hub
- }
- // envSetup initializes the temporary hub and mocks the http client
- func envSetup(t *testing.T) *Hub {
- setResponseByPath()
- log.SetLevel(log.DebugLevel)
- defaultTransport := http.DefaultClient.Transport
- t.Cleanup(func() {
- http.DefaultClient.Transport = defaultTransport
- })
- // Mock the http client
- http.DefaultClient.Transport = newMockTransport()
- hub := testHub(t, true)
- return hub
- }
- type mockTransport struct{}
- func newMockTransport() http.RoundTripper {
- return &mockTransport{}
- }
- // Implement http.RoundTripper
- func (t *mockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
- // Create mocked http.Response
- response := &http.Response{
- Header: make(http.Header),
- Request: req,
- StatusCode: http.StatusOK,
- }
- response.Header.Set("Content-Type", "application/json")
- log.Infof("---> %s", req.URL.Path)
- // FAKE PARSER
- resp, ok := responseByPath[req.URL.Path]
- if !ok {
- log.Fatalf("unexpected url :/ %s", req.URL.Path)
- }
- response.Body = io.NopCloser(strings.NewReader(resp))
- return response, nil
- }
- func fileToStringX(path string) string {
- f, err := os.Open(path)
- if err != nil {
- panic(err)
- }
- defer f.Close()
- data, err := io.ReadAll(f)
- if err != nil {
- panic(err)
- }
- return strings.ReplaceAll(string(data), "\r\n", "\n")
- }
- func setResponseByPath() {
- responseByPath = map[string]string{
- "/master/parsers/s01-parse/crowdsecurity/foobar_parser.yaml": fileToStringX("./testdata/foobar_parser.yaml"),
- "/master/parsers/s01-parse/crowdsecurity/foobar_subparser.yaml": fileToStringX("./testdata/foobar_parser.yaml"),
- "/master/collections/crowdsecurity/test_collection.yaml": fileToStringX("./testdata/collection_v1.yaml"),
- "/master/.index.json": fileToStringX("./testdata/index1.json"),
- "/master/scenarios/crowdsecurity/foobar_scenario.yaml": `filter: true
- name: crowdsecurity/foobar_scenario`,
- "/master/scenarios/crowdsecurity/barfoo_scenario.yaml": `filter: true
- name: crowdsecurity/foobar_scenario`,
- "/master/collections/crowdsecurity/foobar_subcollection.yaml": `
- blah: blalala
- qwe: jejwejejw`,
- "/master/collections/crowdsecurity/foobar.yaml": `
- blah: blalala
- qwe: jejwejejw`,
- }
- }
|