From 27f4f0527331ad5fa85bcec843cd13cc97c696a8 Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Thu, 25 Mar 2021 18:03:29 +0300 Subject: [PATCH] Pull request: all: imp some tests Merge in DNS/adguard-home from imp-tests to master Squashed commit of the following: commit 6171420068d66808f069f280a81e0c60e61e5ce3 Author: Ainar Garipov Date: Thu Mar 25 17:43:36 2021 +0300 aghnet: imp test msgs commit ed2880ff03ab0e41a77f07aad2caf13bf5f4bd9f Author: Ainar Garipov Date: Thu Mar 25 17:39:44 2021 +0300 all: imp some tests --- internal/agherr/agherr_test.go | 4 +- internal/aghio/limitedreadcloser_test.go | 58 ++++++++++++------------ internal/aghnet/ipdetector_test.go | 2 +- internal/aghnet/net_linux_test.go | 6 ++- internal/aghnet/net_test.go | 6 +-- internal/aghtest/os_windows.go | 6 +-- 6 files changed, 42 insertions(+), 40 deletions(-) diff --git a/internal/agherr/agherr_test.go b/internal/agherr/agherr_test.go index a951010f..e7f53083 100644 --- a/internal/agherr/agherr_test.go +++ b/internal/agherr/agherr_test.go @@ -104,7 +104,7 @@ func TestAnnotate(t *testing.T) { require.NotNil(t, errPtr) err := *errPtr - require.NotNil(t, err) + require.Error(t, err) assert.Equal(t, wantMsg, err.Error()) }) @@ -117,7 +117,7 @@ func TestAnnotate(t *testing.T) { } err := f() - require.NotNil(t, err) + require.Error(t, err) assert.Equal(t, wantMsg, err.Error()) }) diff --git a/internal/aghio/limitedreadcloser_test.go b/internal/aghio/limitedreadcloser_test.go index 9cccda17..a6b6cd55 100644 --- a/internal/aghio/limitedreadcloser_test.go +++ b/internal/aghio/limitedreadcloser_test.go @@ -13,21 +13,21 @@ import ( func TestLimitReadCloser(t *testing.T) { testCases := []struct { + want error name string n int64 - want error }{{ + want: nil, name: "positive", n: 1, - want: nil, }, { + want: nil, name: "zero", n: 0, - want: nil, }, { + want: fmt.Errorf("aghio: invalid n in LimitReadCloser: -1"), name: "negative", n: -1, - want: fmt.Errorf("aghio: invalid n in LimitReadCloser: -1"), }} for _, tc := range testCases { @@ -40,37 +40,37 @@ func TestLimitReadCloser(t *testing.T) { func TestLimitedReadCloser_Read(t *testing.T) { testCases := []struct { - name string - limit int64 - rStr string - want int err error + name string + rStr string + limit int64 + want int }{{ - name: "perfectly_match", - limit: 3, - rStr: "abc", - want: 3, err: nil, - }, { - name: "eof", - limit: 3, - rStr: "", - want: 0, - err: io.EOF, - }, { - name: "limit_reached", - limit: 0, + name: "perfectly_match", rStr: "abc", + limit: 3, + want: 3, + }, { + err: io.EOF, + name: "eof", + rStr: "", + limit: 3, want: 0, + }, { err: &LimitReachedError{ Limit: 0, }, - }, { - name: "truncated", - limit: 2, + name: "limit_reached", rStr: "abc", - want: 2, + limit: 0, + want: 0, + }, { err: nil, + name: "truncated", + rStr: "abc", + limit: 2, + want: 2, }} for _, tc := range testCases { @@ -79,7 +79,7 @@ func TestLimitedReadCloser_Read(t *testing.T) { buf := make([]byte, tc.limit+1) lreader, err := LimitReadCloser(readCloser, tc.limit) - require.Nil(t, err) + require.NoError(t, err) n, err := lreader.Read(buf) require.Equal(t, tc.err, err) @@ -90,15 +90,15 @@ func TestLimitedReadCloser_Read(t *testing.T) { func TestLimitedReadCloser_LimitReachedError(t *testing.T) { testCases := []struct { + err error name string want string - err error }{{ - name: "simplest", - want: "attempted to read more than 0 bytes", err: &LimitReachedError{ Limit: 0, }, + name: "simplest", + want: "attempted to read more than 0 bytes", }} for _, tc := range testCases { diff --git a/internal/aghnet/ipdetector_test.go b/internal/aghnet/ipdetector_test.go index 833ef53a..07c89c9e 100644 --- a/internal/aghnet/ipdetector_test.go +++ b/internal/aghnet/ipdetector_test.go @@ -13,7 +13,7 @@ func TestIPDetector_detectSpecialNetwork(t *testing.T) { var err error ipd, err = NewIPDetector() - require.Nil(t, err) + require.NoError(t, err) testCases := []struct { name string diff --git a/internal/aghnet/net_linux_test.go b/internal/aghnet/net_linux_test.go index 81ebb8b0..d2776a6c 100644 --- a/internal/aghnet/net_linux_test.go +++ b/internal/aghnet/net_linux_test.go @@ -49,7 +49,8 @@ func TestDHCPCDStaticConfig(t *testing.T) { t.Run(tc.name, func(t *testing.T) { r := bytes.NewReader(tc.data) has, err := dhcpcdStaticConfig(r, "wlan0") - require.Nil(t, err) + require.NoError(t, err) + assert.Equal(t, tc.want, has) }) } @@ -86,7 +87,8 @@ func TestIfacesStaticConfig(t *testing.T) { t.Run(tc.name, func(t *testing.T) { r := bytes.NewReader(tc.data) has, err := ifacesStaticConfig(r, "enp0s3") - require.Nil(t, err) + require.NoError(t, err) + assert.Equal(t, tc.want, has) }) } diff --git a/internal/aghnet/net_test.go b/internal/aghnet/net_test.go index 45960e5a..3cd2fd6a 100644 --- a/internal/aghnet/net_test.go +++ b/internal/aghnet/net_test.go @@ -8,9 +8,9 @@ import ( func TestGetValidNetInterfacesForWeb(t *testing.T) { ifaces, err := GetValidNetInterfacesForWeb() - require.Nilf(t, err, "Cannot get net interfaces: %s", err) - require.NotEmpty(t, ifaces, "No net interfaces found") + require.NoErrorf(t, err, "cannot get net interfaces: %s", err) + require.NotEmpty(t, ifaces, "no net interfaces found") for _, iface := range ifaces { - require.NotEmptyf(t, iface.Addresses, "No addresses found for %s", iface.Name) + require.NotEmptyf(t, iface.Addresses, "no addresses found for %s", iface.Name) } } diff --git a/internal/aghtest/os_windows.go b/internal/aghtest/os_windows.go index d9f1a68a..4a878988 100644 --- a/internal/aghtest/os_windows.go +++ b/internal/aghtest/os_windows.go @@ -35,10 +35,10 @@ func prepareTestDir(t *testing.T) (dir string) { t.Helper() wd, err := os.Getwd() - require.Nil(t, err) + require.NoError(t, err) dir, err = ioutil.TempDir(wd, "agh-test") - require.Nil(t, err) + require.NoError(t, err) require.NotEmpty(t, dir) t.Cleanup(func() { @@ -56,7 +56,7 @@ func prepareTestDir(t *testing.T) (dir string) { time.Sleep(retryDur) } - assert.Nil(t, err, "after %s", time.Since(start)) + assert.NoError(t, err, "after %s", time.Since(start)) }) return dir