Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: Add hive instance information endpoint #1198

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions hive.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,24 @@ func main() {
clientList = libhive.FilterClients(clientList, filter)
}
}
hiveInfo := libhive.HiveInfo{
Command: os.Args,
ClientFile: clientList,
}

// Build clients and simulators.
if err := runner.Build(ctx, clientList, simList, simBuildArgs); err != nil {
fatal(err)
}
if *simDevMode {
runner.RunDevMode(ctx, env, *simDevModeAPIEndpoint)
runner.RunDevMode(ctx, env, *simDevModeAPIEndpoint, hiveInfo)
return
}

// Run simulators.
var failCount int
for _, sim := range simList {
result, err := runner.Run(ctx, sim, env)
result, err := runner.Run(ctx, sim, env, hiveInfo)
if err != nil {
fatal(err)
}
Expand Down
5 changes: 4 additions & 1 deletion hivesim/hive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,10 @@ func newFakeAPI(hooks *fakes.BackendHooks) (*libhive.TestManager, *httptest.Serv
}
env := libhive.SimEnv{}
backend := fakes.NewContainerBackend(hooks)
tm := libhive.NewTestManager(env, backend, defs)
hiveInfo := libhive.HiveInfo{
Command: []string{"/hive"},
}
tm := libhive.NewTestManager(env, backend, defs, hiveInfo)
srv := httptest.NewServer(tm.API())
return tm, srv
}
12 changes: 10 additions & 2 deletions internal/libhive/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ const hiveEnvvarPrefix = "HIVE_"
const defaultStartTimeout = time.Duration(60 * time.Second)

