...

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