|
| 1 | +/* |
| 2 | +Copyright 2026 The Flux authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controller |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + . "github.com/onsi/gomega" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 29 | + |
| 30 | + gotkmeta "github.com/fluxcd/pkg/apis/meta" |
| 31 | + gotkconditions "github.com/fluxcd/pkg/runtime/conditions" |
| 32 | + gotktestsrv "github.com/fluxcd/pkg/testserver" |
| 33 | + sourcev1 "github.com/fluxcd/source-controller/api/v1" |
| 34 | + |
| 35 | + swapi "github.com/fluxcd/source-watcher/api/v2/v1beta1" |
| 36 | +) |
| 37 | + |
| 38 | +func TestArtifactGeneratorReconciler_DirectSourceFetch(t *testing.T) { |
| 39 | + g := NewWithT(t) |
| 40 | + ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 41 | + defer cancel() |
| 42 | + |
| 43 | + // Create a namespace |
| 44 | + ns, err := testEnv.CreateNamespace(ctx, "direct-fetch-test") |
| 45 | + g.Expect(err).ToNot(HaveOccurred()) |
| 46 | + |
| 47 | + t.Run("reconciles with DirectSourceFetch enabled (uses APIReader)", func(t *testing.T) { |
| 48 | + g := NewWithT(t) |
| 49 | + |
| 50 | + // Create reconciler with DirectSourceFetch enabled |
| 51 | + reconciler := &ArtifactGeneratorReconciler{ |
| 52 | + ControllerName: controllerName, |
| 53 | + Client: testClient, |
| 54 | + APIReader: testClient, |
| 55 | + Scheme: testEnv.Scheme(), |
| 56 | + EventRecorder: testEnv.GetEventRecorderFor(controllerName), |
| 57 | + Storage: testStorage, |
| 58 | + ArtifactFetchRetries: 1, |
| 59 | + DependencyRequeueInterval: 5 * time.Second, |
| 60 | + NoCrossNamespaceRefs: true, |
| 61 | + DirectSourceFetch: true, // Enable DirectSourceFetch |
| 62 | + } |
| 63 | + |
| 64 | + // Create the ArtifactGenerator object |
| 65 | + objKey := client.ObjectKey{ |
| 66 | + Name: "direct-fetch-enabled", |
| 67 | + Namespace: ns.Name, |
| 68 | + } |
| 69 | + obj := &swapi.ArtifactGenerator{ |
| 70 | + TypeMeta: metav1.TypeMeta{ |
| 71 | + Kind: swapi.ArtifactGeneratorKind, |
| 72 | + APIVersion: swapi.GroupVersion.String(), |
| 73 | + }, |
| 74 | + ObjectMeta: metav1.ObjectMeta{ |
| 75 | + Name: objKey.Name, |
| 76 | + Namespace: objKey.Namespace, |
| 77 | + }, |
| 78 | + Spec: swapi.ArtifactGeneratorSpec{ |
| 79 | + Sources: []swapi.SourceReference{ |
| 80 | + { |
| 81 | + Alias: fmt.Sprintf("%s-git", objKey.Name), |
| 82 | + Kind: sourcev1.GitRepositoryKind, |
| 83 | + Name: objKey.Name, |
| 84 | + }, |
| 85 | + }, |
| 86 | + OutputArtifacts: []swapi.OutputArtifact{ |
| 87 | + { |
| 88 | + Name: fmt.Sprintf("%s-git", objKey.Name), |
| 89 | + Copy: []swapi.CopyOperation{ |
| 90 | + { |
| 91 | + From: fmt.Sprintf("@%s-git/**", objKey.Name), |
| 92 | + To: "@artifact/", |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | + err := testClient.Create(ctx, obj) |
| 100 | + g.Expect(err).ToNot(HaveOccurred()) |
| 101 | + |
| 102 | + // Create the GitRepository source |
| 103 | + gitFiles := []gotktestsrv.File{ |
| 104 | + {Name: "app.yaml", Body: "apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: direct-fetch-test"}, |
| 105 | + } |
| 106 | + err = applyGitRepository(objKey, "main@sha256:directfetch123", gitFiles) |
| 107 | + g.Expect(err).ToNot(HaveOccurred()) |
| 108 | + |
| 109 | + // Initialize the ArtifactGenerator with the finalizer |
| 110 | + r, err := reconciler.Reconcile(ctx, reconcile.Request{ |
| 111 | + NamespacedName: objKey, |
| 112 | + }) |
| 113 | + g.Expect(err).ToNot(HaveOccurred()) |
| 114 | + g.Expect(r.RequeueAfter).To(BeEquivalentTo(time.Millisecond)) |
| 115 | + |
| 116 | + // Reconcile to process the sources and build artifacts |
| 117 | + r, err = reconciler.Reconcile(ctx, reconcile.Request{ |
| 118 | + NamespacedName: objKey, |
| 119 | + }) |
| 120 | + g.Expect(err).ToNot(HaveOccurred()) |
| 121 | + g.Expect(r.RequeueAfter).To(Equal(obj.GetRequeueAfter())) |
| 122 | + |
| 123 | + // Verify the ArtifactGenerator status |
| 124 | + err = testClient.Get(ctx, objKey, obj) |
| 125 | + g.Expect(err).ToNot(HaveOccurred()) |
| 126 | + g.Expect(gotkconditions.IsReady(obj)).To(BeTrue()) |
| 127 | + g.Expect(gotkconditions.GetReason(obj, gotkmeta.ReadyCondition)).To(Equal(gotkmeta.SucceededReason)) |
| 128 | + |
| 129 | + t.Log(objToYaml(obj)) |
| 130 | + }) |
| 131 | +} |
0 commit comments