...

Source file src/github.com/chaos-mesh/chaos-mesh/cmd/chaos-controller-manager/provider/updated_client_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/cmd/chaos-controller-manager/provider

     1  // Copyright 2021 Chaos Mesh 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  
    16  package provider
    17  
    18  import (
    19  	"context"
    20  	"strconv"
    21  	"time"
    22  
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  	corev1 "k8s.io/api/core/v1"
    26  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/types"
    28  )
    29  
    30  var _ = Describe("UpdatedClient", func() {
    31  
    32  	BeforeEach(func() {
    33  		// Add any setup steps that needs to be executed before each test
    34  	})
    35  
    36  	AfterEach(func() {
    37  		// Add any teardown steps that needs to be executed after each test
    38  	})
    39  
    40  	Context(("UpdatedClient"), func() {
    41  		It(("Should create and delete successfully"), func() {
    42  			obj := &corev1.ConfigMap{
    43  				ObjectMeta: v1.ObjectMeta{
    44  					Namespace: "default",
    45  					Name:      "test-configmap-create-delete",
    46  				},
    47  				Data: map[string]string{
    48  					"test": "1",
    49  				},
    50  			}
    51  			err := k8sClient.Create(context.TODO(), obj)
    52  			Expect(err).ToNot(HaveOccurred())
    53  
    54  			err = k8sClient.Delete(context.TODO(), obj)
    55  			Expect(err).ToNot(HaveOccurred())
    56  		})
    57  
    58  		It("Data should always be updated", func() {
    59  			obj := &corev1.ConfigMap{
    60  				ObjectMeta: v1.ObjectMeta{
    61  					Namespace: "default",
    62  					Name:      "test-configmap-update",
    63  				},
    64  				Data: map[string]string{
    65  					"test": "0",
    66  				},
    67  			}
    68  			err := k8sClient.Create(context.TODO(), obj)
    69  			Expect(err).ToNot(HaveOccurred())
    70  
    71  			for i := 0; i <= 200; i++ {
    72  				data := strconv.Itoa(i)
    73  
    74  				obj.Data["test"] = data
    75  				err = k8sClient.Update(context.TODO(), obj)
    76  				Expect(err).ToNot(HaveOccurred())
    77  
    78  				err = k8sClient.Get(context.TODO(), types.NamespacedName{
    79  					Namespace: "default",
    80  					Name:      "test-configmap-update",
    81  				}, obj)
    82  				Expect(err).ToNot(HaveOccurred())
    83  
    84  				Expect(obj.Data["test"]).To(Equal(data))
    85  			}
    86  
    87  			err = k8sClient.Delete(context.TODO(), obj)
    88  			Expect(err).ToNot(HaveOccurred())
    89  		})
    90  
    91  		It("Newer data should be returned", func() {
    92  			obj := &corev1.ConfigMap{
    93  				ObjectMeta: v1.ObjectMeta{
    94  					Namespace: "default",
    95  					Name:      "test-configmap-another-update",
    96  				},
    97  				Data: map[string]string{
    98  					"test": "0",
    99  				},
   100  			}
   101  			err := k8sClient.Create(context.TODO(), obj)
   102  			Expect(err).ToNot(HaveOccurred())
   103  
   104  			obj.Data["test"] = "1"
   105  			err = k8sClient.Update(context.TODO(), obj)
   106  			Expect(err).ToNot(HaveOccurred())
   107  
   108  			newObj := &corev1.ConfigMap{}
   109  			err = k8sClient.Get(context.TODO(), types.NamespacedName{
   110  				Namespace: "default",
   111  				Name:      "test-configmap-another-update",
   112  			}, newObj)
   113  			Expect(err).ToNot(HaveOccurred())
   114  
   115  			Expect(newObj.Data["test"]).To(Equal("1"))
   116  			newObj.Data["test"] = "2"
   117  			anotherCleanClient := mgr.GetClient()
   118  			err = anotherCleanClient.Update(context.TODO(), newObj)
   119  			Expect(err).ToNot(HaveOccurred())
   120  
   121  			time.Sleep(1 * time.Second)
   122  			newObj = &corev1.ConfigMap{}
   123  			err = k8sClient.Get(context.TODO(), types.NamespacedName{
   124  				Namespace: "default",
   125  				Name:      "test-configmap-another-update",
   126  			}, newObj)
   127  			Expect(err).ToNot(HaveOccurred())
   128  			Expect(newObj.Data["test"]).To(Equal("2"))
   129  		})
   130  	})
   131  })
   132