Skip to content

Commit 5c4e983

Browse files
author
Dmitriy Matrenichev
committed
fix: restore timeout in OmniSuite.SetupTest
The change from `context.WithTimeout` was unintentional, so lets revert that. Signed-off-by: Dmitriy Matrenichev <[email protected]>
1 parent 72405c7 commit 5c4e983

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

internal/backend/runtime/omni/controllers/omni/config_patch_cleanup_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package omni_test
77

88
import (
9+
"context"
910
"testing"
1011
"testing/synctest"
1112
"time"
@@ -27,6 +28,7 @@ type ConfigPatchCleanupSuite struct {
2728
}
2829

2930
func (suite *ConfigPatchCleanupSuite) SetupTest() {
31+
suite.OmniSuite.ctx, suite.OmniSuite.ctxCancel = context.WithCancel(context.Background()) //nolint:fatcontext
3032
suite.OmniSuite.disableConnections = true
3133
suite.OmniSuite.SetupTest()
3234
suite.startRuntime()

internal/backend/runtime/omni/controllers/omni/etcd_backup.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/cosi-project/runtime/pkg/resource"
1818
"github.com/cosi-project/runtime/pkg/safe"
1919
"github.com/cosi-project/runtime/pkg/state"
20+
"github.com/dustin/go-humanize"
2021
"github.com/siderolabs/gen/channel"
2122
"github.com/siderolabs/gen/containers"
2223
machineapi "github.com/siderolabs/talos/pkg/machinery/api/machine"
@@ -387,6 +388,8 @@ func (ctrl *EtcdBackupController) doBackup(
387388
return fmt.Errorf("failed to get store: %w", err)
388389
}
389390

391+
crdr := &countingReader{rdr: rdr}
392+
390393
if err := st.Upload(
391394
ctx,
392395
etcdbackup.Description{
@@ -399,7 +402,7 @@ func (ctrl *EtcdBackupController) doBackup(
399402
EncryptionKey: backupData.TypedSpec().Value.EncryptionKey,
400403
},
401404
},
402-
rdr,
405+
crdr,
403406
); err != nil {
404407
return fmt.Errorf("failed to upload etcd snapshot for cluster: %w", err)
405408
}
@@ -410,6 +413,7 @@ func (ctrl *EtcdBackupController) doBackup(
410413
"uploaded etcd snapshot",
411414
zap.String("cluster_uuid", backupData.TypedSpec().Value.ClusterUuid),
412415
zap.String("snapshot_name", etcdbackup.CreateSnapshotName(now)),
416+
zap.String("snapshot_size", humanize.IBytes(uint64(crdr.total))),
413417
zap.Time("ts", now),
414418
)
415419

@@ -460,3 +464,17 @@ func (ctrl *EtcdBackupController) updateBackupStatus(
460464
type TalosClient interface {
461465
EtcdSnapshot(ctx context.Context, req *machineapi.EtcdSnapshotRequest, callOptions ...grpc.CallOption) (io.ReadCloser, error)
462466
}
467+
468+
type countingReader struct {
469+
rdr io.Reader
470+
total int
471+
}
472+
473+
func (r *countingReader) Read(p []byte) (int, error) {
474+
n, err := r.rdr.Read(p)
475+
if n > 0 {
476+
r.total += n
477+
}
478+
479+
return n, err
480+
}

internal/backend/runtime/omni/controllers/omni/etcd_backup_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ import (
5757
func TestEtcdBackupControllerSuite(t *testing.T) {
5858
t.Parallel()
5959

60-
synctest.Run(func() {
61-
suite.Run(t, new(EtcdBackupControllerSuite))
62-
})
60+
synctest.Run(func() { suite.Run(t, new(EtcdBackupControllerSuite)) })
6361
}
6462

6563
const description = "test"
@@ -82,6 +80,8 @@ func (suite *EtcdBackupControllerSuite) register2(ctrl controller.Controller, er
8280
}
8381

8482
func (suite *EtcdBackupControllerSuite) SetupTest() {
83+
suite.OmniSuite.ctx, suite.OmniSuite.ctxCancel = context.WithCancel(context.Background()) //nolint:fatcontext
84+
8585
suite.OmniSuite.disableConnections = true
8686

8787
suite.OmniSuite.SetupTest()
@@ -118,7 +118,7 @@ func (suite *EtcdBackupControllerSuite) TestEtcdBackup() {
118118
clustersData := createClusters(suite, clusterNames, time.Hour)
119119
start := time.Now()
120120

121-
time.Sleep(11 * time.Minute)
121+
// Wait for the first backup to be created.
122122
synctest.Wait()
123123

124124
rtestutils.AssertResources(

internal/backend/runtime/omni/controllers/omni/omni_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,15 @@ func (suite *OmniSuite) newServerWithTalosVersion(suffix, talosVersion string) (
406406
}
407407

408408
func (suite *OmniSuite) SetupTest() {
409-
suite.ctx, suite.ctxCancel = context.WithCancel(suite.T().Context())
409+
if suite.ctx == nil {
410+
// This check is important, because some of our tests use synctest package, which
411+
// "mocks" the time, and advances it in multiple hours. Because the context is
412+
// created inside the "bubble" when 20 seconds elapse, the suite will shut down
413+
// before the test is actually finished. There are two test suites that currently
414+
// need their separate contexts: [ConfigPatchCleanupSuite] and
415+
// [EtcdBackupControllerSuite] in which we create context explicitly.
416+
suite.ctx, suite.ctxCancel = context.WithTimeout(suite.T().Context(), 20*time.Second)
417+
}
410418

411419
suite.stateBuilder = dynamicStateBuilder{m: map[resource.Namespace]state.CoreState{}}
412420

@@ -484,6 +492,8 @@ func (suite *OmniSuite) TearDownTest() {
484492
for _, s := range suite.grpcServers {
485493
s.Stop()
486494
}
495+
496+
suite.ctx, suite.ctxCancel = nil, nil
487497
}
488498

489499
func assertResource[R rtestutils.ResourceWithRD](suite *OmniSuite, md interface{ ID() resource.ID }, assertionFunc func(r R, assertion *assert.Assertions)) {

0 commit comments

Comments
 (0)