hub_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package cwhub
  2. import (
  3. "fmt"
  4. "os"
  5. "testing"
  6. "github.com/stretchr/testify/require"
  7. "github.com/crowdsecurity/go-cs-lib/cstest"
  8. )
  9. func TestInitHubUpdate(t *testing.T) {
  10. hub := envSetup(t)
  11. _, err := InitHubUpdate(hub.cfg)
  12. require.NoError(t, err)
  13. _, err = GetHub()
  14. require.NoError(t, err)
  15. }
  16. func TestDownloadIndex(t *testing.T) {
  17. back := RawFileURLTemplate
  18. // bad url template
  19. fmt.Println("Test 'bad URL'")
  20. tmpIndex, err := os.CreateTemp("", "index.json")
  21. require.NoError(t, err)
  22. t.Cleanup(func() {
  23. os.Remove(tmpIndex.Name())
  24. })
  25. RawFileURLTemplate = "x"
  26. ret, err := DownloadIndex(tmpIndex.Name())
  27. cstest.RequireErrorContains(t, err, "failed to build request for hub index: parse ")
  28. fmt.Printf("->%+v", ret)
  29. // bad domain
  30. fmt.Println("Test 'bad domain'")
  31. RawFileURLTemplate = "https://baddomain/%s/%s"
  32. ret, err = DownloadIndex(tmpIndex.Name())
  33. cstest.RequireErrorContains(t, err, "failed http request for hub index: Get")
  34. fmt.Printf("->%+v", ret)
  35. // bad target path
  36. fmt.Println("Test 'bad target path'")
  37. RawFileURLTemplate = back
  38. ret, err = DownloadIndex("/does/not/exist/index.json")
  39. cstest.RequireErrorContains(t, err, "while opening hub index file: open /does/not/exist/index.json:")
  40. RawFileURLTemplate = back
  41. fmt.Printf("->%+v", ret)
  42. }