|
| 1 | +// Copyright 2025 The argocd-agent Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package agent |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/argoproj-labs/argocd-agent/internal/config" |
| 22 | + "github.com/argoproj-labs/argocd-agent/pkg/client" |
| 23 | + fakekube "github.com/argoproj-labs/argocd-agent/test/fake/kube" |
| 24 | + "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1" |
| 25 | + "github.com/sirupsen/logrus" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | +) |
| 30 | + |
| 31 | +func TestDefaultAppFilterChain_SkipSyncLabel(t *testing.T) { |
| 32 | + kubec := fakekube.NewKubernetesFakeClientWithApps("argocd") |
| 33 | + remote, err := client.NewRemote("127.0.0.1", 8080) |
| 34 | + require.NoError(t, err) |
| 35 | + agent, err := NewAgent(context.TODO(), kubec, "argocd", WithRemote(remote)) |
| 36 | + require.NoError(t, err) |
| 37 | + |
| 38 | + filterChain := agent.DefaultAppFilterChain() |
| 39 | + |
| 40 | + tests := []struct { |
| 41 | + name string |
| 42 | + app *v1alpha1.Application |
| 43 | + expected bool |
| 44 | + }{ |
| 45 | + { |
| 46 | + name: "Application without skip sync label should be admitted", |
| 47 | + app: &v1alpha1.Application{ |
| 48 | + ObjectMeta: metav1.ObjectMeta{ |
| 49 | + Name: "test-app", |
| 50 | + Namespace: "argocd", |
| 51 | + Labels: map[string]string{}, |
| 52 | + }, |
| 53 | + }, |
| 54 | + expected: true, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "Application with skip sync label set to false should be admitted", |
| 58 | + app: &v1alpha1.Application{ |
| 59 | + ObjectMeta: metav1.ObjectMeta{ |
| 60 | + Name: "test-app", |
| 61 | + Namespace: "argocd", |
| 62 | + Labels: map[string]string{ |
| 63 | + config.SkipSyncLabel: "false", |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + expected: true, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "Application with skip sync label set to empty string should be admitted", |
| 71 | + app: &v1alpha1.Application{ |
| 72 | + ObjectMeta: metav1.ObjectMeta{ |
| 73 | + Name: "test-app", |
| 74 | + Namespace: "argocd", |
| 75 | + Labels: map[string]string{ |
| 76 | + config.SkipSyncLabel: "", |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + expected: true, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "Application with skip sync label set to true should be rejected", |
| 84 | + app: &v1alpha1.Application{ |
| 85 | + ObjectMeta: metav1.ObjectMeta{ |
| 86 | + Name: "test-app", |
| 87 | + Namespace: "argocd", |
| 88 | + Labels: map[string]string{ |
| 89 | + config.SkipSyncLabel: "true", |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + expected: false, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "Application with skip sync label set to TRUE (case sensitive) should be admitted", |
| 97 | + app: &v1alpha1.Application{ |
| 98 | + ObjectMeta: metav1.ObjectMeta{ |
| 99 | + Name: "test-app", |
| 100 | + Namespace: "argocd", |
| 101 | + Labels: map[string]string{ |
| 102 | + config.SkipSyncLabel: "TRUE", |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + expected: true, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "Application with skip sync label and other labels should be rejected when skip sync is true", |
| 110 | + app: &v1alpha1.Application{ |
| 111 | + ObjectMeta: metav1.ObjectMeta{ |
| 112 | + Name: "test-app", |
| 113 | + Namespace: "argocd", |
| 114 | + Labels: map[string]string{ |
| 115 | + config.SkipSyncLabel: "true", |
| 116 | + "app.kubernetes.io/name": "test-app", |
| 117 | + "app.kubernetes.io/instance": "prod", |
| 118 | + "app.kubernetes.io/managed-by": "argocd", |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + expected: false, |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "Application in wrong namespace with skip sync label should be rejected (namespace filter takes precedence)", |
| 126 | + app: &v1alpha1.Application{ |
| 127 | + ObjectMeta: metav1.ObjectMeta{ |
| 128 | + Name: "test-app", |
| 129 | + Namespace: "wrong-namespace", |
| 130 | + Labels: map[string]string{ |
| 131 | + config.SkipSyncLabel: "false", |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + expected: false, |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + for _, tt := range tests { |
| 140 | + t.Run(tt.name, func(t *testing.T) { |
| 141 | + result := filterChain.Admit(tt.app) |
| 142 | + assert.Equal(t, tt.expected, result, "Filter result should match expected value") |
| 143 | + }) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func TestDefaultAppFilterChain_NamespaceAndSkipSyncInteraction(t *testing.T) { |
| 148 | + kubec := fakekube.NewKubernetesFakeClientWithApps("argocd") |
| 149 | + remote, err := client.NewRemote("127.0.0.1", 8080) |
| 150 | + require.NoError(t, err) |
| 151 | + |
| 152 | + // Create agent with multiple allowed namespaces |
| 153 | + agent, err := NewAgent(context.TODO(), kubec, "argocd", |
| 154 | + WithRemote(remote), |
| 155 | + WithAllowedNamespaces("argocd", "apps", "staging")) |
| 156 | + require.NoError(t, err) |
| 157 | + |
| 158 | + filterChain := agent.DefaultAppFilterChain() |
| 159 | + |
| 160 | + tests := []struct { |
| 161 | + name string |
| 162 | + app *v1alpha1.Application |
| 163 | + expected bool |
| 164 | + reason string |
| 165 | + }{ |
| 166 | + { |
| 167 | + name: "App in allowed namespace without skip sync label should be admitted", |
| 168 | + app: &v1alpha1.Application{ |
| 169 | + ObjectMeta: metav1.ObjectMeta{ |
| 170 | + Name: "test-app", |
| 171 | + Namespace: "apps", |
| 172 | + }, |
| 173 | + }, |
| 174 | + expected: true, |
| 175 | + reason: "Namespace is allowed, no skip sync label", |
| 176 | + }, |
| 177 | + { |
| 178 | + name: "App in allowed namespace with skip sync=true should be rejected", |
| 179 | + app: &v1alpha1.Application{ |
| 180 | + ObjectMeta: metav1.ObjectMeta{ |
| 181 | + Name: "test-app", |
| 182 | + Namespace: "staging", |
| 183 | + Labels: map[string]string{ |
| 184 | + config.SkipSyncLabel: "true", |
| 185 | + }, |
| 186 | + }, |
| 187 | + }, |
| 188 | + expected: false, |
| 189 | + reason: "Skip sync label takes effect even in allowed namespace", |
| 190 | + }, |
| 191 | + { |
| 192 | + name: "App in disallowed namespace with skip sync=false should be rejected", |
| 193 | + app: &v1alpha1.Application{ |
| 194 | + ObjectMeta: metav1.ObjectMeta{ |
| 195 | + Name: "test-app", |
| 196 | + Namespace: "production", |
| 197 | + Labels: map[string]string{ |
| 198 | + config.SkipSyncLabel: "false", |
| 199 | + }, |
| 200 | + }, |
| 201 | + }, |
| 202 | + expected: false, |
| 203 | + reason: "Namespace filter rejects before skip sync filter", |
| 204 | + }, |
| 205 | + } |
| 206 | + |
| 207 | + for _, tt := range tests { |
| 208 | + t.Run(tt.name, func(t *testing.T) { |
| 209 | + result := filterChain.Admit(tt.app) |
| 210 | + assert.Equal(t, tt.expected, result, tt.reason) |
| 211 | + }) |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +func TestDefaultAppFilterChain_ProcessChange(t *testing.T) { |
| 216 | + kubec := fakekube.NewKubernetesFakeClientWithApps("argocd") |
| 217 | + remote, err := client.NewRemote("127.0.0.1", 8080) |
| 218 | + require.NoError(t, err) |
| 219 | + agent, err := NewAgent(context.TODO(), kubec, "argocd", WithRemote(remote)) |
| 220 | + require.NoError(t, err) |
| 221 | + |
| 222 | + filterChain := agent.DefaultAppFilterChain() |
| 223 | + |
| 224 | + oldApp := &v1alpha1.Application{ |
| 225 | + ObjectMeta: metav1.ObjectMeta{ |
| 226 | + Name: "test-app", |
| 227 | + Namespace: "argocd", |
| 228 | + Labels: map[string]string{ |
| 229 | + config.SkipSyncLabel: "false", |
| 230 | + }, |
| 231 | + }, |
| 232 | + } |
| 233 | + |
| 234 | + newApp := &v1alpha1.Application{ |
| 235 | + ObjectMeta: metav1.ObjectMeta{ |
| 236 | + Name: "test-app", |
| 237 | + Namespace: "argocd", |
| 238 | + Labels: map[string]string{ |
| 239 | + config.SkipSyncLabel: "true", |
| 240 | + }, |
| 241 | + }, |
| 242 | + } |
| 243 | + |
| 244 | + // Test that change processing works (no change filters are currently implemented) |
| 245 | + result := filterChain.ProcessChange(oldApp, newApp) |
| 246 | + assert.True(t, result, "ProcessChange should return true when no change filters are defined") |
| 247 | +} |
| 248 | + |
| 249 | +func init() { |
| 250 | + logrus.SetLevel(logrus.TraceLevel) |
| 251 | +} |
0 commit comments