mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 20:53:33 +00:00
Add e2e test for Lease API
This commit is contained in:
parent
eadf68ebd9
commit
4be5ebd4dc
@ -24,6 +24,7 @@ go_library(
|
|||||||
"init_container.go",
|
"init_container.go",
|
||||||
"kubelet.go",
|
"kubelet.go",
|
||||||
"kubelet_etc_hosts.go",
|
"kubelet_etc_hosts.go",
|
||||||
|
"lease.go",
|
||||||
"lifecycle_hook.go",
|
"lifecycle_hook.go",
|
||||||
"networking.go",
|
"networking.go",
|
||||||
"node_lease.go",
|
"node_lease.go",
|
||||||
@ -68,6 +69,7 @@ go_library(
|
|||||||
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
||||||
|
"//staging/src/k8s.io/apimachinery/pkg/util/strategicpatch:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/util/uuid:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/util/uuid:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/watch:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/watch:go_default_library",
|
||||||
|
149
test/e2e/common/lease.go
Normal file
149
test/e2e/common/lease.go
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2019 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 common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
coordinationv1 "k8s.io/api/coordination/v1"
|
||||||
|
apiequality "k8s.io/apimachinery/pkg/api/equality"
|
||||||
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
|
"k8s.io/apimachinery/pkg/types"
|
||||||
|
"k8s.io/apimachinery/pkg/util/strategicpatch"
|
||||||
|
"k8s.io/kubernetes/test/e2e/framework"
|
||||||
|
"k8s.io/utils/pointer"
|
||||||
|
|
||||||
|
"github.com/onsi/ginkgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getPatchBytes(oldLease, newLease *coordinationv1.Lease) ([]byte, error) {
|
||||||
|
oldData, err := json.Marshal(oldLease)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to Marshal oldData: %v", err)
|
||||||
|
}
|
||||||
|
newData, err := json.Marshal(newLease)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to Marshal newData: %v", err)
|
||||||
|
}
|
||||||
|
patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, coordinationv1.Lease{})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to CreateTwoWayMergePatch: %v", err)
|
||||||
|
}
|
||||||
|
return patchBytes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = framework.KubeDescribe("Lease", func() {
|
||||||
|
f := framework.NewDefaultFramework("lease-test")
|
||||||
|
|
||||||
|
// TODO(wojtek-t): Promote to Conformance once proved to be stable.
|
||||||
|
ginkgo.It("API should be available", func() {
|
||||||
|
leaseClient := f.ClientSet.CoordinationV1().Leases(f.Namespace.Name)
|
||||||
|
|
||||||
|
name := "lease"
|
||||||
|
lease := &coordinationv1.Lease{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: name,
|
||||||
|
},
|
||||||
|
Spec: coordinationv1.LeaseSpec{
|
||||||
|
HolderIdentity: pointer.StringPtr("holder"),
|
||||||
|
LeaseDurationSeconds: pointer.Int32Ptr(30),
|
||||||
|
AcquireTime: &metav1.MicroTime{Time: time.Time{}.Add(2 * time.Second)},
|
||||||
|
RenewTime: &metav1.MicroTime{Time: time.Time{}.Add(5 * time.Second)},
|
||||||
|
LeaseTransitions: pointer.Int32Ptr(0),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
createdLease, err := leaseClient.Create(lease)
|
||||||
|
framework.ExpectNoError(err, "creating Lease failed")
|
||||||
|
|
||||||
|
readLease, err := leaseClient.Get(name, metav1.GetOptions{})
|
||||||
|
framework.ExpectNoError(err, "couldn't read Lease")
|
||||||
|
framework.ExpectEqual(apiequality.Semantic.DeepEqual(lease.Spec, readLease.Spec), true)
|
||||||
|
|
||||||
|
createdLease.Spec = coordinationv1.LeaseSpec{
|
||||||
|
HolderIdentity: pointer.StringPtr("holder2"),
|
||||||
|
LeaseDurationSeconds: pointer.Int32Ptr(30),
|
||||||
|
AcquireTime: &metav1.MicroTime{Time: time.Time{}.Add(20 * time.Second)},
|
||||||
|
RenewTime: &metav1.MicroTime{Time: time.Time{}.Add(50 * time.Second)},
|
||||||
|
LeaseTransitions: pointer.Int32Ptr(1),
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = leaseClient.Update(createdLease)
|
||||||
|
framework.ExpectNoError(err, "updating Lease failed")
|
||||||
|
|
||||||
|
readLease, err = leaseClient.Get(name, metav1.GetOptions{})
|
||||||
|
framework.ExpectNoError(err, "couldn't read Lease")
|
||||||
|
framework.ExpectEqual(apiequality.Semantic.DeepEqual(createdLease.Spec, readLease.Spec), true)
|
||||||
|
|
||||||
|
patchedLease := readLease.DeepCopy()
|
||||||
|
patchedLease.Spec = coordinationv1.LeaseSpec{
|
||||||
|
HolderIdentity: pointer.StringPtr("holder3"),
|
||||||
|
LeaseDurationSeconds: pointer.Int32Ptr(60),
|
||||||
|
AcquireTime: &metav1.MicroTime{Time: time.Time{}.Add(50 * time.Second)},
|
||||||
|
RenewTime: &metav1.MicroTime{Time: time.Time{}.Add(70 * time.Second)},
|
||||||
|
LeaseTransitions: pointer.Int32Ptr(2),
|
||||||
|
}
|
||||||
|
patchBytes, err := getPatchBytes(readLease, patchedLease)
|
||||||
|
framework.ExpectNoError(err, "creating patch failed")
|
||||||
|
|
||||||
|
_, err = leaseClient.Patch(name, types.StrategicMergePatchType, patchBytes)
|
||||||
|
framework.ExpectNoError(err, "patching Lease failed")
|
||||||
|
|
||||||
|
readLease, err = leaseClient.Get(name, metav1.GetOptions{})
|
||||||
|
framework.ExpectNoError(err, "couldn't read Lease")
|
||||||
|
framework.ExpectEqual(apiequality.Semantic.DeepEqual(patchedLease.Spec, readLease.Spec), true)
|
||||||
|
|
||||||
|
name2 := "lease2"
|
||||||
|
lease2 := &coordinationv1.Lease{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: name2,
|
||||||
|
Labels: map[string]string{"deletecollection": "true"},
|
||||||
|
},
|
||||||
|
Spec: coordinationv1.LeaseSpec{
|
||||||
|
HolderIdentity: pointer.StringPtr("holder"),
|
||||||
|
LeaseDurationSeconds: pointer.Int32Ptr(30),
|
||||||
|
AcquireTime: &metav1.MicroTime{Time: time.Time{}.Add(2 * time.Second)},
|
||||||
|
RenewTime: &metav1.MicroTime{Time: time.Time{}.Add(5 * time.Second)},
|
||||||
|
LeaseTransitions: pointer.Int32Ptr(0),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
_, err = leaseClient.Create(lease2)
|
||||||
|
framework.ExpectNoError(err, "creating Lease failed")
|
||||||
|
|
||||||
|
leases, err := leaseClient.List(metav1.ListOptions{})
|
||||||
|
framework.ExpectNoError(err, "couldn't list Leases")
|
||||||
|
framework.ExpectEqual(len(leases.Items), 2)
|
||||||
|
|
||||||
|
selector := labels.Set(map[string]string{"deletecollection": "true"}).AsSelector()
|
||||||
|
err = leaseClient.DeleteCollection(&metav1.DeleteOptions{}, metav1.ListOptions{LabelSelector: selector.String()})
|
||||||
|
framework.ExpectNoError(err, "couldn't delete collection")
|
||||||
|
|
||||||
|
leases, err = leaseClient.List(metav1.ListOptions{})
|
||||||
|
framework.ExpectNoError(err, "couldn't list Leases")
|
||||||
|
framework.ExpectEqual(len(leases.Items), 1)
|
||||||
|
|
||||||
|
err = leaseClient.Delete(name, &metav1.DeleteOptions{})
|
||||||
|
framework.ExpectNoError(err, "deleting Lease failed")
|
||||||
|
|
||||||
|
_, err = leaseClient.Get(name, metav1.GetOptions{})
|
||||||
|
framework.ExpectEqual(errors.IsNotFound(err), true)
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user