Skip to content

Commit ac67904

Browse files
committed
Move TestListOptions setup before we start API server
This PR changes the TestListOptions setup to execute compaction before we start apiserver allowing the test work with cache snapshots from KEP-4988 by preventing creation of snapshots for compacted revisions. While etcd compaction removes access to old revision, with KEP-4988 those revisions will be still available in watch cache. Implementing compaction for watch cache doesn't make sense as it would only be used for testing, making it unreliable. To properly test how etcd and watch cache behaves on compacted revisions we need to compact etcd before we start apiserver.
1 parent e0ab1a1 commit ac67904

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

test/integration/apiserver/apiserver_test.go

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"testing"
3434
"time"
3535

36+
guuid "github.com/google/uuid"
3637
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
3738
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
3839
apps "k8s.io/api/apps/v1"
@@ -389,59 +390,66 @@ func TestListOptions(t *testing.T) {
389390
for _, watchCacheEnabled := range []bool{true, false} {
390391
t.Run(fmt.Sprintf("watchCacheEnabled=%t", watchCacheEnabled), func(t *testing.T) {
391392
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+
}()
392410

393-
var storageTransport *storagebackend.TransportConfig
394-
clientSet, _, tearDownFn := framework.StartTestServer(tCtx, t, framework.TestServerSetup{
395-
ModifyServerRunOptions: func(opts *options.ServerRunOptions) {
396-
opts.Etcd.EnableWatchCache = watchCacheEnabled
397-
storageTransport = &opts.Etcd.StorageConfig.Transport
398-
},
399-
})
400-
defer tearDownFn()
401-
402-
ns := framework.CreateNamespaceOrDie(clientSet, "list-options", t)
403-
defer framework.DeleteNamespaceOrDie(clientSet, ns, t)
404-
405-
rsClient := clientSet.AppsV1().ReplicaSets(ns.Name)
406-
407-
var compactedRv, oldestUncompactedRv string
411+
var compactedRv string
412+
var oldestUncompactedRv int64
408413
for i := 0; i < 15; i++ {
409-
rs := newRS(ns.Name)
414+
rs := newRS("default")
410415
rs.Name = fmt.Sprintf("test-%d", i)
411-
created, err := rsClient.Create(tCtx, rs, metav1.CreateOptions{})
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
423+
424+
resp, err := kvClient.Put(tCtx, key, buf.String())
412425
if err != nil {
413426
t.Fatal(err)
414427
}
415428
if i == 0 {
416-
compactedRv = created.ResourceVersion // We compact this first resource version below
429+
compactedRv = strconv.FormatInt(resp.Header.Revision, 10) // We compact this first resource version below
417430
}
418431
// delete the first 5, and then compact them
419432
if i < 5 {
420-
var zero int64
421-
if err := rsClient.Delete(tCtx, rs.Name, metav1.DeleteOptions{GracePeriodSeconds: &zero}); err != nil {
433+
if _, err := kvClient.Delete(tCtx, key); err != nil {
422434
t.Fatal(err)
423435
}
424-
oldestUncompactedRv = created.ResourceVersion
436+
oldestUncompactedRv = resp.Header.Revision
425437
}
426438
}
427-
428-
// compact some of the revision history in etcd so we can test "too old" resource versions
429-
rawClient, kvClient, err := integration.GetEtcdClients(*storageTransport)
439+
_, err = kvClient.Compact(tCtx, int64(oldestUncompactedRv))
430440
if err != nil {
431441
t.Fatal(err)
432442
}
433-
// kvClient is a wrapper around rawClient and to avoid leaking goroutines we need to
434-
// close the client (which we can do by closing rawClient).
435-
defer rawClient.Close()
436443

437-
revision, err := strconv.Atoi(oldestUncompactedRv)
438-
if err != nil {
439-
t.Fatal(err)
440-
}
441-
_, err = kvClient.Compact(tCtx, int64(revision))
442-
if err != nil {
443-
t.Fatal(err)
444-
}
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()
451+
452+
rsClient := clientSet.AppsV1().ReplicaSets("default")
445453

446454
listObj, err := rsClient.List(tCtx, metav1.ListOptions{
447455
Limit: 6,

0 commit comments

Comments
 (0)