sic v2 client WP6: resolveVerbs — map hops to verbs by nest depth, fail-loud on overflow
This commit is contained in:
@@ -43,3 +43,21 @@ func verbFor(runtime, name string) (string, error) {
|
||||
return "", fmt.Errorf("unknown runtime: %s", runtime)
|
||||
}
|
||||
}
|
||||
|
||||
func resolveVerbs(hops []string, nest []string) ([]string, error) {
|
||||
if len(hops) > len(nest) {
|
||||
return nil, fmt.Errorf("too many hops for this host's nest depth")
|
||||
}
|
||||
if len(hops) == 0 {
|
||||
return []string{}, nil
|
||||
}
|
||||
verbs := make([]string, len(hops))
|
||||
for i, hop := range hops {
|
||||
verb, err := verbFor(nest[i], hop)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
verbs[i] = verb
|
||||
}
|
||||
return verbs, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// WP6: resolveVerbs maps each hop to its verb via verbFor, keyed by DEPTH in the host's nest.
|
||||
// Too many hops for the declared nest depth is a hard error (fail-loud); an unknown runtime
|
||||
// propagates verbFor's error. Zero hops -> zero verbs (a bare host is a single v2 frame).
|
||||
func TestResolveVerbs(t *testing.T) {
|
||||
got, err := resolveVerbs([]string{"memory", "stash-stash-1"}, []string{"incus", "docker"})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected err: %v", err)
|
||||
}
|
||||
want := []string{"incus exec memory --", "docker exec stash-stash-1"}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("resolveVerbs = %#v, want %#v", got, want)
|
||||
}
|
||||
|
||||
if v, err := resolveVerbs(nil, []string{"incus"}); err != nil || len(v) != 0 {
|
||||
t.Fatalf("zero hops must yield zero verbs, no error; got %#v err %v", v, err)
|
||||
}
|
||||
|
||||
if _, err := resolveVerbs([]string{"a", "b"}, []string{"incus"}); err == nil {
|
||||
t.Fatal("more hops than the host's nest depth must be an error")
|
||||
}
|
||||
|
||||
if _, err := resolveVerbs([]string{"x"}, []string{"bogus"}); err == nil {
|
||||
t.Fatal("an unknown runtime must propagate verbFor's error")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user