f-50: lab: rôles des nœuds, adresses du lien, loopback et installation de FRR #50
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
32940c57b4
commit
b348652995
21 changed files with 962 additions and 58 deletions
|
|
@ -72,7 +72,11 @@ func Prepare(ctx context.Context, p *topology.Plan, o Options) ([]render.Node, e
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nodes, err := render.Render(p, render.Options{RunDir: o.RunDir, AuthorizedKeys: []string{key}})
|
||||
frr, err := ReadFRR(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nodes, err := render.Render(p, render.Options{RunDir: o.RunDir, AuthorizedKeys: []string{key}, FRR: frr})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -84,6 +88,21 @@ func Prepare(ctx context.Context, p *topology.Plan, o Options) ([]render.Node, e
|
|||
return nodes, nil
|
||||
}
|
||||
|
||||
func ReadFRR(p *topology.Plan) (map[string]string, error) {
|
||||
configs := map[string]string{}
|
||||
for _, n := range p.Nodes {
|
||||
if n.FRR == "" {
|
||||
continue
|
||||
}
|
||||
data, err := os.ReadFile(n.FRR)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("node %s: %w", n.Name, err)
|
||||
}
|
||||
configs[n.Name] = string(data)
|
||||
}
|
||||
return configs, nil
|
||||
}
|
||||
|
||||
func EnsureKey(ctx context.Context, r Runner, dir string) (string, error) {
|
||||
private := filepath.Join(dir, KeyFile)
|
||||
if _, err := os.Stat(private); errors.Is(err, os.ErrNotExist) {
|
||||
|
|
|
|||
|
|
@ -315,3 +315,73 @@ func TestEnsureKey_WithTheRealSSHKeygen(t *testing.T) {
|
|||
t.Errorf("private key mode = %v, %v", info.Mode().Perm(), err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadFRR_ReadsOnlyTheNodesThatDeclareOne(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
conf := filepath.Join(dir, "rr1.conf")
|
||||
if err := os.WriteFile(conf, []byte("hostname rr1\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := newMirror(t)
|
||||
p := labPlan(t, m)
|
||||
p.Nodes[1].FRR = conf
|
||||
|
||||
got, err := ReadFRR(p)
|
||||
|
||||
if err != nil || !reflect.DeepEqual(got, map[string]string{"rr1": "hostname rr1\n"}) {
|
||||
t.Errorf("ReadFRR = %q, %v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadFRR_NamesTheNodeOfAMissingFile(t *testing.T) {
|
||||
m := newMirror(t)
|
||||
p := labPlan(t, m)
|
||||
p.Nodes[2].FRR = filepath.Join(t.TempDir(), "absent.conf")
|
||||
|
||||
if _, err := ReadFRR(p); err == nil || !strings.Contains(err.Error(), "node hv1: ") || !strings.Contains(err.Error(), "absent.conf") {
|
||||
t.Errorf("error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepare_PutsTheFRRConfigIntoTheSeed(t *testing.T) {
|
||||
m := newMirror(t)
|
||||
publish(m, []byte("qcow2 image"))
|
||||
root := t.TempDir()
|
||||
conf := filepath.Join(root, "hv1.conf")
|
||||
if err := os.WriteFile(conf, []byte("hostname hv1\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p := labPlan(t, m)
|
||||
p.Nodes[2].FRR = conf
|
||||
|
||||
nodes, err := Prepare(context.Background(), p, Options{
|
||||
RunDir: filepath.Join(root, "run"),
|
||||
Fetcher: Fetcher{Client: m.server.Client(), CacheDir: filepath.Join(root, "cache")},
|
||||
Runner: &fakeRunner{},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare: %v", err)
|
||||
}
|
||||
data, err := os.ReadFile(filepath.Join(nodes[2].Dir, "user-data"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var cfg struct {
|
||||
Files []struct {
|
||||
Path string `yaml:"path"`
|
||||
Content string `yaml:"content"`
|
||||
} `yaml:"write_files"`
|
||||
}
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, f := range cfg.Files {
|
||||
if f.Path == "/etc/lab/frr.conf" {
|
||||
if f.Content != "hostname hv1\n" {
|
||||
t.Errorf("frr.conf = %q", f.Content)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Errorf("hv1 user-data has no /etc/lab/frr.conf:\n%s", data)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue