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

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"sort"
     7  	"testing"
     8  
     9  	"github.com/google/go-cmp/cmp"
    10  	"github.com/google/go-cmp/cmp/cmpopts"
    11  	influxdb "github.com/influxdata/influxdb/v2"
    12  	"github.com/influxdata/influxdb/v2/kit/platform"
    13  	"github.com/influxdata/influxdb/v2/kit/platform/errors"
    14  	"github.com/influxdata/influxdb/v2/mock"
    15  	"github.com/influxdata/influxdb/v2/telegraf/plugins/inputs"
    16  	"github.com/influxdata/influxdb/v2/telegraf/plugins/outputs"
    17  	"github.com/stretchr/testify/assert"
    18  	"github.com/stretchr/testify/require"
    19  )
    20  
    21  var (
    22  	oneID   = platform.ID(1)
    23  	twoID   = platform.ID(2)
    24  	threeID = platform.ID(3)
    25  	fourID  = platform.ID(4)
    26  )
    27  
    28  // TelegrafConfigFields includes prepopulated data for mapping tests.
    29  type TelegrafConfigFields struct {
    30  	IDGenerator     platform.IDGenerator
    31  	TelegrafConfigs []*influxdb.TelegrafConfig
    32  }
    33  
    34  var telegrafCmpOptions = cmp.Options{
    35  	cmpopts.IgnoreUnexported(
    36  		inputs.CPUStats{},
    37  		inputs.MemStats{},
    38  		inputs.Kubernetes{},
    39  		inputs.File{},
    40  		outputs.File{},
    41  		outputs.InfluxDBV2{},
    42  	),
    43  	cmp.Transformer("Sort", func(in []*influxdb.TelegrafConfig) []*influxdb.TelegrafConfig {
    44  		out := append([]*influxdb.TelegrafConfig(nil), in...)
    45  		sort.Slice(out, func(i, j int) bool {
    46  			return out[i].ID > out[j].ID
    47  		})
    48  		return out
    49  	}),
    50  }
    51  
    52  type telegrafTestFactoryFunc func(TelegrafConfigFields, *testing.T) (influxdb.TelegrafConfigStore, func())
    53  
    54  // TelegrafConfigStore tests all the service functions.
    55  func TelegrafConfigStore(
    56  	init telegrafTestFactoryFunc, t *testing.T,
    57  ) {
    58  	tests := []struct {
    59  		name string
    60  		fn   func(init telegrafTestFactoryFunc,
    61  			t *testing.T)
    62  	}{
    63  		{
    64  			name: "CreateTelegrafConfig",
    65  			fn:   CreateTelegrafConfig,
    66  		},
    67  		{
    68  			name: "FindTelegrafConfigByID",
    69  			fn:   FindTelegrafConfigByID,
    70  		},
    71  		{
    72  			name: "FindTelegrafConfigs",
    73  			fn:   FindTelegrafConfigs,
    74  		},
    75  		{
    76  			name: "UpdateTelegrafConfig",
    77  			fn:   UpdateTelegrafConfig,
    78  		},
    79  		{
    80  			name: "DeleteTelegrafConfig",
    81  			fn:   DeleteTelegrafConfig,
    82  		},
    83  	}
    84  	for _, tt := range tests {
    85  		t.Run(tt.name, func(t *testing.T) {
    86  			tt := tt
    87  			t.Parallel()
    88  			tt.fn(init, t)
    89  		})
    90  	}
    91  }
    92  
    93  // CreateTelegrafConfig testing.
    94  func CreateTelegrafConfig(
    95  	init telegrafTestFactoryFunc,
    96  	t *testing.T,
    97  ) {
    98  	type args struct {
    99  		telegrafConfig *influxdb.TelegrafConfig
   100  		userID         platform.ID
   101  	}
   102  	type wants struct {
   103  		err       error
   104  		telegrafs []*influxdb.TelegrafConfig
   105  	}
   106  
   107  	tests := []struct {
   108  		name   string
   109  		fields TelegrafConfigFields
   110  		args   args
   111  		wants  wants
   112  	}{
   113  		{
   114  			name: "create telegraf config without organization ID should error",
   115  			fields: TelegrafConfigFields{
   116  				IDGenerator:     mock.NewStaticIDGenerator(oneID),
   117  				TelegrafConfigs: []*influxdb.TelegrafConfig{},
   118  			},
   119  			args: args{
   120  				telegrafConfig: &influxdb.TelegrafConfig{},
   121  			},
   122  			wants: wants{
   123  				err: &errors.Error{
   124  					Code: errors.EEmptyValue,
   125  					Msg:  influxdb.ErrTelegrafConfigInvalidOrgID,
   126  				},
   127  			},
   128  		},
   129  		{
   130  			name: "create telegraf config with empty set",
   131  			fields: TelegrafConfigFields{
   132  				IDGenerator:     mock.NewStaticIDGenerator(oneID),
   133  				TelegrafConfigs: []*influxdb.TelegrafConfig{},
   134  			},
   135  			args: args{
   136  				userID: threeID,
   137  				telegrafConfig: &influxdb.TelegrafConfig{
   138  					OrgID:    twoID,
   139  					Name:     "name1",
   140  					Config:   "[[inputs.cpu]]\n[[outputs.influxdb_v2]]\n",
   141  					Metadata: map[string]interface{}{"buckets": []interface{}{}},
   142  				},
   143  			},
   144  			wants: wants{
   145  				telegrafs: []*influxdb.TelegrafConfig{
   146  					{
   147  						ID:       oneID,
   148  						OrgID:    twoID,
   149  						Name:     "name1",
   150  						Config:   "[[inputs.cpu]]\n[[outputs.influxdb_v2]]\n",
   151  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   152  					},
   153  				},
   154  			},
   155  		},
   156  		{
   157  			name: "basic create telegraf config",
   158  			fields: TelegrafConfigFields{
   159  				IDGenerator: mock.NewStaticIDGenerator(twoID),
   160  				TelegrafConfigs: []*influxdb.TelegrafConfig{
   161  					{
   162  						ID:       oneID,
   163  						OrgID:    twoID,
   164  						Name:     "tc1",
   165  						Config:   "[[inputs.mem_stats]]\n",
   166  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   167  					},
   168  				},
   169  			},
   170  			args: args{
   171  				userID: threeID,
   172  				telegrafConfig: &influxdb.TelegrafConfig{
   173  					OrgID:    twoID,
   174  					Name:     "name2",
   175  					Config:   "[[inputs.cpu]]\n[[outputs.influxdb_v2]]\n",
   176  					Metadata: map[string]interface{}{"buckets": []interface{}{}}, // for inmem test as it doesn't unmarshal..
   177  				},
   178  			},
   179  			wants: wants{
   180  				telegrafs: []*influxdb.TelegrafConfig{
   181  					{
   182  						ID:       oneID,
   183  						OrgID:    twoID,
   184  						Name:     "tc1",
   185  						Config:   "[[inputs.mem_stats]]\n",
   186  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   187  					},
   188  					{
   189  						ID:       twoID,
   190  						OrgID:    twoID,
   191  						Name:     "name2",
   192  						Config:   "[[inputs.cpu]]\n[[outputs.influxdb_v2]]\n",
   193  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   194  					},
   195  				},
   196  			},
   197  		},
   198  	}
   199  
   200  	for _, tt := range tests {
   201  		t.Run(tt.name, func(t *testing.T) {
   202  			s, done := init(tt.fields, t)
   203  			defer done()
   204  			ctx := context.Background()
   205  			err := s.CreateTelegrafConfig(ctx, tt.args.telegrafConfig, tt.args.userID)
   206  			if (err != nil) != (tt.wants.err != nil) {
   207  				t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
   208  			}
   209  			if tt.wants.err == nil && !tt.args.telegrafConfig.ID.Valid() {
   210  				t.Fatalf("telegraf config ID not set from CreateTelegrafConfig")
   211  			}
   212  
   213  			if err != nil && tt.wants.err != nil {
   214  				if errors.ErrorCode(err) != errors.ErrorCode(tt.wants.err) {
   215  					t.Fatalf("expected error messages to match '%v' got '%v'", errors.ErrorCode(tt.wants.err), errors.ErrorCode(err))
   216  				}
   217  			}
   218  
   219  			filter := influxdb.TelegrafConfigFilter{}
   220  			tcs, _, err := s.FindTelegrafConfigs(ctx, filter)
   221  			if err != nil {
   222  				t.Fatalf("failed to retrieve telegraf configs: %v", err)
   223  			}
   224  			if diff := cmp.Diff(tcs, tt.wants.telegrafs, telegrafCmpOptions...); diff != "" {
   225  				t.Errorf("telegraf configs are different -got/+want\ndiff %s", diff)
   226  			}
   227  		})
   228  	}
   229  }
   230  
   231  // FindTelegrafConfigByID testing.
   232  func FindTelegrafConfigByID(
   233  	init telegrafTestFactoryFunc,
   234  	t *testing.T,
   235  ) {
   236  	type args struct {
   237  		id platform.ID
   238  	}
   239  	type wants struct {
   240  		err            error
   241  		telegrafConfig *influxdb.TelegrafConfig
   242  	}
   243  
   244  	tests := []struct {
   245  		name   string
   246  		fields TelegrafConfigFields
   247  		args   args
   248  		wants  wants
   249  	}{
   250  		{
   251  			name: "bad id",
   252  			fields: TelegrafConfigFields{
   253  				TelegrafConfigs: []*influxdb.TelegrafConfig{
   254  					{
   255  						ID:       oneID,
   256  						OrgID:    twoID,
   257  						Name:     "tc1",
   258  						Config:   "[[inputs.cpu]]\n",
   259  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   260  					},
   261  					{
   262  						ID:       twoID,
   263  						OrgID:    twoID,
   264  						Name:     "tc2",
   265  						Config:   "[[inputs.file]]\n[[inputs.mem]]\n",
   266  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   267  					},
   268  				},
   269  			},
   270  			args: args{
   271  				id: platform.ID(0),
   272  			},
   273  			wants: wants{
   274  				err: fmt.Errorf("provided telegraf configuration ID has invalid format"),
   275  			},
   276  		},
   277  		{
   278  			name: "not found",
   279  			fields: TelegrafConfigFields{
   280  				TelegrafConfigs: []*influxdb.TelegrafConfig{
   281  					{
   282  						ID:     oneID,
   283  						OrgID:  twoID,
   284  						Name:   "tc1",
   285  						Config: "[[inputs.cpu]]\n",
   286  					},
   287  					{
   288  						ID:     twoID,
   289  						OrgID:  twoID,
   290  						Name:   "tc2",
   291  						Config: "[[inputs.file]]\n[[inputs.mem]]\n",
   292  					},
   293  				},
   294  			},
   295  			args: args{
   296  				id: threeID,
   297  			},
   298  			wants: wants{
   299  				err: &errors.Error{
   300  					Code: errors.ENotFound,
   301  					Msg:  "telegraf configuration not found",
   302  				},
   303  			},
   304  		},
   305  		{
   306  			name: "basic find telegraf config by id",
   307  			fields: TelegrafConfigFields{
   308  				TelegrafConfigs: []*influxdb.TelegrafConfig{
   309  					{
   310  						ID:       oneID,
   311  						OrgID:    threeID,
   312  						Name:     "tc1",
   313  						Config:   "[[inputs.cpu]]\n",
   314  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   315  					},
   316  					{
   317  						ID:       twoID,
   318  						OrgID:    threeID,
   319  						Name:     "tc2",
   320  						Config:   "[[inputs.file]]\n[[inputs.mem]]\n",
   321  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   322  					},
   323  				},
   324  			},
   325  			args: args{
   326  				id: twoID,
   327  			},
   328  			wants: wants{
   329  				telegrafConfig: &influxdb.TelegrafConfig{
   330  					ID:       twoID,
   331  					OrgID:    threeID,
   332  					Name:     "tc2",
   333  					Config:   "[[inputs.file]]\n[[inputs.mem]]\n",
   334  					Metadata: map[string]interface{}{"buckets": []interface{}{}},
   335  				},
   336  			},
   337  		},
   338  	}
   339  	for _, tt := range tests {
   340  		t.Run(tt.name, func(t *testing.T) {
   341  			s, done := init(tt.fields, t)
   342  			defer done()
   343  			ctx := context.Background()
   344  
   345  			tc, err := s.FindTelegrafConfigByID(ctx, tt.args.id)
   346  			if (err != nil) != (tt.wants.err != nil) {
   347  				t.Fatalf("expected errors to be equal '%v' got '%v'", tt.wants.err, err)
   348  			}
   349  
   350  			if err != nil && tt.wants.err != nil {
   351  				if want, got := tt.wants.err.Error(), err.Error(); want != got {
   352  					t.Fatalf("expected error '%s' got '%s'", want, got)
   353  				}
   354  			}
   355  			if diff := cmp.Diff(tc, tt.wants.telegrafConfig, telegrafCmpOptions...); diff != "" {
   356  				t.Errorf("telegraf configs are different -got/+want\ndiff %s", diff)
   357  			}
   358  		})
   359  	}
   360  }
   361  
   362  // FindTelegrafConfigs testing
   363  func FindTelegrafConfigs(
   364  	init telegrafTestFactoryFunc,
   365  	t *testing.T,
   366  ) {
   367  	type args struct {
   368  		filter influxdb.TelegrafConfigFilter
   369  		opts   []influxdb.FindOptions
   370  	}
   371  
   372  	type wants struct {
   373  		telegrafConfigs []*influxdb.TelegrafConfig
   374  		err             error
   375  	}
   376  	tests := []struct {
   377  		name   string
   378  		fields TelegrafConfigFields
   379  		args   args
   380  		wants  wants
   381  	}{
   382  		{
   383  			name: "find nothing (empty set)",
   384  			fields: TelegrafConfigFields{
   385  				TelegrafConfigs: []*influxdb.TelegrafConfig{},
   386  			},
   387  			args: args{
   388  				filter: influxdb.TelegrafConfigFilter{},
   389  			},
   390  			wants: wants{
   391  				telegrafConfigs: []*influxdb.TelegrafConfig{},
   392  			},
   393  		},
   394  		{
   395  			name: "find all telegraf configs (across orgs)",
   396  			fields: TelegrafConfigFields{
   397  				IDGenerator: mock.NewIncrementingIDGenerator(oneID),
   398  				TelegrafConfigs: []*influxdb.TelegrafConfig{
   399  					{
   400  						ID:       oneID,
   401  						OrgID:    twoID,
   402  						Name:     "tc1",
   403  						Config:   "[[inputs.cpu]]\n",
   404  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   405  					},
   406  					{
   407  						ID:       twoID,
   408  						OrgID:    threeID,
   409  						Name:     "tc2",
   410  						Config:   "[[inputs.file]]\n[[inputs.mem]]\n",
   411  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   412  					},
   413  				},
   414  			},
   415  			args: args{
   416  				filter: influxdb.TelegrafConfigFilter{},
   417  			},
   418  			wants: wants{
   419  				telegrafConfigs: []*influxdb.TelegrafConfig{
   420  					{
   421  						ID:       oneID,
   422  						OrgID:    twoID,
   423  						Name:     "tc1",
   424  						Config:   "[[inputs.cpu]]\n",
   425  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   426  					},
   427  					{
   428  						ID:       twoID,
   429  						OrgID:    threeID,
   430  						Name:     "tc2",
   431  						Config:   "[[inputs.file]]\n[[inputs.mem]]\n",
   432  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   433  					},
   434  				},
   435  			},
   436  		},
   437  		{
   438  			name: "filter by organization only",
   439  			fields: TelegrafConfigFields{
   440  				IDGenerator: mock.NewIncrementingIDGenerator(oneID),
   441  				TelegrafConfigs: []*influxdb.TelegrafConfig{
   442  					{
   443  						ID:       oneID,
   444  						OrgID:    fourID,
   445  						Name:     "tc1",
   446  						Config:   "[[inputs.cpu]]\n",
   447  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   448  					},
   449  					{
   450  						ID:       twoID,
   451  						OrgID:    fourID,
   452  						Name:     "tc2",
   453  						Config:   "[[inputs.file]]\n[[inputs.mem]]\n",
   454  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   455  					},
   456  					{
   457  						ID:       threeID,
   458  						OrgID:    oneID,
   459  						Name:     "tc3",
   460  						Config:   "[[inputs.cpu]]\n",
   461  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   462  					},
   463  					{
   464  						ID:       fourID,
   465  						OrgID:    oneID,
   466  						Name:     "tc4",
   467  						Config:   "[[inputs.cpu]]\n",
   468  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   469  					},
   470  				},
   471  			},
   472  			args: args{
   473  				filter: influxdb.TelegrafConfigFilter{
   474  					OrgID: &oneID,
   475  				},
   476  			},
   477  			wants: wants{
   478  				telegrafConfigs: []*influxdb.TelegrafConfig{
   479  					{
   480  						ID:       threeID,
   481  						OrgID:    oneID,
   482  						Name:     "tc3",
   483  						Config:   "[[inputs.cpu]]\n",
   484  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   485  					},
   486  					{
   487  						ID:       fourID,
   488  						OrgID:    oneID,
   489  						Name:     "tc4",
   490  						Config:   "[[inputs.cpu]]\n",
   491  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   492  					},
   493  				},
   494  			},
   495  		},
   496  		{
   497  			name: "empty for provided org",
   498  			fields: TelegrafConfigFields{
   499  				IDGenerator: mock.NewIncrementingIDGenerator(oneID),
   500  				TelegrafConfigs: []*influxdb.TelegrafConfig{
   501  					{
   502  						ID:     oneID,
   503  						OrgID:  threeID,
   504  						Name:   "tc1",
   505  						Config: "[[inputs.cpu]]\n",
   506  					},
   507  					{
   508  						ID:     twoID,
   509  						OrgID:  threeID,
   510  						Name:   "tc2",
   511  						Config: "[[inputs.file]]\n[[inputs.mem]]\n",
   512  					},
   513  				},
   514  			},
   515  			args: args{
   516  				filter: influxdb.TelegrafConfigFilter{
   517  					OrgID: &oneID,
   518  				},
   519  			},
   520  			wants: wants{
   521  				telegrafConfigs: []*influxdb.TelegrafConfig{},
   522  			},
   523  		},
   524  	}
   525  
   526  	for _, tt := range tests {
   527  		t.Run(tt.name, func(t *testing.T) {
   528  			s, done := init(tt.fields, t)
   529  			defer done()
   530  			ctx := context.Background()
   531  
   532  			tcs, _, err := s.FindTelegrafConfigs(ctx, tt.args.filter, tt.args.opts...)
   533  			if err != nil && tt.wants.err == nil {
   534  				t.Fatalf("expected errors to be nil got '%v'", err)
   535  			}
   536  
   537  			require.Equal(t, tt.wants.err, err)
   538  			assert.Equal(t, tt.wants.telegrafConfigs, tcs)
   539  		})
   540  	}
   541  }
   542  
   543  // UpdateTelegrafConfig testing.
   544  func UpdateTelegrafConfig(
   545  	init telegrafTestFactoryFunc,
   546  	t *testing.T,
   547  ) {
   548  	type args struct {
   549  		userID         platform.ID
   550  		id             platform.ID
   551  		telegrafConfig *influxdb.TelegrafConfig
   552  	}
   553  
   554  	type wants struct {
   555  		telegrafConfig *influxdb.TelegrafConfig
   556  		err            error
   557  	}
   558  	tests := []struct {
   559  		name   string
   560  		fields TelegrafConfigFields
   561  		args   args
   562  		wants  wants
   563  	}{
   564  		{
   565  			name: "can't find the id",
   566  			fields: TelegrafConfigFields{
   567  				TelegrafConfigs: []*influxdb.TelegrafConfig{
   568  					{
   569  						ID:     oneID,
   570  						OrgID:  fourID,
   571  						Name:   "tc1",
   572  						Config: "[[inputs.cpu]]\n",
   573  					},
   574  					{
   575  						ID:     twoID,
   576  						OrgID:  fourID,
   577  						Name:   "tc2",
   578  						Config: "[[inputs.file]]\n[[inputs.mem]]\n",
   579  					},
   580  				},
   581  			},
   582  			args: args{
   583  				userID: threeID,
   584  				id:     fourID,
   585  				telegrafConfig: &influxdb.TelegrafConfig{
   586  					Name:   "tc2",
   587  					Config: "[[inputs.file]]\n[[inputs.mem]]\n",
   588  				},
   589  			},
   590  			wants: wants{
   591  				err: &errors.Error{
   592  					Code: errors.ENotFound,
   593  					Msg:  fmt.Sprintf("telegraf config with ID %v not found", fourID),
   594  				},
   595  			},
   596  		},
   597  		{
   598  			fields: TelegrafConfigFields{
   599  				TelegrafConfigs: []*influxdb.TelegrafConfig{
   600  					{
   601  						ID:       oneID,
   602  						OrgID:    fourID,
   603  						Name:     "tc1",
   604  						Config:   "[[inputs.cpu]]\n",
   605  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   606  					},
   607  					{
   608  						ID:       twoID,
   609  						OrgID:    fourID,
   610  						Name:     "tc2",
   611  						Config:   "[[inputs.file]]\n[[inputs.mem]]\n",
   612  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   613  					},
   614  				},
   615  			},
   616  			args: args{
   617  				userID: fourID,
   618  				id:     twoID,
   619  				telegrafConfig: &influxdb.TelegrafConfig{
   620  					OrgID:  oneID, // notice this get ignored - ie., resulting TelegrafConfig will have OrgID equal to fourID
   621  					Name:   "tc2",
   622  					Config: "[[inputs.file]]\n[[inputs.mem]]\n",
   623  				},
   624  			},
   625  			wants: wants{
   626  				telegrafConfig: &influxdb.TelegrafConfig{
   627  					ID:     twoID,
   628  					OrgID:  fourID,
   629  					Name:   "tc2",
   630  					Config: "[[inputs.file]]\n[[inputs.mem]]\n",
   631  				},
   632  			},
   633  		},
   634  		{
   635  			name: "config update",
   636  			fields: TelegrafConfigFields{
   637  				TelegrafConfigs: []*influxdb.TelegrafConfig{
   638  					{
   639  						ID:     oneID,
   640  						OrgID:  oneID,
   641  						Name:   "tc1",
   642  						Config: "[[inputs.cpu]]\n",
   643  					},
   644  					{
   645  						ID:     twoID,
   646  						OrgID:  oneID,
   647  						Name:   "tc2",
   648  						Config: "[[inputs.file]]\n[[inputs.kubernetes]]\n[[inputs.kubernetes]]\n",
   649  					},
   650  				},
   651  			},
   652  			args: args{
   653  				userID: fourID,
   654  				id:     twoID,
   655  				telegrafConfig: &influxdb.TelegrafConfig{
   656  					Name:   "tc2",
   657  					Config: "[[inputs.file]]\n[[inputs.kubernetes]]\n[[inputs.kubernetes]]\n",
   658  				},
   659  			},
   660  			wants: wants{
   661  				telegrafConfig: &influxdb.TelegrafConfig{
   662  					ID:     twoID,
   663  					OrgID:  oneID,
   664  					Name:   "tc2",
   665  					Config: "[[inputs.file]]\n[[inputs.kubernetes]]\n[[inputs.kubernetes]]\n",
   666  				},
   667  			},
   668  		},
   669  	}
   670  	for _, tt := range tests {
   671  		t.Run(tt.name, func(t *testing.T) {
   672  			s, done := init(tt.fields, t)
   673  			defer done()
   674  			ctx := context.Background()
   675  
   676  			tc, err := s.UpdateTelegrafConfig(ctx, tt.args.id,
   677  				tt.args.telegrafConfig, tt.args.userID)
   678  			if err != nil && tt.wants.err == nil {
   679  				t.Fatalf("expected errors to be nil got '%v'", err)
   680  			}
   681  			if err != nil && tt.wants.err != nil {
   682  				if errors.ErrorCode(err) != errors.ErrorCode(tt.wants.err) {
   683  					t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
   684  				}
   685  			}
   686  			if diff := cmp.Diff(tc, tt.wants.telegrafConfig, telegrafCmpOptions...); tt.wants.err == nil && diff != "" {
   687  				fmt.Println(tc.Metadata, tt.wants.telegrafConfig.Metadata)
   688  				t.Errorf("telegraf configs are different -got/+want\ndiff %s", diff)
   689  			}
   690  		})
   691  	}
   692  }
   693  
   694  // DeleteTelegrafConfig testing.
   695  func DeleteTelegrafConfig(
   696  	init telegrafTestFactoryFunc,
   697  	t *testing.T,
   698  ) {
   699  	type args struct {
   700  		id platform.ID
   701  	}
   702  
   703  	type wants struct {
   704  		telegrafConfigs []*influxdb.TelegrafConfig
   705  		err             error
   706  	}
   707  	tests := []struct {
   708  		name   string
   709  		fields TelegrafConfigFields
   710  		args   args
   711  		wants  wants
   712  	}{
   713  		{
   714  			name: "bad id",
   715  			fields: TelegrafConfigFields{
   716  				IDGenerator: mock.NewIncrementingIDGenerator(oneID),
   717  				TelegrafConfigs: []*influxdb.TelegrafConfig{
   718  					{
   719  						ID:       oneID,
   720  						OrgID:    fourID,
   721  						Name:     "tc1",
   722  						Config:   "[[inputs.cpu]]\n",
   723  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   724  					},
   725  					{
   726  						ID:       twoID,
   727  						OrgID:    fourID,
   728  						Name:     "tc2",
   729  						Config:   "[[inputs.file]]\n[[inputs.mem]]\n",
   730  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   731  					},
   732  				},
   733  			},
   734  			args: args{
   735  				id: platform.ID(0),
   736  			},
   737  			wants: wants{
   738  				err: fmt.Errorf("provided telegraf configuration ID has invalid format"),
   739  				telegrafConfigs: []*influxdb.TelegrafConfig{
   740  					{
   741  						ID:       oneID,
   742  						OrgID:    fourID,
   743  						Name:     "tc1",
   744  						Config:   "[[inputs.cpu]]\n",
   745  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   746  					},
   747  					{
   748  						ID:       twoID,
   749  						OrgID:    fourID,
   750  						Name:     "tc2",
   751  						Config:   "[[inputs.file]]\n[[inputs.mem]]\n",
   752  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   753  					},
   754  				},
   755  			},
   756  		},
   757  		{
   758  			name: "none existing config",
   759  			fields: TelegrafConfigFields{
   760  				IDGenerator: mock.NewIncrementingIDGenerator(oneID),
   761  				TelegrafConfigs: []*influxdb.TelegrafConfig{
   762  					{
   763  						ID:       oneID,
   764  						OrgID:    threeID,
   765  						Name:     "tc1",
   766  						Config:   "[[inputs.cpu]]\n",
   767  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   768  					},
   769  					{
   770  						ID:       twoID,
   771  						OrgID:    threeID,
   772  						Name:     "tc2",
   773  						Config:   "[[inputs.file]]\n[[inputs.mem]]\n",
   774  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   775  					},
   776  				},
   777  			},
   778  			args: args{
   779  				id: fourID,
   780  			},
   781  			wants: wants{
   782  				err: fmt.Errorf("telegraf configuration not found"),
   783  				telegrafConfigs: []*influxdb.TelegrafConfig{
   784  					{
   785  						ID:       oneID,
   786  						OrgID:    threeID,
   787  						Name:     "tc1",
   788  						Config:   "[[inputs.cpu]]\n",
   789  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   790  					},
   791  					{
   792  						ID:       twoID,
   793  						OrgID:    threeID,
   794  						Name:     "tc2",
   795  						Config:   "[[inputs.file]]\n[[inputs.mem]]\n",
   796  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   797  					},
   798  				},
   799  			},
   800  		},
   801  		{
   802  			name: "regular delete",
   803  			fields: TelegrafConfigFields{
   804  				IDGenerator: mock.NewIncrementingIDGenerator(oneID),
   805  				TelegrafConfigs: []*influxdb.TelegrafConfig{
   806  					{
   807  						ID:       oneID,
   808  						OrgID:    twoID,
   809  						Name:     "tc1",
   810  						Config:   "[[inputs.cpu]]\n",
   811  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   812  					},
   813  					{
   814  						ID:       twoID,
   815  						OrgID:    twoID,
   816  						Name:     "tc2",
   817  						Config:   "[[inputs.file]]\n[[inputs.mem]]\n",
   818  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   819  					},
   820  				},
   821  			},
   822  			args: args{
   823  				id: twoID,
   824  			},
   825  			wants: wants{
   826  				telegrafConfigs: []*influxdb.TelegrafConfig{
   827  					{
   828  						ID:       oneID,
   829  						OrgID:    twoID,
   830  						Name:     "tc1",
   831  						Config:   "[[inputs.cpu]]\n",
   832  						Metadata: map[string]interface{}{"buckets": []interface{}{}},
   833  					},
   834  				},
   835  			},
   836  		},
   837  	}
   838  	for _, tt := range tests {
   839  		t.Run(tt.name, func(t *testing.T) {
   840  			s, done := init(tt.fields, t)
   841  			defer done()
   842  			ctx := context.Background()
   843  			err := s.DeleteTelegrafConfig(ctx, tt.args.id)
   844  			if err != nil && tt.wants.err == nil {
   845  				t.Fatalf("expected errors to be nil got '%v'", err)
   846  			}
   847  
   848  			if err != nil && tt.wants.err != nil {
   849  				if want, got := tt.wants.err.Error(), err.Error(); want != got {
   850  					t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
   851  				}
   852  			}
   853  
   854  			tcs, _, err := s.FindTelegrafConfigs(ctx, influxdb.TelegrafConfigFilter{})
   855  			require.NoError(t, err)
   856  			assert.Equal(t, tt.wants.telegrafConfigs, tcs)
   857  		})
   858  	}
   859  }