Skip to content

Commit

Permalink
Handle duplicated names in a stage
Browse files Browse the repository at this point in the history
relates to kairos-io/kairos#2488

Signed-off-by: Mauro Morales <contact@mauromorales.com>
  • Loading branch information
mauromorales committed Apr 26, 2024
1 parent 9484451 commit fbca905
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 3 deletions.
18 changes: 17 additions & 1 deletion pkg/executor/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,30 @@ func (e *DefaultExecutor) applyStage(stage schema.Stage, fs vfs.FS, console plug
return errs
}

func checkDuplicates(stages []schema.Stage) bool {
stageNames := map[string]bool{}
for _, st := range stages {
if _, ok := stageNames[st.Name]; ok {
return true
}
stageNames[st.Name] = true
}
return false
}

func (e *DefaultExecutor) genOpFromSchema(file, stage string, config schema.YipConfig, fs vfs.FS, console plugins.Console) []*op {
results := []*op{}

currentStages := config.Stages[stage]

duplicatedNames := checkDuplicates(currentStages)

prev := ""
for i, st := range currentStages {
name := st.Name
if duplicatedNames {
name = fmt.Sprintf("%s.%d", st.Name, i)
}

if name == "" {
name = fmt.Sprint(i)
}
Expand Down
97 changes: 95 additions & 2 deletions pkg/executor/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ package executor_test
import (
"bytes"
"fmt"
"github.com/sanity-io/litter"
"github.com/twpayne/go-vfs/v4"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"

"github.com/sanity-io/litter"
"github.com/twpayne/go-vfs/v4"

"github.com/mudler/yip/pkg/console"
"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -476,6 +477,98 @@ stages:
Expect(err).Should(HaveOccurred())
})

It("Unnamed steps are run in sequence", func() {
testConsole := console.NewStandardConsole()

fs2, cleanup2, err := vfst.NewTestFS(map[string]interface{}{})
Expect(err).Should(BeNil())

defer cleanup2()

fs, cleanup, err := vfst.NewTestFS(map[string]interface{}{
"/some/yip/01_first.yaml": `
stages:
initramfs:
- users:
kairos:
groups:
- sudo
passwd: kairos
- users:
kairos:
groups:
- sudo
passwd: kairos
- users:
kairos:
groups:
- sudo
passwd: kairos
- users:
kairos:
groups:
- sudo
passwd: kairos
`,
})
Expect(err).Should(BeNil())
defer cleanup()

err = fs2.Mkdir("/etc", os.ModePerm)
Expect(err).Should(BeNil())

g, err := def.Graph("initramfs", fs, testConsole, "/some/yip")
Expect(err).Should(BeNil())
Expect(len(g)).To(Equal(5), fmt.Sprintf("%#v\n", g))
})

It("Does not try to merge steps as dependencies based on their name", func() {
testConsole := console.NewStandardConsole()

fs2, cleanup2, err := vfst.NewTestFS(map[string]interface{}{})
Expect(err).Should(BeNil())

defer cleanup2()

fs, cleanup, err := vfst.NewTestFS(map[string]interface{}{
"/some/yip/01_first.yaml": `
stages:
initramfs:
- name: Create Kairos User
users:
kairos:
groups:
- sudo
passwd: kairos
- users:
kairos:
groups:
- sudo
passwd: kairos
- name: Create Kairos User
users:
kairos:
groups:
- sudo
passwd: kairos
- users:
kairos:
groups:
- sudo
passwd: kairos
`,
})
Expect(err).Should(BeNil())
defer cleanup()

err = fs2.Mkdir("/etc", os.ModePerm)
Expect(err).Should(BeNil())

g, err := def.Graph("initramfs", fs, testConsole, "/some/yip")
Expect(err).Should(BeNil())
Expect(len(g)).To(Equal(5), fmt.Sprintf("%#v\n", g))
})

It("has multiple instructions", func() {
testConsole := console.NewStandardConsole()

Expand Down

0 comments on commit fbca905

Please sign in to comment.