Skip to content

Commit

Permalink
Merge pull request #98 from hmrc/BDOG-2935
Browse files Browse the repository at this point in the history
BDOG-2935 find scala version when requesting specific service version…
  • Loading branch information
jordanrowe authored Dec 6, 2023
2 parents dda3c4f + 9a08a86 commit 680e913
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 18 deletions.
25 changes: 20 additions & 5 deletions servicemanager/artifactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,21 @@ import (
)

type MavenMetadata struct {
Artifact string `xml:"artifactId"`
Group string `xml:"groupId"`
Latest string `xml:"versioning>latest"`
Release string `xml:"versioning>release"`
Artifact string `xml:"artifactId"`
Group string `xml:"groupId"`
Latest string `xml:"versioning>latest"`
Release string `xml:"versioning>release"`
Versions []string `xml:"versioning>versions>version"`
}

// checks if a given version is in the MavenMetadata versions.
func (m MavenMetadata) ContainsVersion(version string) bool {
for _, v := range m.Versions {
if v == version {
return true
}
}
return false
}

var scalaSuffix *regexp.Regexp = regexp.MustCompile(`_(2\.\d{2}|3|%%)$`)
Expand Down Expand Up @@ -73,7 +84,7 @@ const (
ScalaVersion_Any = "_%%"
)

func (sm *ServiceManager) GetLatestVersions(s ServiceBinary, suppliedScalaVersion string) (MavenMetadata, error) {
func (sm *ServiceManager) GetLatestVersions(s ServiceBinary, suppliedScalaVersion string, suppliedServiceVersion string) (MavenMetadata, error) {
scalaVersions := []string{ScalaVersion_3, ScalaVersion_2_13, ScalaVersion_2_12, ScalaVersion_2_11}

// honours supplied Scala version
Expand All @@ -98,6 +109,10 @@ func (sm *ServiceManager) GetLatestVersions(s ServiceBinary, suppliedScalaVersio
continue
}

if suppliedServiceVersion != "" && metadata.ContainsVersion(suppliedServiceVersion) {
return metadata, nil
}

comparableVersion, err := convertVersionToComparableInt(metadata.Latest)

if err != nil {
Expand Down
54 changes: 45 additions & 9 deletions servicemanager/artifactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestGetLatestVersionForAllScalaVersions(t *testing.T) {
Artifact: "foo_%%",
}

meta, err := sm.GetLatestVersions(sb, "")
meta, err := sm.GetLatestVersions(sb, "", "")

AssertNotErr(t, err)

Expand Down Expand Up @@ -145,7 +145,7 @@ func TestGetLatestVersionGetsArtifactScalaVersion212(t *testing.T) {
GroupId: "foo/bar/",
Artifact: "foo_2.12",
}
meta, err := sm.GetLatestVersions(sb, "")
meta, err := sm.GetLatestVersions(sb, "", "")

AssertNotErr(t, err)

Expand Down Expand Up @@ -181,7 +181,7 @@ func TestGetLatestVersionGetsArtifactScalaVersion213(t *testing.T) {
GroupId: "foo/bar/",
Artifact: "foo_2.13",
}
meta, err := sm.GetLatestVersions(sb, "")
meta, err := sm.GetLatestVersions(sb, "", "")

AssertNotErr(t, err)

Expand Down Expand Up @@ -215,7 +215,7 @@ func TestGetLatestVersionGetsScala212IfMissing(t *testing.T) {
GroupId: "foo/bar/",
Artifact: "foo_2.12",
}
meta, err := sm.GetLatestVersions(sb, "")
meta, err := sm.GetLatestVersions(sb, "", "")

AssertNotErr(t, err)

Expand Down Expand Up @@ -251,7 +251,7 @@ func TestGetLatestVersionHonoursSuppliedScalaVersion(t *testing.T) {
GroupId: "foo/bar/",
Artifact: "foo_2.12",
}
meta, err := sm.GetLatestVersions(sb, "2.13")
meta, err := sm.GetLatestVersions(sb, "2.13", "")

AssertNotErr(t, err)

Expand Down Expand Up @@ -287,7 +287,7 @@ func TestGetLatestVersionHonoursSuppliedScalaVersionForAlwaysUseLatestSyntax(t *
GroupId: "foo/bar/",
Artifact: "foo_%%",
}
meta, err := sm.GetLatestVersions(sb, "2.12")
meta, err := sm.GetLatestVersions(sb, "2.12", "")

AssertNotErr(t, err)

Expand All @@ -300,6 +300,38 @@ func TestGetLatestVersionHonoursSuppliedScalaVersionForAlwaysUseLatestSyntax(t *
}
}

func TestGetLatestVersionExplicitServiceVersionForAlwaysUseLatestSyntax(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/foo/bar/foo_2.13/maven-metadata.xml" {
fmt.Fprint(w, mavenMetadata213)
} else if r.URL.Path == "/foo/bar/foo_2.12/maven-metadata.xml" {
fmt.Fprint(w, mavenMetadata)
} else {
w.WriteHeader(404)
}
}))
defer svr.Close()

