kubesphere.io/api@v0.0.0-20231107125330-c9a03957060c/devops/v1alpha1/s2ibuilder_types_test.go (about)

     1  /*
     2  Copyright 2020 The KubeSphere Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v1alpha1
    18  
    19  import (
    20  	"log"
    21  	"testing"
    22  
    23  	"github.com/onsi/gomega"
    24  	"golang.org/x/net/context"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/types"
    27  	"k8s.io/client-go/kubernetes/scheme"
    28  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    29  )
    30  
    31  func TestStorageS2iBuilder(t *testing.T) {
    32  	err := SchemeBuilder.AddToScheme(scheme.Scheme)
    33  	if err != nil {
    34  		log.Fatal(err)
    35  	}
    36  	c := fake.NewFakeClientWithScheme(scheme.Scheme)
    37  
    38  	key := types.NamespacedName{
    39  		Name:      "foo",
    40  		Namespace: "default",
    41  	}
    42  	created := &S2iBuilder{
    43  		TypeMeta: metav1.TypeMeta{
    44  			Kind:       "S2iBuilder",
    45  			APIVersion: "devops.kubesphere.io/v1alpha1",
    46  		},
    47  		ObjectMeta: metav1.ObjectMeta{
    48  			Name:      "foo",
    49  			Namespace: "default",
    50  		}}
    51  	g := gomega.NewGomegaWithT(t)
    52  
    53  	// Test Create
    54  	fetched := &S2iBuilder{}
    55  	g.Expect(c.Create(context.TODO(), created)).NotTo(gomega.HaveOccurred())
    56  
    57  	g.Expect(c.Get(context.TODO(), key, fetched)).NotTo(gomega.HaveOccurred())
    58  	g.Expect(fetched).To(gomega.Equal(created))
    59  
    60  	// Test Updating the Labels
    61  	updated := fetched.DeepCopy()
    62  	updated.Labels = map[string]string{"hello": "world"}
    63  	g.Expect(c.Update(context.TODO(), updated)).NotTo(gomega.HaveOccurred())
    64  
    65  	g.Expect(c.Get(context.TODO(), key, fetched)).NotTo(gomega.HaveOccurred())
    66  	g.Expect(fetched).To(gomega.Equal(updated))
    67  
    68  	// Test Delete
    69  	g.Expect(c.Delete(context.TODO(), fetched)).NotTo(gomega.HaveOccurred())
    70  	g.Expect(c.Get(context.TODO(), key, fetched)).To(gomega.HaveOccurred())
    71  }