From 9a08a86f28fb454af456d94c211a29deab2e7f48 Mon Sep 17 00:00:00 2001 From: jordanrowe <37838443+jordanrowe@users.noreply.github.com> Date: Wed, 6 Dec 2023 16:25:27 +0000 Subject: [PATCH] BDOG-2935 find scala version when requesting specific service version and using _%% in config --- servicemanager/artifactory.go | 25 ++++++++++--- servicemanager/artifactory_test.go | 54 ++++++++++++++++++++++++----- servicemanager/startservice.go | 15 ++++++-- servicemanager/startservice_test.go | 2 +- 4 files changed, 78 insertions(+), 18 deletions(-) diff --git a/servicemanager/artifactory.go b/servicemanager/artifactory.go index 6864f5c..1df6a21 100644 --- a/servicemanager/artifactory.go +++ b/servicemanager/artifactory.go @@ -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|%%)$`) @@ -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 @@ -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 { diff --git a/servicemanager/artifactory_test.go b/servicemanager/artifactory_test.go index bbcc8a3..21f4ebd 100644 --- a/servicemanager/artifactory_test.go +++ b/servicemanager/artifactory_test.go @@ -109,7 +109,7 @@ func TestGetLatestVersionForAllScalaVersions(t *testing.T) { Artifact: "foo_%%", } - meta, err := sm.GetLatestVersions(sb, "") + meta, err := sm.GetLatestVersions(sb, "", "") AssertNotErr(t, err) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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" { @@ -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) @@ -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/") { @@ -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) @@ -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) { diff --git a/servicemanager/startservice.go b/servicemanager/startservice.go index c3a1b5c..d8b60f5 100644 --- a/servicemanager/startservice.go +++ b/servicemanager/startservice.go @@ -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 } diff --git a/servicemanager/startservice_test.go b/servicemanager/startservice_test.go index c661257..6af81ea 100644 --- a/servicemanager/startservice_test.go +++ b/servicemanager/startservice_test.go @@ -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", "", ""}