Skip to content

Commit

Permalink
Maintenance Window - Block Reconfigure on all errors when reading a s…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kschappacher authored Oct 31, 2024
1 parent 5b26371 commit a34352b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
16 changes: 8 additions & 8 deletions robot/impl/local_robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,11 @@ func (r *localRobot) reconfigure(ctx context.Context, newConfig *config.Config,
} else {
canReconfigure, err := r.checkMaintenanceSensorReadings(ctx, newConfig.MaintenanceConfig.MaintenanceAllowedKey, sensorComponent)
if !canReconfigure {
r.logger.Info("maintenance_allowed_key found from readings on maintenance sensor. Skipping reconfiguration.")
if err != nil {
r.logger.CErrorw(ctx, "error reading maintenance sensor", "error", err)
} else {
r.logger.Info("maintenance_allowed_key found from readings on maintenance sensor. Skipping reconfiguration.")
}
diff, err := config.DiffConfigs(*r.Config(), *newConfig, false)
if err != nil {
r.logger.CErrorw(ctx, "error diffing the configs", "error", err)
Expand All @@ -1218,11 +1222,7 @@ func (r *localRobot) reconfigure(ctx context.Context, newConfig *config.Config,
}
return
}
if err != nil {
r.logger.Warn(err.Error() + ". Starting reconfiguration")
} else {
r.logger.Info("maintenance_allowed_key found from readings on maintenance sensor. Starting reconfiguration")
}
r.logger.Info("maintenance_allowed_key found from readings on maintenance sensor. Starting reconfiguration")
}
}
}
Expand Down Expand Up @@ -1528,11 +1528,11 @@ func (r *localRobot) checkMaintenanceSensorReadings(ctx context.Context,
}
readingVal, ok := readings[maintenanceAllowedKey]
if !ok {
return true, errors.Errorf("error getting maintenance_allowed_key %s from sensor reading", maintenanceAllowedKey)
return false, errors.Errorf("error getting maintenance_allowed_key %s from sensor reading", maintenanceAllowedKey)
}
canReconfigure, ok := readingVal.(bool)
if !ok {
return true, errors.Errorf("maintenance_allowed_key %s is not a bool value", maintenanceAllowedKey)
return false, errors.Errorf("maintenance_allowed_key %s is not a bool value", maintenanceAllowedKey)
}
return canReconfigure, nil
}
10 changes: 5 additions & 5 deletions robot/impl/local_robot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3969,31 +3969,31 @@ func TestCheckMaintenanceSensorReadings(t *testing.T) {
localRobot := r.(*localRobot)
canReconfigure, err := localRobot.checkMaintenanceSensorReadings(context.Background(), "keyDoesNotExist", newValidSensor())

test.That(t, canReconfigure, test.ShouldEqual, true)
test.That(t, canReconfigure, test.ShouldEqual, false)
test.That(t, err.Error(), test.ShouldEqual, "error getting maintenance_allowed_key keyDoesNotExist from sensor reading")
})
t.Run("maintenanceAllowedKey is a number not a boolean", func(t *testing.T) {
r := setupLocalRobot(t, context.Background(), &config.Config{}, logger)
localRobot := r.(*localRobot)
canReconfigure, err := localRobot.checkMaintenanceSensorReadings(context.Background(), "ThatIsNotAWallet", newValidSensor())

test.That(t, canReconfigure, test.ShouldEqual, true)
test.That(t, canReconfigure, test.ShouldEqual, false)
test.That(t, err.Error(), test.ShouldEqual, "maintenance_allowed_key ThatIsNotAWallet is not a bool value")
})
t.Run("maintenanceAllowedKey is one not a boolean", func(t *testing.T) {
r := setupLocalRobot(t, context.Background(), &config.Config{}, logger)
localRobot := r.(*localRobot)
canReconfigure, err := localRobot.checkMaintenanceSensorReadings(context.Background(), "OneIsNotTrue", newValidSensor())

test.That(t, canReconfigure, test.ShouldEqual, true)
test.That(t, canReconfigure, test.ShouldEqual, false)
test.That(t, err.Error(), test.ShouldEqual, "maintenance_allowed_key OneIsNotTrue is not a bool value")
})
t.Run("maintenanceAllowedKey is string true not a boolean", func(t *testing.T) {
r := setupLocalRobot(t, context.Background(), &config.Config{}, logger)
localRobot := r.(*localRobot)
canReconfigure, err := localRobot.checkMaintenanceSensorReadings(context.Background(), "TrueIsNotTrue", newValidSensor())

test.That(t, canReconfigure, test.ShouldEqual, true)
test.That(t, canReconfigure, test.ShouldEqual, false)
test.That(t, err.Error(), test.ShouldEqual, "maintenance_allowed_key TrueIsNotTrue is not a bool value")
})
}
Expand Down Expand Up @@ -4058,7 +4058,7 @@ func newErrorSensor() sensor.Sensor {
func newInvalidSensor() sensor.Sensor {
s := &inject.Sensor{}
s.ReadingsFunc = func(ctx context.Context, extra map[string]interface{}) (map[string]interface{}, error) {
return map[string]any{"ThatsMyWallet": 1, "ThatsNotMyWallet": 2}, nil
return map[string]any{"ThatsMyWallet": true, "ThatsNotMyWallet": false}, nil
}
return s
}
Expand Down

0 comments on commit a34352b

Please sign in to comment.