|
| 1 | +package e2e_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + . "github.com/onsi/ginkgo/v2" |
| 9 | + . "github.com/onsi/gomega" |
| 10 | + |
| 11 | + "github.com/stacklok/toolhive/test/e2e" |
| 12 | +) |
| 13 | + |
| 14 | +var _ = Describe("Server Restart", func() { |
| 15 | + var ( |
| 16 | + config *e2e.TestConfig |
| 17 | + serverName string |
| 18 | + ) |
| 19 | + |
| 20 | + BeforeEach(func() { |
| 21 | + config = e2e.NewTestConfig() |
| 22 | + serverName = generateTestServerName("restart-test") |
| 23 | + |
| 24 | + // Check if thv binary is available |
| 25 | + err := e2e.CheckTHVBinaryAvailable(config) |
| 26 | + Expect(err).ToNot(HaveOccurred(), "thv binary should be available") |
| 27 | + }) |
| 28 | + |
| 29 | + AfterEach(func() { |
| 30 | + if config.CleanupAfter { |
| 31 | + // Clean up the server if it exists |
| 32 | + err := e2e.StopAndRemoveMCPServer(config, serverName) |
| 33 | + Expect(err).ToNot(HaveOccurred(), "Should be able to stop and remove server") |
| 34 | + } |
| 35 | + }) |
| 36 | + |
| 37 | + Describe("Restarting MCP servers", func() { |
| 38 | + Context("when restarting a running server", func() { |
| 39 | + It("should successfully restart and remain accessible", func() { |
| 40 | + By("Starting an OSV MCP server") |
| 41 | + stdout, stderr := e2e.NewTHVCommand(config, "run", "--name", serverName, "osv").ExpectSuccess() |
| 42 | + |
| 43 | + // The command should indicate success |
| 44 | + Expect(stdout+stderr).To(ContainSubstring("osv"), "Output should mention the osv server") |
| 45 | + |
| 46 | + By("Waiting for the server to be running") |
| 47 | + err := e2e.WaitForMCPServer(config, serverName, 60*time.Second) |
| 48 | + Expect(err).ToNot(HaveOccurred(), "Server should be running within 60 seconds") |
| 49 | + |
| 50 | + // Get the server URL before restart |
| 51 | + originalURL, err := e2e.GetMCPServerURL(config, serverName) |
| 52 | + Expect(err).ToNot(HaveOccurred(), "Should be able to get server URL") |
| 53 | + |
| 54 | + By("Restarting the server") |
| 55 | + stdout, stderr = e2e.NewTHVCommand(config, "restart", serverName).ExpectSuccess() |
| 56 | + Expect(stdout+stderr).To(ContainSubstring("restart"), "Output should mention restart operation") |
| 57 | + |
| 58 | + By("Waiting for the server to be running again") |
| 59 | + err = e2e.WaitForMCPServer(config, serverName, 60*time.Second) |
| 60 | + Expect(err).ToNot(HaveOccurred(), "Server should be running again within 60 seconds") |
| 61 | + |
| 62 | + // Get the server URL after restart |
| 63 | + newURL, err := e2e.GetMCPServerURL(config, serverName) |
| 64 | + Expect(err).ToNot(HaveOccurred(), "Should be able to get server URL after restart") |
| 65 | + |
| 66 | + // The URLs should be the same after restart |
| 67 | + Expect(newURL).To(Equal(originalURL), "Server URL should remain the same after restart") |
| 68 | + |
| 69 | + By("Verifying the server is functional after restart") |
| 70 | + // List server to verify it's operational |
| 71 | + stdout, _ = e2e.NewTHVCommand(config, "list").ExpectSuccess() |
| 72 | + Expect(stdout).To(ContainSubstring(serverName), "Server should be listed") |
| 73 | + Expect(stdout).To(ContainSubstring("running"), "Server should be in running state") |
| 74 | + }) |
| 75 | + }) |
| 76 | + |
| 77 | + Context("when restarting a stopped server", func() { |
| 78 | + It("should start the server if it was stopped", func() { |
| 79 | + By("Starting an OSV MCP server") |
| 80 | + stdout, stderr := e2e.NewTHVCommand(config, "run", "--name", serverName, "osv").ExpectSuccess() |
| 81 | + Expect(stdout+stderr).To(ContainSubstring("osv"), "Output should mention the osv server") |
| 82 | + |
| 83 | + By("Waiting for the server to be running") |
| 84 | + err := e2e.WaitForMCPServer(config, serverName, 60*time.Second) |
| 85 | + Expect(err).ToNot(HaveOccurred(), "Server should be running within 60 seconds") |
| 86 | + |
| 87 | + By("Stopping the server") |
| 88 | + stdout, _ = e2e.NewTHVCommand(config, "stop", serverName).ExpectSuccess() |
| 89 | + Expect(stdout).To(ContainSubstring("stop"), "Output should mention stop operation") |
| 90 | + |
| 91 | + By("Verifying the server is stopped") |
| 92 | + Eventually(func() bool { |
| 93 | + stdout, _ := e2e.NewTHVCommand(config, "list", "--all").ExpectSuccess() |
| 94 | + lines := strings.Split(stdout, "\n") |
| 95 | + for _, line := range lines { |
| 96 | + if strings.Contains(line, serverName) { |
| 97 | + // Check if this specific server line contains "running" |
| 98 | + return !strings.Contains(line, "running") |
| 99 | + } |
| 100 | + } |
| 101 | + return false // Server not found in list |
| 102 | + }, 10*time.Second, 1*time.Second).Should(BeTrue(), "Server should be stopped") |
| 103 | + |
| 104 | + By("Restarting the stopped server") |
| 105 | + stdout, stderr = e2e.NewTHVCommand(config, "restart", serverName).ExpectSuccess() |
| 106 | + Expect(stdout+stderr).To(ContainSubstring("restart"), "Output should mention restart operation") |
| 107 | + |
| 108 | + By("Waiting for the server to be running again") |
| 109 | + err = e2e.WaitForMCPServer(config, serverName, 60*time.Second) |
| 110 | + Expect(err).ToNot(HaveOccurred(), "Server should be running again within 60 seconds") |
| 111 | + |
| 112 | + By("Verifying the server is functional after restart") |
| 113 | + stdout, _ = e2e.NewTHVCommand(config, "list").ExpectSuccess() |
| 114 | + Expect(stdout).To(ContainSubstring(serverName), "Server should be listed") |
| 115 | + Expect(stdout).To(ContainSubstring("running"), "Server should be in running state") |
| 116 | + }) |
| 117 | + }) |
| 118 | + |
| 119 | + // TODO: Uncomment when groups are fully supported |
| 120 | + //Context("when restarting servers with --groups flag", func() { |
| 121 | + // It("should restart servers belonging to the specified group", func() { |
| 122 | + // // Define group name |
| 123 | + // groupName := fmt.Sprintf("restart-group-%d", GinkgoRandomSeed()) |
| 124 | + // |
| 125 | + // // Create two servers |
| 126 | + // serverName1 := generateTestServerName("restart-group-test1") |
| 127 | + // serverName2 := generateTestServerName("restart-group-test2") |
| 128 | + // |
| 129 | + // By("Creating a group first") |
| 130 | + // stdout, stderr := e2e.NewTHVCommand(config, "group", "create", groupName).ExpectSuccess() |
| 131 | + // Expect(stdout+stderr).To(ContainSubstring("group"), "Output should mention group creation") |
| 132 | + // |
| 133 | + // By("Starting the first server") |
| 134 | + // stdout, stderr = e2e.NewTHVCommand(config, "run", "--name", serverName1, "--group", groupName, "osv").ExpectSuccess() |
| 135 | + // Expect(stdout+stderr).To(ContainSubstring("osv"), "Output should mention the osv server") |
| 136 | + // |
| 137 | + // By("Starting the second server") |
| 138 | + // stdout, stderr = e2e.NewTHVCommand(config, "run", "--name", serverName2, "--group", groupName, "osv").ExpectSuccess() |
| 139 | + // Expect(stdout+stderr).To(ContainSubstring("osv"), "Output should mention the osv server") |
| 140 | + // |
| 141 | + // By("Waiting for both servers to be running") |
| 142 | + // err := e2e.WaitForMCPServer(config, serverName1, 60*time.Second) |
| 143 | + // Expect(err).ToNot(HaveOccurred(), "First server should be running within 60 seconds") |
| 144 | + // |
| 145 | + // err = e2e.WaitForMCPServer(config, serverName2, 60*time.Second) |
| 146 | + // Expect(err).ToNot(HaveOccurred(), "Second server should be running within 60 seconds") |
| 147 | + // |
| 148 | + // By("Stopping both servers") |
| 149 | + // stdout, _ = e2e.NewTHVCommand(config, "stop", serverName1).ExpectSuccess() |
| 150 | + // Expect(stdout).To(ContainSubstring("stop"), "Output should mention stop operation for first server") |
| 151 | + // |
| 152 | + // stdout, _ = e2e.NewTHVCommand(config, "stop", serverName2).ExpectSuccess() |
| 153 | + // Expect(stdout).To(ContainSubstring("stop"), "Output should mention stop operation for second server") |
| 154 | + // |
| 155 | + // By("Verifying the servers are stopped") |
| 156 | + // Eventually(func() bool { |
| 157 | + // stdout, _ := e2e.NewTHVCommand(config, "list", "--all").ExpectSuccess() |
| 158 | + // lines := strings.Split(stdout, "\n") |
| 159 | + // server1Found := false |
| 160 | + // server2Found := false |
| 161 | + // server1Running := false |
| 162 | + // server2Running := false |
| 163 | + // |
| 164 | + // for _, line := range lines { |
| 165 | + // if strings.Contains(line, serverName1) { |
| 166 | + // server1Found = true |
| 167 | + // server1Running = strings.Contains(line, "running") |
| 168 | + // } |
| 169 | + // if strings.Contains(line, serverName2) { |
| 170 | + // server2Found = true |
| 171 | + // server2Running = strings.Contains(line, "running") |
| 172 | + // } |
| 173 | + // } |
| 174 | + // |
| 175 | + // // Both servers should be found and neither should be running |
| 176 | + // return server1Found && server2Found && !server1Running && !server2Running |
| 177 | + // }, 10*time.Second, 1*time.Second).Should(BeTrue(), "Both servers should be stopped") |
| 178 | + // |
| 179 | + // By("Restarting all servers in the group") |
| 180 | + // stdout, stderr = e2e.NewTHVCommand(config, "restart", "--group", groupName).ExpectSuccess() |
| 181 | + // Expect(stdout+stderr).To(ContainSubstring("restart"), "Output should mention restart operation") |
| 182 | + // |
| 183 | + // By("Waiting for both servers to be running again") |
| 184 | + // err = e2e.WaitForMCPServer(config, serverName1, 60*time.Second) |
| 185 | + // Expect(err).ToNot(HaveOccurred(), "First server should be running again within 60 seconds") |
| 186 | + // |
| 187 | + // err = e2e.WaitForMCPServer(config, serverName2, 60*time.Second) |
| 188 | + // Expect(err).ToNot(HaveOccurred(), "Second server should be running again within 60 seconds") |
| 189 | + // |
| 190 | + // By("Verifying both servers are functional after restart") |
| 191 | + // stdout, _ = e2e.NewTHVCommand(config, "list").ExpectSuccess() |
| 192 | + // Expect(stdout).To(ContainSubstring(serverName1), "First server should be listed") |
| 193 | + // Expect(stdout).To(ContainSubstring(serverName2), "Second server should be listed") |
| 194 | + // Expect(stdout).To(ContainSubstring("running"), "Servers should be in running state") |
| 195 | + // |
| 196 | + // // Clean up these specific servers at the end of the test |
| 197 | + // defer func() { |
| 198 | + // if config.CleanupAfter { |
| 199 | + // _ = e2e.StopAndRemoveMCPServer(config, serverName1) |
| 200 | + // _ = e2e.StopAndRemoveMCPServer(config, serverName2) |
| 201 | + // } |
| 202 | + // }() |
| 203 | + // }) |
| 204 | + //}) |
| 205 | + }) |
| 206 | +}) |
| 207 | + |
| 208 | +// generateTestServerName creates a unique server name for restart tests |
| 209 | +func generateTestServerName(prefix string) string { |
| 210 | + return fmt.Sprintf("%s-%d", prefix, GinkgoRandomSeed()) |
| 211 | +} |
0 commit comments