Browse Source

trim pkg/types: move DataSet/GetData to pkg/cwhub, removed unused Clone function (#2271)

mmetc 2 years ago
parent
commit
76429f033a

+ 12 - 6
pkg/cwhub/cwhub_test.go

@@ -25,7 +25,7 @@ import (
 var responseByPath map[string]string
 var responseByPath map[string]string
 
 
 func TestItemStatus(t *testing.T) {
 func TestItemStatus(t *testing.T) {
-	cfg := envSetup()
+	cfg := envSetup(t)
 	defer envTearDown(cfg)
 	defer envTearDown(cfg)
 
 
 	err := UpdateHubIdx(cfg.Hub)
 	err := UpdateHubIdx(cfg.Hub)
@@ -73,7 +73,7 @@ func TestItemStatus(t *testing.T) {
 }
 }
 
 
 func TestGetters(t *testing.T) {
 func TestGetters(t *testing.T) {
-	cfg := envSetup()
+	cfg := envSetup(t)
 	defer envTearDown(cfg)
 	defer envTearDown(cfg)
 
 
 	err := UpdateHubIdx(cfg.Hub)
 	err := UpdateHubIdx(cfg.Hub)
@@ -134,7 +134,7 @@ func TestGetters(t *testing.T) {
 }
 }
 
 
 func TestIndexDownload(t *testing.T) {
 func TestIndexDownload(t *testing.T) {
-	cfg := envSetup()
+	cfg := envSetup(t)
 	defer envTearDown(cfg)
 	defer envTearDown(cfg)
 
 
 	err := UpdateHubIdx(cfg.Hub)
 	err := UpdateHubIdx(cfg.Hub)
@@ -155,10 +155,16 @@ func getTestCfg() (cfg *csconfig.Config) {
 	return
 	return
 }
 }
 
 
-func envSetup() *csconfig.Config {
+func envSetup(t *testing.T) *csconfig.Config {
 	resetResponseByPath()
 	resetResponseByPath()
 	log.SetLevel(log.DebugLevel)
 	log.SetLevel(log.DebugLevel)
 	cfg := getTestCfg()
 	cfg := getTestCfg()
+
+	defaultTransport := http.DefaultClient.Transport
+	t.Cleanup(func() {
+		http.DefaultClient.Transport = defaultTransport
+	})
+
 	//Mock the http client
 	//Mock the http client
 	http.DefaultClient.Transport = newMockTransport()
 	http.DefaultClient.Transport = newMockTransport()
 
 
@@ -321,7 +327,7 @@ func TestInstallParser(t *testing.T) {
 	 - check its status
 	 - check its status
 	 - remove it
 	 - remove it
 	*/
 	*/
-	cfg := envSetup()
+	cfg := envSetup(t)
 	defer envTearDown(cfg)
 	defer envTearDown(cfg)
 
 
 	getHubIdxOrFail(t)
 	getHubIdxOrFail(t)
@@ -353,7 +359,7 @@ func TestInstallCollection(t *testing.T) {
 	 - check its status
 	 - check its status
 	 - remove it
 	 - remove it
 	*/
 	*/
-	cfg := envSetup()
+	cfg := envSetup(t)
 	defer envTearDown(cfg)
 	defer envTearDown(cfg)
 
 
 	getHubIdxOrFail(t)
 	getHubIdxOrFail(t)

+ 5 - 15
pkg/types/dataset.go → pkg/cwhub/dataset.go

@@ -1,4 +1,4 @@
-package types
+package cwhub
 
 
 import (
 import (
 	"fmt"
 	"fmt"
@@ -6,24 +6,14 @@ import (
 	"net/http"
 	"net/http"
 	"os"
 	"os"
 	"path"
 	"path"
-	"time"
 
 
 	log "github.com/sirupsen/logrus"
 	log "github.com/sirupsen/logrus"
-)
 
 
-type DataSource struct {
-	SourceURL string `yaml:"source_url"`
-	DestPath  string `yaml:"dest_file"`
-	Type      string `yaml:"type"`
-	//Control cache strategy on expensive regexps
-	Cache    *bool          `yaml:"cache"`
-	Strategy *string        `yaml:"strategy"`
-	Size     *int           `yaml:"size"`
-	TTL      *time.Duration `yaml:"ttl"`
-}
+	"github.com/crowdsecurity/crowdsec/pkg/types"
+)
 
 
 type DataSet struct {
 type DataSet struct {
-	Data []*DataSource `yaml:"data,omitempty"`
+	Data []*types.DataSource `yaml:"data,omitempty"`
 }
 }
 
 
 func downloadFile(url string, destPath string) error {
 func downloadFile(url string, destPath string) error {
@@ -66,7 +56,7 @@ func downloadFile(url string, destPath string) error {
 	return nil
 	return nil
 }
 }
 
 
-func GetData(data []*DataSource, dataDir string) error {
+func GetData(data []*types.DataSource, dataDir string) error {
 	for _, dataS := range data {
 	for _, dataS := range data {
 		destPath := path.Join(dataDir, dataS.DestPath)
 		destPath := path.Join(dataDir, dataS.DestPath)
 		log.Infof("downloading data '%s' in '%s'", dataS.SourceURL, destPath)
 		log.Infof("downloading data '%s' in '%s'", dataS.SourceURL, destPath)

+ 1 - 1
pkg/types/dataset_test.go → pkg/cwhub/dataset_test.go

@@ -1,4 +1,4 @@
-package types
+package cwhub
 
 
 import (
 import (
 	"os"
 	"os"

+ 2 - 3
pkg/cwhub/download.go

@@ -12,7 +12,6 @@ import (
 	"strings"
 	"strings"
 
 
 	"github.com/crowdsecurity/crowdsec/pkg/csconfig"
 	"github.com/crowdsecurity/crowdsec/pkg/csconfig"
-	"github.com/crowdsecurity/crowdsec/pkg/types"
 	"github.com/pkg/errors"
 	"github.com/pkg/errors"
 	log "github.com/sirupsen/logrus"
 	log "github.com/sirupsen/logrus"
 	"gopkg.in/yaml.v2"
 	"gopkg.in/yaml.v2"
@@ -256,7 +255,7 @@ func downloadData(dataFolder string, force bool, reader io.Reader) error {
 	dec := yaml.NewDecoder(reader)
 	dec := yaml.NewDecoder(reader)
 
 
 	for {
 	for {
-		data := &types.DataSet{}
+		data := &DataSet{}
 		err = dec.Decode(data)
 		err = dec.Decode(data)
 		if err != nil {
 		if err != nil {
 			if errors.Is(err, io.EOF) {
 			if errors.Is(err, io.EOF) {
@@ -272,7 +271,7 @@ func downloadData(dataFolder string, force bool, reader io.Reader) error {
 			}
 			}
 		}
 		}
 		if download || force {
 		if download || force {
-			err = types.GetData(data.Data, dataFolder)
+			err = GetData(data.Data, dataFolder)
 			if err != nil {
 			if err != nil {
 				return errors.Wrap(err, "while getting data")
 				return errors.Wrap(err, "while getting data")
 			}
 			}

+ 3 - 3
pkg/cwhub/helpers_test.go

@@ -9,7 +9,7 @@ import (
 //Download index, install collection. Add scenario to collection (hub-side), update index, upgrade collection
 //Download index, install collection. Add scenario to collection (hub-side), update index, upgrade collection
 // We expect the new scenario to be installed
 // We expect the new scenario to be installed
 func TestUpgradeConfigNewScenarioInCollection(t *testing.T) {
 func TestUpgradeConfigNewScenarioInCollection(t *testing.T) {
-	cfg := envSetup()
+	cfg := envSetup(t)
 	defer envTearDown(cfg)
 	defer envTearDown(cfg)
 
 
 	// fresh install of collection
 	// fresh install of collection
@@ -55,7 +55,7 @@ func TestUpgradeConfigNewScenarioInCollection(t *testing.T) {
 // Install a collection, disable a scenario.
 // Install a collection, disable a scenario.
 // Upgrade should install should not enable/download the disabled scenario.
 // Upgrade should install should not enable/download the disabled scenario.
 func TestUpgradeConfigInDisabledSceanarioShouldNotBeInstalled(t *testing.T) {
 func TestUpgradeConfigInDisabledSceanarioShouldNotBeInstalled(t *testing.T) {
-	cfg := envSetup()
+	cfg := envSetup(t)
 	defer envTearDown(cfg)
 	defer envTearDown(cfg)
 
 
 	// fresh install of collection
 	// fresh install of collection
@@ -103,7 +103,7 @@ func getHubIdxOrFail(t *testing.T) {
 // Upgrade should not enable/download the disabled scenario.
 // Upgrade should not enable/download the disabled scenario.
 // Upgrade should install and enable the newly added scenario.
 // Upgrade should install and enable the newly added scenario.
 func TestUpgradeConfigNewScenarioIsInstalledWhenReferencedScenarioIsDisabled(t *testing.T) {
 func TestUpgradeConfigNewScenarioIsInstalledWhenReferencedScenarioIsDisabled(t *testing.T) {
-	cfg := envSetup()
+	cfg := envSetup(t)
 	defer envTearDown(cfg)
 	defer envTearDown(cfg)
 
 
 	// fresh install of collection
 	// fresh install of collection

+ 16 - 0
pkg/types/datasource.go

@@ -0,0 +1,16 @@
+package types
+
+import (
+	"time"
+)
+
+type DataSource struct {
+	SourceURL string `yaml:"source_url"`
+	DestPath  string `yaml:"dest_file"`
+	Type      string `yaml:"type"`
+	//Control cache strategy on expensive regexps
+	Cache    *bool          `yaml:"cache"`
+	Strategy *string        `yaml:"strategy"`
+	Size     *int           `yaml:"size"`
+	TTL      *time.Duration `yaml:"ttl"`
+}

+ 0 - 15
pkg/types/utils.go

@@ -2,8 +2,6 @@ package types
 
 
 import (
 import (
 	"bufio"
 	"bufio"
-	"bytes"
-	"encoding/gob"
 	"fmt"
 	"fmt"
 	"os"
 	"os"
 	"path/filepath"
 	"path/filepath"
@@ -70,19 +68,6 @@ func ConfigureLogger(clog *log.Logger) error {
 	return nil
 	return nil
 }
 }
 
 
-func Clone(a, b interface{}) error {
-	buff := new(bytes.Buffer)
-	enc := gob.NewEncoder(buff)
-	dec := gob.NewDecoder(buff)
-	if err := enc.Encode(a); err != nil {
-		return fmt.Errorf("failed cloning %T", a)
-	}
-	if err := dec.Decode(b); err != nil {
-		return fmt.Errorf("failed cloning %T", b)
-	}
-	return nil
-}
-
 func ParseDuration(d string) (time.Duration, error) {
 func ParseDuration(d string) (time.Duration, error) {
 	durationStr := d
 	durationStr := d
 	if strings.HasSuffix(d, "d") {
 	if strings.HasSuffix(d, "d") {