Skip to content

Commit d367e0b

Browse files
authored
Merge pull request kubernetes#130865 from serathius/integration-refactor
Split subfunction to allow adding more subtests
2 parents eec06b4 + 414bfc7 commit d367e0b

File tree

1 file changed

+120
-116
lines changed

1 file changed

+120
-116
lines changed

test/integration/apiserver/apiserver_test.go

Lines changed: 120 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -389,132 +389,136 @@ func TestListOptions(t *testing.T) {
389389

390390
for _, watchCacheEnabled := range []bool{true, false} {
391391
t.Run(fmt.Sprintf("watchCacheEnabled=%t", watchCacheEnabled), func(t *testing.T) {
392-
tCtx := ktesting.Init(t)
393-
prefix := path.Join("/", guuid.New().String(), "registry")
394-
etcdConfig := storagebackend.Config{
395-
Prefix: prefix,
396-
Transport: storagebackend.TransportConfig{ServerList: []string{framework.GetEtcdURL()}},
397-
}
398-
rawClient, kvClient, err := integration.GetEtcdClients(etcdConfig.Transport)
399-
if err != nil {
400-
t.Fatal(err)
401-
}
402-
// kvClient is a wrapper around rawClient and to avoid leaking goroutines we need to
403-
// close the client (which we can do by closing rawClient).
404-
defer func() {
405-
err := rawClient.Close()
406-
if err != nil {
407-
t.Fatal(err)
408-
}
409-
}()
392+
testListOptions(t, watchCacheEnabled)
393+
})
394+
}
395+
}
410396

