github.com/netdata/go.d.plugin@v0.58.1/agent/agent_test.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package agent
     4  
     5  import (
     6  	"bytes"
     7  	"context"
     8  	"sync"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/netdata/go.d.plugin/agent/module"
    13  	"github.com/netdata/go.d.plugin/agent/safewriter"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  // TODO: tech debt
    18  func TestNew(t *testing.T) {
    19  
    20  }
    21  
    22  func TestAgent_Run(t *testing.T) {
    23  	a := New(Config{
    24  		Name:              "",
    25  		ConfDir:           nil,
    26  		ModulesConfDir:    nil,
    27  		ModulesSDConfPath: nil,
    28  		StateFile:         "",
    29  		ModuleRegistry:    nil,
    30  		RunModule:         "",
    31  		MinUpdateEvery:    0,
    32  	})
    33  
    34  	var buf bytes.Buffer
    35  	a.Out = safewriter.New(&buf)
    36  
    37  	var mux sync.Mutex
    38  	stats := make(map[string]int)
    39  	a.ModuleRegistry = prepareRegistry(&mux, stats, "module1", "module2")
    40  
    41  	ctx, cancel := context.WithCancel(context.Background())
    42  	var wg sync.WaitGroup
    43  
    44  	wg.Add(1)
    45  	go func() { defer wg.Done(); a.run(ctx) }()
    46  
    47  	time.Sleep(time.Second * 2)
    48  	cancel()
    49  	wg.Wait()
    50  
    51  	assert.Equalf(t, 1, stats["module1_init"], "module1 init")
    52  	assert.Equalf(t, 1, stats["module2_init"], "module2 init")
    53  	assert.Equalf(t, 1, stats["module1_check"], "module1 check")
    54  	assert.Equalf(t, 1, stats["module2_check"], "module2 check")
    55  	assert.Equalf(t, 1, stats["module1_charts"], "module1 charts")
    56  	assert.Equalf(t, 1, stats["module2_charts"], "module2 charts")
    57  	assert.Truef(t, stats["module1_collect"] > 0, "module1 collect")
    58  	assert.Truef(t, stats["module2_collect"] > 0, "module2 collect")
    59  	assert.Equalf(t, 1, stats["module1_cleanup"], "module1 cleanup")
    60  	assert.Equalf(t, 1, stats["module2_cleanup"], "module2 cleanup")
    61  	assert.True(t, buf.String() != "")
    62  }
    63  
    64  func prepareRegistry(mux *sync.Mutex, stats map[string]int, names ...string) module.Registry {
    65  	reg := module.Registry{}
    66  	for _, name := range names {
    67  		name := name
    68  		reg.Register(name, module.Creator{
    69  			Create: func() module.Module { return prepareMockModule(name, mux, stats) },
    70  		})
    71  	}
    72  	return reg
    73  }
    74  
    75  func prepareMockModule(name string, mux *sync.Mutex, stats map[string]int) module.Module {
    76  	return &module.MockModule{
    77  		InitFunc: func() bool {
    78  			mux.Lock()
    79  			defer mux.Unlock()
    80  			stats[name+"_init"]++
    81  			return true
    82  		},
    83  		CheckFunc: func() bool {
    84  			mux.Lock()
    85  			defer mux.Unlock()
    86  			stats[name+"_check"]++
    87  			return true
    88  		},
    89  		ChartsFunc: func() *module.Charts {
    90  			mux.Lock()
    91  			defer mux.Unlock()
    92  			stats[name+"_charts"]++
    93  			return &module.Charts{
    94  				&module.Chart{ID: "id", Title: "title", Units: "units", Dims: module.Dims{{ID: "id1"}}},
    95  			}
    96  		},
    97  		CollectFunc: func() map[string]int64 {
    98  			mux.Lock()
    99  			defer mux.Unlock()
   100  			stats[name+"_collect"]++
   101  			return map[string]int64{"id1": 1}
   102  		},
   103  		CleanupFunc: func() {
   104  			mux.Lock()
   105  			defer mux.Unlock()
   106  			stats[name+"_cleanup"]++
   107  		},
   108  	}
   109  }