bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/snmp/get_test.go (about)

     1  // +build ignore
     2  
     3  package snmp
     4  
     5  import (
     6  	"fmt"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  type GetTest struct {
    12  	oid    string
    13  	expect interface{}
    14  }
    15  
    16  var getTests = []GetTest{
    17  	{stringType, []byte(nil)},
    18  	{oidType, []int(nil)},
    19  	{timeticksType, int64(0)},
    20  	{counter32Type, int64(0)},
    21  	{counter64Type, int64(0)},
    22  	{gauge32Type, int64(0)},
    23  }
    24  
    25  func TestGetNil(t *testing.T) {
    26  	var v interface{}
    27  	for _, test := range getTests {
    28  		if err := Get("localhost", "public", test.oid, &v); err != nil {
    29  			t.Errorf("%s unexpected error: %v", test.oid, err)
    30  			continue
    31  		}
    32  		have := fmt.Sprintf("%T", v)
    33  		want := fmt.Sprintf("%T", test.expect)
    34  		if have != want {
    35  			t.Errorf("%s bad type, want=%s, have=%s", test.oid, want, have)
    36  		}
    37  	}
    38  }
    39  
    40  type HostTest struct {
    41  	host   string
    42  	expect string
    43  }
    44  
    45  var hostTests = []string{
    46  	"localhost",
    47  	"localhost:161",
    48  }
    49  
    50  func TestHostPort(t *testing.T) {
    51  	var want string
    52  	for i, host := range hostTests {
    53  		var v []byte
    54  		if err := Get(host, "public", stringType, &v); err != nil {
    55  			t.Errorf("%s unexpected error: %v", host, err)
    56  			continue
    57  		}
    58  		if have := string(v); i == 0 {
    59  			want = have
    60  		} else if have != want {
    61  			t.Errorf("%s wrong host, want=%s, have=%s", host, want, have)
    62  		}
    63  	}
    64  }
    65  
    66  type CommunityTest struct {
    67  	str string
    68  	ok  bool
    69  	err error
    70  }
    71  
    72  var communityTests = []CommunityTest{
    73  	{str: "", ok: false},
    74  	{str: "public", ok: true},
    75  	{str: "invalid", ok: false},
    76  }
    77  
    78  func TestCommunity(t *testing.T) {
    79  	ch := make(chan CommunityTest)
    80  	for _, test := range communityTests {
    81  		str := test.str
    82  		test := test
    83  		go func() {
    84  			var v interface{}
    85  			test.err = Get("localhost", str, stringType, &v)
    86  			ch <- test
    87  		}()
    88  	}
    89  	for range communityTests {
    90  		test := <-ch
    91  		if (test.err == nil) != test.ok {
    92  			t.Errorf("%s invalid err: %s", test.str, test.err)
    93  		}
    94  	}
    95  }
    96  
    97  type TestRun struct {
    98  	req *Request
    99  }
   100  
   101  func TestParallel(t *testing.T) {
   102  	const N = 1000
   103  	done := make(chan bool)
   104  	for i := 0; i < N; i++ {
   105  		go func() {
   106  			var v interface{}
   107  			if err := Get("localhost", "public", stringType, &v); err != nil {
   108  				t.Errorf("%d unexpected error: %v", i, err)
   109  			}
   110  			done <- true
   111  		}()
   112  	}
   113  	for i := 0; i < N; i++ {
   114  		<-done
   115  	}
   116  }
   117  
   118  type MultiTest struct {
   119  	args   []interface{}
   120  	expect string
   121  }
   122  
   123  var outInt int
   124  
   125  var multiTests = []MultiTest{
   126  	{[]interface{}{}, ""},
   127  	{[]interface{}{"IF-MIB::ifMtu.1", &outInt}, ""},
   128  	{[]interface{}{"IF-MIB::ifMtu.1", &outInt, "IF-MIB::ifSpeed.1", &outInt}, ""},
   129  }
   130  
   131  func TestGetMulti(t *testing.T) {
   132  	for _, test := range multiTests {
   133  		err := Get("localhost", "public", test.args...)
   134  		if (test.expect == "") != (err == nil) {
   135  			t.Errorf("%v: unexpected error: %v", test.args, err)
   136  			continue
   137  		}
   138  		if err == nil {
   139  			continue
   140  		}
   141  		if i := strings.Index(err.Error(), test.expect); i < 0 {
   142  			t.Errorf("%v: unexpected error: %v", test.args, err)
   143  		}
   144  	}
   145  }