Skip to content

Commit

Permalink
Clean shutdown of flowcontrol integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtek-t committed Jun 15, 2022
1 parent 57242e3 commit 6b59525
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 34 deletions.
46 changes: 17 additions & 29 deletions test/integration/apiserver/flowcontrol/concurrency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"fmt"
"io"
"net/http/httptest"
"strings"
"sync"
"testing"
Expand All @@ -31,14 +30,13 @@ import (

flowcontrol "k8s.io/api/flowcontrol/v1beta2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
genericfeatures "k8s.io/apiserver/pkg/features"
utilfeature "k8s.io/apiserver/pkg/util/feature"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
featuregatetesting "k8s.io/component-base/featuregate/testing"
"k8s.io/kubernetes/pkg/controlplane"
"k8s.io/kubernetes/cmd/kube-apiserver/app/options"
"k8s.io/kubernetes/test/integration/framework"
)

Expand All @@ -50,33 +48,27 @@ const (
timeout = time.Second * 10
)

func setup(t testing.TB, maxReadonlyRequestsInFlight, MaxMutatingRequestsInFlight int) (*httptest.Server, *rest.Config, framework.CloseFunc) {
opts := framework.ControlPlaneConfigOptions{EtcdOptions: framework.DefaultEtcdOptions()}
opts.EtcdOptions.DefaultStorageMediaType = "application/vnd.kubernetes.protobuf"
controlPlaneConfig := framework.NewIntegrationTestControlPlaneConfigWithOptions(&opts)
resourceConfig := controlplane.DefaultAPIResourceConfigSource()
resourceConfig.EnableVersions(schema.GroupVersion{
Group: "flowcontrol.apiserver.k8s.io",
Version: "v1alpha1",
func setup(t testing.TB, maxReadonlyRequestsInFlight, MaxMutatingRequestsInFlight int) (*rest.Config, framework.TearDownFunc) {
_, kubeConfig, tearDownFn := framework.StartTestServer(t, framework.TestServerSetup{
ModifyServerRunOptions: func(opts *options.ServerRunOptions) {
// Ensure all clients are allowed to send requests.
opts.Authorization.Modes = []string{"AlwaysAllow"}
opts.GenericServerRunOptions.MaxRequestsInFlight = maxReadonlyRequestsInFlight
opts.GenericServerRunOptions.MaxMutatingRequestsInFlight = MaxMutatingRequestsInFlight
},
})
controlPlaneConfig.GenericConfig.MaxRequestsInFlight = maxReadonlyRequestsInFlight
controlPlaneConfig.GenericConfig.MaxMutatingRequestsInFlight = MaxMutatingRequestsInFlight
controlPlaneConfig.GenericConfig.OpenAPIConfig = framework.DefaultOpenAPIConfig()
controlPlaneConfig.ExtraConfig.APIResourceConfigSource = resourceConfig
_, s, closeFn := framework.RunAnAPIServer(controlPlaneConfig)

return s, controlPlaneConfig.GenericConfig.LoopbackClientConfig, closeFn
return kubeConfig, tearDownFn
}

func TestPriorityLevelIsolation(t *testing.T) {
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, genericfeatures.APIPriorityAndFairness, true)()
// NOTE: disabling the feature should fail the test
_, loopbackConfig, closeFn := setup(t, 1, 1)
kubeConfig, closeFn := setup(t, 1, 1)
defer closeFn()

loopbackClient := clientset.NewForConfigOrDie(loopbackConfig)
noxu1Client := getClientFor(loopbackConfig, "noxu1")
noxu2Client := getClientFor(loopbackConfig, "noxu2")
loopbackClient := clientset.NewForConfigOrDie(kubeConfig)
noxu1Client := getClientFor(kubeConfig, "noxu1")
noxu2Client := getClientFor(kubeConfig, "noxu2")

queueLength := 50
concurrencyShares := 1
Expand Down Expand Up @@ -153,13 +145,9 @@ func TestPriorityLevelIsolation(t *testing.T) {
}

func getClientFor(loopbackConfig *rest.Config, username string) clientset.Interface {
config := &rest.Config{
Host: loopbackConfig.Host,
QPS: -1,
BearerToken: loopbackConfig.BearerToken,
Impersonate: rest.ImpersonationConfig{
UserName: username,
},
config := rest.CopyConfig(loopbackConfig)
config.Impersonate = rest.ImpersonationConfig{
UserName: username,
}
return clientset.NewForConfigOrDie(config)
}
Expand Down
4 changes: 2 additions & 2 deletions test/integration/apiserver/flowcontrol/fight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ func (ft *fightTest) evaluate(tBeforeCreate, tAfterCreate time.Time) {
}
func TestConfigConsumerFight(t *testing.T) {
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, genericfeatures.APIPriorityAndFairness, true)()
_, loopbackConfig, closeFn := setup(t, 100, 100)
kubeConfig, closeFn := setup(t, 100, 100)
defer closeFn()
const teamSize = 3
ft := newFightTest(t, loopbackConfig, teamSize)
ft := newFightTest(t, kubeConfig, teamSize)
tBeforeCreate := time.Now()
ft.createMainInformer()
ft.foreach(ft.createController)
Expand Down
4 changes: 2 additions & 2 deletions test/integration/apiserver/flowcontrol/fs_condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ import (
func TestConditionIsolation(t *testing.T) {
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, genericfeatures.APIPriorityAndFairness, true)()
// NOTE: disabling the feature should fail the test
_, loopbackConfig, closeFn := setup(t, 10, 10)
kubeConfig, closeFn := setup(t, 10, 10)
defer closeFn()

loopbackClient := clientset.NewForConfigOrDie(loopbackConfig)
loopbackClient := clientset.NewForConfigOrDie(kubeConfig)

stopCh := make(chan struct{})
defer close(stopCh)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/framework/test_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type TestServerSetup struct {
type TearDownFunc func()

// StartTestServer runs a kube-apiserver, optionally calling out to the setup.ModifyServerRunOptions and setup.ModifyServerConfig functions
func StartTestServer(t *testing.T, setup TestServerSetup) (client.Interface, *rest.Config, TearDownFunc) {
func StartTestServer(t testing.TB, setup TestServerSetup) (client.Interface, *rest.Config, TearDownFunc) {
certDir, err := os.MkdirTemp("", "test-integration-"+strings.ReplaceAll(t.Name(), "/", "_"))
if err != nil {
t.Fatalf("Couldn't create temp dir: %v", err)
Expand Down

0 comments on commit 6b59525

Please sign in to comment.