|
| 1 | +// SPDX-FileCopyrightText: Copyright 2026 Stacklok, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package e2e_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | + |
| 13 | + . "github.com/onsi/ginkgo/v2" |
| 14 | + . "github.com/onsi/gomega" |
| 15 | + |
| 16 | + "github.com/stacklok/toolhive/pkg/runner" |
| 17 | + "github.com/stacklok/toolhive/pkg/workloads/upgrade" |
| 18 | + "github.com/stacklok/toolhive/test/e2e" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + // rawOSVImage is the concrete image the bundled "osv" registry entry resolves |
| 23 | + // to. Running it by its raw reference (rather than by a registry name) |
| 24 | + // produces a working MCP server whose RunConfig has no RegistryServerName, |
| 25 | + // which is exactly the input the "not-registry-sourced" path needs. |
| 26 | + rawOSVImage = "ghcr.io/stackloklabs/osv-mcp/server:0.1.3" |
| 27 | + |
| 28 | + // osvImageLow and osvImageHigh are two real, pullable osv-mcp releases. The |
| 29 | + // upgrade-flow fixtures advertise these tags so the checker reports an |
| 30 | + // available upgrade from low to high. Both tags run identically (only a minor |
| 31 | + // version bump), and osv declares no required env vars or secrets, so |
| 32 | + // `upgrade apply --yes` runs non-interactively without prompting. |
| 33 | + osvImageLow = "ghcr.io/stackloklabs/osv-mcp/server:0.1.1" |
| 34 | + osvImageHigh = "ghcr.io/stackloklabs/osv-mcp/server:0.1.3" |
| 35 | + |
| 36 | + // upgradeRegistryServerName is the server name the fixtures expose (a bare |
| 37 | + // "name": "osv-upgrade-test", so the recorded RegistryServerName matches it |
| 38 | + // exactly). `thv run` is invoked with this name so RegistryServerName is set. |
| 39 | + upgradeRegistryServerName = "osv-upgrade-test" |
| 40 | +) |
| 41 | + |
| 42 | +// upgradeCheckResults mirrors the JSON emitted by `thv upgrade check --format |
| 43 | +// json`, which is an array of upgrade.CheckResult. We decode into the real type |
| 44 | +// so the test breaks if the wire shape changes. |
| 45 | +type upgradeCheckResults []upgrade.CheckResult |
| 46 | + |
| 47 | +var _ = Describe("Upgrade Command", Label("core", "upgrade", "e2e"), func() { |
| 48 | + var ( |
| 49 | + config *e2e.TestConfig |
| 50 | + serverName string |
| 51 | + |
| 52 | + // tempHome / tempData isolate this spec's ToolHive config and workload |
| 53 | + // state from the developer's / CI runner's real ToolHive directories. |
| 54 | + // `thv config set-registry` writes to $XDG_CONFIG_HOME/toolhive, and |
| 55 | + // workload state lives under $XDG_DATA_HOME/toolhive; routing every thv |
| 56 | + // invocation (run, check, apply, export, set/unset-registry, and cleanup) |
| 57 | + // through thvCmd keeps both off the real config and consistent across the |
| 58 | + // run -> check -> apply -> export -> cleanup sequence so they all see the |
| 59 | + // same workload. The container runtime (Docker/Podman socket) is not |
| 60 | + // HOME-dependent, so an isolated HOME does not affect it; PATH and the rest |
| 61 | + // of the environment are inherited unchanged. |
| 62 | + tempHome string |
| 63 | + tempData string |
| 64 | + |
| 65 | + // thvCmd builds a THVCommand bound to the isolated config/home/data dirs. |
| 66 | + thvCmd func(args ...string) *e2e.THVCommand |
| 67 | + ) |
| 68 | + |
| 69 | + BeforeEach(func() { |
| 70 | + config = e2e.NewTestConfig() |
| 71 | + serverName = e2e.GenerateUniqueServerName("upgrade-test") |
| 72 | + tempHome = GinkgoT().TempDir() |
| 73 | + tempData = GinkgoT().TempDir() |
| 74 | + |
| 75 | + thvCmd = func(args ...string) *e2e.THVCommand { |
| 76 | + return e2e.NewTHVCommand(config, args...). |
| 77 | + WithEnv( |
| 78 | + "XDG_CONFIG_HOME="+tempHome, |
| 79 | + "HOME="+tempHome, |
| 80 | + "XDG_DATA_HOME="+tempData, |
| 81 | + ) |
| 82 | + } |
| 83 | + |
| 84 | + err := e2e.CheckTHVBinaryAvailable(config) |
| 85 | + Expect(err).ToNot(HaveOccurred(), "thv binary should be available") |
| 86 | + }) |
| 87 | + |
| 88 | + AfterEach(func() { |
| 89 | + if config.CleanupAfter { |
| 90 | + // Stop and remove the workload using the SAME isolated env so cleanup |
| 91 | + // targets the workload created by this spec. Tolerate a missing |
| 92 | + // workload (a spec may have failed before creating it). |
| 93 | + _, _, _ = thvCmd("stop", serverName).Run() |
| 94 | + _, _, _ = thvCmd("rm", serverName).Run() |
| 95 | + } |
| 96 | + // The registry config lives under the discarded temp dirs, so there is |
| 97 | + // nothing to restore; the real ToolHive config is never touched. |
| 98 | + }) |
| 99 | + |
| 100 | + // Negative / cheap path: a workload created from a raw image reference is not |
| 101 | + // registry-sourced, so no upgrade can ever be determined for it. This needs |
| 102 | + // no registry fixture and is the safety-net coverage. |
| 103 | + Describe("Checking a workload that is not registry-sourced", func() { |
| 104 | + Context("when the workload was run from a raw image reference", func() { |
| 105 | + It("should report not-registry-sourced in text output", func() { |
| 106 | + By("Running an MCP server from a raw image reference") |
| 107 | + thvCmd("run", "--name", serverName, rawOSVImage).ExpectSuccess() |
| 108 | + |
| 109 | + By("Waiting for the server to be running") |
| 110 | + waitForIsolatedMCPServer(thvCmd, serverName, 60*time.Second) |
| 111 | + |
| 112 | + By("Checking the workload for an available upgrade") |
| 113 | + stdout, _ := thvCmd("upgrade", "check", serverName).ExpectSuccess() |
| 114 | + |
| 115 | + By("Verifying the report says the workload is not registry-sourced") |
| 116 | + Expect(stdout).To(ContainSubstring(serverName), "Report should name the workload") |
| 117 | + Expect(stdout).To(ContainSubstring(string(upgrade.StatusNotRegistrySourced)), |
| 118 | + "Report should show the not-registry-sourced status") |
| 119 | + }) |
| 120 | + |
| 121 | + It("should emit parseable JSON with the expected fields", func() { |
| 122 | + By("Running an MCP server from a raw image reference") |
| 123 | + thvCmd("run", "--name", serverName, rawOSVImage).ExpectSuccess() |
| 124 | + |
| 125 | + By("Waiting for the server to be running") |
| 126 | + waitForIsolatedMCPServer(thvCmd, serverName, 60*time.Second) |
| 127 | + |
| 128 | + By("Checking the workload for an available upgrade in JSON format") |
| 129 | + stdout, _ := thvCmd("upgrade", "check", serverName, "--format", "json").ExpectSuccess() |
| 130 | + |
| 131 | + By("Verifying the JSON output is valid and contains the expected fields") |
| 132 | + var results upgradeCheckResults |
| 133 | + err := json.Unmarshal([]byte(stdout), &results) |
| 134 | + Expect(err).ToNot(HaveOccurred(), "Output should be valid JSON") |
| 135 | + Expect(results).To(HaveLen(1), "JSON should contain exactly one result") |
| 136 | + |
| 137 | + result := results[0] |
| 138 | + Expect(result.WorkloadName).To(Equal(serverName), "JSON should name the workload") |
| 139 | + Expect(result.Status).To(Equal(upgrade.StatusNotRegistrySourced), |
| 140 | + "JSON should show the not-registry-sourced status") |
| 141 | + Expect(result.CurrentImage).To(Equal(rawOSVImage), |
| 142 | + "JSON should record the raw image the workload is running") |
| 143 | + Expect(result.RegistryServer).To(BeEmpty(), |
| 144 | + "A raw-image workload should not have a registry server name") |
| 145 | + Expect(result.CandidateImage).To(BeEmpty(), |
| 146 | + "There should be no candidate image when no upgrade can be determined") |
| 147 | + }) |
| 148 | + }) |
| 149 | + }) |
| 150 | + |
| 151 | + // Full upgrade flow: a registry-sourced workload whose registry advertises a |
| 152 | + // newer tag is upgraded in place, and we verify it runs on the new image with |
| 153 | + // its prior configuration preserved. |
| 154 | + // |
| 155 | + // Determinism: the two fixtures (testdata/upgrade/registry-{low,high}.json) |
| 156 | + // advertise tags 0.1.1 and 0.1.3 of ghcr.io/stackloklabs/osv-mcp/server, both |
| 157 | + // real, pullable releases that run identically. The fixtures are in the |
| 158 | + // upstream MCP-registry format that `thv config set-registry` requires for a |
| 159 | + // local file (the local provider rejects the legacy ToolHive-native format), |
| 160 | + // and they mirror the bundled osv entry's transport (streamable-http) and |
| 161 | + // internal target port (8080) exactly so the workload comes up the same way |
| 162 | + // `thv run osv` would. osv declares no required env vars, so `upgrade apply |
| 163 | + // --yes` never prompts. This runs by default in CI. |
| 164 | + Describe("Applying an available upgrade to a registry-sourced workload", func() { |
| 165 | + var ( |
| 166 | + tempDir string |
| 167 | + fixtureLow string |
| 168 | + fixtureHigh string |
| 169 | + ) |
| 170 | + |
| 171 | + BeforeEach(func() { |
| 172 | + tempDir = GinkgoT().TempDir() |
| 173 | + |
| 174 | + // set-registry persists the path and resolves it later, so it must be |
| 175 | + // absolute regardless of the working directory at resolution time. |
| 176 | + var err error |
| 177 | + fixtureLow, err = filepath.Abs(filepath.Join("testdata", "upgrade", "registry-low.json")) |
| 178 | + Expect(err).ToNot(HaveOccurred()) |
| 179 | + fixtureHigh, err = filepath.Abs(filepath.Join("testdata", "upgrade", "registry-high.json")) |
| 180 | + Expect(err).ToNot(HaveOccurred()) |
| 181 | + Expect(fixtureLow).To(BeAnExistingFile(), "low-tag registry fixture should exist") |
| 182 | + Expect(fixtureHigh).To(BeAnExistingFile(), "high-tag registry fixture should exist") |
| 183 | + }) |
| 184 | + |
| 185 | + It("should pull the candidate, recreate the workload, and preserve config", func() { |
| 186 | + By("Pointing thv at the older-tag registry fixture") |
| 187 | + thvCmd("config", "set-registry", fixtureLow).ExpectSuccess() |
| 188 | + |
| 189 | + By("Running the server by its registry name with an environment variable") |
| 190 | + thvCmd("run", "--name", serverName, "--env", "FOO=bar", upgradeRegistryServerName).ExpectSuccess() |
| 191 | + |
| 192 | + By("Waiting for the server to be running") |
| 193 | + waitForIsolatedMCPServer(thvCmd, serverName, 120*time.Second) |
| 194 | + |
| 195 | + By("Confirming the workload is registry-sourced and up to date against the older fixture") |
| 196 | + stdout, _ := thvCmd("upgrade", "check", serverName, "--format", "json").ExpectSuccess() |
| 197 | + var before upgradeCheckResults |
| 198 | + Expect(json.Unmarshal([]byte(stdout), &before)).To(Succeed(), "Output should be valid JSON") |
| 199 | + Expect(before).To(HaveLen(1)) |
| 200 | + Expect(before[0].RegistryServer).To(Equal(upgradeRegistryServerName), |
| 201 | + "Running by registry name should record the registry server name") |
| 202 | + Expect(before[0].CurrentImage).To(Equal(osvImageLow), |
| 203 | + "The workload should be running the lower-tag image") |
| 204 | + Expect(before[0].Status).To(Equal(upgrade.StatusUpToDate), |
| 205 | + "No upgrade should be available against the older-tag fixture") |
| 206 | + |
| 207 | + By("Repointing thv at the registry fixture advertising the newer tag") |
| 208 | + thvCmd("config", "set-registry", fixtureHigh).ExpectSuccess() |
| 209 | + |
| 210 | + By("Checking that an upgrade is now available") |
| 211 | + stdout, _ = thvCmd("upgrade", "check", serverName, "--format", "json").ExpectSuccess() |
| 212 | + var avail upgradeCheckResults |
| 213 | + Expect(json.Unmarshal([]byte(stdout), &avail)).To(Succeed(), "Output should be valid JSON") |
| 214 | + Expect(avail).To(HaveLen(1)) |
| 215 | + Expect(avail[0].Status).To(Equal(upgrade.StatusUpgradeAvailable), |
| 216 | + "An upgrade should be available after repointing to the newer tag") |
| 217 | + Expect(avail[0].CandidateImage).To(Equal(osvImageHigh), |
| 218 | + "The candidate image should carry the newer tag") |
| 219 | + |
| 220 | + By("Applying the upgrade non-interactively") |
| 221 | + thvCmd("upgrade", "apply", serverName, "--yes").ExpectSuccess() |
| 222 | + |
| 223 | + By("Waiting for the upgraded workload to be running again") |
| 224 | + waitForIsolatedMCPServer(thvCmd, serverName, 120*time.Second) |
| 225 | + |
| 226 | + By("Verifying the recorded image carries the newer tag and config is preserved") |
| 227 | + exportPath := filepath.Join(tempDir, "upgraded-export.json") |
| 228 | + thvCmd("export", serverName, exportPath).ExpectSuccess() |
| 229 | + |
| 230 | + fileContent, err := os.ReadFile(exportPath) |
| 231 | + Expect(err).ToNot(HaveOccurred()) |
| 232 | + |
| 233 | + var runConfig runner.RunConfig |
| 234 | + Expect(json.Unmarshal(fileContent, &runConfig)).To(Succeed(), "Export should be valid JSON") |
| 235 | + Expect(runConfig.Image).To(Equal(osvImageHigh), |
| 236 | + "The upgraded workload should record the newer image tag") |
| 237 | + Expect(runConfig.EnvVars).To(HaveKeyWithValue("FOO", "bar"), |
| 238 | + "The upgrade should preserve the workload's environment variables") |
| 239 | + }) |
| 240 | + }) |
| 241 | +}) |
| 242 | + |
| 243 | +// waitForIsolatedMCPServer polls `thv list` (through the supplied isolated-env |
| 244 | +// command builder) until the named workload reports running, or fails the spec |
| 245 | +// on timeout. It mirrors e2e.WaitForMCPServer but runs every poll under the same |
| 246 | +// isolated config/home/data env as the rest of the spec, so it observes the |
| 247 | +// workload created in that isolated state rather than the real ToolHive config. |
| 248 | +func waitForIsolatedMCPServer(thvCmd func(args ...string) *e2e.THVCommand, serverName string, timeout time.Duration) { |
| 249 | + GinkgoHelper() |
| 250 | + Eventually(func() bool { |
| 251 | + stdout, _, err := thvCmd("list").Run() |
| 252 | + if err != nil { |
| 253 | + return false |
| 254 | + } |
| 255 | + return strings.Contains(stdout, serverName) && strings.Contains(stdout, "running") |
| 256 | + }, timeout, 1*time.Second).Should(BeTrue(), |
| 257 | + "workload %q should be running within %s", serverName, timeout) |
| 258 | +} |
0 commit comments