|
| 1 | +/* |
| 2 | +Copyright 2023 Red Hat |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package helpers |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + |
| 19 | + t "github.com/onsi/gomega" |
| 20 | + memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/v1beta1" |
| 21 | + "github.com/openstack-k8s-operators/lib-common/modules/common/condition" |
| 22 | + k8s_errors "k8s.io/apimachinery/pkg/api/errors" |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + "k8s.io/apimachinery/pkg/types" |
| 25 | + |
| 26 | + corev1 "k8s.io/api/core/v1" |
| 27 | +) |
| 28 | + |
| 29 | +// CreateMemcached creates a new Memcached instance with the specified namespace in the Kubernetes cluster. |
| 30 | +func (tc *TestHelper) CreateMemcached(namespace string, memcachedName string, spec memcachedv1.MemcachedSpec) types.NamespacedName { |
| 31 | + name := types.NamespacedName{ |
| 32 | + Name: memcachedName, |
| 33 | + Namespace: namespace, |
| 34 | + } |
| 35 | + |
| 36 | + mc := &memcachedv1.Memcached{ |
| 37 | + TypeMeta: metav1.TypeMeta{ |
| 38 | + APIVersion: "memcached.openstack.org/v1beta1", |
| 39 | + Kind: "Memcached", |
| 40 | + }, |
| 41 | + ObjectMeta: metav1.ObjectMeta{ |
| 42 | + Name: memcachedName, |
| 43 | + Namespace: namespace, |
| 44 | + }, |
| 45 | + Spec: spec, |
| 46 | + } |
| 47 | + |
| 48 | + t.Expect(tc.K8sClient.Create(tc.Ctx, mc)).Should(t.Succeed()) |
| 49 | + |
| 50 | + return name |
| 51 | +} |
| 52 | + |
| 53 | +// DeleteMemcached deletes a Memcached instance from the Kubernetes cluster. |
| 54 | +func (tc *TestHelper) DeleteMemcached(name types.NamespacedName) { |
| 55 | + t.Eventually(func(g t.Gomega) { |
| 56 | + service := &corev1.Service{} |
| 57 | + err := tc.K8sClient.Get(tc.Ctx, name, service) |
| 58 | + // if it is already gone that is OK |
| 59 | + if k8s_errors.IsNotFound(err) { |
| 60 | + return |
| 61 | + } |
| 62 | + g.Expect(err).NotTo(t.HaveOccurred()) |
| 63 | + |
| 64 | + g.Expect(tc.K8sClient.Delete(tc.Ctx, service)).Should(t.Succeed()) |
| 65 | + |
| 66 | + err = tc.K8sClient.Get(tc.Ctx, name, service) |
| 67 | + g.Expect(k8s_errors.IsNotFound(err)).To(t.BeTrue()) |
| 68 | + }, tc.Timeout, tc.Interval).Should(t.Succeed()) |
| 69 | +} |
| 70 | + |
| 71 | +// GetMemcached waits for and retrieves a Memcached instance from the Kubernetes cluster |
| 72 | +func (tc *TestHelper) GetMemcached(name types.NamespacedName) *memcachedv1.Memcached { |
| 73 | + mc := &memcachedv1.Memcached{} |
| 74 | + t.Eventually(func(g t.Gomega) { |
| 75 | + g.Expect(tc.K8sClient.Get(tc.Ctx, name, mc)).Should(t.Succeed()) |
| 76 | + }, tc.Timeout, tc.Interval).Should(t.Succeed()) |
| 77 | + return mc |
| 78 | +} |
| 79 | + |
| 80 | +// SimulateMemcachedReady simulates a ready state for a Memcached instance in a Kubernetes cluster. |
| 81 | +func (tc *TestHelper) SimulateMemcachedReady(name types.NamespacedName) { |
| 82 | + t.Eventually(func(g t.Gomega) { |
| 83 | + mc := tc.GetMemcached(name) |
| 84 | + mc.Status.Conditions.MarkTrue(condition.ReadyCondition, condition.ReadyMessage) |
| 85 | + mc.Status.ReadyCount = mc.Spec.Replicas |
| 86 | + |
| 87 | + serverList := []string{} |
| 88 | + serverListWithInet := []string{} |
| 89 | + for i := 0; i < int(mc.Spec.Replicas); i++ { |
| 90 | + serverList = append(serverList, fmt.Sprintf("%s-%d.%s:11211", mc.Name, i, mc.Name)) |
| 91 | + serverListWithInet = append(serverList, fmt.Sprintf("inet:[%s-%d.%s]:11211", mc.Name, i, mc.Name)) |
| 92 | + } |
| 93 | + mc.Status.ServerList = serverList |
| 94 | + mc.Status.ServerListWithInet = serverListWithInet |
| 95 | + |
| 96 | + // This can return conflict so we have the t.Eventually block to retry |
| 97 | + g.Expect(tc.K8sClient.Status().Update(tc.Ctx, mc)).To(t.Succeed()) |
| 98 | + |
| 99 | + }, tc.Timeout, tc.Interval).Should(t.Succeed()) |
| 100 | + |
| 101 | + tc.Logger.Info("Simulated memcached ready", "on", name) |
| 102 | +} |
0 commit comments