2023-10-20 12:32:35 +00:00
|
|
|
package cwhub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/go-cs-lib/cstest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestInitHubUpdate(t *testing.T) {
|
|
|
|
hub := envSetup(t)
|
|
|
|
|
2023-10-31 11:47:39 +00:00
|
|
|
remote := &RemoteHubCfg{
|
2023-10-30 16:23:50 +00:00
|
|
|
URLTemplate: mockURLTemplate,
|
2023-10-31 11:47:39 +00:00
|
|
|
Branch: "master",
|
|
|
|
IndexPath: ".index.json",
|
2023-10-30 16:23:50 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 11:47:39 +00:00
|
|
|
_, err := NewHub(hub.local, remote, true)
|
2023-10-20 12:32:35 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = GetHub()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDownloadIndex(t *testing.T) {
|
|
|
|
// bad url template
|
|
|
|
fmt.Println("Test 'bad URL'")
|
|
|
|
|
|
|
|
tmpIndex, err := os.CreateTemp("", "index.json")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Remove(tmpIndex.Name())
|
|
|
|
})
|
|
|
|
|
2023-10-30 16:23:50 +00:00
|
|
|
hub := envSetup(t)
|
|
|
|
|
2023-10-31 11:47:39 +00:00
|
|
|
hub.remote = &RemoteHubCfg{
|
2023-10-30 16:23:50 +00:00
|
|
|
URLTemplate: "x",
|
2023-10-31 11:47:39 +00:00
|
|
|
Branch: "",
|
|
|
|
IndexPath: "",
|
2023-10-30 16:23:50 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 09:27:33 +00:00
|
|
|
err = hub.remote.downloadIndex(tmpIndex.Name())
|
2023-10-30 16:23:50 +00:00
|
|
|
cstest.RequireErrorContains(t, err, "failed to build hub index request: invalid URL template 'x'")
|
2023-10-20 12:32:35 +00:00
|
|
|
|
|
|
|
// bad domain
|
|
|
|
fmt.Println("Test 'bad domain'")
|
|
|
|
|
2023-10-31 11:47:39 +00:00
|
|
|
hub.remote = &RemoteHubCfg{
|
2023-10-30 16:23:50 +00:00
|
|
|
URLTemplate: "https://baddomain/%s/%s",
|
2023-10-31 11:47:39 +00:00
|
|
|
Branch: "master",
|
|
|
|
IndexPath: ".index.json",
|
2023-10-30 16:23:50 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 09:27:33 +00:00
|
|
|
err = hub.remote.downloadIndex(tmpIndex.Name())
|
2023-10-31 12:12:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-10-31 11:47:39 +00:00
|
|
|
// XXX: this is not failing
|
|
|
|
// cstest.RequireErrorContains(t, err, "failed http request for hub index: Get")
|
2023-10-20 12:32:35 +00:00
|
|
|
|
|
|
|
// bad target path
|
|
|
|
fmt.Println("Test 'bad target path'")
|
|
|
|
|
2023-10-31 11:47:39 +00:00
|
|
|
hub.remote = &RemoteHubCfg{
|
2023-10-30 16:23:50 +00:00
|
|
|
URLTemplate: mockURLTemplate,
|
2023-10-31 11:47:39 +00:00
|
|
|
Branch: "master",
|
|
|
|
IndexPath: ".index.json",
|
2023-10-30 16:23:50 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 09:27:33 +00:00
|
|
|
err = hub.remote.downloadIndex("/does/not/exist/index.json")
|
2023-10-20 12:32:35 +00:00
|
|
|
cstest.RequireErrorContains(t, err, "while opening hub index file: open /does/not/exist/index.json:")
|
|
|
|
}
|