Skip to content

Commit

Permalink
[BEAM-14029] Add getter, setter for target maven repo (#16995)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmccluskey authored Mar 3, 2022
1 parent e210662 commit 1294c2e
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
32 changes: 31 additions & 1 deletion sdks/go/pkg/beam/core/runtime/xlangx/expansionx/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,43 @@ func newJarGetter() *jarGetter {
return &jarGetter{repository: apacheRepository, groupID: beamGroupID, jarCache: cacheDir}
}

// GetRepositoryURL returns the current target URL for the defaultJarGetter,
// indicating what repository will be connected to when getting a Beam JAR.
func GetDefaultRepositoryURL() string {
return defaultJarGetter.getRepositoryURL()
}

// SetRepositoryURL updates the target URL for the defaultJarGetter, changing
// which Maven repository will be connected to when getting a Beam JAR. Also
// validates that it has been passed a URL and returns an error if not.
//
// When changing the target repository, make sure that the value is the prefix
// up to "org/apache/beam" and that the organization of the repository matches
// that of the default from that point on to ensure that the conversion of the
// Gradle target to the JAR name is correct.
func SetDefaultRepositoryURL(repoURL string) error {
return defaultJarGetter.setRepositoryURL(repoURL)
}

// GetBeamJar checks a temporary directory for the desired Beam JAR, downloads the
// appropriate JAR from Maven if not present, then returns the file path to the
// JAR.
func GetBeamJar(gradleTarget, version string) (string, error) {
return defaultJarGetter.getJar(gradleTarget, version)
}

func (j *jarGetter) getRepositoryURL() string {
return string(j.repository)
}

func (j *jarGetter) setRepositoryURL(repoURL string) error {
if !strings.HasPrefix(repoURL, "http") {
return fmt.Errorf("repo URL %v does not have an http or https prefix", repoURL)
}
j.repository = url(strings.TrimSuffix(repoURL, "/"))
return nil
}

func (j *jarGetter) getJar(gradleTarget, version string) (string, error) {
strippedTarget := dropEndOfGradleTarget(gradleTarget)
fullURL, jarName := j.getURLForBeamJar(strippedTarget, version)
Expand Down Expand Up @@ -84,7 +114,7 @@ func (j *jarGetter) getJar(gradleTarget, version string) (string, error) {
defer resp.Body.Close()

if resp.StatusCode != 200 {
return "", fmt.Errorf("received non 200 response code, got %v", resp.StatusCode)
return "", fmt.Errorf("failed to connect to %v: received non 200 response code, got %v", fullURL, resp.StatusCode)
}

file, err := os.Create(jarPath)
Expand Down
66 changes: 66 additions & 0 deletions sdks/go/pkg/beam/core/runtime/xlangx/expansionx/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,72 @@ import (
"testing"
)

func TestGetAndSetRepositoryURL(t *testing.T) {
tests := []struct {
name string
newRepo string
expRepo string
}{
{
"correct URL",
"http://new.repo.org",
"http://new.repo.org",
},
{
"correct URL https",
"https://new.repo.org",
"https://new.repo.org",
},
{
"correct URL with trailing backslash",
"http://new.repo.org/",
"http://new.repo.org",
},
{
"correct URL https with trailing backslash",
"https://new.repo.org/",
"https://new.repo.org",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
j := newJarGetter()
err := j.setRepositoryURL(test.newRepo)
if err != nil {
t.Errorf("failed to set repository URL, got %v", err)
}
if got, want := j.getRepositoryURL(), test.expRepo; got != want {
t.Errorf("getRepositoryURL() got %v, want %v", got, want)
}
})
}
}

func TestGetAndSetRepositoryURL_bad(t *testing.T) {
tests := []struct {
name string
newRepo string
}{
{
"no http",
"new.maven.repo.com",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
j := newJarGetter()
err := j.setRepositoryURL(test.newRepo)
if err == nil {
t.Errorf("setRepositoryURL(%v) succeeded when it should have failed", test.newRepo)
}
// Check that the failed Set call did not change the URL.
if got, want := j.getRepositoryURL(), string(apacheRepository); got != want {
t.Errorf("getRepositoryURL() got %v, want %v", got, want)
}
})
}
}

func TestGetURLForBeamJar(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 1294c2e

Please sign in to comment.