diff --git a/testing/integration/upgrade_test.go b/testing/integration/upgrade_test.go index ab1da0086cf..010c57c2dd4 100644 --- a/testing/integration/upgrade_test.go +++ b/testing/integration/upgrade_test.go @@ -266,7 +266,103 @@ func TestStandaloneUpgrade(t *testing.T) { buildFragments[1], ) +<<<<<<< HEAD t.Logf("Upgrading to version %q", upgradeInputVersion) +======= + t.Logf("Targeting upgrade to version %+v", upgradeInputVersion) + parsedFromVersion, err := version.ParseVersion(define.Version()) + require.NoErrorf(t, err, "define.Version() %q cannot be parsed as agent version", define.Version()) + testStandaloneUpgrade(ctx, t, agentFixture, parsedFromVersion, upgradeInputVersion, expectedAgentHashAfterUpgrade, false) + +} + +func getUpgradableVersions(ctx context.Context, t *testing.T, upgradeToVersion string) (upgradableVersions []*version.ParsedSemVer) { + t.Helper() + + const currentMajorVersions = 2 + const previousMajorVersions = 1 + + aac := tools.NewArtifactAPIClient() + vList, err := aac.GetVersions(ctx) + require.NoError(t, err, "error retrieving versions from Artifact API") + require.NotEmpty(t, vList) + + parsedUpgradeToVersion, err := version.ParseVersion(upgradeToVersion) + require.NoErrorf(t, err, "upgradeToVersion %q is not a valid version string", upgradeToVersion) + currentMajor := parsedUpgradeToVersion.Major() + var currentMajorSelected, previousMajorSelected int + + sortedParsedVersions := make(version.SortableParsedVersions, 0, len(vList.Versions)) + for _, v := range vList.Versions { + pv, err := version.ParseVersion(v) + require.NoErrorf(t, err, "invalid version retrieved from artifact API: %q", v) + sortedParsedVersions = append(sortedParsedVersions, pv) + } + + require.NotEmpty(t, sortedParsedVersions) + + // normally the output of the versions returned by artifact API is already sorted in ascending order, + // we want to sort in descending orders, so we sort them + sort.Sort(sort.Reverse(sortedParsedVersions)) + + for _, parsedVersion := range sortedParsedVersions { + if currentMajorSelected == currentMajorVersions && previousMajorSelected == previousMajorVersions { + // we got all the versions we need, break the loop + break + } + + if !parsedVersion.Less(*parsedUpgradeToVersion) { + // skip upgrade from newer versions than the one under test + t.Logf("Skipping version %q since it's newer or equal to version after upgrade %q", parsedVersion, parsedUpgradeToVersion) + continue + } + + if parsedVersion.IsSnapshot() { + // skip all snapshots + continue + } + + if parsedVersion.Major() == currentMajor && currentMajorSelected < currentMajorVersions { + upgradableVersions = append(upgradableVersions, parsedVersion) + currentMajorSelected++ + continue + } + + if parsedVersion.Major() < currentMajor && previousMajorSelected < previousMajorVersions { + upgradableVersions = append(upgradableVersions, parsedVersion) + previousMajorSelected++ + continue + } + + } + return +} + +func testStandaloneUpgrade( + ctx context.Context, + t *testing.T, + f *atesting.Fixture, + parsedFromVersion *version.ParsedSemVer, + parsedUpgradeVersion *version.ParsedSemVer, + expectedAgentHashAfterUpgrade string, + allowLocalPackage bool, +) { + + var nonInteractiveFlag bool + if version_8_2_0.Less(*parsedFromVersion) { + nonInteractiveFlag = true + } + installOpts := atesting.InstallOpts{ + NonInteractive: nonInteractiveFlag, + Force: true, + } + + output, err := tools.InstallAgent(installOpts, f) + t.Logf("Agent installation output: %q", string(output)) + require.NoError(t, err) + + c := f.Client() +>>>>>>> 3de585e784 (Skip versions newer than version under test for upgrade tests (#3169)) err = c.Connect(ctx) require.NoError(t, err, "error connecting client to agent")