Skip to content

Commit 1d8d410

Browse files
committed
Add a test CLI-Svc
This test service can be started with `make start-test-cli-service`. The service provides three endpoints: - localhost:8080/cli/v1/install/ - localhost:8080/cli/v1/binary/ - localhost:8080/cli/v1/plugin/discovery/ The "plugin/discovery" endpoint will randomly pick what to recommend between different versions of the plugin group; this allows to simulate a different recommendation that can happen at any time, as if TP had been upgraded. Signed-off-by: Marc Khouzam <marc.khouzam@broadcom.com>
1 parent cd34ef2 commit 1d8d410

File tree

9 files changed

+151
-3
lines changed

9 files changed

+151
-3
lines changed

Makefile

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ start-test-central-repo: stop-test-central-repo setup-custom-cert-for-test-centr
289289
-e REGISTRY_HTTP_TLS_KEY=/certs/localhost.key \
290290
-v $(ROOT_DIR)/hack/central-repo/registry-content:/var/lib/registry \
291291
$(REGISTRY_IMAGE) > /dev/null && \
292-
echo "Started docker test central repo with images:" && \
293-
$(ROOT_DIR)/hack/central-repo/upload-plugins.sh info
292+
echo "Started docker test central repo with images:" && \
293+
$(ROOT_DIR)/hack/central-repo/upload-plugins.sh info
294294

295295
.PHONY: stop-test-central-repo
296296
stop-test-central-repo: ## Stops and removes the local test central repository
@@ -309,7 +309,7 @@ start-airgapped-local-registry: stop-airgapped-local-registry
309309
-e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
310310
-e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd \
311311
$(REGISTRY_IMAGE) > /dev/null && \
312-
echo "Started docker test airgapped repo with authentication at 'localhost:6002'."
312+
echo "Started docker test airgapped repo with authentication at 'localhost:6002'."
313313

314314
@docker logout localhost:6002 || true
315315

@@ -318,6 +318,20 @@ stop-airgapped-local-registry:
318318
@docker stop temp-airgapped-local-registry temp-airgapped-local-registry-with-auth > /dev/null 2>&1 && \
319319
echo "Stopping docker test airgapped repo if running..." || true
320320

321+
.PHONY: start-test-cli-service
322+
start-test-cli-service: stop-test-cli-service ## Starts a test CLI service locally with docker
323+
@docker run -d --rm --name cli-service -p 8080:80 \
324+
-v $(ROOT_DIR)/hack/service/install.sh:/var/www/html/cli/v1/install/install.txt \
325+
-v $(ROOT_DIR)/hack/service/discovery:/var/www/html/cli/v1/plugin/discovery \
326+
-v $(ROOT_DIR)/hack/service/binaries:/var/www/html/cli/v1/binary \
327+
-v $(ROOT_DIR)/hack/service/cli-service.conf:/etc/nginx/conf.d/cli-service.conf \
328+
nginx:alpine && \
329+
echo "Started docker test cli service at 'localhost:8080'"
330+
331+
.PHONY: stop-test-cli-service
332+
stop-test-cli-service: ## Stops and removes the local test CLI service
333+
@docker stop cli-service > /dev/null 2>&1 && echo "Stopped docker test cli service" || true
334+
321335
.PHONY: fmt
322336
fmt: $(GOIMPORTS) ## Run goimports
323337
$(GOIMPORTS) -w -local github.com/vmware-tanzu ./

