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

     1  package testutil
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	"github.com/influxdata/telegraf"
     9  	"github.com/influxdata/telegraf/metric"
    10  )
    11  
    12  func TestRequireMetricEqual(t *testing.T) {
    13  	tests := []struct {
    14  		name string
    15  		got  telegraf.Metric
    16  		want telegraf.Metric
    17  	}{
    18  		{
    19  			name: "equal metrics should be equal",
    20  			got: func() telegraf.Metric {
    21  				m := metric.New(
    22  					"test",
    23  					map[string]string{
    24  						"t1": "v1",
    25  						"t2": "v2",
    26  					},
    27  					map[string]interface{}{
    28  						"f1": 1,
    29  						"f2": 3.14,
    30  						"f3": "v3",
    31  					},
    32  					time.Unix(0, 0),
    33  				)
    34  				return m
    35  			}(),
    36  			want: func() telegraf.Metric {
    37  				m := metric.New(
    38  					"test",
    39  					map[string]string{
    40  						"t1": "v1",
    41  						"t2": "v2",
    42  					},
    43  					map[string]interface{}{
    44  						"f1": int64(1),
    45  						"f2": 3.14,
    46  						"f3": "v3",
    47  					},
    48  					time.Unix(0, 0),
    49  				)
    50  				return m
    51  			}(),
    52  		},
    53  	}
    54  	for _, tt := range tests {
    55  		t.Run(tt.name, func(t *testing.T) {
    56  			RequireMetricEqual(t, tt.want, tt.got)
    57  		})
    58  	}
    59  }
    60  
    61  func TestRequireMetricsEqual(t *testing.T) {
    62  	tests := []struct {
    63  		name string
    64  		got  []telegraf.Metric
    65  		want []telegraf.Metric
    66  		opts []cmp.Option
    67  	}{
    68  		{
    69  			name: "sort metrics option sorts by name",
    70  			got: []telegraf.Metric{
    71  				MustMetric(
    72  					"cpu",
    73  					map[string]string{},
    74  					map[string]interface{}{},
    75  					time.Unix(0, 0),
    76  				),
    77  				MustMetric(
    78  					"net",
    79  					map[string]string{},
    80  					map[string]interface{}{},
    81  					time.Unix(0, 0),
    82  				),
    83  			},
    84  			want: []telegraf.Metric{
    85  				MustMetric(
    86  					"net",
    87  					map[string]string{},
    88  					map[string]interface{}{},
    89  					time.Unix(0, 0),
    90  				),
    91  				MustMetric(
    92  					"cpu",
    93  					map[string]string{},
    94  					map[string]interface{}{},
    95  					time.Unix(0, 0),
    96  				),
    97  			},
    98  			opts: []cmp.Option{SortMetrics()},
    99  		},
   100  	}
   101  	for _, tt := range tests {
   102  		t.Run(tt.name, func(t *testing.T) {
   103  			RequireMetricsEqual(t, tt.want, tt.got, tt.opts...)
   104  		})
   105  	}
   106  }
   107  
   108  func TestRequireMetricsSubset(t *testing.T) {
   109  	tests := []struct {
   110  		name string
   111  		got  []telegraf.Metric
   112  		want []telegraf.Metric
   113  		opts []cmp.Option
   114  	}{
   115  		{
   116  			name: "subset of metrics",
   117  			got: []telegraf.Metric{
   118  				MustMetric(
   119  					"cpu",
   120  					map[string]string{},
   121  					map[string]interface{}{"value": float64(3.14)},
   122  					time.Unix(0, 0),
   123  				),
   124  				MustMetric(
   125  					"net",
   126  					map[string]string{},
   127  					map[string]interface{}{"value": int64(42)},
   128  					time.Unix(0, 0),
   129  				),
   130  				MustMetric(
   131  					"superfluous",
   132  					map[string]string{},
   133  					map[string]interface{}{"value": true},
   134  					time.Unix(0, 0),
   135  				),
   136  			},
   137  			want: []telegraf.Metric{
   138  				MustMetric(
   139  					"net",
   140  					map[string]string{},
   141  					map[string]interface{}{"value": int64(42)},
   142  					time.Unix(0, 0),
   143  				),
   144  				MustMetric(
   145  					"cpu",
   146  					map[string]string{},
   147  					map[string]interface{}{"value": float64(3.14)},
   148  					time.Unix(0, 0),
   149  				),
   150  			},
   151  			opts: []cmp.Option{SortMetrics()},
   152  		},
   153  	}
   154  	for _, tt := range tests {
   155  		t.Run(tt.name, func(t *testing.T) {
   156  			RequireMetricsSubset(t, tt.want, tt.got, tt.opts...)
   157  		})
   158  	}
   159  }
   160  
   161  func TestRequireMetricsStructureEqual(t *testing.T) {
   162  	tests := []struct {
   163  		name string
   164  		got  []telegraf.Metric
   165  		want []telegraf.Metric
   166  		opts []cmp.Option
   167  	}{
   168  		{
   169  			name: "compare structure",
   170  			got: []telegraf.Metric{
   171  				MustMetric(
   172  					"cpu",
   173  					map[string]string{},
   174  					map[string]interface{}{"value": float64(3.14)},
   175  					time.Unix(0, 0),
   176  				),
   177  				MustMetric(
   178  					"net",
   179  					map[string]string{},
   180  					map[string]interface{}{"value": int64(42)},
   181  					time.Unix(0, 0),
   182  				),
   183  			},
   184  			want: []telegraf.Metric{
   185  				MustMetric(
   186  					"net",
   187  					map[string]string{},
   188  					map[string]interface{}{"value": int64(0)},
   189  					time.Unix(0, 0),
   190  				),
   191  				MustMetric(
   192  					"cpu",
   193  					map[string]string{},
   194  					map[string]interface{}{"value": float64(0)},
   195  					time.Unix(0, 0),
   196  				),
   197  			},
   198  			opts: []cmp.Option{SortMetrics()},
   199  		},
   200  	}
   201  	for _, tt := range tests {
   202  		t.Run(tt.name, func(t *testing.T) {
   203  			RequireMetricsStructureEqual(t, tt.want, tt.got, tt.opts...)
   204  		})
   205  	}
   206  }
   207  
   208  func TestRequireMetricsStructureSubset(t *testing.T) {
   209  	tests := []struct {
   210  		name string
   211  		got  []telegraf.Metric
   212  		want []telegraf.Metric
   213  		opts []cmp.Option
   214  	}{
   215  		{
   216  			name: "subset of metric structure",
   217  			got: []telegraf.Metric{
   218  				MustMetric(
   219  					"cpu",
   220  					map[string]string{},
   221  					map[string]interface{}{"value": float64(3.14)},
   222  					time.Unix(0, 0),
   223  				),
   224  				MustMetric(
   225  					"net",
   226  					map[string]string{},
   227  					map[string]interface{}{"value": int64(42)},
   228  					time.Unix(0, 0),
   229  				),
   230  				MustMetric(
   231  					"superfluous",
   232  					map[string]string{},
   233  					map[string]interface{}{"value": true},
   234  					time.Unix(0, 0),
   235  				),
   236  			},
   237  			want: []telegraf.Metric{
   238  				MustMetric(
   239  					"net",
   240  					map[string]string{},
   241  					map[string]interface{}{"value": int64(0)},
   242  					time.Unix(0, 0),
   243  				),
   244  				MustMetric(
   245  					"cpu",
   246  					map[string]string{},
   247  					map[string]interface{}{"value": float64(0)},
   248  					time.Unix(0, 0),
   249  				),
   250  			},
   251  			opts: []cmp.Option{SortMetrics()},
   252  		},
   253  	}
   254  	for _, tt := range tests {
   255  		t.Run(tt.name, func(t *testing.T) {
   256  			RequireMetricsStructureSubset(t, tt.want, tt.got, tt.opts...)
   257  		})
   258  	}
   259  }