// newSimulationAPI creates handlers for the simulation API.
func newSimulationAPI(b ContainerBackend, env SimEnv, tm *TestManager) http.Handler {
api := &simAPI{backend: b, env: env, tm: tm}
func newSimulationAPI(b ContainerBackend, env SimEnv, tm *TestManager, hive HiveInfo) http.Handler {
api := &simAPI{backend: b, env: env, tm: tm, hive: hive}

// API routes.
router := mux.NewRouter()
router.HandleFunc("/hive", api.getHiveInfo).Methods("GET")
router.HandleFunc("/clients", api.getClientTypes).Methods("GET")
router.HandleFunc("/testsuite/{suite}/test/{test}/node/{node}/exec", api.execInClient).Methods("POST")
router.HandleFunc("/testsuite/{suite}/test/{test}/node/{node}", api.getNodeStatus).Methods("GET")
Expand All @@ -56,6 +57,13 @@ type simAPI struct {
backend ContainerBackend
env SimEnv
tm *TestManager
hive HiveInfo
}

// getHiveInfo returns information about the hive server instance.
func (api *simAPI) getHiveInfo(w http.ResponseWriter, r *http.Request) {
slog.Info("API: hive info requested")
serveJSON(w, api.hive)
}

// getClientTypes returns all known client types.
Expand Down
8 changes: 4 additions & 4 deletions internal/libhive/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,19 @@ func loadClientMetadata(path string) (m ClientMetadata, err error) {
type ClientDesignator struct {
// Client is the client name.
// This must refer to a subdirectory of clients/
Client string `yaml:"client"`
Client string `yaml:"client" json:"client"`

// Nametag is used in the name of the client image.
// This is for assigning meaningful names to different builds of the same client.
// If unspecified, a default value is chosen to make client names unique.
Nametag string `yaml:"nametag,omitempty"`
Nametag string `yaml:"nametag,omitempty" json:"nametag,omitempty"`

// DockerfileExt is the extension of the Docker that should be used to build the
// client. Example: setting this to "git" will build using "Dockerfile.git".
DockerfileExt string `yaml:"dockerfile,omitempty"`
DockerfileExt string `yaml:"dockerfile,omitempty" json:"dockerfile,omitempty"`

// Arguments passed to the docker build.
BuildArgs map[string]string `yaml:"build_args,omitempty"`
BuildArgs map[string]string `yaml:"build_args,omitempty" json:"build_args,omitempty"`
}

func (c ClientDesignator) buildString() string {
Expand Down
12 changes: 6 additions & 6 deletions internal/libhive/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,28 +99,28 @@ func (r *Runner) buildSimulators(ctx context.Context, simList []string, buildArg
return nil
}

func (r *Runner) Run(ctx context.Context, sim string, env SimEnv) (SimResult, error) {
func (r *Runner) Run(ctx context.Context, sim string, env SimEnv, hiveInfo HiveInfo) (SimResult, error) {
if err := createWorkspace(env.LogDir); err != nil {
return SimResult{}, err
}
writeInstanceInfo(env.LogDir)
return r.run(ctx, sim, env)
return r.run(ctx, sim, env, hiveInfo)
}

// RunDevMode starts simulator development mode. In this mode, the simulator is not
// launched and the API server runs on the local network instead of listening for requests
// on the docker network.
//
// Note: Sim* options in env are ignored, but Client* options and LogDir still apply.
func (r *Runner) RunDevMode(ctx context.Context, env SimEnv, endpoint string) error {
func (r *Runner) RunDevMode(ctx context.Context, env SimEnv, endpoint string, hiveInfo HiveInfo) error {
if err := createWorkspace(env.LogDir); err != nil {
return err
}
clientDefs := make([]*ClientDefinition, 0)
for _, def := range r.clientDefs {
clientDefs = append(clientDefs, def)
}
tm := NewTestManager(env, r.container, clientDefs)
tm := NewTestManager(env, r.container, clientDefs, hiveInfo)
defer func() {
if err := tm.Terminate(); err != nil {
slog.Error("could not terminate test manager", "error", err)
Expand Down Expand Up @@ -158,7 +158,7 @@ HIVE_SIMULATOR=http://%v
}

// run runs one simulation.
func (r *Runner) run(ctx context.Context, sim string, env SimEnv) (SimResult, error) {
func (r *Runner) run(ctx context.Context, sim string, env SimEnv, hiveInfo HiveInfo) (SimResult, error) {
slog.Info(fmt.Sprintf("running simulation: %s", sim))

clientDefs := make([]*ClientDefinition, 0)
Expand All @@ -182,7 +182,7 @@ func (r *Runner) run(ctx context.Context, sim string, env SimEnv) (SimResult, er
}

// Start the simulation API.
tm := NewTestManager(env, r.container, clientDefs)
tm := NewTestManager(env, r.container, clientDefs, hiveInfo)
defer func() {
if err := tm.Terminate(); err != nil {
slog.Error("could not terminate test manager", "error", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/libhive/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestRunner(t *testing.T) {
if err := runner.Build(ctx, allClients, simList, buildArgs); err != nil {
t.Fatal("Build() failed:", err)
}
if _, err := runner.Run(context.Background(), "sim-1", simOpt); err != nil {
if _, err := runner.Run(context.Background(), "sim-1", simOpt, libhive.HiveInfo{}); err != nil {
t.Fatal("Run() failed:", err)
}

Expand Down
17 changes: 15 additions & 2 deletions internal/libhive/testmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,20 @@ type SimResult struct {
TestsFailed int
}

// HiveInfo contains information about the hive instance running the simulation.
type HiveInfo struct {
Command []string `json:"command"`
ClientFile []ClientDesignator `json:"clientFile"`
Commit string `json:"commit"`
Date string `json:"date"`
}

// TestManager collects test results during a simulation run.
type TestManager struct {
config SimEnv
backend ContainerBackend
clientDefs []*ClientDefinition
hiveInfo HiveInfo

simContainerID string
simLogFile string
Expand All @@ -81,11 +90,15 @@ type TestManager struct {
results map[TestSuiteID]*TestSuite
}

func NewTestManager(config SimEnv, b ContainerBackend, clients []*ClientDefinition) *TestManager {
func NewTestManager(config SimEnv, b ContainerBackend, clients []*ClientDefinition, hiveInfo HiveInfo) *TestManager {
if hiveInfo.Commit == "" && hiveInfo.Date == "" {
hiveInfo.Commit, hiveInfo.Date = hiveVersion()
}
return &TestManager{
clientDefs: clients,
config: config,
backend: b,
hiveInfo: hiveInfo,
runningTestSuites: make(map[TestSuiteID]*TestSuite),
runningTestCases: make(map[TestID]*TestCase),
results: make(map[TestSuiteID]*TestSuite),
Expand Down Expand Up @@ -115,7 +128,7 @@ func (manager *TestManager) Results() map[TestSuiteID]*TestSuite {

// API returns the simulation API handler.
func (manager *TestManager) API() http.Handler {
return newSimulationAPI(manager.backend, manager.config, manager)
return newSimulationAPI(manager.backend, manager.config, manager, manager.hiveInfo)
}

// IsTestSuiteRunning checks if the test suite is still running and returns it if so
Expand Down