Paillasse/correcteur/lab.go

72 lines
1.7 KiB
Go
Raw Permalink Normal View History

package main
import (
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
)
type attendu struct {
CodeRetour int `yaml:"code_retour"`
SortieContient string `yaml:"sortie_contient"`
}
func (a attendu) satisfait(code int, sortie string) bool {
if code != a.CodeRetour {
return false
}
return a.SortieContient == "" || strings.Contains(sortie, a.SortieContient)
}
type verification struct {
ID string `yaml:"id"`
Machine string `yaml:"machine"`
Commande string `yaml:"commande"`
Attendu attendu `yaml:"attendu"`
}
type lab struct {
Machines []struct {
Nom string `yaml:"nom"`
} `yaml:"machines"`
Verifications []verification `yaml:"verifications"`
}
// L'identifiant a déjà été validé (^[a-z0-9][a-z0-9-]*$) : pas de « .. »
// possible dans le chemin.
func chargerLab(dossier, id string) (*lab, error) {
contenu, err := os.ReadFile(filepath.Join(dossier, id, "lab.yaml"))
if err != nil {
return nil, err
}
var l lab
if err := yaml.Unmarshal(contenu, &l); err != nil {
return nil, err
}
return &l, nil
}
func (l *lab) verification(id string) (verification, bool) {
for _, v := range l.Verifications {
if v.ID == id {
return v, true
}
}
return verification{}, false
}
// Le portail nomme chaque VM « {préfixe}-{machine} » (GenerateurTemplateHeat).
// On retrouve la machine par le suffixe le plus long : avec des machines
// « a » et « pc-a », le nom « …-pc-a » désigne bien pc-a, pas a.
func (l *lab) machineDuNom(nomInstance string) string {
trouvee := ""
for _, m := range l.Machines {
if strings.HasSuffix(nomInstance, "-"+m.Nom) && len(m.Nom) > len(trouvee) {
trouvee = m.Nom
}
}
return trouvee
}