Skip to content

Commit

Permalink
Merge pull request #1389 from marmijo/marmijo_issue_1339
Browse files Browse the repository at this point in the history
units: Sort systemd presets in alphabetical order in 20-ignition.preset file
  • Loading branch information
marmijo committed Jun 3, 2022
2 parents 8792d03 + a46c71c commit c41589b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
13 changes: 12 additions & 1 deletion internal/exec/stages/files/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"path/filepath"
"sort"
"strings"

"github.com/coreos/ignition/v2/config/shared/errors"
Expand Down Expand Up @@ -151,7 +152,17 @@ func (s *stage) createSystemdPresetFile(presets map[string]*Preset) error {
return err
}
hasInstanceUnit := false
for _, value := range presets {

// sort the units before writing to the systemd presets file to ensure
// the file is written in a consistent order across multiple runs
unitNames := make([]string, 0, len(presets))
for unit := range presets {
unitNames = append(unitNames, unit)
}
sort.Strings(unitNames)

for _, name := range unitNames {
value := presets[name]
unitString := value.unit
if value.instantiatable {
hasInstanceUnit = true
Expand Down
55 changes: 55 additions & 0 deletions tests/positive/files/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func init() {
register.Register(register.PositiveTest, TestUnmaskUnit())
register.Register(register.PositiveTest, TestMaskUnit())
register.Register(register.PositiveTest, RemoveEnablementSymLinksforUnit())
register.Register(register.PositiveTest, TestPresetsFileInAlphabeticalOrder())
}

func CreateInstantiatedService() types.Test {
Expand Down Expand Up @@ -320,3 +321,57 @@ func RemoveEnablementSymLinksforUnit() types.Test {
ConfigMinVersion: configMinVersion,
}
}

// TestPresetsFileinAlphabeticalOrder checks if Ignition
// writes the systemd presets in alphabetical order by
// unit name into the 20-ignition.preset file.
func TestPresetsFileInAlphabeticalOrder() types.Test {
name := "unit.presets.sorted"
in := types.GetBaseDisk()
out := types.GetBaseDisk()
config := `{
"ignition": { "version": "$version" },
"systemd": {
"units": [
{
"enabled": true,
"name": "foo.service"
},
{
"enabled": true,
"name": "bar.service"
},
{
"enabled": true,
"name": "qux.service"
},
{
"enabled": true,
"name": "baz.service"
},
{
"enabled": true,
"name": "garply.service"
}
]
}
}`
out[0].Partitions.AddFiles("ROOT", []types.File{
{
Node: types.Node{
Name: "20-ignition.preset",
Directory: "etc/systemd/system-preset",
},
Contents: "enable bar.service\nenable baz.service\nenable foo.service\nenable garply.service\nenable qux.service\n",
},
})
configMinVersion := "3.0.0"

return types.Test{
Name: name,
In: in,
Out: out,
Config: config,
ConfigMinVersion: configMinVersion,
}
}

0 comments on commit c41589b

Please sign in to comment.