Skip to content

Commit 8dfee7f

Browse files
committed
Adjusted Kiali tests to endpoints.
1 parent ae96122 commit 8dfee7f

2 files changed

Lines changed: 202 additions & 684 deletions

File tree

pkg/kiali/tests/README.md

Lines changed: 23 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
# Kiali API Backend Tests
1+
# Kiali MCP Contract Tests
22

3-
This directory contains test files.
4-
5-
The file api_backend_test.go contains contract tests for the Kiali API endpoints used by the `kubernetes-mcp-server`.
6-
These tests validate that the API contract/interface remains stable and that all endpoints return expected response structures.
7-
Are excluded by tests run by default.
3+
This directory contains contract tests for the Kiali MCP tool endpoints used by the `kubernetes-mcp-server`.
84

95
## Overview
106

7+
After the v2.25 refactor, the kubernetes-mcp-server delegates all Kiali tool calls to
8+
Kiali's backend via `POST /api/chat/mcp/<tool>`. The contract tests in
9+
`backend/api_backend_test.go` verify that every MCP endpoint constant defined in
10+
`pkg/toolsets/kiali/tools/endpoints.go` is registered and responsive on a live Kiali instance.
11+
1112
Contract tests ensure that:
12-
- All Kiali API endpoints used by the MCP server return expected status codes
13-
- Response structures match expected types
14-
- Required fields are present in responses
13+
- All `/api/chat/mcp/<tool>` endpoints return non-404 status codes (i.e. they are registered)
1514
- The API contract remains stable across Kiali versions
15+
- Tracing endpoints (`list_traces`, `get_trace_details`) correctly return 404 when tracing is disabled
1616

1717
## Prerequisites
1818

19-
- A running Kiali instance
19+
- A running Kiali instance (with bookinfo demo app deployed)
2020
- Go 1.24 or later
21-
- Access to a Kubernetes cluster with Kiali installed (or a test environment)
2221

2322
## Configuration
2423

@@ -27,178 +26,41 @@ Tests are configured via environment variables:
2726
### Required
2827

2928
- `KIALI_URL` - Base URL of the Kiali instance (default: `http://localhost:20001/kiali`)
30-
```bash
31-
export KIALI_URL="http://localhost:20001/kiali"
32-
```
3329

3430
### Optional
3531

3632
- `KIALI_TOKEN` - Bearer token for authentication (if required)
37-
```bash
38-
export KIALI_TOKEN="your-token-here"
39-
```
40-
4133
- `TEST_NAMESPACE` - Namespace to use for tests (default: `bookinfo`)
42-
```bash
43-
export TEST_NAMESPACE="bookinfo"
44-
```
45-
46-
- `TEST_SERVICE` - Service name to use for tests (default: `productpage`)
47-
```bash
48-
export TEST_SERVICE="productpage"
49-
```
50-
51-
- `TEST_WORKLOAD` - Workload name to use for tests (default: `productpage-v1`)
52-
```bash
53-
export TEST_WORKLOAD="productpage-v1"
54-
```
55-
56-
- `TEST_APP` - Application name to use for tests (default: `productpage`)
57-
```bash
58-
export TEST_APP="productpage"
59-
```
60-
61-
- `TEST_POD` - Pod name to use for tests (optional, will be extracted from workload if not set)
62-
```bash
63-
export TEST_POD="productpage-v1-54bb874995-4gvgd"
64-
```
6534

6635
## Running Tests
6736

68-
### Run all contract tests
69-
70-
```bash
71-
go test ./pkg/kiali/tests/backend/api_backend_test.go -v
72-
```
73-
74-
### Run from the test directory
37+
The tests use the `kiali_contract` build tag and are excluded from default test runs.
7538

7639
```bash
77-
cd pkg/kiali/tests/backend/api_backend_test.go
78-
go test -v
40+
cd pkg/kiali/tests/backend
41+
go test -tags kiali_contract -v ./...
7942
```
8043

8144
### Run a specific test
8245

8346
```bash
84-
go test ./pkg/kiali/tests/backend/api_backend_test.go -v -run TestAuthInfo
85-
```
86-
87-
### Run with verbose output
88-
89-
```bash
90-
go test ./pkg/kiali/tests/backend/api_backend_test.go -v -count=1
47+
go test -tags kiali_contract -v -run TestContract/TestGetMeshStatus ./...
9148
```
9249

9350
## Test Structure
9451

9552
Tests are organized using the `testify/suite` pattern:
9653

