diff --git a/robot/impl/local_robot_test.go b/robot/impl/local_robot_test.go index 9a4cd7ab898..1aaa07ff516 100644 --- a/robot/impl/local_robot_test.go +++ b/robot/impl/local_robot_test.go @@ -4013,72 +4013,3 @@ func TestRemovingOfflineRemotes(t *testing.T) { cancelReconfig() wg.Wait() } - -// Tests that machine state properly reports initializing and running. - - -//ctx := context.Background() - -// completeComponentConstruction := make(chan struct{}, 1) - -//// Register a `foo` component whose construction completion can be delayed, -//// and defer its deregistration. -// resource.RegisterComponent(generic.API, fooModel, resource.Registration[resource.Resource, -// resource.NoNativeConfig]{ -//Constructor: func( -//ctx context.Context, -//deps resource.Dependencies, -//conf resource.Config, -//logger logging.Logger, -//) (resource.Resource, error) { -//// Delay completion of constructor until `completeComponentConstruction` is closed. -//<-completeComponentConstruction - -// return &fooComponent{ -//Named: conf.ResourceName().AsNamed(), -//logger: logger, -// }, nil -//}, -//}) -//defer func() { -//resource.Deregister(generic.API, fooModel) -//}() - -// cfg := &config.Config{ -//Components: []resource.Config{ -//{ -//Name: "foo", -//API: generic.API, -//Model: fooModel, -// }, -//}, -//} -//r := setupLocalRobot(t, ctx, cfg, logger) - -//// Assert that robot reports a state of "initializing" until `foo` completes construction. -// machineStatus, err := r.MachineStatus(ctx) -// test.That(t, err, test.ShouldBeNil) -//test.That(t, machineStatus, test.ShouldNotBeNil) -//test.That(t, machineStatus.State, test.ShouldEqual, robot.StateInitializing) - -// close(completeComponentConstruction) - -//// Assert that robot reports a state of "running" after `foo` completes -//// construction. -// machineStatus, err = r.MachineStatus(ctx) -// test.That(t, err, test.ShouldBeNil) -//test.That(t, machineStatus, test.ShouldNotBeNil) -//test.That(t, machineStatus.State, test.ShouldEqual, robot.StateRunning) - -//// Reconfigure robot to replace `foo` with idential `bar` component (should build -//// immediately, as `completeComponentConstruction` has already been closed.) -// cfg.Components[0].Name = "bar" -// r.Reconfigure(ctx, cfg) - -//// Assert that robot continues to report a state of "running" even after further -//// reconfiguration. -// machineStatus, err = r.MachineStatus(ctx) -// test.That(t, err, test.ShouldBeNil) -//test.That(t, machineStatus, test.ShouldNotBeNil) -//test.That(t, machineStatus.State, test.ShouldEqual, robot.StateRunning) -//} diff --git a/web/server/entrypoint_test.go b/web/server/entrypoint_test.go index 7b8cf98771c..8256e069ee8 100644 --- a/web/server/entrypoint_test.go +++ b/web/server/entrypoint_test.go @@ -27,6 +27,7 @@ import ( "go.viam.com/rdk/config" "go.viam.com/rdk/logging" "go.viam.com/rdk/resource" + "go.viam.com/rdk/robot" _ "go.viam.com/rdk/services/register" "go.viam.com/rdk/testutils" "go.viam.com/rdk/testutils/robottestutils" @@ -192,3 +193,53 @@ func isExpectedShutdownError(err error, testLogger logging.Logger) bool { testLogger.Errorw("Unexpected shutdown error", "err", err) return false } + +// Tests that machine state properly reports initializing or running. +func TestMachineState(t *testing.T) { + if runtime.GOARCH == "arm" { + t.Skip("skipping on 32-bit ARM, subprocess build warnings cause failure") + } + + logger, logObserver := logging.NewObservedTestLogger(t) + + cfgFilename := utils.ResolveFile("/etc/configs/fake.json") + cfg, err := config.Read(context.Background(), cfgFilename, logger) + test.That(t, err, test.ShouldBeNil) + + var port int + var success bool + var server pexec.ManagedProcess + for portTryNum := 0; portTryNum < 10; portTryNum++ { + p, err := goutils.TryReserveRandomPort() + port = p + test.That(t, err, test.ShouldBeNil) + + cfg.Network.BindAddress = fmt.Sprintf(":%d", port) + cfgFilename, err = robottestutils.MakeTempConfig(t, cfg, logger) + test.That(t, err, test.ShouldBeNil) + + server = robottestutils.ServerAsSeparateProcess(t, cfgFilename, logger) + err = server.Start(context.Background()) + test.That(t, err, test.ShouldBeNil) + + if success = robottestutils.WaitForServing(logObserver, port); success { + defer func() { + test.That(t, server.Stop(), test.ShouldBeNil) + }() + break + } + logger.Infow("Port in use. Restarting on new port.", "port", port, "err", err) + server.Stop() + continue + } + test.That(t, success, test.ShouldBeTrue) + + addr := "localhost:" + strconv.Itoa(port) + rc := robottestutils.NewRobotClient(t, logger, addr, time.Second) + + machineStatus, err := rc.MachineStatus(context.Background()) + test.That(t, err, test.ShouldBeNil) + test.That(t, machineStatus, test.ShouldNotBeNil) + test.That(t, machineStatus.State, test.ShouldBeIn, + []robot.MachineState{robot.StateInitializing, robot.StateRunning}) +}