1
0
Fork 0
mirror of https://github.com/external-secrets/external-secrets.git synced 2024-12-14 11:57:59 +00:00
external-secrets/pkg/controllers/secretstore/suite_test.go

117 lines
3.4 KiB
Go
Raw Normal View History

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 (
"context"
2020-11-23 14:21:01 +00:00
"path/filepath"
"testing"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
2020-11-23 14:21:01 +00:00
"sigs.k8s.io/controller-runtime/pkg/client"
"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"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
2020-11-23 14:21:01 +00:00
esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
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"
. "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
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{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "deploy", "crds")},
2020-11-23 14:21:01 +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())
err = esapi.AddToScheme(scheme.Scheme)
2020-11-23 14:21:01 +00:00
Expect(err).NotTo(HaveOccurred())
k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme.Scheme,
Metrics: server.Options{
BindAddress: "0", // avoid port collision when testing
},
})
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())
err = (&StoreReconciler{
Client: k8sClient,
Scheme: k8sManager.GetScheme(),
Log: ctrl.Log.WithName("controllers").WithName("SecretStore"),
ControllerClass: defaultControllerClass,
}).SetupWithManager(k8sManager, controller.Options{})
Expect(err).ToNot(HaveOccurred())
err = (&ClusterStoreReconciler{
Client: k8sClient,
Scheme: k8sManager.GetScheme(),
ControllerClass: defaultControllerClass,
Log: ctrl.Log.WithName("controllers").WithName("ClusterSecretStore"),
}).SetupWithManager(k8sManager, controller.Options{})
Expect(err).ToNot(HaveOccurred())
go func() {
defer GinkgoRecover()
Expect(k8sManager.Start(ctx)).ToNot(HaveOccurred())
}()
})
2020-11-23 14:21:01 +00:00
var _ = AfterSuite(func() {
By("tearing down the test environment")
cancel() // stop manager
2020-11-23 14:21:01 +00:00
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
})
func init() {
ctrlmetrics.SetUpLabelNames(false)
cssmetrics.SetUpMetrics()
ssmetrics.SetUpMetrics()
}