From beeb27810c0f5d6259be625e299f2ab8bfb836de Mon Sep 17 00:00:00 2001 From: gotjosh Date: Thu, 1 Nov 2018 20:17:03 +0000 Subject: [PATCH] Add tests for probe/cri/registry Unhappy path tests try to cover three scenarios: - When the endpoint URL scheme is not explicitly supported e.g. HTTP - When the endpoint URL scheme is TCP which is also not supported - When the fail to parse the given URL (to extract the scheme) The happy path covers two scenarios: - When we specify the supported scheme in the URL which is an unix socket e.g. unix///var/run/dockershim.sock - When we pass a socket address but fail to specify the scheme but our registry attempts to use the fallback protocol e.g. var/run/dockershim.sock --- probe/cri/registry_test.go | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/probe/cri/registry_test.go b/probe/cri/registry_test.go index 916f06e4f..2c91a1684 100644 --- a/probe/cri/registry_test.go +++ b/probe/cri/registry_test.go @@ -7,15 +7,36 @@ import ( "github.com/weaveworks/scope/probe/cri" ) -func TestParseHttpEndpointUrl(t *testing.T) { - _, err := cri.NewCRIClient("http://xyz.com") - - assert.Equal(t, "protocol \"http\" not supported", err.Error()) +var nonUnixSocketsTest = []struct { + endpoint string + errorMessage string +}{ + {"http://xyz.com", "protocol \"http\" not supported"}, + {"tcp://var/unix.sock", "endpoint was not unix socket tcp"}, + {"http://[fe80::%31]/", "parse http://[fe80::%31]/: invalid URL escape \"%31\""}, } -func TestParseTcpEndpointUrl(t *testing.T) { - client, err := cri.NewCRIClient("127.0.0.1") +func TestParseNonUnixEndpointUrl(t *testing.T) { + for _, tt := range nonUnixSocketsTest { + _, err := cri.NewCRIClient(tt.endpoint) + + assert.Equal(t, tt.errorMessage, err.Error()) + } +} + +var unixSocketsTest = []string{ + "127.0.0.1", // tests the fallback endpoint + "unix://127.0.0.1", + "unix///var/run/dockershim.sock", + "var/run/dockershim.sock", +} + +func TestParseUnixEndpointUrl(t *testing.T) { + for _, tt := range unixSocketsTest { + client, err := cri.NewCRIClient(tt) + + assert.Equal(t, nil, err) + assert.NotEqual(t, nil, client) + } - assert.Equal(t, nil, err) - assert.NotEqual(t, nil, client) }