-
Notifications
You must be signed in to change notification settings - Fork 104
/
tcp_migration_test.go
123 lines (103 loc) · 3.9 KB
/
tcp_migration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2022 Clastix Labs
// SPDX-License-Identifier: Apache-2.0
package e2e
import (
"context"
"fmt"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/tools/clientcmd"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
kamajiv1alpha1 "github.com/clastix/kamaji/api/v1alpha1"
"github.com/clastix/kamaji/internal/utilities"
)
var _ = Describe("When migrating a Tenant Control Plane to another datastore", func() {
var tcp *kamajiv1alpha1.TenantControlPlane
// Create a TenantControlPlane resource into the cluster
JustBeforeEach(func() {
// Fill TenantControlPlane object
tcp = &kamajiv1alpha1.TenantControlPlane{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("migrating-%s-etcd", rand.String(5)),
Namespace: "default",
},
Spec: kamajiv1alpha1.TenantControlPlaneSpec{
DataStore: "etcd-bronze",
ControlPlane: kamajiv1alpha1.ControlPlane{
Deployment: kamajiv1alpha1.DeploymentSpec{
Replicas: 1,
},
Service: kamajiv1alpha1.ServiceSpec{
ServiceType: "NodePort",
},
},
NetworkProfile: kamajiv1alpha1.NetworkProfileSpec{
Address: GetKindIPAddress(),
Port: int32(rand.Int63nRange(31000, 32000)),
},
Kubernetes: kamajiv1alpha1.KubernetesSpec{
Version: "v1.23.6",
Kubelet: kamajiv1alpha1.KubeletSpec{
CGroupFS: "cgroupfs",
},
},
},
}
Expect(k8sClient.Create(context.Background(), tcp)).NotTo(HaveOccurred())
StatusMustEqualTo(tcp, kamajiv1alpha1.VersionReady)
})
// Delete the TenantControlPlane resource after test is finished
JustAfterEach(func() {
Expect(k8sClient.Delete(context.Background(), tcp)).Should(Succeed())
})
// Check if TenantControlPlane resource has been created
It("Should contain all the migrated data", func() {
time.Sleep(10 * time.Second)
By("getting TCP rest.Config")
config, err := utilities.GetTenantKubeconfig(context.Background(), k8sClient, tcp)
Expect(err).ToNot(HaveOccurred())
b, err := utilities.EncodeToYaml(config)
Expect(err).ToNot(HaveOccurred())
clientCfg, err := clientcmd.NewClientConfigFromBytes(b)
Expect(err).ToNot(HaveOccurred())
restConfig, err := clientCfg.ClientConfig()
Expect(err).ToNot(HaveOccurred())
tcpClient, err := ctrlclient.New(restConfig, ctrlclient.Options{})
Expect(err).ToNot(HaveOccurred())
ns := &corev1.Namespace{}
ns.SetName("kamaji-test")
Expect(tcpClient.Create(context.Background(), ns)).ToNot(HaveOccurred())
By("start migration to a new DataStore")
Eventually(func() error {
if err := k8sClient.Get(context.Background(), types.NamespacedName{Namespace: tcp.GetNamespace(), Name: tcp.GetName()}, tcp); err != nil {
return err
}
tcp.Spec.DataStore = "etcd-silver"
return k8sClient.Update(context.Background(), tcp)
}, time.Minute, time.Second).ShouldNot(HaveOccurred())
By("waiting for the migrating status")
StatusMustEqualTo(tcp, kamajiv1alpha1.VersionMigrating)
By("ensuring changes are not allowed")
Consistently(func() error {
return tcpClient.Delete(context.Background(), ns)
}, 10*time.Second, time.Second).Should(HaveOccurred())
By("waiting for completion of migration")
StatusMustEqualTo(tcp, kamajiv1alpha1.VersionReady)
By("checking the DataStore of the TCP")
Eventually(func() string {
if err := k8sClient.Get(context.Background(), types.NamespacedName{Name: tcp.GetName(), Namespace: tcp.GetNamespace()}, tcp); err != nil {
return ""
}
return tcp.Status.Storage.DataStoreName
}, time.Minute, time.Second).Should(BeEquivalentTo("etcd-silver"))
By("checking the presence of the previous Namespace")
Eventually(func() error {
return tcpClient.Get(context.Background(), types.NamespacedName{Name: ns.GetName()}, &corev1.Namespace{})
}).ShouldNot(HaveOccurred())
})
})