-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1762 from SaschaSchwarze0/sascha-test-connection
Test connectivity to the Git or Registry host before accessing the source
- Loading branch information
Showing
9 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright The Shipwright Contributors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package image | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/google/go-containerregistry/pkg/name" | ||
) | ||
|
||
// ExtractHostnamePort tries to extract the hostname and port of the provided image URL | ||
func ExtractHostnamePort(url string) (string, int, error) { | ||
ref, err := name.ParseReference(url) | ||
if err != nil { | ||
return "", 0, err | ||
} | ||
|
||
registry := ref.Context().Registry | ||
host := registry.RegistryStr() | ||
hostname := host | ||
port := 0 | ||
|
||
parts := strings.SplitN(host, ":", 2) | ||
if len(parts) == 2 { | ||
hostname = parts[0] | ||
if port, err = strconv.Atoi(parts[1]); err != nil { | ||
return "", 0, err | ||
} | ||
} else { | ||
scheme := registry.Scheme() | ||
|
||
switch scheme { | ||
case "http": | ||
port = 80 | ||
case "https": | ||
port = 443 | ||
|
||
default: | ||
return "", 0, fmt.Errorf("unknown protocol: %s", scheme) | ||
} | ||
} | ||
|
||
return hostname, port, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright The Shipwright Contributors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package image_test | ||
|
||
import ( | ||
"github.com/shipwright-io/build/pkg/image" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Endpoints", func() { | ||
|
||
DescribeTable("the extraction of hostname and port", | ||
func(url string, expectedHost string, expectedPort int, expectError bool) { | ||
host, port, err := image.ExtractHostnamePort(url) | ||
if expectError { | ||
Expect(err).To(HaveOccurred()) | ||
} else { | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(host).To(Equal(expectedHost), "for "+url) | ||
Expect(port).To(Equal(expectedPort), "for "+url) | ||
} | ||
}, | ||
Entry("Check a URL with default port", "registry.access.redhat.com/ubi9/ubi-minimal", "registry.access.redhat.com", 443, false), | ||
Entry("Check a URL with custom port", "registry.access.redhat.com:9443/ubi9/ubi-minimal", "registry.access.redhat.com", 9443, false), | ||
Entry("Check a URL without host", "ubuntu", "index.docker.io", 443, false), | ||
Entry("Check invalid URL", "ftp://registry.access.redhat.com/ubi9/ubi-minimal", "", 0, true), | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright The Shipwright Contributors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package util | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"time" | ||
) | ||
|
||
// TestConnection tries to establish a connection to a provided host using a 5 seconds timeout. | ||
func TestConnection(hostname string, port int, retries int) bool { | ||
host := fmt.Sprintf("%s:%d", hostname, port) | ||
|
||
dialer := &net.Dialer{ | ||
Timeout: 5 * time.Second, | ||
} | ||
|
||
for i := 0; i <= retries; i++ { | ||
conn, _ := dialer.Dial("tcp", host) | ||
if conn != nil { | ||
_ = conn.Close() | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright The Shipwright Contributors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package util_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/shipwright-io/build/pkg/util" | ||
) | ||
|
||
var _ = Describe("TCP", func() { | ||
|
||
Context("TestConnection", func() { | ||
|
||
var result bool | ||
var hostname string | ||
var port int | ||
|
||
JustBeforeEach(func() { | ||
result = util.TestConnection(hostname, port, 1) | ||
}) | ||
|
||
Context("For a broken endpoint", func() { | ||
|
||
BeforeEach(func() { | ||
hostname = "shipwright.io" | ||
port = 33333 | ||
}) | ||
|
||
It("returns false", func() { | ||
Expect(result).To(BeFalse()) | ||
}) | ||
}) | ||
|
||
Context("For an unknown host", func() { | ||
|
||
BeforeEach(func() { | ||
hostname = "shipwright-dhasldglidgewidgwd.io" | ||
port = 33333 | ||
}) | ||
|
||
It("returns false", func() { | ||
Expect(result).To(BeFalse()) | ||
}) | ||
}) | ||
|
||
Context("For a functional endpoint", func() { | ||
|
||
BeforeEach(func() { | ||
hostname = "github.com" | ||
port = 443 | ||
}) | ||
|
||
It("returns true", func() { | ||
Expect(result).To(BeTrue()) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright The Shipwright Contributors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package util_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestGit(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Util Suite") | ||
} |