github.com/m-lab/tcp-info@v1.9.0/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/m-lab/go/osx"
     9  	"github.com/m-lab/go/rtx"
    10  )
    11  
    12  func TestMain(t *testing.T) {
    13  	// Write files to a temp directory.
    14  	dir, err := ioutil.TempDir("", "TestMain")
    15  	rtx.Must(err, "Could not create tempdir")
    16  	defer os.RemoveAll(dir)
    17  
    18  	// Make sure that starting up main() does not cause any panics. There's not
    19  	// a lot else we can test, but we can at least make sure that it doesn't
    20  	// immediately crash.
    21  	for _, v := range []struct{ name, val string }{
    22  		{"REPS", "1"},
    23  		{"TRACE", "true"},
    24  		{"OUTPUT", dir},
    25  		{"TCPINFO_EVENTSOCKET", dir + "/eventsock.sock"},
    26  		{"PROMETHEUSX_LISTEN_ADDRESS", ":0"},
    27  	} {
    28  		cleanup := osx.MustSetenv(v.name, v.val)
    29  		defer cleanup()
    30  	}
    31  
    32  	// REPS=1 should cause main to run once and then exit.
    33  	main()
    34  }
    35  
    36  func TestMainWithExcludeOptions(t *testing.T) {
    37  	// Write files to a temp directory.
    38  	dir, err := ioutil.TempDir("", "TestMain")
    39  	rtx.Must(err, "Could not create tempdir")
    40  	defer os.RemoveAll(dir)
    41  
    42  	// Make sure that starting up main() does not cause any panics. There's not
    43  	// a lot else we can test, but we can at least make sure that it doesn't
    44  	// immediately crash.
    45  	for _, v := range []struct{ name, val string }{
    46  		{"REPS", "1"},
    47  		{"TRACE", "true"},
    48  		{"OUTPUT", dir},
    49  		{"TCPINFO_EVENTSOCKET", dir + "/eventsock.sock"},
    50  		{"PROMETHEUSX_LISTEN_ADDRESS", ":0"},
    51  		{"EXCLUDE_SRCPORT", "443"},
    52  		{"EXCLUDE_DSTIP", "172.25.0.1"},
    53  	} {
    54  		cleanup := osx.MustSetenv(v.name, v.val)
    55  		defer cleanup()
    56  	}
    57  
    58  	// REPS=1 should cause main to run once and then exit.
    59  	main()
    60  }
    61  
    62  func TestMainWithBadExcludeOptions(t *testing.T) {
    63  	// Write files to a temp directory.
    64  	dir, err := ioutil.TempDir("", "TestMain")
    65  	rtx.Must(err, "Could not create tempdir")
    66  	defer os.RemoveAll(dir)
    67  
    68  	// Make sure that starting up main() does not cause any panics. There's not
    69  	// a lot else we can test, but we can at least make sure that it doesn't
    70  	// immediately crash.
    71  	for _, v := range []struct{ name, val string }{
    72  		{"REPS", "1"},
    73  		{"TRACE", "true"},
    74  		{"OUTPUT", dir},
    75  		{"TCPINFO_EVENTSOCKET", dir + "/eventsock.sock"},
    76  		{"PROMETHEUSX_LISTEN_ADDRESS", ":0"},
    77  		{"EXCLUDE_SRCPORT", "NOT_AN_INT"},
    78  		{"EXCLUDE_DSTIP", ";not-an-ip;"},
    79  	} {
    80  		cleanup := osx.MustSetenv(v.name, v.val)
    81  		defer cleanup()
    82  	}
    83  
    84  	// REPS=1 should cause main to run once and then exit.
    85  	main()
    86  }