hack/service/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Test CLI-Svc
2+
3+
This mock allows to run an nginx docker image which serves the same endpoints as
4+
the ones the real CLI-Svc serves. It allows to test the CLI without using the
5+
real CLI-Svc.
6+
7+
The endpoints being mocked are:
8+
9+
- localhost:8080/cli/v1/install
10+
- localhost:8080/cli/v1/plugin/discovery
11+
- localhost:8080/cli/v1/binary
12+
13+
## Using the test CLI-Svc
14+
15+
From the root directory of the tanzu-cli repo, run `make start-test-cli-service`.
16+
This will start the test CLI-Svc as a configured nginx docker image.
17+
18+
To access the endpoints manually, e.g.,:
19+
20+
```console
21+
# NOTE: the trailing / is essential
22+
curl localhost:8080/cli/v1/plugin/discovery/
23+
```
24+
25+
## Testing plugin discovery
26+
27+
If testing plugin discovery (localhost:8080/cli/v1/plugin/discovery/), the
28+
test CLI-Svc will randomly serve different discovery data which is configured in
29+
`hack/service/cli-service.conf`.
30+
31+
To tell the CLI to use the test CLI-Svc we must execute:
32+
33+
```console
34+
export TANZU_CLI_PLUGIN_DISCOVERY_HOST_FOR_TANZU_CONTEXT=http://localhost:8080
35+
```
36+
37+
To allow testing using different central repositories the endpoint serves some
38+
discoveries using both:
39+
40+
- projects.packages.broadcom.com/tanzu_cli/plugins/plugin-inventory:latest
41+
- localhost:9876/tanzu-cli/plugins/central:small
42+
43+
Therefore, it is required to also start the test central repo by doing:
44+
45+
```console
46+
export TANZU_CLI_PLUGIN_DISCOVERY_IMAGE_SIGNATURE_VERIFICATION_SKIP_LIST=localhost:9876/tanzu-cli/plugins/central:small
47+
make start-test-central-repo
48+
49+
# If the certificates should be handled automatically by the CLI itself
50+
# we should remove their configuration done by the make file so that the
51+
# testing is more appropriate. This would be done by doing:
52+
tanzu config cert delete localhost:9876
53+
```

hack/service/binaries/tanzu-v1.5.2

Whitespace-only changes.

hack/service/binaries/tanzu-v1.5.3

Whitespace-only changes.

hack/service/cli-service.conf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use the current time a semi-random hash.
2+
# This will allow to sometimes get a different set of recommended plugins when querying the endpoint.
3+
# Beyond possibly changing the recommended plugins when changing contexts, it also simulates
4+
# the upgrade of an existing context, which can happen at any time.
5+
split_clients "${msec}" $discovery {
6+
50% discovery-with-certs.txt;
7+
# 33% discovery-1.0.txt;
8+
* discovery-1.1.txt;
9+
}
10+
11+
server {
12+
listen 80;
13+
server_name localhost;
14+
root /var/www/html;
15+
16+
location /cli/v1/install {
17+
index install.txt;
18+
}
19+
20+
location /cli/v1/plugin/discovery {
21+
index $discovery;
22+
}
23+
24+
location /cli/v1/binary {
25+
autoindex on;
26+
}
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"pluginGroups": [
3+
{
4+
"name": "vmware-tanzu/platform-engineer",
5+
"recommendedVersion": "v1.0"
6+
}
7+
],
8+
"pluginSources": [
9+
{
10+
"oci": {
11+
"name": "discovered-from-tpsm",
12+
"image": "projects.packages.broadcom.com/tanzu_cli/plugins/plugin-inventory:latest"
13+
}
14+
}
15+
]
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"pluginGroups": [
3+
{
4+
"name": "vmware-tanzu/platform-engineer",
5+
"recommendedVersion": "v1.1"
6+
}
7+
],
8+
"pluginSources": [
9+
{
10+
"oci": {
11+
"name": "discovered-from-tpsm",
12+
"image": "projects.packages.broadcom.com/tanzu_cli/plugins/plugin-inventory:latest"
13+
}
14+
}
15+
]
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"pluginGroups": [
3+
{
4+
"name": "vmware-tkg/default",
5+
"recommendedVersion": "v9.9.9"
6+
}
7+
],
8+
"pluginSources": [
9+
{
10+
"oci": {
11+
"name": "discovered-from-tpsm",
12+
"image": "localhost:9876/tanzu-cli/plugins/central:small"
13+
}
14+
}
15+
]
16+
}

hack/service/install.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!sh
2+
3+
# Copyright 2025 VMware, Inc. All Rights Reserved.
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
echo "Fake installation of the Tanzu CLI"

0 commit comments

Comments
 (0)