github.com/Jeffail/benthos/v3@v3.65.0/lib/stream/manager/from_directory_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 TestFromDirectory(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.json")
    24  	barPath := filepath.Join(barDir, "test.yaml")
    25  
    26  	fooConf := stream.NewConfig()
    27  	fooConf.Input.Type = "bloblang"
    28  
    29  	barConf := stream.NewConfig()
    30  	barConf.Input.Type = "nanomsg"
    31  
    32  	expConfs := map[string]stream.Config{
    33  		"foo":      fooConf,
    34  		"bar_test": barConf,
    35  	}
    36  
    37  	var fooBytes []byte
    38  	var err error
    39  	if fooBytes, err = json.Marshal(fooConf); err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	var barBytes []byte
    43  	if barBytes, err = yaml.Marshal(barConf); err != nil {
    44  		t.Fatal(err)
    45  	}
    46  
    47  	if err = os.WriteFile(fooPath, fooBytes, 0o666); err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	if err = os.WriteFile(barPath, barBytes, 0o666); err != nil {
    51  		t.Fatal(err)
    52  	}
    53  
    54  	var actConfs map[string]stream.Config
    55  	if actConfs, err = LoadStreamConfigsFromDirectory(true, testDir); err != nil {
    56  		t.Fatal(err)
    57  	}
    58  
    59  	var actKeys, expKeys []string
    60  	for id := range actConfs {
    61  		actKeys = append(actKeys, id)
    62  	}
    63  	sort.Strings(actKeys)
    64  	for id := range expConfs {
    65  		expKeys = append(expKeys, id)
    66  	}
    67  	sort.Strings(expKeys)
    68  
    69  	if !reflect.DeepEqual(actKeys, expKeys) {
    70  		t.Errorf("Wrong keys in loaded set: %v != %v", actKeys, expKeys)
    71  	}
    72  
    73  	if exp, act := "bloblang", actConfs["foo"].Input.Type; exp != act {
    74  		t.Errorf("Wrong value in loaded set: %v != %v", act, exp)
    75  	}
    76  	if exp, act := "nanomsg", actConfs["bar_test"].Input.Type; exp != act {
    77  		t.Errorf("Wrong value in loaded set: %v != %v", act, exp)
    78  	}
    79  }