Skip to content

Commit

Permalink
internal/libhive: return clients in the order defined in the client-f…
Browse files Browse the repository at this point in the history
…ile (ethereum#898)
  • Loading branch information
marioevz authored and Eikix committed Mar 1, 2024
1 parent 3aad27f commit 925ab45
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
6 changes: 3 additions & 3 deletions hivesim/hive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 6 additions & 13 deletions internal/libhive/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net/http"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
33 changes: 20 additions & 13 deletions internal/libhive/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/libhive/testmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type SimResult struct {
type TestManager struct {
config SimEnv
backend ContainerBackend
clientDefs map[string]*ClientDefinition
clientDefs []*ClientDefinition

simContainerID string
simLogFile string
Expand All @@ -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,
Expand Down

0 comments on commit 925ab45

Please sign in to comment.