Skip to content

Commit

Permalink
test: fix flaky test in ClusterMachineConfigStatus unit tests
Browse files Browse the repository at this point in the history
The test `TestSchematicChanges` was flaky, as it was doing the following:

1. Trigger one or more `factory.talos.dev` build image generations & talos node upgrades by modifying resources
2. Capture these upgrade calls
3. Assert the build image in these calls (to be one or more with, always the expected image)
4. Clear captured upgrade calls
5. Trigger one or more `ghcr.io` build image generations & talos node upgrades by modifying resources
6. Capture these upgrade calls
7. Assert the build image in these new calls (to be one or more with, always the expected image)

The problem was, between step 4 and 7, an old `factory.talos.dev` build image could sometimes still be captured due to the nature of controllers asynchronously reconciling resources.

Replace this logic by not clearing the upgrade requests at all, instead, simply ignoring the previous image's preceding captured requests in step 7.

Signed-off-by: Utku Ozdemir <utku.ozdemir@siderolabs.com>
  • Loading branch information
utkuozdemir committed Jun 7, 2024
1 parent b7d48aa commit 0965091
Showing 1 changed file with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/cosi-project/runtime/pkg/resource/rtestutils"
"github.com/cosi-project/runtime/pkg/safe"
"github.com/google/uuid"
"github.com/siderolabs/gen/xslices"
"github.com/siderolabs/go-retry/retry"
"github.com/siderolabs/talos/pkg/machinery/api/machine"
"github.com/siderolabs/talos/pkg/machinery/resources/runtime"
Expand Down Expand Up @@ -530,24 +531,23 @@ func (suite *ClusterMachineConfigStatusSuite) TestSchematicChanges() {
suite.Require().NoError(err)
}

expectedFactoryImage := "factory.talos.dev/installer/bbbb:v1.3.0"

suite.Require().NoError(retry.Constant(time.Second * 5).Retry(func() error {
requests := suite.machineService.getUpgradeRequests()
if len(requests) == 0 {
return retry.ExpectedErrorf("no upgrade requests received")
}

expectedImage := "factory.talos.dev/installer/bbbb:v1.3.0"
for i, r := range requests {
if r.Image != expectedImage {
return fmt.Errorf("%d request image is invalid: expected %q got %q", i, expectedImage, r.Image)
if r.Image != expectedFactoryImage {
return fmt.Errorf("%d request image is invalid: expected %q got %q", i, expectedFactoryImage, r.Image)
}
}

return nil
}))

suite.machineService.clearUpgradeRequests()

// check fallback to ghcr image if the schematic is invalid
for _, m := range machines {
machineStatus := omni.NewMachineStatus(resources.DefaultNamespace, m.Metadata().ID())
Expand All @@ -561,16 +561,29 @@ func (suite *ClusterMachineConfigStatusSuite) TestSchematicChanges() {
suite.Require().NoError(err)
}

trimLeadingImages := func(images []string, trim string) []string {
for i, image := range images {
if image != trim {
return images[i:]
}
}

return nil
}

suite.Require().NoError(retry.Constant(time.Second * 5).Retry(func() error {
requests := suite.machineService.getUpgradeRequests()
if len(requests) == 0 {
return retry.ExpectedErrorf("no upgrade requests received")
images := xslices.Map(requests, func(r *machine.UpgradeRequest) string { return r.Image })
trimmedImages := trimLeadingImages(images, expectedFactoryImage)

if len(trimmedImages) == 0 {
return retry.ExpectedErrorf("no new upgrade requests received")
}

expectedImage := "ghcr.io/siderolabs/installer:v1.3.0"
for i, r := range requests {
if r.Image != expectedImage {
return fmt.Errorf("%d request image is invalid: expected %q got %q", i, expectedImage, r.Image)
for i, image := range trimmedImages {
if image != expectedImage {
return fmt.Errorf("%d request image is invalid: expected %q got %q", i, expectedImage, image)
}
}

Expand All @@ -579,6 +592,8 @@ func (suite *ClusterMachineConfigStatusSuite) TestSchematicChanges() {
}

func (suite *ClusterMachineConfigStatusSuite) TestSecureBootInstallImage() {
suite.T().Cleanup(suite.machineService.clearUpgradeRequests)

suite.startRuntime()

suite.Require().NoError(suite.machineService.state.Create(suite.ctx, runtime.NewSecurityStateSpec(runtime.NamespaceName)))
Expand Down Expand Up @@ -658,8 +673,6 @@ func (suite *ClusterMachineConfigStatusSuite) TestSecureBootInstallImage() {

return nil
}))

suite.machineService.clearUpgradeRequests()
}

func (suite *ClusterMachineConfigStatusSuite) TestGenerationErrorPropagation() {
Expand Down

0 comments on commit 0965091

Please sign in to comment.