Compare commits
3 Commits
f8707d55e5
...
393465300a
| Author | SHA1 | Date | |
|---|---|---|---|
| 393465300a | |||
| 1dfd0d472b | |||
| 8509cf8d2f |
@ -2,20 +2,174 @@ package controllers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"reflect"
|
//"k8s.io/apimachinery/pkg/types"
|
||||||
"time"
|
//"reflect"
|
||||||
|
//"fmt"
|
||||||
|
//"time"
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
batchv1 "k8s.io/api/batch/v1"
|
//batchv1 "k8s.io/api/batch/v1"
|
||||||
|
formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1"
|
||||||
|
appsv1 "k8s.io/api/apps/v1"
|
||||||
batchv1beta1 "k8s.io/api/batch/v1beta1"
|
batchv1beta1 "k8s.io/api/batch/v1beta1"
|
||||||
v1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
//"k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/types"
|
"k8s.io/apimachinery/pkg/types"
|
||||||
|
|
||||||
formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = Describe("BackupConfiguration controller", func() {
|
var _ = Describe("Testing BackupConf controller", func() {
|
||||||
|
const (
|
||||||
|
BackupConfName = "test-backupconf"
|
||||||
|
)
|
||||||
|
var (
|
||||||
|
key = types.NamespacedName{
|
||||||
|
Name: BackupConfName,
|
||||||
|
Namespace: TestNamespace,
|
||||||
|
}
|
||||||
|
ctx = context.Background()
|
||||||
|
backupConf = &formolv1alpha1.BackupConfiguration{}
|
||||||
|
)
|
||||||
|
|
||||||
|
BeforeEach(func() {
|
||||||
|
backupConf = &formolv1alpha1.BackupConfiguration{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: BackupConfName,
|
||||||
|
Namespace: TestNamespace,
|
||||||
|
},
|
||||||
|
Spec: formolv1alpha1.BackupConfigurationSpec{
|
||||||
|
Repository: RepoName,
|
||||||
|
Schedule: "1 * * * *",
|
||||||
|
Targets: []formolv1alpha1.Target{
|
||||||
|
formolv1alpha1.Target{
|
||||||
|
Kind: "Deployment",
|
||||||
|
Name: DeploymentName,
|
||||||
|
},
|
||||||
|
formolv1alpha1.Target{
|
||||||
|
Kind: "Task",
|
||||||
|
Name: BackupFuncName,
|
||||||
|
Steps: []formolv1alpha1.Step{
|
||||||
|
formolv1alpha1.Step{
|
||||||
|
Name: BackupFuncName,
|
||||||
|
Namespace: TestNamespace,
|
||||||
|
Env: []corev1.EnvVar{
|
||||||
|
corev1.EnvVar{
|
||||||
|
Name: "foo",
|
||||||
|
Value: "bar",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
Context("There is a backupconf", func() {
|
||||||
|
JustBeforeEach(func() {
|
||||||
|
Eventually(func() error {
|
||||||
|
return k8sClient.Create(ctx, backupConf)
|
||||||
|
}, timeout, interval).Should(Succeed())
|
||||||
|
})
|
||||||
|
AfterEach(func() {
|
||||||
|
Expect(k8sClient.Delete(ctx, backupConf)).Should(Succeed())
|
||||||
|
})
|
||||||
|
It("Has a schedule", func() {
|
||||||
|
realBackupConf := &formolv1alpha1.BackupConfiguration{}
|
||||||
|
Eventually(func() bool {
|
||||||
|
err := k8sClient.Get(ctx, key, realBackupConf)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}, timeout, interval).Should(BeTrue())
|
||||||
|
Expect(realBackupConf.Spec.Schedule).Should(Equal("1 * * * *"))
|
||||||
|
})
|
||||||
|
It("Should also create a CronJob", func() {
|
||||||
|
cronJob := &batchv1beta1.CronJob{}
|
||||||
|
Eventually(func() bool {
|
||||||
|
err := k8sClient.Get(ctx, types.NamespacedName{
|
||||||
|
Name: "backup-" + BackupConfName,
|
||||||
|
Namespace: TestNamespace,
|
||||||
|
}, cronJob)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}, timeout, interval).Should(BeTrue())
|
||||||
|
Expect(cronJob.Spec.Schedule).Should(Equal("1 * * * *"))
|
||||||
|
})
|
||||||
|
It("Should also create a sidecar container", func() {
|
||||||
|
realDeployment := &appsv1.Deployment{}
|
||||||
|
Eventually(func() (int, error) {
|
||||||
|
err := k8sClient.Get(ctx, types.NamespacedName{
|
||||||
|
Name: DeploymentName,
|
||||||
|
Namespace: TestNamespace,
|
||||||
|
}, realDeployment)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return len(realDeployment.Spec.Template.Spec.Containers), nil
|
||||||
|
}, timeout, interval).Should(Equal(2))
|
||||||
|
})
|
||||||
|
It("Should also update the CronJob", func() {
|
||||||
|
realBackupConf := &formolv1alpha1.BackupConfiguration{}
|
||||||
|
Eventually(func() bool {
|
||||||
|
err := k8sClient.Get(ctx, key, realBackupConf)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}, timeout, interval).Should(BeTrue())
|
||||||
|
realBackupConf.Spec.Schedule = "1 0 * * *"
|
||||||
|
suspend := true
|
||||||
|
realBackupConf.Spec.Suspend = &suspend
|
||||||
|
Expect(k8sClient.Update(ctx, realBackupConf)).Should(Succeed())
|
||||||
|
cronJob := &batchv1beta1.CronJob{}
|
||||||
|
Eventually(func() (string, error) {
|
||||||
|
err := k8sClient.Get(ctx, types.NamespacedName{
|
||||||
|
Name: "backup-" + BackupConfName,
|
||||||
|
Namespace: TestNamespace,
|
||||||
|
}, cronJob)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return cronJob.Spec.Schedule, nil
|
||||||
|
}, timeout, interval).Should(Equal("1 0 * * *"))
|
||||||
|
Eventually(func() (bool, error) {
|
||||||
|
err := k8sClient.Get(ctx, types.NamespacedName{
|
||||||
|
Name: "backup-" + BackupConfName,
|
||||||
|
Namespace: TestNamespace,
|
||||||
|
}, cronJob)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return *cronJob.Spec.Suspend == true, nil
|
||||||
|
}, timeout, interval).Should(BeTrue())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
Context("Deleting a backupconf", func() {
|
||||||
|
JustBeforeEach(func() {
|
||||||
|
Eventually(func() error {
|
||||||
|
return k8sClient.Create(ctx, backupConf)
|
||||||
|
}, timeout, interval).Should(Succeed())
|
||||||
|
})
|
||||||
|
It("Should also delete the sidecar container", func() {
|
||||||
|
Expect(k8sClient.Delete(ctx, backupConf)).Should(Succeed())
|
||||||
|
realDeployment := &appsv1.Deployment{}
|
||||||
|
Eventually(func() (int, error) {
|
||||||
|
err := k8sClient.Get(ctx, types.NamespacedName{
|
||||||
|
Name: DeploymentName,
|
||||||
|
Namespace: TestNamespace,
|
||||||
|
}, realDeployment)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return len(realDeployment.Spec.Template.Spec.Containers), nil
|
||||||
|
}, timeout, interval).Should(Equal(1))
|
||||||
|
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|||||||
@ -17,31 +17,120 @@ limitations under the License.
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
|
appsv1 "k8s.io/api/apps/v1"
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/client-go/kubernetes/scheme"
|
"k8s.io/client-go/kubernetes/scheme"
|
||||||
"k8s.io/client-go/rest"
|
"k8s.io/client-go/rest"
|
||||||
|
ctrl "sigs.k8s.io/controller-runtime"
|
||||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
"sigs.k8s.io/controller-runtime/pkg/envtest"
|
"sigs.k8s.io/controller-runtime/pkg/envtest"
|
||||||
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
|
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
|
||||||
logf "sigs.k8s.io/controller-runtime/pkg/log"
|
logf "sigs.k8s.io/controller-runtime/pkg/log"
|
||||||
"sigs.k8s.io/controller-runtime/pkg/log/zap"
|
"sigs.k8s.io/controller-runtime/pkg/log/zap"
|
||||||
|
|
||||||
formoldesmojimfrv1alpha1 "github.com/desmo999r/formol/api/v1alpha1"
|
|
||||||
formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1"
|
formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1"
|
||||||
// +kubebuilder:scaffold:imports
|
// +kubebuilder:scaffold:imports
|
||||||
)
|
)
|
||||||
|
|
||||||
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
|
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
|
||||||
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
|
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
|
||||||
|
const (
|
||||||
|
BackupFuncName = "test-backup-func"
|
||||||
|
TestNamespace = "test-namespace"
|
||||||
|
RepoName = "test-repo"
|
||||||
|
DeploymentName = "test-deployment"
|
||||||
|
timeout = time.Second * 10
|
||||||
|
interval = time.Millisecond * 250
|
||||||
|
)
|
||||||
|
|
||||||
var cfg *rest.Config
|
var cfg *rest.Config
|
||||||
var k8sClient client.Client
|
var k8sClient client.Client
|
||||||
var testEnv *envtest.Environment
|
var testEnv *envtest.Environment
|
||||||
|
|
||||||
|
var (
|
||||||
|
namespace = &corev1.Namespace{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: TestNamespace,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
deployment = &appsv1.Deployment{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: DeploymentName,
|
||||||
|
Namespace: TestNamespace,
|
||||||
|
},
|
||||||
|
Spec: appsv1.DeploymentSpec{
|
||||||
|
Selector: &metav1.LabelSelector{
|
||||||
|
MatchLabels: map[string]string{"app": "test-deployment"},
|
||||||
|
},
|
||||||
|
Template: corev1.PodTemplateSpec{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Labels: map[string]string{"app": "test-deployment"},
|
||||||
|
},
|
||||||
|
Spec: corev1.PodSpec{
|
||||||
|
Containers: []corev1.Container{
|
||||||
|
corev1.Container{
|
||||||
|
Name: "test-container",
|
||||||
|
Image: "test-image",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
sa = &corev1.ServiceAccount{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "default",
|
||||||
|
Namespace: TestNamespace,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
secret = &corev1.Secret{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "test-secret",
|
||||||
|
Namespace: TestNamespace,
|
||||||
|
},
|
||||||
|
Data: map[string][]byte{
|
||||||
|
"RESTIC_PASSWORD": []byte("toto"),
|
||||||
|
"AWS_ACCESS_KEY_ID": []byte("titi"),
|
||||||
|
"AWS_SECRET_ACCESS_KEY": []byte("tata"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
repo = &formolv1alpha1.Repo{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: RepoName,
|
||||||
|
Namespace: TestNamespace,
|
||||||
|
},
|
||||||
|
Spec: formolv1alpha1.RepoSpec{
|
||||||
|
Backend: formolv1alpha1.Backend{
|
||||||
|
S3: formolv1alpha1.S3{
|
||||||
|
Server: "raid5.desmojim.fr:9000",
|
||||||
|
Bucket: "testbucket2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
RepositorySecrets: "test-secret",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
function = &formolv1alpha1.Function{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: BackupFuncName,
|
||||||
|
Namespace: TestNamespace,
|
||||||
|
},
|
||||||
|
Spec: corev1.Container{
|
||||||
|
Name: "backup-func",
|
||||||
|
Image: "myimage",
|
||||||
|
Args: []string{"a", "set", "of", "args"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
func TestAPIs(t *testing.T) {
|
func TestAPIs(t *testing.T) {
|
||||||
RegisterFailHandler(Fail)
|
RegisterFailHandler(Fail)
|
||||||
|
|
||||||
@ -50,39 +139,54 @@ func TestAPIs(t *testing.T) {
|
|||||||
[]Reporter{printer.NewlineReporter{}})
|
[]Reporter{printer.NewlineReporter{}})
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ = BeforeSuite(func(done Done) {
|
var _ = BeforeSuite(func() {
|
||||||
logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))
|
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
|
||||||
|
|
||||||
By("bootstrapping test environment")
|
By("bootstrapping test environment")
|
||||||
testEnv = &envtest.Environment{
|
testEnv = &envtest.Environment{
|
||||||
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
|
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
cfg, err := testEnv.Start()
|
||||||
cfg, err = testEnv.Start()
|
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
Expect(cfg).ToNot(BeNil())
|
Expect(cfg).ToNot(BeNil())
|
||||||
|
|
||||||
err = formolv1alpha1.AddToScheme(scheme.Scheme)
|
err = formolv1alpha1.AddToScheme(scheme.Scheme)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
err = formolv1alpha1.AddToScheme(scheme.Scheme)
|
|
||||||
Expect(err).NotTo(HaveOccurred())
|
|
||||||
|
|
||||||
err = formoldesmojimfrv1alpha1.AddToScheme(scheme.Scheme)
|
|
||||||
Expect(err).NotTo(HaveOccurred())
|
|
||||||
|
|
||||||
// +kubebuilder:scaffold:scheme
|
// +kubebuilder:scaffold:scheme
|
||||||
|
|
||||||
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
|
k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
|
||||||
|
Scheme: scheme.Scheme,
|
||||||
|
})
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
Expect(k8sClient).ToNot(BeNil())
|
|
||||||
|
|
||||||
close(done)
|
err = (&BackupConfigurationReconciler{
|
||||||
|
Client: k8sManager.GetClient(),
|
||||||
|
Scheme: k8sManager.GetScheme(),
|
||||||
|
Log: ctrl.Log.WithName("controllers").WithName("BackupConfiguration"),
|
||||||
|
}).SetupWithManager(k8sManager)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
err = k8sManager.Start(ctrl.SetupSignalHandler())
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
}()
|
||||||
|
|
||||||
|
k8sClient = k8sManager.GetClient()
|
||||||
|
ctx := context.Background()
|
||||||
|
Expect(k8sClient).ToNot(BeNil())
|
||||||
|
Expect(k8sClient.Create(ctx, namespace)).Should(Succeed())
|
||||||
|
Expect(k8sClient.Create(ctx, sa)).Should(Succeed())
|
||||||
|
Expect(k8sClient.Create(ctx, secret)).Should(Succeed())
|
||||||
|
Expect(k8sClient.Create(ctx, repo)).Should(Succeed())
|
||||||
|
Expect(k8sClient.Create(ctx, deployment)).Should(Succeed())
|
||||||
|
Expect(k8sClient.Create(ctx, function)).Should(Succeed())
|
||||||
}, 60)
|
}, 60)
|
||||||
|
|
||||||
var _ = AfterSuite(func() {
|
var _ = AfterSuite(func() {
|
||||||
By("tearing down the test environment")
|
By("tearing down the test environment")
|
||||||
err := testEnv.Stop()
|
err := testEnv.Stop()
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
fmt.Println("coucou")
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user