From 7dac216e6d03918f21ceee5f8ab53deaaa966783 Mon Sep 17 00:00:00 2001 From: Antoine Eiche Date: Mon, 11 Mar 2024 20:20:09 +0100 Subject: [PATCH] Fix optionnal the comin.machineId value It was actually previously required by comin at evaluation time, even if not specified in the comin service. --- internal/generation/generation.go | 4 ++-- internal/manager/manager_test.go | 37 ++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/internal/generation/generation.go b/internal/generation/generation.go index bea361a..caa0b7f 100644 --- a/internal/generation/generation.go +++ b/internal/generation/generation.go @@ -102,8 +102,8 @@ func (g Generation) Eval(ctx context.Context) (result chan EvalResult) { if err == nil { evaluationResult.DrvPath = drvPath evaluationResult.OutPath = outPath - if g.machineId != "" && g.machineId != machineId { - evaluationResult.Err = fmt.Errorf("The defined comin.machineId (%s) is different to the machine id (%s) of this machine", + if machineId != "" && g.machineId != machineId { + evaluationResult.Err = fmt.Errorf("The evaluated comin.machineId '%s' is different from the /etc/machine-id '%s' of this machine", g.machineId, machineId) } } else { diff --git a/internal/manager/manager_test.go b/internal/manager/manager_test.go index d9f37fd..5639433 100644 --- a/internal/manager/manager_test.go +++ b/internal/manager/manager_test.go @@ -34,7 +34,7 @@ func TestRun(t *testing.T) { buildDone := make(chan struct{}) nixEvalMock := func(ctx context.Context, repositoryPath string, hostname string) (string, string, string, error) { <-evalDone - return "drv-path", "out-path", "machine-id", nil + return "drv-path", "out-path", "", nil } nixBuildMock := func(ctx context.Context, drvPath string) error { <-buildDone @@ -123,6 +123,38 @@ func TestRestartComin(t *testing.T) { } +func TestOptionnalMachineId(t *testing.T) { + logrus.SetLevel(logrus.DebugLevel) + r := newRepositoryMock() + m := New(r, "", "", "the-test-machine-id") + + evalDone := make(chan struct{}) + buildDone := make(chan struct{}) + nixEvalMock := func(ctx context.Context, repositoryPath string, hostname string) (string, string, string, error) { + <-evalDone + // When comin.machineId is empty, comin evaluates it as an empty string + evaluatedMachineId := "" + return "drv-path", "out-path", evaluatedMachineId, nil + } + nixBuildMock := func(ctx context.Context, drvPath string) error { + <-buildDone + return nil + } + m.evalFunc = nixEvalMock + m.buildFunc = nixBuildMock + + go m.Run() + m.Fetch("origin") + r.rsCh <- repository.RepositoryStatus{SelectedCommitId: "foo"} + + // we simulate the end of the evaluation + close(evalDone) + + assert.EventuallyWithT(t, func(c *assert.CollectT) { + assert.True(t, m.GetState().IsRunning) + }, 5*time.Second, 100*time.Millisecond, "evaluation is not finished") +} + func TestIncorrectMachineId(t *testing.T) { logrus.SetLevel(logrus.DebugLevel) r := newRepositoryMock() @@ -156,8 +188,7 @@ func TestIncorrectMachineId(t *testing.T) { close(evalDone) assert.EventuallyWithT(t, func(c *assert.CollectT) { - // The manager is now longer running since the machine id is not correct in the generation. + // The manager is no longer running since the machine id are not identical assert.False(t, m.GetState().IsRunning) }, 5*time.Second, 100*time.Millisecond, "evaluation is not finished") - }