github.com/geerlingguy/operator-sdk@v0.8.2/test/e2e/incluster-test-code/memcached_test.go.tmpl (about)

     1  // Copyright 2018 The Operator-SDK Authors
     2  //
     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  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package e2e
    16  
    17  import (
    18  	goctx "context"
    19  	"fmt"
    20  	"testing"
    21  	"time"
    22  
    23  	operator "github.com/example-inc/memcached-operator/pkg/apis/cache/v1alpha1"
    24  	apis "github.com/example-inc/memcached-operator/pkg/apis"
    25  	framework "github.com/operator-framework/operator-sdk/pkg/test"
    26  	"github.com/operator-framework/operator-sdk/pkg/test/e2eutil"
    27  
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	"k8s.io/apimachinery/pkg/types"
    30  )
    31  
    32  var (
    33  	retryInterval        = time.Second * 5
    34  	timeout              = time.Second * 30
    35  	cleanupRetryInterval = time.Second * 1
    36  	cleanupTimeout       = time.Second * 5
    37  )
    38  
    39  func TestMemcached(t *testing.T) {
    40  	memcachedList := &operator.MemcachedList{
    41  		TypeMeta: metav1.TypeMeta{
    42  			Kind:       "Memcached",
    43  			APIVersion: "cache.example.com/v1alpha1",
    44  		},
    45  	}
    46  	err := framework.AddToFrameworkScheme(apis.AddToScheme, memcachedList)
    47  	if err != nil {
    48  		t.Fatalf("Failed to add custom resource scheme to framework: %v", err)
    49  	}
    50  	// run subtests
    51  	t.Run("memcached-group", func(t *testing.T) {
    52  		t.Run("Cluster", MemcachedCluster)
    53  		t.Run("Cluster2", MemcachedCluster)
    54  	})
    55  }
    56  
    57  func memcachedScaleTest(t *testing.T, f *framework.Framework, ctx *framework.TestCtx) error {
    58  	namespace, err := ctx.GetNamespace()
    59  	if err != nil {
    60  		return fmt.Errorf("could not get namespace: %v", err)
    61  	}
    62  	// create memcached custom resource
    63  	exampleMemcached := &operator.Memcached{
    64  		TypeMeta: metav1.TypeMeta{
    65  			Kind:       "Memcached",
    66  			APIVersion: "cache.example.com/v1alpha1",
    67  		},
    68  		ObjectMeta: metav1.ObjectMeta{
    69  			Name:      "example-memcached",
    70  			Namespace: namespace,
    71  		},
    72  		Spec: operator.MemcachedSpec{
    73  			Size: 3,
    74  		},
    75  	}
    76  	// use TestCtx's create helper to create the object and add a cleanup function for the new object
    77  	err = f.Client.Create(goctx.TODO(), exampleMemcached, &framework.CleanupOptions{TestContext: ctx, Timeout: cleanupTimeout, RetryInterval: cleanupRetryInterval})
    78  	if err != nil {
    79  		return err
    80  	}
    81  	// wait for example-memcached to reach 3 replicas
    82  	err = e2eutil.WaitForDeployment(t, f.KubeClient, namespace, "example-memcached", 3, retryInterval, timeout)
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	err = f.Client.Get(goctx.TODO(), types.NamespacedName{Name: "example-memcached", Namespace: namespace}, exampleMemcached)
    88  	if err != nil {
    89  		return err
    90  	}
    91  	exampleMemcached.Spec.Size = 4
    92  	err = f.Client.Update(goctx.TODO(), exampleMemcached)
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	// wait for example-memcached to reach 4 replicas
    98  	return e2eutil.WaitForDeployment(t, f.KubeClient, namespace, "example-memcached", 4, retryInterval, timeout)
    99  }
   100  
   101  func MemcachedCluster(t *testing.T) {
   102  	t.Parallel()
   103  	ctx := framework.NewTestCtx(t)
   104  	defer ctx.Cleanup()
   105  	err := ctx.InitializeClusterResources(&framework.CleanupOptions{TestContext: ctx, Timeout: cleanupTimeout, RetryInterval: cleanupRetryInterval})
   106  	if err != nil {
   107  		t.Fatalf("Failed to initialize cluster resources: %v", err)
   108  	}
   109  	t.Log("Initialized cluster resources")
   110  	namespace, err := ctx.GetNamespace()
   111  	if err != nil {
   112  		t.Fatal(err)
   113  	}
   114  	// get global framework variables
   115  	f := framework.Global
   116  	// wait for memcached-operator to be ready
   117  	err = e2eutil.WaitForOperatorDeployment(t, f.KubeClient, namespace, "memcached-operator", 2, retryInterval, timeout)
   118  	if err != nil {
   119  		t.Fatal(err)
   120  	}
   121  
   122  	if err = memcachedScaleTest(t, f, ctx); err != nil {
   123  		t.Fatal(err)
   124  	}
   125  }