Test finalization of CRD

Updates #45511
This commit is contained in:
Mikhail Mazurskiy 2017-06-01 09:18:42 +10:00
parent 7043372d05
commit 5ac9d860a3
No known key found for this signature in database
GPG Key ID: 93551ECC96E2F568
3 changed files with 112 additions and 0 deletions

View File

@ -254,6 +254,10 @@
"ImportPath": "github.com/pkg/errors",
"Rev": "a22138067af1c4942683050411a841ade67fe1eb"
},
{
"ImportPath": "github.com/pmezard/go-difflib/difflib",
"Rev": "d8ed2627bdf02c080bf22230dbb337003b7aba2d"
},
{
"ImportPath": "github.com/prometheus/client_golang/prometheus",
"Rev": "e51041b3fa41cece0dca035740ba6411905be473"
@ -282,6 +286,14 @@
"ImportPath": "github.com/spf13/pflag",
"Rev": "9ff6c6923cfffbcd502984b8e0c80539a94968b7"
},
{
"ImportPath": "github.com/stretchr/testify/assert",
"Rev": "e3a8ff8ce36581f87a15341206f205b1da467059"
},
{
"ImportPath": "github.com/stretchr/testify/require",
"Rev": "e3a8ff8ce36581f87a15341206f205b1da467059"
},
{
"ImportPath": "github.com/ugorji/go/codec",
"Rev": "ded73eae5db7e7a0ef6f55aace87a2873c5d2b74"

View File

@ -11,6 +11,7 @@ go_test(
name = "go_default_test",
srcs = [
"basic_test.go",
"finalization_test.go",
"registration_test.go",
],
tags = [
@ -19,6 +20,7 @@ go_test(
],
deps = [
"//vendor/github.com/coreos/etcd/clientv3:go_default_library",
"//vendor/github.com/stretchr/testify/require:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/meta:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",

View File

@ -0,0 +1,98 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package integration
import (
"testing"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apiextensionsv1beta1 "k8s.io/kube-apiextensions-server/pkg/apis/apiextensions/v1beta1"
"k8s.io/kube-apiextensions-server/test/integration/testserver"
)
func TestFinalization(t *testing.T) {
stopCh, apiExtensionClient, clientPool, err := testserver.StartDefaultServer()
require.NoError(t, err)
defer close(stopCh)
noxuDefinition := testserver.NewNoxuCustomResourceDefinition(apiextensionsv1beta1.ClusterScoped)
noxuVersionClient, err := testserver.CreateNewCustomResourceDefinition(noxuDefinition, apiExtensionClient, clientPool)
require.NoError(t, err)
ns := "not-the-default"
name := "foo123"
noxuResourceClient := NewNamespacedCustomResourceClient(ns, noxuVersionClient, noxuDefinition)
instance := testserver.NewNoxuInstance(ns, name)
instance.SetFinalizers([]string{"noxu.example.com/finalizer"})
createdNoxuInstance, err := instantiateCustomResource(t, instance, noxuResourceClient, noxuDefinition)
require.NoError(t, err)
uid := createdNoxuInstance.GetUID()
err = noxuResourceClient.Delete(name, &metav1.DeleteOptions{
Preconditions: &metav1.Preconditions{
UID: &uid,
},
})
require.NoError(t, err)
// Deleting something with a finalizer sets deletion timestamp to a not-nil value but does not
// remove the object from the API server. Here we read it to confirm this.
gottenNoxuInstance, err := noxuResourceClient.Get(name)
require.NoError(t, err)
require.NotNil(t, gottenNoxuInstance.GetDeletionTimestamp())
// Trying to delete it again to confirm it will not remove the object because finalizer is still there.
err = noxuResourceClient.Delete(name, &metav1.DeleteOptions{
Preconditions: &metav1.Preconditions{
UID: &uid,
},
})
require.NoError(t, err)
// Removing the finalizers to allow the following delete remove the object.
// This step will fail if previous delete wrongly removed the object.
for {
gottenNoxuInstance.SetFinalizers(nil)
_, err = noxuResourceClient.Update(gottenNoxuInstance)
if err == nil {
break
}
if !errors.IsConflict(err) {
require.NoError(t, err) // Fail on unexpected error
}
gottenNoxuInstance, err = noxuResourceClient.Get(name)
require.NoError(t, err)
}
// Now when finalizer is not there it should be possible to actually remove the object from the server.
err = noxuResourceClient.Delete(name, &metav1.DeleteOptions{
Preconditions: &metav1.Preconditions{
UID: &uid,
},
})
require.NoError(t, err)
// Check that the object is actually gone.
_, err = noxuResourceClient.Get(name)
require.Error(t, err)
require.True(t, errors.IsNotFound(err), "%#v", err)
}