github.com/influxdata/telegraf@v1.30.3/testutil/testutil.go (about)

     1  package testutil
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"net/url"
     7  	"os"
     8  	"regexp"
     9  	"time"
    10  
    11  	"github.com/google/go-cmp/cmp"
    12  	"github.com/influxdata/telegraf"
    13  	"github.com/influxdata/telegraf/metric"
    14  	"github.com/influxdata/telegraf/plugins/serializers/influx"
    15  )
    16  
    17  var localhost = "localhost"
    18  
    19  // GetLocalHost returns the DOCKER_HOST environment variable, parsing
    20  // out any scheme or ports so that only the IP address is returned.
    21  func GetLocalHost() string {
    22  	if dockerHostVar := os.Getenv("DOCKER_HOST"); dockerHostVar != "" {
    23  		u, err := url.Parse(dockerHostVar)
    24  		if err != nil {
    25  			return dockerHostVar
    26  		}
    27  
    28  		// split out the ip addr from the port
    29  		host, _, err := net.SplitHostPort(u.Host)
    30  		if err != nil {
    31  			return dockerHostVar
    32  		}
    33  
    34  		return host
    35  	}
    36  	return localhost
    37  }
    38  
    39  // MockMetrics returns a mock []telegraf.Metric object for using in unit tests
    40  // of telegraf output sinks.
    41  func MockMetrics() []telegraf.Metric {
    42  	metrics := make([]telegraf.Metric, 0)
    43  	// Create a new point batch
    44  	metrics = append(metrics, TestMetric(1.0))
    45  	return metrics
    46  }
    47  
    48  func MockMetricsWithValue(value float64) []telegraf.Metric {
    49  	metrics := make([]telegraf.Metric, 0)
    50  	// Create a new point batch
    51  	metrics = append(metrics, TestMetric(value))
    52  	return metrics
    53  }
    54  
    55  // TestMetric Returns a simple test point:
    56  //
    57  //	measurement -> "test1" or name
    58  //	tags -> "tag1":"value1"
    59  //	value -> value
    60  //	time -> time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
    61  func TestMetric(value interface{}, name ...string) telegraf.Metric {
    62  	if value == nil {
    63  		panic("Cannot use a nil value")
    64  	}
    65  	measurement := "test1"
    66  	if len(name) > 0 {
    67  		measurement = name[0]
    68  	}
    69  	tags := map[string]string{"tag1": "value1"}
    70  	pt := metric.New(
    71  		measurement,
    72  		tags,
    73  		map[string]interface{}{"value": value},
    74  		time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
    75  	)
    76  	return pt
    77  }
    78  
    79  // OnlyTags returns an option for keeping only "Tags" for a given Metric
    80  func OnlyTags() cmp.Option {
    81  	f := func(p cmp.Path) bool { return p.String() != "Tags" && p.String() != "" }
    82  	return cmp.FilterPath(f, cmp.Ignore())
    83  }
    84  
    85  func PrintMetrics(m []telegraf.Metric) {
    86  	s := &influx.Serializer{
    87  		SortFields:  true,
    88  		UintSupport: true,
    89  	}
    90  	if err := s.Init(); err != nil {
    91  		panic(err)
    92  	}
    93  	buf, err := s.SerializeBatch(m)
    94  	if err != nil {
    95  		panic(err)
    96  	}
    97  	fmt.Println(string(buf))
    98  }
    99  
   100  // DefaultSampleConfig returns the sample config with the default parameters
   101  // uncommented to also be able to test the validity of default setting.
   102  func DefaultSampleConfig(sampleConfig string) []byte {
   103  	re := regexp.MustCompile(`(?m)(^\s+)#\s*`)
   104  	return []byte(re.ReplaceAllString(sampleConfig, "$1"))
   105  }