github.com/netdata/go.d.plugin@v0.58.1/agent/discovery/sd/hostsocket/net_test.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package hostsocket
     4  
     5  import (
     6  	"context"
     7  	"errors"
     8  	"testing"
     9  
    10  	"github.com/netdata/go.d.plugin/agent/discovery/sd/model"
    11  )
    12  
    13  var (
    14  	localListenersOutputSample = []byte(`
    15  UDP6|::1|8125|/opt/netdata/usr/sbin/netdata -P /run/netdata/netdata.pid -D
    16  TCP6|::1|8125|/opt/netdata/usr/sbin/netdata -P /run/netdata/netdata.pid -D
    17  TCP|127.0.0.1|8125|/opt/netdata/usr/sbin/netdata -P /run/netdata/netdata.pid -D
    18  UDP|127.0.0.1|53768|/opt/netdata/usr/libexec/netdata/plugins.d/go.d.plugin 1
    19  `)
    20  )
    21  
    22  func TestNetSocketDiscoverer_Discover(t *testing.T) {
    23  	tests := map[string]discoverySim{
    24  		"valid response": {
    25  			mock:                 &mockLocalListenersExec{},
    26  			wantDoneBeforeCancel: false,
    27  			wantTargetGroups: []model.TargetGroup{&netSocketTargetGroup{
    28  				provider: "hostsocket",
    29  				source:   "net",
    30  				targets: []model.Target{
    31  					withHash(&NetSocketTarget{
    32  						Protocol: "UDP6",
    33  						Address:  "::1",
    34  						Port:     "8125",
    35  						Comm:     "netdata",
    36  						Cmdline:  "/opt/netdata/usr/sbin/netdata -P /run/netdata/netdata.pid -D",
    37  					}),
    38  					withHash(&NetSocketTarget{
    39  						Protocol: "TCP6",
    40  						Address:  "::1",
    41  						Port:     "8125",
    42  						Comm:     "netdata",
    43  						Cmdline:  "/opt/netdata/usr/sbin/netdata -P /run/netdata/netdata.pid -D",
    44  					}),
    45  					withHash(&NetSocketTarget{
    46  						Protocol: "TCP",
    47  						Address:  "127.0.0.1",
    48  						Port:     "8125",
    49  						Comm:     "netdata",
    50  						Cmdline:  "/opt/netdata/usr/sbin/netdata -P /run/netdata/netdata.pid -D",
    51  					}),
    52  					withHash(&NetSocketTarget{
    53  						Protocol: "UDP",
    54  						Address:  "127.0.0.1",
    55  						Port:     "53768",
    56  						Comm:     "go.d.plugin",
    57  						Cmdline:  "/opt/netdata/usr/libexec/netdata/plugins.d/go.d.plugin 1",
    58  					}),
    59  				},
    60  			}},
    61  		},
    62  		"empty response": {
    63  			mock:                 &mockLocalListenersExec{emptyResponse: true},
    64  			wantDoneBeforeCancel: false,
    65  			wantTargetGroups: []model.TargetGroup{&netSocketTargetGroup{
    66  				provider: "hostsocket",
    67  				source:   "net",
    68  			}},
    69  		},
    70  		"error on exec": {
    71  			mock:                 &mockLocalListenersExec{err: true},
    72  			wantDoneBeforeCancel: true,
    73  			wantTargetGroups:     nil,
    74  		},
    75  		"invalid data": {
    76  			mock:                 &mockLocalListenersExec{invalidResponse: true},
    77  			wantDoneBeforeCancel: true,
    78  			wantTargetGroups:     nil,
    79  		},
    80  	}
    81  
    82  	for name, sim := range tests {
    83  		t.Run(name, func(t *testing.T) {
    84  			sim.run(t)
    85  		})
    86  	}
    87  }
    88  
    89  func withHash(l *NetSocketTarget) *NetSocketTarget {
    90  	l.hash, _ = calcHash(l)
    91  	tags, _ := model.ParseTags("hostsocket net")
    92  	l.Tags().Merge(tags)
    93  	return l
    94  }
    95  
    96  type mockLocalListenersExec struct {
    97  	err             bool
    98  	emptyResponse   bool
    99  	invalidResponse bool
   100  }
   101  
   102  func (m *mockLocalListenersExec) discover(context.Context) ([]byte, error) {
   103  	if m.err {
   104  		return nil, errors.New("mock discover() error")
   105  	}
   106  	if m.emptyResponse {
   107  		return nil, nil
   108  	}
   109  	if m.invalidResponse {
   110  		return []byte("this is very incorrect data"), nil
   111  	}
   112  	return localListenersOutputSample, nil
   113  }