Skip to content

Commit

Permalink
v3_5_experimental: add validations
Browse files Browse the repository at this point in the history
add logic to cover the test scenerios
  • Loading branch information
yasminvalim committed Feb 6, 2024
1 parent a32d127 commit 08b04e9
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions config/v3_5_experimental/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ var (
func (cfg Config) Validate(c path.ContextPath) (r report.Report) {
systemdPath := "/etc/systemd/system/"
unitPaths := map[string]struct{}{}
paths := make(map[string]struct{})

for _, unit := range cfg.Systemd.Units {
if !util.NilOrEmpty(unit.Contents) {
pathString := systemdPath + unit.Name
Expand All @@ -47,19 +49,49 @@ func (cfg Config) Validate(c path.ContextPath) (r report.Report) {
}
}
for i, f := range cfg.Storage.Files {
paths[f.Path] = struct{}{}

filesPath := getParentDirectory(f.Path)
if _, exists := paths[filesPath]; exists {
r.AddOnError(c.Append("storage", "files", i, "path"), errors.ErrPathConflictsParentDir)
}

if _, exists := unitPaths[f.Path]; exists {
r.AddOnError(c.Append("storage", "files", i, "path"), errors.ErrPathConflictsSystemd)
}
}
for i, d := range cfg.Storage.Directories {
paths[d.Path] = struct{}{}

dirPath := getParentDirectory(d.Path)
if _, exists := paths[dirPath]; exists {
r.AddOnError(c.Append("storage", "directories", i, "path"), errors.ErrPathConflictsParentDir)
}

if _, exists := unitPaths[d.Path]; exists {
r.AddOnError(c.Append("storage", "directories", i, "path"), errors.ErrPathConflictsSystemd)
}
}
for i, l := range cfg.Storage.Links {
paths[l.Path] = struct{}{}

linkPath := getParentDirectory(l.Path)
if _, exists := paths[linkPath]; exists {
r.AddOnError(c.Append("storage", "links", i, "path"), errors.ErrPathConflictsParentDir)
}

if _, exists := unitPaths[l.Path]; exists {
r.AddOnError(c.Append("storage", "links", i, "path"), errors.ErrPathConflictsSystemd)
}
}
return
}

func getParentDirectory(p string) string {
index := len(p) - 1
for index > 0 && p[index-1] != '/' {
index--
}

return p[:index]
}

0 comments on commit 08b04e9

Please sign in to comment.