github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/utils/fqdn/fqdn_test.go (about)

     1  package fqdn
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"os"
     7  	"sync"
     8  	"testing"
     9  )
    10  
    11  func TestFind(t *testing.T) {
    12  	ip41 := net.ParseIP("192.0.2.1")
    13  	if ip41 == nil {
    14  		panic("failed to parse ip41")
    15  	}
    16  	ip42 := net.ParseIP("192.0.2.2")
    17  	if ip42 == nil {
    18  		panic("failed to parse ip42")
    19  	}
    20  	ip61 := net.ParseIP("2001:db8::68")
    21  	if ip61 == nil {
    22  		panic("failed to parse ip61")
    23  	}
    24  	ip62 := net.ParseIP("2001:db8::69")
    25  	if ip62 == nil {
    26  		panic("failed to parse ip62")
    27  	}
    28  	invalidIP := (net.IP)([]byte("012345678901234567890"))
    29  	tests := []struct {
    30  		name          string
    31  		want          string
    32  		osHostname    func() (string, error)
    33  		netLookupIP   func(string) ([]net.IP, error)
    34  		netLookupAddr func(string) ([]string, error)
    35  		pre           func()
    36  	}{
    37  		{
    38  			name: "os.Hostname errors",
    39  			want: unknownHostname,
    40  			osHostname: func() (string, error) {
    41  				return "", fmt.Errorf("failed to get hostname from the kernel")
    42  			},
    43  		},
    44  		{
    45  			name: "os.Hostname does not error but returns empty",
    46  			want: unknownHostname,
    47  			osHostname: func() (string, error) {
    48  				return "", nil
    49  			},
    50  		},
    51  		{
    52  			name: "net.LookupIP errors",
    53  			want: constMyhostname,
    54  			osHostname: func() (string, error) {
    55  				return constMyhostname, nil
    56  			},
    57  			netLookupIP: func(string) ([]net.IP, error) {
    58  				return nil, fmt.Errorf("massive error")
    59  			},
    60  		},
    61  		{
    62  			name: "net.LookupIP returns empty",
    63  			want: constMyhostname,
    64  			osHostname: func() (string, error) {
    65  				return constMyhostname, nil
    66  			},
    67  			netLookupIP: func(string) ([]net.IP, error) {
    68  				return []net.IP{}, nil
    69  			},
    70  		},
    71  		{
    72  			name: "net.LookupIP returns addresses which cannot be reversed looked up",
    73  			want: constMyhostname,
    74  			osHostname: func() (string, error) {
    75  				return constMyhostname, nil
    76  			},
    77  			netLookupIP: func(string) ([]net.IP, error) {
    78  				return []net.IP{ip41, ip42}, nil
    79  			},
    80  		},
    81  		{
    82  			// NOTE: this will not cover the branch if `ipv4.MarshalText()` errors, because that is an impossible branch
    83  			name: "net.LookupIP returns an invalid IP address",
    84  			want: constMyhostname,
    85  			osHostname: func() (string, error) {
    86  				return constMyhostname, nil
    87  			},
    88  			netLookupIP: func(string) ([]net.IP, error) {
    89  				return []net.IP{invalidIP}, nil
    90  			},
    91  		},
    92  		{
    93  			name: "net.LookupIP returns one address which can be reversed looked up",
    94  			want: "myhostname.local",
    95  			osHostname: func() (string, error) {
    96  				return constMyhostname, nil
    97  			},
    98  			netLookupIP: func(string) ([]net.IP, error) {
    99  				return []net.IP{ip41, ip42}, nil
   100  			},
   101  			netLookupAddr: func(addr string) ([]string, error) {
   102  				if addr == "192.0.2.2" {
   103  					return []string{"myhostname.local."}, nil
   104  				}
   105  				return nil, nil
   106  			},
   107  		},
   108  		{
   109  			name: "net.LookupIP returns IPv6 addresses which cannot be reversed looked up",
   110  			want: constMyhostname,
   111  			osHostname: func() (string, error) {
   112  				return constMyhostname, nil
   113  			},
   114  			netLookupIP: func(string) ([]net.IP, error) {
   115  				return []net.IP{ip61, ip62}, nil
   116  			},
   117  		},
   118  		{
   119  			name: "net.LookupIP returns one IPv6 address which can be reversed looked up",
   120  			want: "myhostname.local",
   121  			osHostname: func() (string, error) {
   122  				return constMyhostname, nil
   123  			},
   124  			netLookupIP: func(string) ([]net.IP, error) {
   125  				return []net.IP{ip61, ip62}, nil
   126  			},
   127  			netLookupAddr: func(addr string) ([]string, error) {
   128  				if addr == "2001:db8::69" {
   129  					return []string{"myhostname.local."}, nil
   130  				}
   131  				return nil, nil
   132  			},
   133  		},
   134  		{
   135  			name: "if an alternative hostname is set, return with this instead",
   136  			want: constHostname,
   137  			pre: func() {
   138  				InitializeAlternativeHostname(constHostname)
   139  			},
   140  		},
   141  	}
   142  	for _, tt := range tests {
   143  		t.Run(tt.name, func(t *testing.T) {
   144  			if tt.pre != nil {
   145  				tt.pre()
   146  			}
   147  			if tt.osHostname != nil {
   148  				osHostname = tt.osHostname
   149  			} else {
   150  				osHostname = os.Hostname
   151  			}
   152  			if tt.netLookupIP != nil {
   153  				netLookupIP = tt.netLookupIP
   154  			} else {
   155  				netLookupIP = net.LookupIP
   156  			}
   157  			if tt.netLookupAddr != nil {
   158  				netLookupAddr = tt.netLookupAddr
   159  			} else {
   160  				netLookupAddr = net.LookupAddr
   161  			}
   162  			if got := Find(); got != tt.want {
   163  				t.Errorf("FindFQDN() = %v, want %v", got, tt.want)
   164  			}
   165  			// reset everything after each run
   166  			alternativeHostnameLock.Lock()
   167  			defer alternativeHostnameLock.Unlock()
   168  			alternativeHostname = ""
   169  			alternativeHostnameOnce = &sync.Once{}
   170  		})
   171  	}
   172  }
   173  
   174  const (
   175  	constHostname   = "alternative.hostname"
   176  	constMyhostname = "myhostname"
   177  )
   178  
   179  func TestInitializeAlternativeHostname(t *testing.T) {
   180  	type args struct {
   181  		hostname string
   182  	}
   183  	tests := []struct {
   184  		name string
   185  		args args
   186  		want string
   187  		pre  func()
   188  	}{
   189  		{
   190  			name: "set and success",
   191  			args: args{hostname: constHostname},
   192  			want: constHostname,
   193  		},
   194  		{
   195  			name: "second initialize call will not override",
   196  			args: args{hostname: "hostname2"},
   197  			want: constHostname,
   198  			pre: func() {
   199  				InitializeAlternativeHostname(constHostname)
   200  			},
   201  		},
   202  		{
   203  			name: "an empty initialize call does not initialize it",
   204  			args: args{hostname: constHostname},
   205  			want: constHostname,
   206  			pre: func() {
   207  				InitializeAlternativeHostname("")
   208  			},
   209  		},
   210  	}
   211  	for _, tt := range tests {
   212  		t.Run(tt.name, func(t *testing.T) {
   213  			if tt.pre != nil {
   214  				tt.pre()
   215  			}
   216  			InitializeAlternativeHostname(tt.args.hostname)
   217  			if got := getAlternativeHostname(); got != tt.want {
   218  				t.Errorf("IntializeAlternativeHostname() = %v, want %v", got, tt.want)
   219  			}
   220  			// reset everything after each run
   221  			alternativeHostnameLock.Lock()
   222  			defer alternativeHostnameLock.Unlock()
   223  			alternativeHostname = ""
   224  			alternativeHostnameOnce = &sync.Once{}
   225  		})
   226  	}
   227  }