github.com/Jeffail/benthos/v3@v3.65.0/lib/stream/manager/from_path_test.go (about)

     1  package manager
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"path/filepath"
     7  	"reflect"
     8  	"sort"
     9  	"testing"
    10  
    11  	"github.com/Jeffail/benthos/v3/lib/stream"
    12  	yaml "gopkg.in/yaml.v3"
    13  )
    14  
    15  func TestFromPathHappy(t *testing.T) {
    16  	testDir := t.TempDir()
    17  
    18  	barDir := filepath.Join(testDir, "bar")
    19  	if err := os.Mkdir(barDir, 0o777); err != nil {
    20  		t.Fatal(err)
    21  	}
    22  
    23  	fooPath := filepath.Join(testDir, "foo.yml")
    24  	barPath := filepath.Join(barDir, "test.yaml")
    25  	ignorePath := filepath.Join(barDir, "test_benthos_test.yaml")
    26  
    27  	fooConf := stream.NewConfig()
    28  	fooConf.Input.Type = "bloblang"
    29  
    30  	barConf := stream.NewConfig()
    31  	barConf.Input.Type = "nanomsg"
    32  
    33  	expConfs := map[string]stream.Config{
    34  		"foo":      fooConf,
    35  		"bar_test": barConf,
    36  	}
    37  
    38  	var fooBytes []byte
    39  	var err error
    40  	if fooBytes, err = json.Marshal(fooConf); err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	var barBytes []byte
    44  	if barBytes, err = yaml.Marshal(barConf); err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	if err = os.WriteFile(fooPath, fooBytes, 0o666); err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	if err = os.WriteFile(barPath, barBytes, 0o666); err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	if err = os.WriteFile(ignorePath, []byte("ignore me please"), 0o666); err != nil {
    55  		t.Fatal(err)
    56  	}
    57  
    58  	actConfs := map[string]stream.Config{}
    59  	var lints []string
    60  	if lints, err = LoadStreamConfigsFromPath(testDir, "benthos_test", actConfs); err != nil {
    61  		t.Fatal(err)
    62  	}
    63  
    64  	if len(lints) == 0 {
    65  		t.Error("Expected lint errors")
    66  	}
    67  
    68  	var actKeys, expKeys []string
    69  	for id := range actConfs {
    70  		actKeys = append(actKeys, id)
    71  	}
    72  	sort.Strings(actKeys)
    73  	for id := range expConfs {
    74  		expKeys = append(expKeys, id)
    75  	}
    76  	sort.Strings(expKeys)
    77  
    78  	if !reflect.DeepEqual(actKeys, expKeys) {
    79  		t.Errorf("Wrong keys in loaded set: %v != %v", actKeys, expKeys)
    80  	}
    81  
    82  	if exp, act := "bloblang", actConfs["foo"].Input.Type; exp != act {
    83  		t.Errorf("Wrong value in loaded set: %v != %v", act, exp)
    84  	}
    85  	if exp, act := "nanomsg", actConfs["bar_test"].Input.Type; exp != act {
    86  		t.Errorf("Wrong value in loaded set: %v != %v", act, exp)
    87  	}
    88  }