github.com/influxdata/influxdb/v2@v2.7.6/telegraf/service/telegraf_test.go (about)

     1  package service_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/influxdata/influxdb/v2"
     8  	"github.com/influxdata/influxdb/v2/kv"
     9  	telegrafservice "github.com/influxdata/influxdb/v2/telegraf/service"
    10  	telegraftesting "github.com/influxdata/influxdb/v2/telegraf/service/testing"
    11  	itesting "github.com/influxdata/influxdb/v2/testing"
    12  )
    13  
    14  func TestBoltTelegrafService(t *testing.T) {
    15  	telegraftesting.TelegrafConfigStore(initBoltTelegrafService, t)
    16  }
    17  
    18  func initBoltTelegrafService(f telegraftesting.TelegrafConfigFields, t *testing.T) (influxdb.TelegrafConfigStore, func()) {
    19  	s, closeBolt := itesting.NewTestBoltStore(t)
    20  	svc, closeSvc := initTelegrafService(s, f, t)
    21  	return svc, func() {
    22  		closeSvc()
    23  		closeBolt()
    24  	}
    25  }
    26  
    27  func initTelegrafService(s kv.SchemaStore, f telegraftesting.TelegrafConfigFields, t *testing.T) (influxdb.TelegrafConfigStore, func()) {
    28  	ctx := context.Background()
    29  
    30  	svc := telegrafservice.New(s)
    31  	svc.IDGenerator = f.IDGenerator
    32  
    33  	for _, tc := range f.TelegrafConfigs {
    34  		if err := svc.PutTelegrafConfig(ctx, tc); err != nil {
    35  			t.Fatalf("failed to populate telegraf config: %v", err)
    36  		}
    37  	}
    38  
    39  	return svc, func() {
    40  		for _, tc := range f.TelegrafConfigs {
    41  			if err := svc.DeleteTelegrafConfig(ctx, tc.ID); err != nil {
    42  				t.Logf("failed to remove telegraf config: %v", err)
    43  			}
    44  		}
    45  	}
    46  }