Skip to content

Commit

Permalink
use full hash on foreach instances and fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
wildum committed Jan 27, 2025
1 parent a5a204e commit e16e3e3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
3 changes: 2 additions & 1 deletion internal/runtime/foreach_stringer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"
"time"

"github.com/grafana/alloy/internal/featuregate"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -30,7 +31,7 @@ func TestForeachStringer(t *testing.T) {
func testConfigForEachStringer(t *testing.T, config string, expectedDebugInfo string) {
defer verifyNoGoroutineLeaks(t)
reg := prometheus.NewRegistry()
ctrl, f := setup(t, config, reg)
ctrl, f := setup(t, config, reg, featuregate.StabilityExperimental)

err := ctrl.LoadSource(f, nil, "")
require.NoError(t, err)
Expand Down
3 changes: 1 addition & 2 deletions internal/runtime/internal/controller/node_config_foreach.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,7 @@ func (fi *forEachChild) Equals(other runner.Task) bool {
func computeHash(s string) string {
hasher := sha256.New()
hasher.Write([]byte(s))
fullHash := hasher.Sum(nil)
return hex.EncodeToString(fullHash[:12]) // taking only the 12 first char of the hash should be enough
return hex.EncodeToString(hasher.Sum(nil))
}

func hashObject(obj any) string {
Expand Down
10 changes: 5 additions & 5 deletions internal/runtime/internal/controller/node_config_foreach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ func TestCreateCustomComponentsCollectionObjectsWithUpdate(t *testing.T) {
}
require.NoError(t, foreachConfigNode.Evaluate(vm.NewScope(vars)))
customComponentIds := foreachConfigNode.moduleController.(*ModuleControllerMock).CustomComponents
require.ElementsMatch(t, customComponentIds, []string{"foreach_be19d02a2ccb2cbc2c47e90d_1", "foreach_b335d50e2e8490eb8bf5f51b_1"})
require.ElementsMatch(t, customComponentIds, []string{"foreach_be19d02a2ccb2cbc2c47e90dcad8446a50459577449624176398d1f2aa6cd23a_1", "foreach_b335d50e2e8490eb8bf5f51b3ca8b1599d811514ca40d28ada5214294d49752d_1"})
keys := make([]string, 0, len(foreachConfigNode.customComponents))
for key := range foreachConfigNode.customComponents {
keys = append(keys, key)
}
require.ElementsMatch(t, keys, []string{"foreach_be19d02a2ccb2cbc2c47e90d_1", "foreach_b335d50e2e8490eb8bf5f51b_1"})
require.ElementsMatch(t, keys, []string{"foreach_be19d02a2ccb2cbc2c47e90dcad8446a50459577449624176398d1f2aa6cd23a_1", "foreach_b335d50e2e8490eb8bf5f51b3ca8b1599d811514ca40d28ada5214294d49752d_1"})

newConfig := `foreach "default" {
collection = [obj1, obj3]
Expand All @@ -216,14 +216,14 @@ func TestCreateCustomComponentsCollectionObjectsWithUpdate(t *testing.T) {
customComponentIds = foreachConfigNode.moduleController.(*ModuleControllerMock).CustomComponents

// Create only the custom component for the obj3 because the one for obj1 was already created
require.ElementsMatch(t, customComponentIds, []string{"foreach_1464766cf9c8fd1095d0f7a2_1"})
require.ElementsMatch(t, customComponentIds, []string{"foreach_1464766cf9c8fd1095d0f7a22abe0632b6a6d44c3eeae65766086350eef3ac33_1"})

// "foreachb335d50e2e8490eb8bf5f51b1" was removed, "foreach1464766cf9c8fd1095d0f7a21" was added
// "foreach_b335d50e2e8490eb8bf5f51b3ca8b1599d811514ca40d28ada5214294d49752d_1" was removed, "foreach_1464766cf9c8fd1095d0f7a22abe0632b6a6d44c3eeae65766086350eef3ac33_1" was added
keys = make([]string, 0, len(foreachConfigNode.customComponents))
for key := range foreachConfigNode.customComponents {
keys = append(keys, key)
}
require.ElementsMatch(t, keys, []string{"foreach_be19d02a2ccb2cbc2c47e90d_1", "foreach_1464766cf9c8fd1095d0f7a2_1"})
require.ElementsMatch(t, keys, []string{"foreach_be19d02a2ccb2cbc2c47e90dcad8446a50459577449624176398d1f2aa6cd23a_1", "foreach_1464766cf9c8fd1095d0f7a22abe0632b6a6d44c3eeae65766086350eef3ac33_1"})
}

func getBlockFromConfig(t *testing.T, config string) *ast.BlockStmt {
Expand Down

0 comments on commit e16e3e3

Please sign in to comment.