Files
cd/test/e2e/managed_by_url_test.go
zeekay 01a52090a9 pkg/client, mocks: regenerate off the apps.hanzo.ai group
clientgen/mockgen hadn't been rerun since the group rename, even though
pkg/apis/application/v1alpha1/doc.go and the shipped CRDs already say
apps.hanzo.ai. pkg/client/** still derived ArgoprojV1alpha1/Argoproj()
from the old argoproj.io group, and .mockery.yaml still named ArgoDB
(renamed to DB in util/db/db.go a few commits back) and
util/notification/cd (deleted in b2d7140a0, we use hanzoai/notify).

Reran clientgen (deepcopy-gen/client-gen/lister-gen/informer-gen) off
the current group, and mockgen after fixing .mockery.yaml. Moved every
hand-written call site that holds a versioned.Interface or
SharedInformerFactory with it: .ArgoprojV1alpha1() -> .AppsV1alpha1(),
ArgoprojV1alpha1Interface/Client -> AppsV1alpha1Interface/Client,
.Argoproj() -> .Apps() across controller, server, util, cmd,
hack/gen-resources, and test/e2e. controller/metrics/metrics_test.go's
mocks.NewArgoDB -> mocks.NewDB. Deleted the orphaned
util/db/mocks/ArgoDB.go; mockery writes DB.go for the renamed
interface but doesn't clean up the old filename on its own.

Left CHANGELOG.md/SECURITY.md's real argoproj/argo-cd history and
gitops-engine/pkg/diff/testdata/openapiv2.bin's Argo Rollouts OpenAPI
fixture untouched - genuine history and a real third-party schema, not
our naming.

go build/vet clean. Same 19 pre-existing package-level test failures
as baseline (missing git-verify-wrapper.sh, no live cluster, etc.),
none new. All 32 manifests/ kustomization paths still build;
manifests/ha/** untouched.
2026-09-05 13:44:46 -07:00

208 lines
7.3 KiB
Go

package e2e
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/hanzoai/cd/pkg/apiclient/application"
"github.com/hanzoai/cd/pkg/apiclient/settings"
. "github.com/hanzoai/cd/pkg/apis/application/v1alpha1"
"github.com/hanzoai/cd/test/e2e/fixture"
. "github.com/hanzoai/cd/test/e2e/fixture/app"
)
const managedByURLTestPath = "guestbook"
// TestManagedByURLWithAnnotation tests that applications with managed-by-url annotation
// include the managed-by-url in their deep links
func TestManagedByURLWithAnnotation(t *testing.T) {
managedByURL := "https://cd-instance-b.example.com"
ctx := Given(t)
ctx.
Project("default").
Path(managedByURLTestPath).
When().
CreateApp().
And(func() {
// Add managed-by-url annotation to the application with retry logic
for i := range 3 {
appObj, err := fixture.AppClientset.AppsV1alpha1().Applications(fixture.E2ENamespace).Get(t.Context(), ctx.GetName(), metav1.GetOptions{})
require.NoError(t, err)
if appObj.Annotations == nil {
appObj.Annotations = make(map[string]string)
}
appObj.Annotations[AnnotationKeyManagedByURL] = managedByURL
_, err = fixture.AppClientset.AppsV1alpha1().Applications(fixture.E2ENamespace).Update(t.Context(), appObj, metav1.UpdateOptions{})
if err == nil {
break
}
if i == 2 {
require.NoError(t, err)
}
time.Sleep(100 * time.Millisecond)
}
}).
And(func() {
// Configure deep links that use the managed-by-url template variable
deepLinksConfig := `- url: "{{.managedByURL}}/applications/{{.app.metadata.name}}"
title: "Managed By Instance"
description: "Open in managing Hanzo CD instance"`
// Update the cd-cm configmap to include our test deep links and URL
configMap, err := fixture.KubeClientset.CoreV1().ConfigMaps(fixture.E2ENamespace).Get(t.Context(), "cd-cm", metav1.GetOptions{})
require.NoError(t, err)
if configMap.Data == nil {
configMap.Data = make(map[string]string)
}
configMap.Data["application.links"] = deepLinksConfig
configMap.Data["url"] = "https://cd-test.example.com"
_, err = fixture.KubeClientset.CoreV1().ConfigMaps(fixture.E2ENamespace).Update(t.Context(), configMap, metav1.UpdateOptions{})
require.NoError(t, err)
// Wait a moment for the config to be picked up
time.Sleep(2 * time.Second)
}).
Then().
And(func(app *Application) {
// Test that the managed-by-url annotation is preserved
assert.Equal(t, managedByURL, app.Annotations[AnnotationKeyManagedByURL])
// Test that the application links include the managed-by-url in the deep links
conn, appClient, err := fixture.CDClientset.NewApplicationClient()
require.NoError(t, err)
defer conn.Close()
links, err := appClient.ListLinks(t.Context(), &application.ListAppLinksRequest{
Name: new(app.Name),
})
require.NoError(t, err)
// Verify that deep links are generated with the managed-by-url
assert.NotNil(t, links)
assert.Len(t, links.Items, 1, "Should have 1 deep link configured")
// Check that the link uses the managed-by-url
expectedLink := managedByURL + "/applications/" + app.Name
found := false
for _, link := range links.Items {
if link.Url != nil && *link.Url == expectedLink {
found = true
assert.Equal(t, "Managed By Instance", *link.Title)
assert.Equal(t, "Open in managing Hanzo CD instance", *link.Description)
break
}
}
assert.True(t, found, "Deep link should use managed-by-url: %s", expectedLink)
}).
And(func(_ *Application) {
// Clean up: remove the test deep links configuration
configMap, err := fixture.KubeClientset.CoreV1().ConfigMaps(fixture.E2ENamespace).Get(t.Context(), "cd-cm", metav1.GetOptions{})
if err == nil && configMap.Data != nil {
delete(configMap.Data, "application.links")
delete(configMap.Data, "url")
_, err := fixture.KubeClientset.CoreV1().ConfigMaps(fixture.E2ENamespace).Update(t.Context(), configMap, metav1.UpdateOptions{})
if err != nil {
t.Logf("Failed to clean up configmap: %v", err)
}
}
})
}
// TestManagedByURLFallbackToCurrentInstance tests that when no managed-by-url is set,
// the current instance URL is used as fallback
func TestManagedByURLFallbackToCurrentInstance(t *testing.T) {
Given(t).
Project("default").
Path(managedByURLTestPath).
When().
CreateApp().
And(func() {
// Configure deep links that use the managed-by-url template variable
deepLinksConfig := `- url: "{{.managedByURL}}/applications/{{.app.metadata.name}}"
title: "Managed By Instance"
description: "Open in managing Hanzo CD instance"`
// Update the cd-cm configmap to include our test deep links and URL
configMap, err := fixture.KubeClientset.CoreV1().ConfigMaps(fixture.E2ENamespace).Get(t.Context(), "cd-cm", metav1.GetOptions{})
require.NoError(t, err)
if configMap.Data == nil {
configMap.Data = make(map[string]string)
}
configMap.Data["application.links"] = deepLinksConfig
configMap.Data["url"] = "https://cd-test.example.com"
_, err = fixture.KubeClientset.CoreV1().ConfigMaps(fixture.E2ENamespace).Update(t.Context(), configMap, metav1.UpdateOptions{})
require.NoError(t, err)
// Wait a moment for the config to be picked up
time.Sleep(2 * time.Second)
}).
Then().
And(func(app *Application) {
// Test that the application links use the current instance URL as fallback
conn, appClient, err := fixture.CDClientset.NewApplicationClient()
require.NoError(t, err)
defer conn.Close()
links, err := appClient.ListLinks(t.Context(), &application.ListAppLinksRequest{
Name: new(app.Name),
})
require.NoError(t, err)
// Verify that deep links are generated with the current instance URL as fallback
assert.NotNil(t, links)
assert.Len(t, links.Items, 1, "Should have 1 deep link configured")
// Get the current Hanzo CD server URL from settings
conn2, settingsClient, err := fixture.CDClientset.NewSettingsClient()
require.NoError(t, err)
defer conn2.Close()
settings, err := settingsClient.Get(t.Context(), &settings.SettingsQuery{})
require.NoError(t, err)
// Check that the link uses the current instance URL as fallback
expectedLink := settings.URL + "/applications/" + app.Name
found := false
for _, link := range links.Items {
if link.Url != nil && *link.Url == expectedLink {
found = true
assert.Equal(t, "Managed By Instance", *link.Title)
assert.Equal(t, "Open in managing Hanzo CD instance", *link.Description)
break
}
}
if !found {
t.Log("Returned links:")
for _, link := range links.Items {
if link.Url != nil {
t.Logf("- %s", *link.Url)
}
}
}
assert.True(t, found, "Deep link should use current instance URL as fallback: %s", expectedLink)
}).
And(func(_ *Application) {
// Clean up: remove the test deep links configuration
configMap, err := fixture.KubeClientset.CoreV1().ConfigMaps(fixture.E2ENamespace).Get(t.Context(), "cd-cm", metav1.GetOptions{})
if err == nil && configMap.Data != nil {
delete(configMap.Data, "application.links")
delete(configMap.Data, "url")
_, err := fixture.KubeClientset.CoreV1().ConfigMaps(fixture.E2ENamespace).Update(t.Context(), configMap, metav1.UpdateOptions{})
if err != nil {
t.Logf("Failed to clean up configmap: %v", err)
}
}
})
}