|
1 | 1 | package agent
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "testing"
|
5 | 6 |
|
| 7 | + "github.com/alicebob/miniredis/v2" |
| 8 | + "github.com/argoproj-labs/argocd-agent/internal/argocd/cluster" |
6 | 9 | "github.com/argoproj-labs/argocd-agent/internal/event"
|
| 10 | + "github.com/argoproj-labs/argocd-agent/pkg/client" |
7 | 11 | "github.com/argoproj-labs/argocd-agent/pkg/types"
|
| 12 | + "github.com/argoproj-labs/argocd-agent/test/fake/kube" |
8 | 13 | "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
|
| 14 | + cacheutil "github.com/argoproj/argo-cd/v3/util/cache" |
9 | 15 | "github.com/stretchr/testify/assert"
|
10 | 16 | "github.com/stretchr/testify/require"
|
11 | 17 | corev1 "k8s.io/api/core/v1"
|
@@ -371,3 +377,45 @@ func Test_addAppProjectDeletionToQueue(t *testing.T) {
|
371 | 377 | require.False(t, a.projectManager.IsManaged("test-project"))
|
372 | 378 | })
|
373 | 379 | }
|
| 380 | + |
| 381 | +func Test_addClusterCacheInfoUpdateToQueue(t *testing.T) { |
| 382 | + miniRedis, err := miniredis.Run() |
| 383 | + require.NoError(t, err) |
| 384 | + require.NotNil(t, miniRedis) |
| 385 | + defer miniRedis.Close() |
| 386 | + |
| 387 | + kubec := kube.NewKubernetesFakeClientWithApps("argocd") |
| 388 | + remote, err := client.NewRemote("127.0.0.1", 8080) |
| 389 | + require.NoError(t, err) |
| 390 | + |
| 391 | + a, err := NewAgent(context.TODO(), kubec, "argocd", WithRemote(remote), WithRedisHost(miniRedis.Addr())) |
| 392 | + require.NoError(t, err) |
| 393 | + |
| 394 | + a.remote.SetClientID("agent") |
| 395 | + a.emitter = event.NewEventSource("principal") |
| 396 | + |
| 397 | + // First populate the cache with dummy data |
| 398 | + clusterMgr, err := cluster.NewManager(a.context, a.namespace, miniRedis.Addr(), cacheutil.RedisCompressionGZip, a.kubeClient.Clientset) |
| 399 | + require.NoError(t, err) |
| 400 | + err = clusterMgr.MapCluster("test-agent", &v1alpha1.Cluster{ |
| 401 | + Name: "test-cluster", |
| 402 | + Server: "https://kubernetes.default.svc", |
| 403 | + }) |
| 404 | + require.NoError(t, err) |
| 405 | + |
| 406 | + // Set dummy cluster cache stats |
| 407 | + clusterMgr.SetClusterCacheStats(&event.ClusterCacheInfo{ |
| 408 | + ApplicationsCount: 5, |
| 409 | + APIsCount: 10, |
| 410 | + ResourcesCount: 100, |
| 411 | + }, "test-agent") |
| 412 | + |
| 413 | + // Should not have an event in queue |
| 414 | + require.Equal(t, 0, a.queues.SendQ(defaultQueueName).Len()) |
| 415 | + |
| 416 | + // Add a cluster cache info update event to the queue |
| 417 | + a.addClusterCacheInfoUpdateToQueue() |
| 418 | + |
| 419 | + // Should have an event in queue |
| 420 | + require.Equal(t, 1, a.queues.SendQ(defaultQueueName).Len()) |
| 421 | +} |
0 commit comments