diff --git a/hivesim/hive_test.go b/hivesim/hive_test.go index 48863b109e..64c5053cdb 100644 --- a/hivesim/hive_test.go +++ b/hivesim/hive_test.go @@ -400,9 +400,9 @@ func TestStartClientInitialNetworks(t *testing.T) { } func newFakeAPI(hooks *fakes.BackendHooks) (*libhive.TestManager, *httptest.Server) { - defs := map[string]*libhive.ClientDefinition{ - "client-1": {Name: "client-1", Image: "/ignored/in/api", Version: "client-1-version", Meta: libhive.ClientMetadata{Roles: []string{"eth1"}}}, - "client-2": {Name: "client-2", Image: "/not/exposed/", Version: "client-2-version", Meta: libhive.ClientMetadata{Roles: []string{"beacon"}}}, + defs := []*libhive.ClientDefinition{ + {Name: "client-1", Image: "/ignored/in/api", Version: "client-1-version", Meta: libhive.ClientMetadata{Roles: []string{"eth1"}}}, + {Name: "client-2", Image: "/not/exposed/", Version: "client-2-version", Meta: libhive.ClientMetadata{Roles: []string{"beacon"}}}, } env := libhive.SimEnv{} backend := fakes.NewContainerBackend(hooks) diff --git a/internal/libhive/api.go b/internal/libhive/api.go index d0a051e3fa..06c89876d7 100644 --- a/internal/libhive/api.go +++ b/internal/libhive/api.go @@ -10,7 +10,6 @@ import ( "net/http" "path" "path/filepath" - "sort" "strconv" "strings" "time" @@ -61,14 +60,7 @@ type simAPI struct { // getClientTypes returns all known client types. func (api *simAPI) getClientTypes(w http.ResponseWriter, r *http.Request) { - clients := make([]*ClientDefinition, 0, len(api.tm.clientDefs)) - for _, def := range api.tm.clientDefs { - clients = append(clients, def) - } - sort.Slice(clients, func(i, j int) bool { - return clients[i].Name < clients[j].Name - }) - serveJSON(w, clients) + serveJSON(w, api.tm.clientDefs) } // startSuite starts a suite. @@ -332,11 +324,12 @@ func (api *simAPI) checkClient(req *simapi.NodeConfig) (*ClientDefinition, error if req.Client == "" { return nil, errors.New("missing client type in start request") } - def, ok := api.tm.clientDefs[req.Client] - if !ok { - return nil, errors.New("unknown client type in start request") + for _, client := range api.tm.clientDefs { + if client.Name == req.Client { + return client, nil + } } - return def, nil + return nil, errors.New("unknown client type in start request") } // checkClientNetworks pre-checks the existence of initial networks for a client container. diff --git a/internal/libhive/run.go b/internal/libhive/run.go index 37e0f8bd11..42b985e834 100644 --- a/internal/libhive/run.go +++ b/internal/libhive/run.go @@ -30,7 +30,7 @@ type Runner struct { // This holds the image names of all built simulators. simImages map[string]string - clientDefs map[string]*ClientDefinition + clientDefs []*ClientDefinition } func NewRunner(inv Inventory, b Builder, cb ContainerBackend) *Runner { @@ -58,11 +58,11 @@ func (r *Runner) buildClients(ctx context.Context, clientList []ClientDesignator return errors.New("client list is empty, cannot simulate") } - r.clientDefs = make(map[string]*ClientDefinition, len(clientList)) + r.clientDefs = make([]*ClientDefinition, len(clientList)) var anyBuilt bool log15.Info(fmt.Sprintf("building %d clients...", len(clientList))) - for _, client := range clientList { + for i, client := range clientList { image, err := r.builder.BuildClientImage(ctx, client) if err != nil { continue @@ -72,7 +72,7 @@ func (r *Runner) buildClients(ctx context.Context, clientList []ClientDesignator if err != nil { log15.Warn("can't read version info of "+client.Client, "image", image, "err", err) } - r.clientDefs[client.Name()] = &ClientDefinition{ + r.clientDefs[i] = &ClientDefinition{ Name: client.Name(), Version: strings.TrimSpace(string(version)), Image: image, @@ -117,8 +117,11 @@ func (r *Runner) RunDevMode(ctx context.Context, env SimEnv, endpoint string) er if err := createWorkspace(env.LogDir); err != nil { return err } - - tm := NewTestManager(env, r.container, r.clientDefs) + clientDefs := make([]*ClientDefinition, 0) + for _, def := range r.clientDefs { + clientDefs = append(clientDefs, def) + } + tm := NewTestManager(env, r.container, clientDefs) defer func() { if err := tm.Terminate(); err != nil { log15.Error("could not terminate test manager", "error", err) @@ -159,19 +162,23 @@ HIVE_SIMULATOR=http://%v func (r *Runner) run(ctx context.Context, sim string, env SimEnv) (SimResult, error) { log15.Info(fmt.Sprintf("running simulation: %s", sim)) - clientDefs := make(map[string]*ClientDefinition) + clientDefs := make([]*ClientDefinition, 0) if env.ClientList == nil { // Unspecified, make all clients available. - for name, def := range r.clientDefs { - clientDefs[name] = def - } + clientDefs = append(clientDefs, r.clientDefs...) } else { for _, client := range env.ClientList { - def, ok := r.clientDefs[client.Client] - if !ok { + found := false + for _, def := range r.clientDefs { + if def.Name == client.Client { + clientDefs = append(clientDefs, def) + found = true + break + } + } + if !found { return SimResult{}, fmt.Errorf("unknown client %q in simulation client list", client.Client) } - clientDefs[client.Client] = def } } diff --git a/internal/libhive/testmanager.go b/internal/libhive/testmanager.go index beee5ff621..18d5ffedae 100644 --- a/internal/libhive/testmanager.go +++ b/internal/libhive/testmanager.go @@ -61,7 +61,7 @@ type SimResult struct { type TestManager struct { config SimEnv backend ContainerBackend - clientDefs map[string]*ClientDefinition + clientDefs []*ClientDefinition simContainerID string simLogFile string @@ -80,7 +80,7 @@ type TestManager struct { results map[TestSuiteID]*TestSuite } -func NewTestManager(config SimEnv, b ContainerBackend, clients map[string]*ClientDefinition) *TestManager { +func NewTestManager(config SimEnv, b ContainerBackend, clients []*ClientDefinition) *TestManager { return &TestManager{ clientDefs: clients, config: config,