...

Source file src/k8s.io/apiextensions-apiserver/test/integration/apply_test.go

Documentation: k8s.io/apiextensions-apiserver/test/integration

     1  /*
     2  Copyright 2018 The Kubernetes 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 integration
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"testing"
    23  
    24  	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    25  	"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
    26  	"k8s.io/apiextensions-apiserver/test/integration/fixtures"
    27  	"k8s.io/apimachinery/pkg/api/errors"
    28  	"k8s.io/apimachinery/pkg/types"
    29  	"k8s.io/client-go/dynamic"
    30  )
    31  
    32  func TestApplyBasic(t *testing.T) {
    33  	tearDown, config, _, err := fixtures.StartDefaultServer(t)
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	defer tearDown()
    38  
    39  	apiExtensionClient, err := clientset.NewForConfig(config)
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	dynamicClient, err := dynamic.NewForConfig(config)
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	noxuDefinition := fixtures.NewNoxuV1CustomResourceDefinition(apiextensionsv1.ClusterScoped)
    49  	noxuDefinition, err = fixtures.CreateNewV1CustomResourceDefinition(noxuDefinition, apiExtensionClient, dynamicClient)
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  
    54  	kind := noxuDefinition.Spec.Names.Kind
    55  	apiVersion := noxuDefinition.Spec.Group + "/" + noxuDefinition.Spec.Versions[0].Name
    56  
    57  	rest := apiExtensionClient.Discovery().RESTClient()
    58  	yamlBody := []byte(fmt.Sprintf(`
    59  apiVersion: %s
    60  kind: %s
    61  metadata:
    62    name: mytest
    63  values:
    64    numVal: 1
    65    boolVal: true
    66    stringVal: "1"`, apiVersion, kind))
    67  	result, err := rest.Patch(types.ApplyPatchType).
    68  		AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Versions[0].Name, noxuDefinition.Spec.Names.Plural).
    69  		Name("mytest").
    70  		Param("fieldManager", "apply_test").
    71  		Body(yamlBody).
    72  		DoRaw(context.TODO())
    73  	if err != nil {
    74  		t.Fatal(err, string(result))
    75  	}
    76  
    77  	result, err = rest.Patch(types.MergePatchType).
    78  		AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Versions[0].Name, noxuDefinition.Spec.Names.Plural).
    79  		Name("mytest").
    80  		Body([]byte(`{"values":{"numVal": 5}}`)).
    81  		DoRaw(context.TODO())
    82  	if err != nil {
    83  		t.Fatal(err, string(result))
    84  	}
    85  
    86  	// Re-apply the same object, we should get conflicts now.
    87  	result, err = rest.Patch(types.ApplyPatchType).
    88  		AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Versions[0].Name, noxuDefinition.Spec.Names.Plural).
    89  		Name("mytest").
    90  		Param("fieldManager", "apply_test").
    91  		Body(yamlBody).
    92  		DoRaw(context.TODO())
    93  	if err == nil {
    94  		t.Fatalf("Expecting to get conflicts when applying object, got no error: %s", result)
    95  	}
    96  	status, ok := err.(*errors.StatusError)
    97  	if !ok {
    98  		t.Fatalf("Expecting to get conflicts as API error")
    99  	}
   100  	if len(status.Status().Details.Causes) < 1 {
   101  		t.Fatalf("Expecting to get at least one conflict when applying object, got: %v", status.Status().Details.Causes)
   102  	}
   103  
   104  	// Re-apply with force, should work fine.
   105  	result, err = rest.Patch(types.ApplyPatchType).
   106  		AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Versions[0].Name, noxuDefinition.Spec.Names.Plural).
   107  		Name("mytest").
   108  		Param("force", "true").
   109  		Param("fieldManager", "apply_test").
   110  		Body(yamlBody).
   111  		DoRaw(context.TODO())
   112  	if err != nil {
   113  		t.Fatal(err, string(result))
   114  	}
   115  
   116  }
   117  

View as plain text