2020-11-23 14:21:01 +00:00
|
|
|
/*
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-12-29 20:50:43 +00:00
|
|
|
package secretstore
|
2020-11-23 14:21:01 +00:00
|
|
|
|
|
|
|
import (
|
2022-02-07 10:42:18 +00:00
|
|
|
"context"
|
2020-11-23 14:21:01 +00:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"k8s.io/client-go/kubernetes/scheme"
|
|
|
|
"k8s.io/client-go/rest"
|
2022-02-07 10:42:18 +00:00
|
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
2020-11-23 14:21:01 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
2023-06-12 10:58:29 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/controller"
|
2020-11-23 14:21:01 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/envtest"
|
|
|
|
logf "sigs.k8s.io/controller-runtime/pkg/log"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/log/zap"
|
2023-08-28 09:50:46 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
|
2020-11-23 14:21:01 +00:00
|
|
|
|
2022-02-08 17:07:34 +00:00
|
|
|
esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
|
2023-06-05 19:26:25 +00:00
|
|
|
ctrlmetrics "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
|
|
|
|
"github.com/external-secrets/external-secrets/pkg/controllers/secretstore/cssmetrics"
|
|
|
|
"github.com/external-secrets/external-secrets/pkg/controllers/secretstore/ssmetrics"
|
2024-04-25 09:36:11 +00:00
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
|
|
. "github.com/onsi/gomega"
|
2020-11-23 14:21:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var cfg *rest.Config
|
|
|
|
var k8sClient client.Client
|
|
|
|
var testEnv *envtest.Environment
|
2022-02-07 10:42:18 +00:00
|
|
|
var cancel context.CancelFunc
|
2020-11-23 14:21:01 +00:00
|
|
|
|
|
|
|
func TestAPIs(t *testing.T) {
|
|
|
|
RegisterFailHandler(Fail)
|
2021-05-22 19:53:36 +00:00
|
|
|
RunSpecs(t, "Controller Suite")
|
2020-11-23 14:21:01 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 19:53:36 +00:00
|
|
|
var _ = BeforeSuite(func() {
|
2021-02-06 12:16:36 +00:00
|
|
|
log := zap.New(zap.WriteTo(GinkgoWriter))
|
|
|
|
logf.SetLogger(log)
|
2020-11-23 14:21:01 +00:00
|
|
|
|
|
|
|
By("bootstrapping test environment")
|
|
|
|
testEnv = &envtest.Environment{
|
2021-03-27 22:14:54 +00:00
|
|
|
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "deploy", "crds")},
|
2020-11-23 14:21:01 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 10:42:18 +00:00
|
|
|
var ctx context.Context
|
|
|
|
ctx, cancel = context.WithCancel(context.Background())
|
|
|
|
|
2020-11-23 14:21:01 +00:00
|
|
|
var err error
|
|
|
|
cfg, err = testEnv.Start()
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(cfg).ToNot(BeNil())
|
|
|
|
|
2022-02-07 10:42:18 +00:00
|
|
|
err = esapi.AddToScheme(scheme.Scheme)
|
2020-11-23 14:21:01 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2022-02-07 10:42:18 +00:00
|
|
|
k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
|
2023-08-28 09:50:46 +00:00
|
|
|
Scheme: scheme.Scheme,
|
|
|
|
Metrics: server.Options{
|
|
|
|
BindAddress: "0", // avoid port collision when testing
|
|
|
|
},
|
2022-02-07 10:42:18 +00:00
|
|
|
})
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2020-11-23 14:21:01 +00:00
|
|
|
|
|
|
|
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(k8sClient).ToNot(BeNil())
|
2022-02-07 10:42:18 +00:00
|
|
|
|
|
|
|
err = (&StoreReconciler{
|
|
|
|
Client: k8sClient,
|
|
|
|
Scheme: k8sManager.GetScheme(),
|
|
|
|
Log: ctrl.Log.WithName("controllers").WithName("SecretStore"),
|
|
|
|
ControllerClass: defaultControllerClass,
|
2023-06-12 10:58:29 +00:00
|
|
|
}).SetupWithManager(k8sManager, controller.Options{})
|
2022-02-07 10:42:18 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
err = (&ClusterStoreReconciler{
|
|
|
|
Client: k8sClient,
|
|
|
|
Scheme: k8sManager.GetScheme(),
|
|
|
|
ControllerClass: defaultControllerClass,
|
|
|
|
Log: ctrl.Log.WithName("controllers").WithName("ClusterSecretStore"),
|
2024-11-19 11:20:05 +00:00
|
|
|
}).SetupWithManager(k8sManager, controller.Options{})
|
2022-02-07 10:42:18 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer GinkgoRecover()
|
|
|
|
Expect(k8sManager.Start(ctx)).ToNot(HaveOccurred())
|
|
|
|
}()
|
2022-01-21 20:05:37 +00:00
|
|
|
})
|
2020-11-23 14:21:01 +00:00
|
|
|
|
|
|
|
var _ = AfterSuite(func() {
|
|
|
|
By("tearing down the test environment")
|
2022-02-07 10:42:18 +00:00
|
|
|
cancel() // stop manager
|
2020-11-23 14:21:01 +00:00
|
|
|
err := testEnv.Stop()
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
})
|
2023-06-05 19:26:25 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
ctrlmetrics.SetUpLabelNames(false)
|
|
|
|
cssmetrics.SetUpMetrics()
|
|
|
|
ssmetrics.SetUpMetrics()
|
|
|
|
}
|