mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-05 10:19:50 +00:00
remove duplicate, flaky tests
This commit is contained in:
parent
7f8fd32ecb
commit
d441568312
@ -10,25 +10,16 @@ load(
|
|||||||
|
|
||||||
go_test(
|
go_test(
|
||||||
name = "go_default_test",
|
name = "go_default_test",
|
||||||
srcs = [
|
srcs = ["autoregister_controller_test.go"],
|
||||||
"autoregister_controller_new_test.go",
|
|
||||||
"autoregister_controller_test.go",
|
|
||||||
],
|
|
||||||
library = ":go_default_library",
|
library = ":go_default_library",
|
||||||
tags = ["automanaged"],
|
tags = ["automanaged"],
|
||||||
deps = [
|
deps = [
|
||||||
"//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",
|
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
|
||||||
"//vendor/k8s.io/apimachinery/pkg/watch:go_default_library",
|
|
||||||
"//vendor/k8s.io/client-go/testing:go_default_library",
|
"//vendor/k8s.io/client-go/testing:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/tools/cache:go_default_library",
|
"//vendor/k8s.io/client-go/tools/cache:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/util/workqueue:go_default_library",
|
"//vendor/k8s.io/client-go/util/workqueue:go_default_library",
|
||||||
"//vendor/k8s.io/kube-aggregator/pkg/apis/apiregistration:go_default_library",
|
"//vendor/k8s.io/kube-aggregator/pkg/apis/apiregistration:go_default_library",
|
||||||
"//vendor/k8s.io/kube-aggregator/pkg/client/clientset_generated/internalclientset:go_default_library",
|
|
||||||
"//vendor/k8s.io/kube-aggregator/pkg/client/clientset_generated/internalclientset/fake:go_default_library",
|
"//vendor/k8s.io/kube-aggregator/pkg/client/clientset_generated/internalclientset/fake:go_default_library",
|
||||||
"//vendor/k8s.io/kube-aggregator/pkg/client/informers/internalversion:go_default_library",
|
|
||||||
"//vendor/k8s.io/kube-aggregator/pkg/client/listers/apiregistration/internalversion:go_default_library",
|
"//vendor/k8s.io/kube-aggregator/pkg/client/listers/apiregistration/internalversion:go_default_library",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
@ -1,232 +0,0 @@
|
|||||||
/*
|
|
||||||
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 autoregister
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
||||||
clienttesting "k8s.io/client-go/testing"
|
|
||||||
"k8s.io/client-go/tools/cache"
|
|
||||||
"k8s.io/client-go/util/workqueue"
|
|
||||||
"k8s.io/kube-aggregator/pkg/apis/apiregistration"
|
|
||||||
"k8s.io/kube-aggregator/pkg/client/clientset_generated/internalclientset/fake"
|
|
||||||
listers "k8s.io/kube-aggregator/pkg/client/listers/apiregistration/internalversion"
|
|
||||||
)
|
|
||||||
|
|
||||||
func newAutoRegisterManagedAPIService(name string) *apiregistration.APIService {
|
|
||||||
return &apiregistration.APIService{
|
|
||||||
ObjectMeta: metav1.ObjectMeta{Name: name, Labels: map[string]string{AutoRegisterManagedLabel: string("true")}},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func newAutoRegisterManagedModifiedAPIService(name string) *apiregistration.APIService {
|
|
||||||
return &apiregistration.APIService{
|
|
||||||
ObjectMeta: metav1.ObjectMeta{Name: name, Labels: map[string]string{AutoRegisterManagedLabel: string("true")}},
|
|
||||||
Spec: apiregistration.APIServiceSpec{
|
|
||||||
Group: "something",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func newAPIService(name string) *apiregistration.APIService {
|
|
||||||
return &apiregistration.APIService{
|
|
||||||
ObjectMeta: metav1.ObjectMeta{Name: name},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkForNothing(name string, client *fake.Clientset) error {
|
|
||||||
if len(client.Actions()) > 0 {
|
|
||||||
return fmt.Errorf("unexpected action: %v", client.Actions())
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkForCreate(name string, client *fake.Clientset) error {
|
|
||||||
if len(client.Actions()) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if len(client.Actions()) > 1 {
|
|
||||||
return fmt.Errorf("unexpected action: %v", client.Actions())
|
|
||||||
}
|
|
||||||
|
|
||||||
action := client.Actions()[0]
|
|
||||||
|
|
||||||
createAction, ok := action.(clienttesting.CreateAction)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("unexpected action: %v", client.Actions())
|
|
||||||
}
|
|
||||||
apiService := createAction.GetObject().(*apiregistration.APIService)
|
|
||||||
if apiService.Name != name || apiService.Labels[AutoRegisterManagedLabel] != "true" {
|
|
||||||
return fmt.Errorf("bad name or label %v", createAction)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkForUpdate(name string, client *fake.Clientset) error {
|
|
||||||
if len(client.Actions()) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if len(client.Actions()) > 1 {
|
|
||||||
return fmt.Errorf("unexpected action: %v", client.Actions())
|
|
||||||
}
|
|
||||||
|
|
||||||
action := client.Actions()[0]
|
|
||||||
updateAction, ok := action.(clienttesting.UpdateAction)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("unexpected action: %v", client.Actions())
|
|
||||||
}
|
|
||||||
apiService := updateAction.GetObject().(*apiregistration.APIService)
|
|
||||||
if apiService.Name != name || apiService.Labels[AutoRegisterManagedLabel] != "true" || apiService.Spec.Group != "" {
|
|
||||||
return fmt.Errorf("bad name, label, or group %v", updateAction)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkForDelete(name string, client *fake.Clientset) error {
|
|
||||||
if len(client.Actions()) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, action := range client.Actions() {
|
|
||||||
deleteAction, ok := action.(clienttesting.DeleteAction)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("unexpected action: %v", client.Actions())
|
|
||||||
}
|
|
||||||
if deleteAction.GetName() != name {
|
|
||||||
return fmt.Errorf("bad name %v", deleteAction)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSync(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
apiServiceName string
|
|
||||||
addAPIServices []*apiregistration.APIService
|
|
||||||
updateAPIServices []*apiregistration.APIService
|
|
||||||
addSyncAPIServices []*apiregistration.APIService
|
|
||||||
delSyncAPIServices []string
|
|
||||||
expectedResults func(name string, client *fake.Clientset) error
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "adding an API service which isn't auto-managed does nothing",
|
|
||||||
apiServiceName: "foo",
|
|
||||||
addAPIServices: []*apiregistration.APIService{newAPIService("foo")},
|
|
||||||
updateAPIServices: []*apiregistration.APIService{},
|
|
||||||
addSyncAPIServices: []*apiregistration.APIService{},
|
|
||||||
delSyncAPIServices: []string{},
|
|
||||||
expectedResults: checkForNothing,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "adding one to auto-register should create",
|
|
||||||
apiServiceName: "foo",
|
|
||||||
addAPIServices: []*apiregistration.APIService{},
|
|
||||||
updateAPIServices: []*apiregistration.APIService{},
|
|
||||||
addSyncAPIServices: []*apiregistration.APIService{newAPIService("foo")},
|
|
||||||
delSyncAPIServices: []string{},
|
|
||||||
expectedResults: checkForCreate,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "duplicate AddAPIServiceToSync don't panic",
|
|
||||||
apiServiceName: "foo",
|
|
||||||
addAPIServices: []*apiregistration.APIService{newAutoRegisterManagedAPIService("foo")},
|
|
||||||
updateAPIServices: []*apiregistration.APIService{},
|
|
||||||
addSyncAPIServices: []*apiregistration.APIService{newAutoRegisterManagedAPIService("foo"), newAutoRegisterManagedAPIService("foo")},
|
|
||||||
delSyncAPIServices: []string{},
|
|
||||||
expectedResults: checkForNothing,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "duplicate RemoveAPIServiceToSync don't panic",
|
|
||||||
apiServiceName: "foo",
|
|
||||||
addAPIServices: []*apiregistration.APIService{newAutoRegisterManagedAPIService("foo")},
|
|
||||||
updateAPIServices: []*apiregistration.APIService{},
|
|
||||||
addSyncAPIServices: []*apiregistration.APIService{},
|
|
||||||
delSyncAPIServices: []string{"foo", "foo"},
|
|
||||||
expectedResults: checkForDelete,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "removing auto-manged then RemoveAPIService should not touch APIService",
|
|
||||||
apiServiceName: "foo",
|
|
||||||
addAPIServices: []*apiregistration.APIService{},
|
|
||||||
updateAPIServices: []*apiregistration.APIService{newAPIService("foo")},
|
|
||||||
addSyncAPIServices: []*apiregistration.APIService{},
|
|
||||||
delSyncAPIServices: []string{"foo"},
|
|
||||||
expectedResults: checkForNothing,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "create managed apiservice without a matching request",
|
|
||||||
apiServiceName: "foo",
|
|
||||||
addAPIServices: []*apiregistration.APIService{newAPIService("foo")},
|
|
||||||
updateAPIServices: []*apiregistration.APIService{newAutoRegisterManagedAPIService("foo")},
|
|
||||||
addSyncAPIServices: []*apiregistration.APIService{},
|
|
||||||
delSyncAPIServices: []string{},
|
|
||||||
expectedResults: checkForDelete,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "modifying it should result in stomping",
|
|
||||||
apiServiceName: "foo",
|
|
||||||
addAPIServices: []*apiregistration.APIService{},
|
|
||||||
updateAPIServices: []*apiregistration.APIService{newAutoRegisterManagedModifiedAPIService("foo")},
|
|
||||||
addSyncAPIServices: []*apiregistration.APIService{newAutoRegisterManagedAPIService("foo")},
|
|
||||||
delSyncAPIServices: []string{},
|
|
||||||
expectedResults: checkForUpdate,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
fakeClient := fake.NewSimpleClientset()
|
|
||||||
apiServiceIndexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
|
|
||||||
|
|
||||||
c := autoRegisterController{
|
|
||||||
apiServiceClient: fakeClient.Apiregistration(),
|
|
||||||
apiServiceLister: listers.NewAPIServiceLister(apiServiceIndexer),
|
|
||||||
apiServicesToSync: map[string]*apiregistration.APIService{},
|
|
||||||
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "autoregister"),
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, obj := range test.addAPIServices {
|
|
||||||
apiServiceIndexer.Add(obj)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, obj := range test.updateAPIServices {
|
|
||||||
apiServiceIndexer.Update(obj)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, obj := range test.addSyncAPIServices {
|
|
||||||
c.AddAPIServiceToSync(obj)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, objName := range test.delSyncAPIServices {
|
|
||||||
c.RemoveAPIServiceToSync(objName)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.checkAPIService(test.apiServiceName)
|
|
||||||
|
|
||||||
//compare the expected results
|
|
||||||
err := test.expectedResults(test.apiServiceName, fakeClient)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("%s %v", test.name, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -18,326 +18,215 @@ package autoregister
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
||||||
"k8s.io/apimachinery/pkg/api/meta"
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
clienttesting "k8s.io/client-go/testing"
|
||||||
"k8s.io/apimachinery/pkg/watch"
|
"k8s.io/client-go/tools/cache"
|
||||||
core "k8s.io/client-go/testing"
|
"k8s.io/client-go/util/workqueue"
|
||||||
|
|
||||||
"k8s.io/kube-aggregator/pkg/apis/apiregistration"
|
"k8s.io/kube-aggregator/pkg/apis/apiregistration"
|
||||||
"k8s.io/kube-aggregator/pkg/client/clientset_generated/internalclientset"
|
|
||||||
"k8s.io/kube-aggregator/pkg/client/clientset_generated/internalclientset/fake"
|
"k8s.io/kube-aggregator/pkg/client/clientset_generated/internalclientset/fake"
|
||||||
informers "k8s.io/kube-aggregator/pkg/client/informers/internalversion"
|
listers "k8s.io/kube-aggregator/pkg/client/listers/apiregistration/internalversion"
|
||||||
)
|
)
|
||||||
|
|
||||||
func alwaysReady() bool { return true }
|
func newAutoRegisterManagedAPIService(name string) *apiregistration.APIService {
|
||||||
|
return &apiregistration.APIService{
|
||||||
func waitForNothing(startTime time.Time, client *fake.Clientset) (bool, error) {
|
ObjectMeta: metav1.ObjectMeta{Name: name, Labels: map[string]string{AutoRegisterManagedLabel: string("true")}},
|
||||||
if len(client.Actions()) > 0 {
|
|
||||||
return false, fmt.Errorf("unexpected action: %v", client.Actions())
|
|
||||||
}
|
|
||||||
if time.Now().After(startTime.Add(3 * time.Second)) {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func waitForCreate(name string) func(startTime time.Time, client *fake.Clientset) (bool, error) {
|
|
||||||
return func(startTime time.Time, client *fake.Clientset) (bool, error) {
|
|
||||||
if len(client.Actions()) == 0 {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
if len(client.Actions()) > 1 {
|
|
||||||
return false, fmt.Errorf("unexpected action: %v", client.Actions())
|
|
||||||
}
|
|
||||||
|
|
||||||
action := client.Actions()[0]
|
|
||||||
createAction, ok := action.(core.CreateAction)
|
|
||||||
if !ok {
|
|
||||||
return false, fmt.Errorf("unexpected action: %v", client.Actions())
|
|
||||||
}
|
|
||||||
apiService := createAction.GetObject().(*apiregistration.APIService)
|
|
||||||
if apiService.Name != name || apiService.Labels[AutoRegisterManagedLabel] != "true" {
|
|
||||||
return false, fmt.Errorf("bad name or label %v", createAction)
|
|
||||||
}
|
|
||||||
|
|
||||||
return true, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func waitForUpdate(name string) func(startTime time.Time, client *fake.Clientset) (bool, error) {
|
func newAutoRegisterManagedModifiedAPIService(name string) *apiregistration.APIService {
|
||||||
return func(startTime time.Time, client *fake.Clientset) (bool, error) {
|
return &apiregistration.APIService{
|
||||||
if len(client.Actions()) == 0 {
|
ObjectMeta: metav1.ObjectMeta{Name: name, Labels: map[string]string{AutoRegisterManagedLabel: string("true")}},
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
if len(client.Actions()) > 1 {
|
|
||||||
return false, fmt.Errorf("unexpected action: %v", client.Actions())
|
|
||||||
}
|
|
||||||
|
|
||||||
action := client.Actions()[0]
|
|
||||||
updateAction, ok := action.(core.UpdateAction)
|
|
||||||
if !ok {
|
|
||||||
return false, fmt.Errorf("unexpected action: %v", client.Actions())
|
|
||||||
}
|
|
||||||
apiService := updateAction.GetObject().(*apiregistration.APIService)
|
|
||||||
if apiService.Name != name || apiService.Labels[AutoRegisterManagedLabel] != "true" || apiService.Spec.Group != "" {
|
|
||||||
return false, fmt.Errorf("bad name, label, or group %v", updateAction)
|
|
||||||
}
|
|
||||||
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func waitForDelete(name string) func(startTime time.Time, client *fake.Clientset) (bool, error) {
|
|
||||||
return func(startTime time.Time, client *fake.Clientset) (bool, error) {
|
|
||||||
if len(client.Actions()) == 0 {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// tolerate delete being called multiple times. This happens if the delete fails on missing resource which
|
|
||||||
// happens on an unsynced cache
|
|
||||||
for _, action := range client.Actions() {
|
|
||||||
deleteAction, ok := action.(core.DeleteAction)
|
|
||||||
if !ok {
|
|
||||||
return false, fmt.Errorf("unexpected action: %v", client.Actions())
|
|
||||||
}
|
|
||||||
if deleteAction.GetName() != name {
|
|
||||||
return false, fmt.Errorf("bad name %v", deleteAction)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCheckAPIService(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
|
|
||||||
steps []func(c AutoAPIServiceRegistration, fakeWatch *watch.FakeWatcher, client internalclientset.Interface)
|
|
||||||
expectedResults []func(startTime time.Time, client *fake.Clientset) (bool, error)
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "do nothing",
|
|
||||||
steps: []func(c AutoAPIServiceRegistration, fakeWatch *watch.FakeWatcher, client internalclientset.Interface){
|
|
||||||
// adding an API service which isn't auto-managed does nothing
|
|
||||||
func(c AutoAPIServiceRegistration, fakeWatch *watch.FakeWatcher, client internalclientset.Interface) {
|
|
||||||
fakeWatch.Add(&apiregistration.APIService{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
|
|
||||||
},
|
|
||||||
// removing an auto-sync that doesn't exist should do nothing
|
|
||||||
func(c AutoAPIServiceRegistration, fakeWatch *watch.FakeWatcher, client internalclientset.Interface) {
|
|
||||||
c.RemoveAPIServiceToSync("bar")
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedResults: []func(startTime time.Time, client *fake.Clientset) (bool, error){
|
|
||||||
waitForNothing,
|
|
||||||
waitForNothing,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "simple create and delete",
|
|
||||||
steps: []func(c AutoAPIServiceRegistration, fakeWatch *watch.FakeWatcher, client internalclientset.Interface){
|
|
||||||
// adding one to auto-register should create
|
|
||||||
func(c AutoAPIServiceRegistration, fakeWatch *watch.FakeWatcher, client internalclientset.Interface) {
|
|
||||||
c.AddAPIServiceToSync(&apiregistration.APIService{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
|
|
||||||
},
|
|
||||||
// adding the same item again shouldn't do anything
|
|
||||||
func(c AutoAPIServiceRegistration, fakeWatch *watch.FakeWatcher, client internalclientset.Interface) {
|
|
||||||
c.AddAPIServiceToSync(&apiregistration.APIService{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
|
|
||||||
},
|
|
||||||
// removing entry should delete the API service since its managed
|
|
||||||
func(c AutoAPIServiceRegistration, fakeWatch *watch.FakeWatcher, client internalclientset.Interface) {
|
|
||||||
c.RemoveAPIServiceToSync("foo")
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedResults: []func(startTime time.Time, client *fake.Clientset) (bool, error){
|
|
||||||
waitForCreate("foo"),
|
|
||||||
waitForNothing,
|
|
||||||
waitForDelete("foo"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "create, user manage, then delete",
|
|
||||||
steps: []func(c AutoAPIServiceRegistration, fakeWatch *watch.FakeWatcher, client internalclientset.Interface){
|
|
||||||
// adding one to auto-register should create
|
|
||||||
func(c AutoAPIServiceRegistration, fakeWatch *watch.FakeWatcher, client internalclientset.Interface) {
|
|
||||||
c.AddAPIServiceToSync(&apiregistration.APIService{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
|
|
||||||
},
|
|
||||||
// adding an API service to take ownership shouldn't cause the controller to do anything
|
|
||||||
func(c AutoAPIServiceRegistration, fakeWatch *watch.FakeWatcher, client internalclientset.Interface) {
|
|
||||||
fakeWatch.Modify(&apiregistration.APIService{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
|
|
||||||
},
|
|
||||||
// removing entry should NOT delete the API service since its user owned
|
|
||||||
func(c AutoAPIServiceRegistration, fakeWatch *watch.FakeWatcher, client internalclientset.Interface) {
|
|
||||||
c.RemoveAPIServiceToSync("foo")
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedResults: []func(startTime time.Time, client *fake.Clientset) (bool, error){
|
|
||||||
waitForCreate("foo"),
|
|
||||||
waitForNothing,
|
|
||||||
waitForNothing,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "create managed apiservice without a matching request",
|
|
||||||
steps: []func(c AutoAPIServiceRegistration, fakeWatch *watch.FakeWatcher, client internalclientset.Interface){
|
|
||||||
// adding an API service which isn't auto-managed does nothing
|
|
||||||
func(c AutoAPIServiceRegistration, fakeWatch *watch.FakeWatcher, client internalclientset.Interface) {
|
|
||||||
fakeWatch.Add(&apiregistration.APIService{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
|
|
||||||
},
|
|
||||||
// adding an API service which claims to be managed but isn't should be deleted
|
|
||||||
func(c AutoAPIServiceRegistration, fakeWatch *watch.FakeWatcher, client internalclientset.Interface) {
|
|
||||||
fakeWatch.Modify(&apiregistration.APIService{
|
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
|
||||||
Name: "foo",
|
|
||||||
Labels: map[string]string{AutoRegisterManagedLabel: "true"},
|
|
||||||
}})
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedResults: []func(startTime time.Time, client *fake.Clientset) (bool, error){
|
|
||||||
waitForNothing,
|
|
||||||
waitForDelete("foo"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "modifying it should result in stomping",
|
|
||||||
steps: []func(c AutoAPIServiceRegistration, fakeWatch *watch.FakeWatcher, client internalclientset.Interface){
|
|
||||||
// adding one to auto-register should create
|
|
||||||
func(c AutoAPIServiceRegistration, fakeWatch *watch.FakeWatcher, client internalclientset.Interface) {
|
|
||||||
c.AddAPIServiceToSync(&apiregistration.APIService{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
|
|
||||||
},
|
|
||||||
// updating a managed APIService should result in stomping it
|
|
||||||
func(c AutoAPIServiceRegistration, fakeWatch *watch.FakeWatcher, client internalclientset.Interface) {
|
|
||||||
fakeWatch.Modify(&apiregistration.APIService{
|
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
|
||||||
Name: "foo",
|
|
||||||
Labels: map[string]string{AutoRegisterManagedLabel: "true"},
|
|
||||||
},
|
|
||||||
Spec: apiregistration.APIServiceSpec{
|
Spec: apiregistration.APIServiceSpec{
|
||||||
Group: "something",
|
Group: "something",
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
},
|
}
|
||||||
},
|
|
||||||
expectedResults: []func(startTime time.Time, client *fake.Clientset) (bool, error){
|
func newAPIService(name string) *apiregistration.APIService {
|
||||||
waitForCreate("foo"),
|
return &apiregistration.APIService{
|
||||||
waitForUpdate("foo"),
|
ObjectMeta: metav1.ObjectMeta{Name: name},
|
||||||
},
|
}
|
||||||
},
|
}
|
||||||
}
|
|
||||||
|
func checkForNothing(name string, client *fake.Clientset) error {
|
||||||
NextTest:
|
if len(client.Actions()) > 0 {
|
||||||
for _, test := range tests {
|
return fmt.Errorf("unexpected action: %v", client.Actions())
|
||||||
client := fake.NewSimpleClientset()
|
}
|
||||||
informerFactory := informers.NewSharedInformerFactory(client, 0)
|
|
||||||
fakeWatch := watch.NewFake()
|
return nil
|
||||||
client.PrependWatchReactor("apiservices", core.DefaultWatchReactor(fakeWatch, nil))
|
}
|
||||||
|
|
||||||
c := NewAutoRegisterController(informerFactory.Apiregistration().InternalVersion().APIServices(), client.Apiregistration())
|
func checkForCreate(name string, client *fake.Clientset) error {
|
||||||
|
if len(client.Actions()) == 0 {
|
||||||
stopCh := make(chan struct{})
|
return nil
|
||||||
go informerFactory.Start(stopCh)
|
}
|
||||||
go c.Run(3, stopCh)
|
if len(client.Actions()) > 1 {
|
||||||
|
return fmt.Errorf("unexpected action: %v", client.Actions())
|
||||||
// wait for the initial sync to complete
|
}
|
||||||
err := wait.PollImmediate(10*time.Millisecond, 10*time.Second, func() (bool, error) {
|
|
||||||
return c.apiServiceSynced(), nil
|
action := client.Actions()[0]
|
||||||
})
|
|
||||||
if err != nil {
|
createAction, ok := action.(clienttesting.CreateAction)
|
||||||
t.Errorf("%s %v", test.name, err)
|
if !ok {
|
||||||
close(stopCh)
|
return fmt.Errorf("unexpected action: %v", client.Actions())
|
||||||
continue NextTest
|
}
|
||||||
}
|
apiService := createAction.GetObject().(*apiregistration.APIService)
|
||||||
|
if apiService.Name != name || apiService.Labels[AutoRegisterManagedLabel] != "true" {
|
||||||
for i, step := range test.steps {
|
return fmt.Errorf("bad name or label %v", createAction)
|
||||||
client.ClearActions()
|
}
|
||||||
step(c, fakeWatch, client)
|
|
||||||
|
return nil
|
||||||
startTime := time.Now()
|
}
|
||||||
err := wait.PollImmediate(10*time.Millisecond, 20*time.Second, func() (bool, error) {
|
|
||||||
return test.expectedResults[i](startTime, client)
|
func checkForUpdate(name string, client *fake.Clientset) error {
|
||||||
})
|
if len(client.Actions()) == 0 {
|
||||||
if err != nil {
|
return nil
|
||||||
t.Errorf("%s[%d] %v", test.name, i, err)
|
}
|
||||||
close(stopCh)
|
if len(client.Actions()) > 1 {
|
||||||
continue NextTest
|
return fmt.Errorf("unexpected action: %v", client.Actions())
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure that any create/update/delete is propagated to the watch
|
action := client.Actions()[0]
|
||||||
for _, a := range client.Actions() {
|
updateAction, ok := action.(clienttesting.UpdateAction)
|
||||||
switch action := a.(type) {
|
if !ok {
|
||||||
case core.CreateAction:
|
return fmt.Errorf("unexpected action: %v", client.Actions())
|
||||||
fakeWatch.Add(action.GetObject())
|
}
|
||||||
metadata, err := meta.Accessor(action.GetObject())
|
apiService := updateAction.GetObject().(*apiregistration.APIService)
|
||||||
if err != nil {
|
if apiService.Name != name || apiService.Labels[AutoRegisterManagedLabel] != "true" || apiService.Spec.Group != "" {
|
||||||
t.Fatal(err)
|
return fmt.Errorf("bad name, label, or group %v", updateAction)
|
||||||
}
|
}
|
||||||
err = wait.PollImmediate(10*time.Millisecond, 10*time.Second, func() (bool, error) {
|
|
||||||
if _, err := c.apiServiceLister.Get(metadata.GetName()); err == nil {
|
return nil
|
||||||
return true, nil
|
}
|
||||||
}
|
|
||||||
return false, nil
|
func checkForDelete(name string, client *fake.Clientset) error {
|
||||||
})
|
if len(client.Actions()) == 0 {
|
||||||
if err != nil {
|
return nil
|
||||||
t.Errorf("%s[%d] %v", test.name, i, err)
|
}
|
||||||
close(stopCh)
|
|
||||||
continue NextTest
|
for _, action := range client.Actions() {
|
||||||
}
|
deleteAction, ok := action.(clienttesting.DeleteAction)
|
||||||
|
if !ok {
|
||||||
case core.DeleteAction:
|
return fmt.Errorf("unexpected action: %v", client.Actions())
|
||||||
obj, err := c.apiServiceLister.Get(action.GetName())
|
}
|
||||||
if apierrors.IsNotFound(err) {
|
if deleteAction.GetName() != name {
|
||||||
close(stopCh)
|
return fmt.Errorf("bad name %v", deleteAction)
|
||||||
continue NextTest
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
return nil
|
||||||
}
|
}
|
||||||
fakeWatch.Delete(obj)
|
|
||||||
err = wait.PollImmediate(10*time.Millisecond, 10*time.Second, func() (bool, error) {
|
func TestSync(t *testing.T) {
|
||||||
if _, err := c.apiServiceLister.Get(action.GetName()); apierrors.IsNotFound(err) {
|
tests := []struct {
|
||||||
return true, nil
|
name string
|
||||||
}
|
apiServiceName string
|
||||||
return false, nil
|
addAPIServices []*apiregistration.APIService
|
||||||
})
|
updateAPIServices []*apiregistration.APIService
|
||||||
if err != nil {
|
addSyncAPIServices []*apiregistration.APIService
|
||||||
t.Errorf("%s[%d] %v", test.name, i, err)
|
delSyncAPIServices []string
|
||||||
close(stopCh)
|
expectedResults func(name string, client *fake.Clientset) error
|
||||||
continue NextTest
|
}{
|
||||||
}
|
{
|
||||||
|
name: "adding an API service which isn't auto-managed does nothing",
|
||||||
case core.UpdateAction:
|
apiServiceName: "foo",
|
||||||
fakeWatch.Modify(action.GetObject())
|
addAPIServices: []*apiregistration.APIService{newAPIService("foo")},
|
||||||
metadata, err := meta.Accessor(action.GetObject())
|
updateAPIServices: []*apiregistration.APIService{},
|
||||||
if err != nil {
|
addSyncAPIServices: []*apiregistration.APIService{},
|
||||||
t.Fatal(err)
|
delSyncAPIServices: []string{},
|
||||||
}
|
expectedResults: checkForNothing,
|
||||||
err = wait.PollImmediate(10*time.Millisecond, 10*time.Second, func() (bool, error) {
|
},
|
||||||
obj, err := c.apiServiceLister.Get(metadata.GetName())
|
{
|
||||||
if err != nil {
|
name: "adding one to auto-register should create",
|
||||||
return false, err
|
apiServiceName: "foo",
|
||||||
}
|
addAPIServices: []*apiregistration.APIService{},
|
||||||
if reflect.DeepEqual(obj, action.GetObject()) {
|
updateAPIServices: []*apiregistration.APIService{},
|
||||||
return true, nil
|
addSyncAPIServices: []*apiregistration.APIService{newAPIService("foo")},
|
||||||
}
|
delSyncAPIServices: []string{},
|
||||||
|
expectedResults: checkForCreate,
|
||||||
return false, nil
|
},
|
||||||
})
|
{
|
||||||
if err != nil {
|
name: "duplicate AddAPIServiceToSync don't panic",
|
||||||
t.Errorf("%s[%d] %v", test.name, i, err)
|
apiServiceName: "foo",
|
||||||
close(stopCh)
|
addAPIServices: []*apiregistration.APIService{newAutoRegisterManagedAPIService("foo")},
|
||||||
continue NextTest
|
updateAPIServices: []*apiregistration.APIService{},
|
||||||
}
|
addSyncAPIServices: []*apiregistration.APIService{newAutoRegisterManagedAPIService("foo"), newAutoRegisterManagedAPIService("foo")},
|
||||||
|
delSyncAPIServices: []string{},
|
||||||
}
|
expectedResults: checkForNothing,
|
||||||
}
|
},
|
||||||
}
|
{
|
||||||
|
name: "duplicate RemoveAPIServiceToSync don't panic",
|
||||||
close(stopCh)
|
apiServiceName: "foo",
|
||||||
|
addAPIServices: []*apiregistration.APIService{newAutoRegisterManagedAPIService("foo")},
|
||||||
|
updateAPIServices: []*apiregistration.APIService{},
|
||||||
|
addSyncAPIServices: []*apiregistration.APIService{},
|
||||||
|
delSyncAPIServices: []string{"foo", "foo"},
|
||||||
|
expectedResults: checkForDelete,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "removing auto-manged then RemoveAPIService should not touch APIService",
|
||||||
|
apiServiceName: "foo",
|
||||||
|
addAPIServices: []*apiregistration.APIService{},
|
||||||
|
updateAPIServices: []*apiregistration.APIService{newAPIService("foo")},
|
||||||
|
addSyncAPIServices: []*apiregistration.APIService{},
|
||||||
|
delSyncAPIServices: []string{"foo"},
|
||||||
|
expectedResults: checkForNothing,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "create managed apiservice without a matching request",
|
||||||
|
apiServiceName: "foo",
|
||||||
|
addAPIServices: []*apiregistration.APIService{newAPIService("foo")},
|
||||||
|
updateAPIServices: []*apiregistration.APIService{newAutoRegisterManagedAPIService("foo")},
|
||||||
|
addSyncAPIServices: []*apiregistration.APIService{},
|
||||||
|
delSyncAPIServices: []string{},
|
||||||
|
expectedResults: checkForDelete,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "modifying it should result in stomping",
|
||||||
|
apiServiceName: "foo",
|
||||||
|
addAPIServices: []*apiregistration.APIService{},
|
||||||
|
updateAPIServices: []*apiregistration.APIService{newAutoRegisterManagedModifiedAPIService("foo")},
|
||||||
|
addSyncAPIServices: []*apiregistration.APIService{newAutoRegisterManagedAPIService("foo")},
|
||||||
|
delSyncAPIServices: []string{},
|
||||||
|
expectedResults: checkForUpdate,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
fakeClient := fake.NewSimpleClientset()
|
||||||
|
apiServiceIndexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
|
||||||
|
|
||||||
|
c := autoRegisterController{
|
||||||
|
apiServiceClient: fakeClient.Apiregistration(),
|
||||||
|
apiServiceLister: listers.NewAPIServiceLister(apiServiceIndexer),
|
||||||
|
apiServicesToSync: map[string]*apiregistration.APIService{},
|
||||||
|
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "autoregister"),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, obj := range test.addAPIServices {
|
||||||
|
apiServiceIndexer.Add(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, obj := range test.updateAPIServices {
|
||||||
|
apiServiceIndexer.Update(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, obj := range test.addSyncAPIServices {
|
||||||
|
c.AddAPIServiceToSync(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, objName := range test.delSyncAPIServices {
|
||||||
|
c.RemoveAPIServiceToSync(objName)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.checkAPIService(test.apiServiceName)
|
||||||
|
|
||||||
|
//compare the expected results
|
||||||
|
err := test.expectedResults(test.apiServiceName, fakeClient)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%s %v", test.name, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user