97-
- **ContractTestSuite** - Main test suite that contains all contract tests
98-
- Each test method validates a specific Kiali API endpoint
99-
- Tests use the endpoint constants from `pkg/kiali/endpoints.go` to ensure consistency
100-
101-
### Test Coverage
102-
103-
The tests cover all Kiali API endpoints used by the MCP server:
104-
105-
- **Authentication**: `/api/auth/info`
106-
- **Namespaces**: `/api/namespaces`
107-
- **Graph**: `/api/mesh/graph`, `/api/namespaces/graph`
108-
- **Health**: `/api/clusters/health`
109-
- **Istio Config**: `/api/istio/config`, `/api/istio/validations`
110-
- **Services**: `/api/clusters/services`, `/api/namespaces/{namespace}/services/{service}`, `/api/namespaces/{namespace}/services/{service}/metrics`
111-
- **Workloads**: `/api/clusters/workloads`, `/api/namespaces/{namespace}/workloads/{workload}`, `/api/namespaces/{namespace}/workloads/{workload}/metrics`
112-
- **Pods**: `/api/namespaces/{namespace}/pods/{pod}`, `/api/namespaces/{namespace}/pods/{pod}/logs`
113-
- **Traces**: `/api/namespaces/{namespace}/apps/{app}/traces`, `/api/namespaces/{namespace}/services/{service}/traces`, `/api/namespaces/{namespace}/workloads/{workload}/traces`, `/api/traces/{traceId}`
114-
- **Istio Objects**: CRUD operations for Istio configuration objects
115-
116-
## Dynamic Test Data
117-
118-
Some tests automatically extract test data from API responses:
119-
120-
- **Pod names**: Extracted from workload details if `TEST_POD` is not set
121-
- **Trace IDs**: Extracted from traces list responses for trace detail tests
122-
- **ServiceEntry names**: Created dynamically for Istio object CRUD tests
123-
124-
## Adding New Tests
125-
126-
When adding a new endpoint to `pkg/kiali/endpoints.go`, you must add a corresponding test:
127-
128-
1. Add the endpoint constant to `pkg/kiali/endpoints.go`
129-
2. Add a test method in `pkg/kiali/tests/backend/api_backend_test.go` that uses the endpoint constant
130-
3. The pre-commit hook will validate that all endpoints have tests (static check)
131-
132-
Example:
133-
134-
```go
135-
func (s *ContractTestSuite) TestNewEndpoint() {
136-
s.Run("returns expected structure", func() {
137-
endpoint := kiali.NewEndpoint
138-
resp, body, err := s.apiCall(http.MethodGet, endpoint, nil)
139-
s.Require().NoError(err)
140-
s.Equal(http.StatusOK, resp.StatusCode)
141-
142-
var data map[string]interface{}
143-
err = json.Unmarshal(body, &data)
144-
s.NoError(err)
145-
s.Contains(data, "expectedField")
146-
})
147-
}
148-
```
149-
150-
## Validation
151-
152-
The endpoints_converage_test validates that all endpoints have corresponding tests.
153-
154-
To manually run it:
54+
- **ContractTestSuite** - Main test suite that POSTs to each `/api/chat/mcp/<tool>` endpoint
55+
- Each test method validates a specific MCP tool endpoint using the constants from `pkg/toolsets/kiali/tools/endpoints.go`
15556

156-
```bash
157-
cd pkg/kiali/tests/backend
158-
go test api_backend_test.go -v
159-
```
160-
161-
## Troubleshooting
162-
163-
### Tests fail with 404 errors
164-
165-
- Verify that `KIALI_URL` is correct and Kiali is running
166-
- Check that the test namespace exists in your cluster
167-
- Ensure test resources (services, workloads, pods) exist
168-
169-
### Tests fail with authentication errors
170-
171-
- Set `KIALI_TOKEN` if your Kiali instance requires authentication
172-
- Verify the token has necessary permissions
173-
174-
### Tests timeout
175-
176-
- Some tests wait for resources to be available (e.g., Istio objects)
177-
- Increase timeout if your cluster is slow to propagate changes
178-
- Check network connectivity to Kiali
179-
180-
### Pod tests are skipped
181-
182-
- Ensure `TEST_WORKLOAD` is set to a workload that has running pods
183-
- Or set `TEST_POD` directly to a pod name
184-
185-
## Integration with CI/CD
186-
187-
These tests can be integrated into CI/CD pipelines:
188-
189-
```yaml
190-
# Example GitHub Actions workflow
191-
- name: Run Kiali Contract Tests
192-
env:
193-
KIALI_URL: ${{ secrets.KIALI_URL }}
194-
KIALI_TOKEN: ${{ secrets.KIALI_TOKEN }}
195-
TEST_NAMESPACE: bookinfo
196-
run: go test ./pkg/kiali/tests -v
197-
```
57+
### Endpoint Coverage
19858

199-
## Related Documentation
59+
The `endpoints_coverage_test.go` in the parent directory validates that every endpoint
60+
constant in `endpoints.go` is referenced by at least one tool implementation.
20061

201-
- [Kiali API Documentation](https://kiali.io/documentation/latest/api/)
202-
- [Main Project README](../../../../README.md)
203-
- [Testing Guidelines](../../../../AGENTS.md#tests)
62+
## Integration with Kiali CI
20463

64+
These tests are executed by Kiali's CI via the `mcp-contract-tests.yml` workflow,
65+
which checks out this repository and runs the contract tests against a live Kiali
66+
instance on a KinD cluster.

0 commit comments

Comments
 (0)