411-
var compactedRv string
412-
var oldestUncompactedRv int64
413-
for i := 0; i < 15; i++ {
414-
rs := newRS("default")
415-
rs.Name = fmt.Sprintf("test-%d", i)
416-
serializer := protobuf.NewSerializer(nil, nil)
417-
buf := bytes.Buffer{}
418-
err := serializer.Encode(rs, &buf)
419-
if err != nil {
420-
t.Fatal(err)
421-
}
422-
key := prefix + "/replicasets/default/" + rs.Name
397+
func testListOptions(t *testing.T, watchCacheEnabled bool) {
398+
tCtx := ktesting.Init(t)
399+
prefix := path.Join("/", guuid.New().String(), "registry")
400+
etcdConfig := storagebackend.Config{
401+
Prefix: prefix,
402+
Transport: storagebackend.TransportConfig{ServerList: []string{framework.GetEtcdURL()}},
403+
}
404+
rawClient, kvClient, err := integration.GetEtcdClients(etcdConfig.Transport)
405+
if err != nil {
406+
t.Fatal(err)
407+
}
408+
// kvClient is a wrapper around rawClient and to avoid leaking goroutines we need to
409+
// close the client (which we can do by closing rawClient).
410+
defer func() {
411+
err := rawClient.Close()
412+
if err != nil {
413+
t.Fatal(err)
414+
}
415+
}()
423416

424-
resp, err := kvClient.Put(tCtx, key, buf.String())
425-
if err != nil {
426-
t.Fatal(err)
427-
}
428-
if i == 0 {
429-
compactedRv = strconv.FormatInt(resp.Header.Revision, 10) // We compact this first resource version below
430-
}
431-
// delete the first 5, and then compact them
432-
if i < 5 {
433-
if _, err := kvClient.Delete(tCtx, key); err != nil {
434-
t.Fatal(err)
435-
}
436-
oldestUncompactedRv = resp.Header.Revision
437-
}
438-
}
439-
_, err = kvClient.Compact(tCtx, int64(oldestUncompactedRv))
440-
if err != nil {
417+
var compactedRv string
418+
var oldestUncompactedRv int64
419+
for i := 0; i < 15; i++ {
420+
rs := newRS("default")
421+
rs.Name = fmt.Sprintf("test-%d", i)
422+
serializer := protobuf.NewSerializer(nil, nil)
423+
buf := bytes.Buffer{}
424+
err := serializer.Encode(rs, &buf)
425+
if err != nil {
426+
t.Fatal(err)
427+
}
428+
key := prefix + "/replicasets/default/" + rs.Name
429+
430+
resp, err := kvClient.Put(tCtx, key, buf.String())
431+
if err != nil {
432+
t.Fatal(err)
433+
}
434+
if i == 0 {
435+
compactedRv = strconv.FormatInt(resp.Header.Revision, 10) // We compact this first resource version below
436+
}
437+
// delete the first 5, and then compact them
438+
if i < 5 {
439+
if _, err := kvClient.Delete(tCtx, key); err != nil {
441440
t.Fatal(err)
442441
}
442+
oldestUncompactedRv = resp.Header.Revision
443+
}
444+
}
445+
_, err = kvClient.Compact(tCtx, int64(oldestUncompactedRv))
446+
if err != nil {
447+
t.Fatal(err)
448+
}
443449

444-
clientSet, _, tearDownFn := framework.StartTestServer(tCtx, t, framework.TestServerSetup{
445-
ModifyServerRunOptions: func(opts *options.ServerRunOptions) {
446-
opts.Etcd.EnableWatchCache = watchCacheEnabled
447-
opts.Etcd.StorageConfig = etcdConfig
448-
},
449-
})
450-
defer tearDownFn()
450+
clientSet, _, tearDownFn := framework.StartTestServer(tCtx, t, framework.TestServerSetup{
451+
ModifyServerRunOptions: func(opts *options.ServerRunOptions) {
452+
opts.Etcd.EnableWatchCache = watchCacheEnabled
453+
opts.Etcd.StorageConfig = etcdConfig
454+
},
455+
})
456+
defer tearDownFn()
451457

452-
rsClient := clientSet.AppsV1().ReplicaSets("default")
458+
rsClient := clientSet.AppsV1().ReplicaSets("default")
453459

454-
listObj, err := rsClient.List(tCtx, metav1.ListOptions{
455-
Limit: 6,
456-
})
457-
if err != nil {
458-
t.Fatalf("unexpected error: %v", err)
459-
}
460-
validContinueToken := listObj.Continue
461-
462-
// test all combinations of these, for both watch cache enabled and disabled:
463-
limits := []int64{0, 6}
464-
continueTokens := []string{"", validContinueToken, invalidContinueToken}
465-
rvs := []string{"", "0", compactedRv, invalidResourceVersion}
466-
rvMatches := []metav1.ResourceVersionMatch{
467-
"",
468-
metav1.ResourceVersionMatchNotOlderThan,
469-
metav1.ResourceVersionMatchExact,
470-
invalidResourceVersionMatch,
471-
}
472-
473-
for _, limit := range limits {
474-
for _, continueToken := range continueTokens {
475-
for _, rv := range rvs {
476-
for _, rvMatch := range rvMatches {
477-
rvName := ""
478-
switch rv {
479-
case "":
480-
rvName = "empty"
481-
case "0":
482-
rvName = "0"
483-
case compactedRv:
484-
rvName = "compacted"
485-
case invalidResourceVersion:
486-
rvName = "invalid"
487-
default:
488-
rvName = "unknown"
489-
}
490-
491-
continueName := ""
492-
switch continueToken {
493-
case "":
494-
continueName = "empty"
495-
case validContinueToken:
496-
continueName = "valid"
497-
case invalidContinueToken:
498-
continueName = "invalid"
499-
default:
500-
continueName = "unknown"
501-
}
502-
503-
name := fmt.Sprintf("limit=%d continue=%s rv=%s rvMatch=%s", limit, continueName, rvName, rvMatch)
504-
t.Run(name, func(t *testing.T) {
505-
opts := metav1.ListOptions{
506-
ResourceVersion: rv,
507-
ResourceVersionMatch: rvMatch,
508-
Continue: continueToken,
509-
Limit: limit,
510-
}
511-
testListOptionsCase(t, rsClient, watchCacheEnabled, opts, compactedRv)
512-
})
513-
}
460+
listObj, err := rsClient.List(tCtx, metav1.ListOptions{
461+
Limit: 6,
462+
})
463+
if err != nil {
464+
t.Fatalf("unexpected error: %v", err)
465+
}
466+
validContinueToken := listObj.Continue
467+
468+
// test all combinations of these, for both watch cache enabled and disabled:
469+
limits := []int64{0, 6}
470+
continueTokens := []string{"", validContinueToken, invalidContinueToken}
471+
rvs := []string{"", "0", compactedRv, invalidResourceVersion}
472+
rvMatches := []metav1.ResourceVersionMatch{
473+
"",
474+
metav1.ResourceVersionMatchNotOlderThan,
475+
metav1.ResourceVersionMatchExact,
476+
invalidResourceVersionMatch,
477+
}
478+
479+
for _, limit := range limits {
480+
for _, continueToken := range continueTokens {
481+
for _, rv := range rvs {
482+
for _, rvMatch := range rvMatches {
483+
rvName := ""
484+
switch rv {
485+
case "":
486+
rvName = "empty"
487+
case "0":
488+
rvName = "0"
489+
case compactedRv:
490+
rvName = "compacted"
491+
case invalidResourceVersion:
492+
rvName = "invalid"
493+
default:
494+
rvName = "unknown"
495+
}
496+
497+
continueName := ""
498+
switch continueToken {
499+
case "":
500+
continueName = "empty"
501+
case validContinueToken:
502+
continueName = "valid"
503+
case invalidContinueToken:
504+
continueName = "invalid"
505+
default:
506+
continueName = "unknown"
514507
}
508+
509+
name := fmt.Sprintf("limit=%d continue=%s rv=%s rvMatch=%s", limit, continueName, rvName, rvMatch)
510+
t.Run(name, func(t *testing.T) {
511+
opts := metav1.ListOptions{
512+
ResourceVersion: rv,
513+
ResourceVersionMatch: rvMatch,
514+
Continue: continueToken,
515+
Limit: limit,
516+
}
517+
testListOptionsCase(t, rsClient, watchCacheEnabled, opts, compactedRv)
518+
})
515519
}
516520
}
517-
})
521+
}
518522
}
519523
}
520524

0 commit comments

Comments
 (0)