@@ -12,12 +12,15 @@ import (
1212 "testing"
1313 "time"
1414
15+ toolchaintests "github.com/codeready-toolchain/toolchain-e2e/testsupport/metrics"
16+
1517 argocdv3 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
1618 "github.com/codeready-toolchain/argocd-mcp-server/internal/argocd"
1719 "github.com/modelcontextprotocol/go-sdk/mcp"
1820 "github.com/stretchr/testify/assert"
1921 "github.com/stretchr/testify/require"
2022 "k8s.io/apimachinery/pkg/runtime"
23+ "k8s.io/client-go/rest"
2124)
2225
2326// ------------------------------------------------------------------------------------------------
@@ -100,6 +103,14 @@ func TestServer(t *testing.T) {
100103 err = runtime .DefaultUnstructuredConverter .FromUnstructured (result .StructuredContent .(map [string ]any ), & actualStructuredContent )
101104 require .NoError (t , err )
102105 assert .Equal (t , expectedContent , actualStructuredContent )
106+ // also, check the metrics when the server runs on HTTP
107+ if td .name == "http" {
108+ // get the metrics
109+ metric , err := toolchaintests .GetMetricValue (& rest.Config {}, "http://" + MCPServerListen , `mcp_calls_total` , []string {"method" , "tools/call" , "name" , "unhealthyApplications" , "success" , "true" })
110+ require .NoError (t , err )
111+ assert .InEpsilon (t , 1 , metric , 0.001 )
112+ }
113+
103114 })
104115
105116 t .Run ("call/unhealthyApplicationResources/ok" , func (t * testing.T ) {
@@ -161,6 +172,12 @@ func TestServer(t *testing.T) {
161172 err = runtime .DefaultUnstructuredConverter .FromUnstructured (result .StructuredContent .(map [string ]any ), & actualStructuredContent )
162173 require .NoError (t , err )
163174 assert .Equal (t , expectedContent , actualStructuredContent )
175+ if td .name == "http" {
176+ // get the metrics
177+ metric , err := toolchaintests .GetMetricValue (& rest.Config {}, "http://" + MCPServerListen , `mcp_calls_total` , []string {"method" , "tools/call" , "name" , "unhealthyApplicationResources" , "success" , "true" })
178+ require .NoError (t , err )
179+ assert .InEpsilon (t , 1 , metric , 0.001 )
180+ }
164181 })
165182
166183 t .Run ("call/unhealthyApplicationResources/argocd-error" , func (t * testing.T ) {
@@ -175,6 +192,12 @@ func TestServer(t *testing.T) {
175192 // then
176193 require .NoError (t , err )
177194 assert .True (t , result .IsError )
195+ if td .name == "http" {
196+ // get the metrics
197+ metric , err := toolchaintests .GetMetricValue (& rest.Config {}, "http://" + MCPServerListen , `mcp_calls_total` , []string {"method" , "tools/call" , "name" , "unhealthyApplicationResources" , "success" , "false" })
198+ require .NoError (t , err )
199+ assert .InEpsilon (t , 1 , metric , 0.001 )
200+ }
178201 })
179202 })
180203 }
@@ -184,11 +207,11 @@ func TestServer(t *testing.T) {
184207 init func (* testing.T ) (* mcp.ClientSession , KillMCPServerFunc )
185208 }{
186209 {
187- name : "stdio" ,
210+ name : "stdio-unreachable " ,
188211 init : newStdioSession (MCPServerListen , MCPServerDebug , "http://localhost:50085" , "another-token" ), // invalid URL and token for the Argo CD server
189212 },
190213 {
191- name : "http" ,
214+ name : "http-unreachable " ,
192215 init : newHTTPSession (MCPServerListen , MCPServerDebug , "http://localhost:50085" , "another-token" ), // invalid URL and token for the Argo CD server
193216 },
194217 }
@@ -223,7 +246,7 @@ func newStdioSession(mcpServerListenPort string, mcpServerDebug bool, argocdURL
223246 cmd := newServerCmd (ctx , "stdio" , mcpServerListenPort , strconv .FormatBool (mcpServerDebug ), argocdURL , argocdToken )
224247 cl := mcp .NewClient (& mcp.Implementation {Name : "e2e-test-client" , Version : "v1.0.0" }, nil )
225248 session , err := cl .Connect (ctx , & mcp.CommandTransport {Command : cmd }, nil )
226- require .NoError (t , err )
249+ require .NoError (t , err , "failed to connect to the MCP server with stdio transport: process exited with code=%v" , cmd . ProcessState . ExitCode () )
227250 return session , func () {
228251 // nothing to do
229252 }
@@ -241,7 +264,7 @@ func newHTTPSession(mcpServerListen string, mcpServerDebug bool, argocdURL strin
241264 exitErr := & exec.ExitError {}
242265 // Ignore expected exit error when the process is killed in teardown.
243266 if ! errors .As (err , & exitErr ) {
244- t .Errorf ("failed to run command: %v" , err )
267+ t .Logf ("failed to run command: %v" , err )
245268 }
246269 }
247270 }()
0 commit comments