|
| 1 | +/* |
| 2 | +Copyright 2025 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 test |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "testing" |
| 24 | + "time" |
| 25 | + |
| 26 | + rclient "github.com/fluxcd/pkg/runtime/client" |
| 27 | + helper "github.com/fluxcd/pkg/runtime/controller" |
| 28 | + "github.com/fluxcd/pkg/runtime/testenv" |
| 29 | + "github.com/fluxcd/pkg/testserver" |
| 30 | + sourcev1 "github.com/fluxcd/source-controller/api/v1" |
| 31 | + "go.uber.org/zap/zapcore" |
| 32 | + "helm.sh/helm/v3/pkg/kube" |
| 33 | + corev1 "k8s.io/api/core/v1" |
| 34 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 35 | + "k8s.io/apimachinery/pkg/runtime" |
| 36 | + utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 37 | + "k8s.io/client-go/rest" |
| 38 | + ctrl "sigs.k8s.io/controller-runtime" |
| 39 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 40 | + controllerLog "sigs.k8s.io/controller-runtime/pkg/log" |
| 41 | + "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 42 | + |
| 43 | + v2 "github.com/fluxcd/helm-controller/api/v2" |
| 44 | + "github.com/fluxcd/helm-controller/internal/controller" |
| 45 | + // +kubebuilder:scaffold:imports |
| 46 | +) |
| 47 | + |
| 48 | +var ( |
| 49 | + controllerName = "helm-controller" |
| 50 | + testEnv *testenv.Environment |
| 51 | + testServer *testserver.HTTPServer |
| 52 | + k8sClient client.Client |
| 53 | + |
| 54 | + testCtx = ctrl.SetupSignalHandler() |
| 55 | +) |
| 56 | + |
| 57 | +func NewTestScheme() *runtime.Scheme { |
| 58 | + s := runtime.NewScheme() |
| 59 | + utilruntime.Must(corev1.AddToScheme(s)) |
| 60 | + utilruntime.Must(apiextensionsv1.AddToScheme(s)) |
| 61 | + utilruntime.Must(sourcev1.AddToScheme(s)) |
| 62 | + utilruntime.Must(v2.AddToScheme(s)) |
| 63 | + return s |
| 64 | +} |
| 65 | + |
| 66 | +func TestMain(m *testing.M) { |
| 67 | + // Set logs on development mode and debug level. |
| 68 | + controllerLog.SetLogger(zap.New(zap.WriteTo(os.Stderr), zap.UseDevMode(true), zap.Level(zapcore.DebugLevel))) |
| 69 | + |
| 70 | + // Set the managedFields owner for resources reconciled from Helm charts. |
| 71 | + kube.ManagedFieldsManager = controllerName |
| 72 | + |
| 73 | + // Initialize the test environment with source and helm controller CRDs. |
| 74 | + testEnv = testenv.New( |
| 75 | + testenv.WithCRDPath( |
| 76 | + filepath.Join("..", "..", "build", "config", "crd", "bases"), |
| 77 | + filepath.Join("..", "..", "config", "crd", "bases"), |
| 78 | + ), |
| 79 | + testenv.WithScheme(NewTestScheme()), |
| 80 | + ) |
| 81 | + |
| 82 | + // Start a local test HTTP server to serve chart and artifact files. |
| 83 | + var err error |
| 84 | + if testServer, err = testserver.NewTempHTTPServer(); err != nil { |
| 85 | + panic(fmt.Sprintf("Failed to create a temporary storage server: %v", err)) |
| 86 | + } |
| 87 | + fmt.Println("Starting the test storage server") |
| 88 | + testServer.Start() |
| 89 | + |
| 90 | + // Start the test environment. |
| 91 | + go func() { |
| 92 | + fmt.Println("Starting the test environment") |
| 93 | + if err := testEnv.Start(testCtx); err != nil { |
| 94 | + panic(fmt.Sprintf("Failed to start the test environment manager: %v", err)) |
| 95 | + } |
| 96 | + }() |
| 97 | + <-testEnv.Manager.Elected() |
| 98 | + |
| 99 | + // Client with caching disabled. |
| 100 | + k8sClient, err = client.New(testEnv.Config, client.Options{Scheme: NewTestScheme()}) |
| 101 | + if err != nil { |
| 102 | + panic(fmt.Sprintf("Failed to create client: %v", err)) |
| 103 | + } |
| 104 | + |
| 105 | + // Start the HelmRelease controller. |
| 106 | + if err := StartController(); err != nil { |
| 107 | + panic(fmt.Sprintf("Failed to start HelmRelease controller: %v", err)) |
| 108 | + } |
| 109 | + |
| 110 | + code := m.Run() |
| 111 | + |
| 112 | + fmt.Println("Stopping the test environment") |
| 113 | + if err := testEnv.Stop(); err != nil { |
| 114 | + panic(fmt.Sprintf("Failed to stop the test environment: %v", err)) |
| 115 | + } |
| 116 | + |
| 117 | + fmt.Println("Stopping the test storage server") |
| 118 | + testServer.Stop() |
| 119 | + if err := os.RemoveAll(testServer.Root()); err != nil { |
| 120 | + panic(fmt.Sprintf("Failed to remove storage server dir: %v", err)) |
| 121 | + } |
| 122 | + |
| 123 | + os.Exit(code) |
| 124 | +} |
| 125 | + |
| 126 | +// GetTestClusterConfig returns a copy of the test cluster config. |
| 127 | +func GetTestClusterConfig() (*rest.Config, error) { |
| 128 | + return rest.CopyConfig(testEnv.GetConfig()), nil |
| 129 | +} |
| 130 | + |
| 131 | +// StartController adds the HelmReleaseReconciler to the test controller manager |
| 132 | +// and starts the reconciliation loops. |
| 133 | +func StartController() error { |
| 134 | + timeout := time.Second * 10 |
| 135 | + reconciler := &controller.HelmReleaseReconciler{ |
| 136 | + Client: testEnv, |
| 137 | + EventRecorder: testEnv.GetEventRecorderFor(controllerName), |
| 138 | + Metrics: helper.Metrics{}, |
| 139 | + GetClusterConfig: GetTestClusterConfig, |
| 140 | + ClientOpts: rclient.Options{ |
| 141 | + QPS: 50.0, |
| 142 | + Burst: 300, |
| 143 | + }, |
| 144 | + KubeConfigOpts: rclient.KubeConfigOptions{ |
| 145 | + InsecureExecProvider: false, |
| 146 | + InsecureTLS: false, |
| 147 | + UserAgent: controllerName, |
| 148 | + Timeout: &timeout, |
| 149 | + }, |
| 150 | + APIReader: testEnv, |
| 151 | + TokenCache: nil, |
| 152 | + FieldManager: controllerName, |
| 153 | + DefaultServiceAccount: "", |
| 154 | + DisableChartDigestTracking: false, |
| 155 | + AdditiveCELDependencyCheck: false, |
| 156 | + } |
| 157 | + |
| 158 | + watchConfigsPredicate, err := helper.GetWatchConfigsPredicate(helper.WatchOptions{}) |
| 159 | + if err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + |
| 163 | + return (reconciler).SetupWithManager(testCtx, testEnv, controller.HelmReleaseReconcilerOptions{ |
| 164 | + WatchConfigsPredicate: watchConfigsPredicate, |
| 165 | + DependencyRequeueInterval: 5 * time.Second, |
| 166 | + HTTPRetry: 1, |
| 167 | + }) |
| 168 | +} |
0 commit comments