From 0a0ab77e63f29aad69c766df224e15ab2e2c8b3e Mon Sep 17 00:00:00 2001 From: Bob Melander Date: Mon, 28 Oct 2024 21:24:22 +0100 Subject: [PATCH] fix: Fix so that get version handles necessary errors --- internal/anchore/anchoreclient.go | 22 +++++++-- internal/anchore/anchoreclient_test.go | 67 ++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/internal/anchore/anchoreclient.go b/internal/anchore/anchoreclient.go index d19819e..3315310 100644 --- a/internal/anchore/anchoreclient.go +++ b/internal/anchore/anchoreclient.go @@ -11,6 +11,7 @@ import ( "github.com/anchore/k8s-inventory/internal/tracker" "github.com/h2non/gock" "io" + "net" "net/http" "net/url" "os" @@ -225,11 +226,26 @@ func ServerIsOffline(err error) bool { return true } - if errors.Is(err, syscall.ECONNREFUSED) { - return true + offlineErrors := []error{ + syscall.ENETDOWN, + syscall.ENETUNREACH, + syscall.ENETRESET, + syscall.ECONNABORTED, + syscall.ECONNRESET, + syscall.ETIMEDOUT, + syscall.ECONNREFUSED, + syscall.EHOSTDOWN, + syscall.EHOSTUNREACH, + } + + for _, e := range offlineErrors { + if errors.Is(err, e) { + return true + } } - if errors.Is(err, syscall.ECONNRESET) { + var dnsError *net.DNSError + if errors.As(err, &dnsError) { return true } diff --git a/internal/anchore/anchoreclient_test.go b/internal/anchore/anchoreclient_test.go index a4a74b3..2adf7cc 100644 --- a/internal/anchore/anchoreclient_test.go +++ b/internal/anchore/anchoreclient_test.go @@ -83,6 +83,44 @@ var ( Err: &httpError{err: "net/http: timeout awaiting response headers", timeout: true}, } + networkDownError = url.Error{ + Op: "Post", + URL: "http://127.0.0.1:8228/v2/system/integrations/registration", + Err: &net.OpError{ + Op: "dial", + Net: "tcp", + Source: nil, + Addr: &net.TCPAddr{ + IP: net.ParseIP("127.0.0.1"), + Port: 8228, + Zone: "", + }, + Err: &os.SyscallError{ + Syscall: "connect", + Err: syscall.ENETDOWN, + }, + }, + } + + networkUnreachableError = url.Error{ + Op: "Post", + URL: "http://127.0.0.1:8228/v2/system/integrations/registration", + Err: &net.OpError{ + Op: "dial", + Net: "tcp", + Source: nil, + Addr: &net.TCPAddr{ + IP: net.ParseIP("127.0.0.1"), + Port: 8228, + Zone: "", + }, + Err: &os.SyscallError{ + Syscall: "connect", + Err: syscall.ENETUNREACH, + }, + }, + } + connectionRefusedError = url.Error{ Op: "Post", URL: "http://127.0.0.1:8228/v2/system/integrations/registration", @@ -125,6 +163,20 @@ var ( }, } + dnsError = url.Error{ + Op: "Post", + URL: "http://doesnotexist:8228/v2/system/integrations/registration", + Err: &net.OpError{ + Op: "read", + Net: "tcp", + Err: &net.DNSError{ + Err: "no such host", + Name: "http://doesnotexist:8228/v2/system/integrations/registration", + IsNotFound: true, + }, + }, + } + badGatewayError = APIClientError{ HTTPStatusCode: http.StatusBadGateway, Message: "Bad Gateway", @@ -502,6 +554,16 @@ func TestAnchoreIsOffline(t *testing.T) { err error want bool }{ + { + name: "Network unreachable returns true", + err: &networkUnreachableError, + want: true, + }, + { + name: "Network down returns true", + err: &networkDownError, + want: true, + }, { name: "Connection timeout returns true", err: &connectionTimeoutError, @@ -517,6 +579,11 @@ func TestAnchoreIsOffline(t *testing.T) { err: &connectionResetError, want: true, }, + { + name: "DNSError returns true", + err: &dnsError, + want: true, + }, { name: "AnchoreAPIClientError with 502 http_status returns true", err: &badGatewayError,