hubtest.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package hubtest
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "github.com/crowdsecurity/crowdsec/pkg/cwhub"
  8. )
  9. type HubTest struct {
  10. CrowdSecPath string
  11. CscliPath string
  12. HubPath string
  13. HubTestPath string
  14. HubIndexFile string
  15. TemplateConfigPath string
  16. TemplateProfilePath string
  17. TemplateSimulationPath string
  18. HubIndex *HubIndex
  19. Tests []*HubTestItem
  20. }
  21. const (
  22. templateConfigFile = "template_config.yaml"
  23. templateSimulationFile = "template_simulation.yaml"
  24. templateProfileFile = "template_profiles.yaml"
  25. )
  26. func NewHubTest(hubPath string, crowdsecPath string, cscliPath string) (HubTest, error) {
  27. var err error
  28. hubPath, err = filepath.Abs(hubPath)
  29. if err != nil {
  30. return HubTest{}, fmt.Errorf("can't get absolute path of hub: %+v", err)
  31. }
  32. // we can't use hubtest without the hub
  33. if _, err := os.Stat(hubPath); os.IsNotExist(err) {
  34. return HubTest{}, fmt.Errorf("path to hub '%s' doesn't exist, can't run", hubPath)
  35. }
  36. HubTestPath := filepath.Join(hubPath, "./.tests/")
  37. // we can't use hubtest without crowdsec binary
  38. if _, err := exec.LookPath(crowdsecPath); err != nil {
  39. if _, err := os.Stat(crowdsecPath); os.IsNotExist(err) {
  40. return HubTest{}, fmt.Errorf("path to crowdsec binary '%s' doesn't exist or is not in $PATH, can't run", crowdsecPath)
  41. }
  42. }
  43. // we can't use hubtest without cscli binary
  44. if _, err := exec.LookPath(cscliPath); err != nil {
  45. if _, err := os.Stat(cscliPath); os.IsNotExist(err) {
  46. return HubTest{}, fmt.Errorf("path to cscli binary '%s' doesn't exist or is not in $PATH, can't run", cscliPath)
  47. }
  48. }
  49. hubIndexFile := filepath.Join(hubPath, ".index.json")
  50. bidx, err := os.ReadFile(hubIndexFile)
  51. if err != nil {
  52. return HubTest{}, fmt.Errorf("unable to read index file: %s", err)
  53. }
  54. // load hub index
  55. hubIndex, err := cwhub.LoadPkgIndex(bidx)
  56. if err != nil {
  57. return HubTest{}, fmt.Errorf("unable to load hub index file: %s", err)
  58. }
  59. templateConfigFilePath := filepath.Join(HubTestPath, templateConfigFile)
  60. templateProfilePath := filepath.Join(HubTestPath, templateProfileFile)
  61. templateSimulationPath := filepath.Join(HubTestPath, templateSimulationFile)
  62. return HubTest{
  63. CrowdSecPath: crowdsecPath,
  64. CscliPath: cscliPath,
  65. HubPath: hubPath,
  66. HubTestPath: HubTestPath,
  67. HubIndexFile: hubIndexFile,
  68. TemplateConfigPath: templateConfigFilePath,
  69. TemplateProfilePath: templateProfilePath,
  70. TemplateSimulationPath: templateSimulationPath,
  71. HubIndex: &HubIndex{Data: hubIndex},
  72. }, nil
  73. }
  74. func (h *HubTest) LoadTestItem(name string) (*HubTestItem, error) {
  75. HubTestItem := &HubTestItem{}
  76. testItem, err := NewTest(name, h)
  77. if err != nil {
  78. return HubTestItem, err
  79. }
  80. h.Tests = append(h.Tests, testItem)
  81. return testItem, nil
  82. }
  83. func (h *HubTest) LoadAllTests() error {
  84. testsFolder, err := os.ReadDir(h.HubTestPath)
  85. if err != nil {
  86. return err
  87. }
  88. for _, f := range testsFolder {
  89. if f.IsDir() {
  90. if _, err := h.LoadTestItem(f.Name()); err != nil {
  91. return fmt.Errorf("while loading %s: %w", f.Name(), err)
  92. }
  93. }
  94. }
  95. return nil
  96. }