sm := ServiceManager{
Client: &http.Client{},
Config: ServiceManagerConfig{
ArtifactoryRepoUrl: svr.URL,
},
}

sb := ServiceBinary{
GroupId: "foo/bar/",
Artifact: "foo_%%",
}
meta, err := sm.GetLatestVersions(sb, "", "2.0.22")

AssertNotErr(t, err)

if meta.Artifact != "foo_2.12" {
t.Errorf("artifact was not foo_2.12 it was %s", meta.Artifact)
}
}

func TestGetLatestVersionGetsJavaService(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/foo/bar/foo/maven-metadata.xml" {
Expand All @@ -321,7 +353,7 @@ func TestGetLatestVersionGetsJavaService(t *testing.T) {
GroupId: "foo/bar/",
Artifact: "foo",
}
meta, err := sm.GetLatestVersions(sb, "")
meta, err := sm.GetLatestVersions(sb, "", "")

AssertNotErr(t, err)

Expand Down Expand Up @@ -355,7 +387,7 @@ func TestGetLatestVersionSetsUserAgent(t *testing.T) {
Artifact: "foo_2.12",
}

_, err := sm.GetLatestVersions(sb, "")
_, err := sm.GetLatestVersions(sb, "", "")
AssertNotErr(t, err)

if !strings.HasPrefix(userAgent, "sm2/") {
Expand All @@ -380,7 +412,7 @@ func TestGetLatestVersion(t *testing.T) {
GroupId: "foo/bar/",
Artifact: "foo_2.12",
}
meta, err := sm.GetLatestVersions(sb, "")
meta, err := sm.GetLatestVersions(sb, "", "")

AssertNotErr(t, err)

Expand Down Expand Up @@ -409,6 +441,10 @@ func TestParseValidMetadata(t *testing.T) {
if metadata.Group != "foo.bar" {
t.Errorf("metadata group was not foo.bar it was %s", metadata.Group)
}

if !metadata.ContainsVersion("2.0.22") {
t.Errorf("metadata versions did not contain 2.0.22, versions: %v", metadata.Versions)
}
}

func TestDownloadAndDecompress(t *testing.T) {
Expand Down
15 changes: 12 additions & 3 deletions servicemanager/startservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,18 +227,27 @@ func findHealthcheckUrl(service Service, port int) string {
return defaultHealthcheckUrl(port)
}

func whatVersionToRun(service Service, serviceAndVersion ServiceAndVersion, offline bool, getLatest func(ServiceBinary, string) (MavenMetadata, error)) (string, string, string, error) {
func whatVersionToRun(service Service, serviceAndVersion ServiceAndVersion, offline bool, getLatest func(ServiceBinary, string, string) (MavenMetadata, error)) (string, string, string, error) {
versionToInstall := serviceAndVersion.version
group := service.Binary.GroupId
artifact := service.Binary.Artifact

// override scala version if required
// override scala version if provided e.g. sm2 --start MY_SERVICE_2.13
if serviceAndVersion.scalaVersion != "" {
artifact = scalaSuffix.ReplaceAllLiteralString(artifact, "_"+serviceAndVersion.scalaVersion)
}

// find the scala version if reliant on _%% but requesting specific service version e.g. sm2 --start MY_SERVICE -r 1.0.0 OR sm2 --start MY_SERVICE:1.0.0
if serviceAndVersion.version != "" && strings.HasSuffix(artifact, "_%%") && !offline {
metadata, err := getLatest(service.Binary, serviceAndVersion.scalaVersion, versionToInstall)
if err != nil {
return "", "", "", err
}
artifact = metadata.Artifact
}

if versionToInstall == "" && !offline {
metadata, err := getLatest(service.Binary, serviceAndVersion.scalaVersion)
metadata, err := getLatest(service.Binary, serviceAndVersion.scalaVersion, versionToInstall)
if err != nil {
return "", "", "", err
}
Expand Down
2 changes: 1 addition & 1 deletion servicemanager/startservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestWhatVersionToRun(t *testing.T) {
Latest: "2.0.0",
Release: "2.0.0",
}
latestFunc := func(b ServiceBinary, s string) (MavenMetadata, error) {
latestFunc := func(b ServiceBinary, s string, v string) (MavenMetadata, error) {
return latest, nil
}
caseServiceOnly := ServiceAndVersion{"FOO", "", ""}
Expand Down

0 comments on commit 680e913

Please sign in to comment.