github.com/yandex/pandora@v0.5.32/tests/acceptance/common.go (about)

     1  package acceptance
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"os"
     7  	"sync"
     8  	"testing"
     9  	"text/template"
    10  
    11  	"github.com/spf13/afero"
    12  	"github.com/stretchr/testify/require"
    13  	"github.com/yandex/pandora/cli"
    14  	grpc "github.com/yandex/pandora/components/grpc/import"
    15  	"github.com/yandex/pandora/components/guns"
    16  	phttpimport "github.com/yandex/pandora/components/phttp/import"
    17  	"github.com/yandex/pandora/core"
    18  	"github.com/yandex/pandora/core/config"
    19  	coreimport "github.com/yandex/pandora/core/import"
    20  	"gopkg.in/yaml.v2"
    21  )
    22  
    23  func importDependencies(fs afero.Fs) func() {
    24  	return func() {
    25  		coreimport.Import(fs)
    26  		phttpimport.Import(fs)
    27  		grpc.Import(fs)
    28  		guns.Import(fs)
    29  	}
    30  }
    31  
    32  func parseConfigFile(t *testing.T, filename string, serverAddr string) *cli.CliConfig {
    33  	t.Helper()
    34  	mapCfg := unmarshalConfigFile(t, filename, serverAddr)
    35  	conf := decodeConfig(t, mapCfg)
    36  	return conf
    37  }
    38  
    39  func decodeConfig(t *testing.T, mapCfg map[string]any) *cli.CliConfig {
    40  	t.Helper()
    41  	conf := cli.DefaultConfig()
    42  	err := config.DecodeAndValidate(mapCfg, conf)
    43  	require.NoError(t, err)
    44  	return conf
    45  }
    46  
    47  func unmarshalConfigFile(t *testing.T, filename string, serverAddr string) map[string]any {
    48  	t.Helper()
    49  	f, err := os.ReadFile(filename)
    50  	require.NoError(t, err)
    51  	tmpl, err := template.New("x").Parse(string(f))
    52  	require.NoError(t, err)
    53  	b := &bytes.Buffer{}
    54  	err = tmpl.Execute(b, map[string]string{"target": serverAddr})
    55  	require.NoError(t, err)
    56  	mapCfg := map[string]any{}
    57  	err = yaml.Unmarshal(b.Bytes(), &mapCfg)
    58  	require.NoError(t, err)
    59  	return mapCfg
    60  }
    61  
    62  type aggregator struct {
    63  	mx      sync.Mutex
    64  	samples []core.Sample
    65  }
    66  
    67  func (a *aggregator) Run(ctx context.Context, deps core.AggregatorDeps) error {
    68  	return nil
    69  }
    70  
    71  func (a *aggregator) Report(s core.Sample) {
    72  	a.mx.Lock()
    73  	defer a.mx.Unlock()
    74  	a.samples = append(a.samples, s)
    75  }