Skip to content

Commit

Permalink
basic entrypoint test
Browse files Browse the repository at this point in the history
  • Loading branch information
benjirewis committed Dec 11, 2024
1 parent f1b6605 commit 533ae80
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 69 deletions.
69 changes: 0 additions & 69 deletions robot/impl/local_robot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
//}
51 changes: 51 additions & 0 deletions web/server/entrypoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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})
}

0 comments on commit 533ae80

Please sign in to comment.