-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds login1 package, which is a small subset of github.com/coreos/go-systemd/v22/login1 package with ability to use shared D-Bus connection and with proper error handling for Reboot method call, which is not yet provided by the upstream. The idea is to use this package in favor of github.com/coreos/go-systemd in agent code responsible for rebooting the node. However, this requires tests in agent code, so it will be done in the next step. See coreos/go-systemd#387 for more details. Signed-off-by: Mateusz Gozdek <[email protected]>
- Loading branch information
Showing
2 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Package login1 is a small subset of github.com/coreos/go-systemd/v22/login1 package with | ||
// ability to use shared D-Bus connection and with proper error handling for Reboot method call, which | ||
// is not yet provided by the upstream. | ||
package login1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
godbus "github.com/godbus/dbus/v5" | ||
) | ||
|
||
const ( | ||
// DBusDest is an object path used by systemd-logind. | ||
DBusDest = "org.freedesktop.login1" | ||
// DBusInterface is an systemd-logind intefrace name. | ||
DBusInterface = "org.freedesktop.login1.Manager" | ||
// DBusPath is a standard path to systemd-logind interface. | ||
DBusPath = "/org/freedesktop/login1" | ||
// DBusMethodNameReboot is a login1 manager interface method name responsible for rebooting. | ||
DBusMethodNameReboot = "Reboot" | ||
) | ||
|
||
// Client describes functionality of provided login1 client. | ||
type Client interface { | ||
Reboot(context.Context) error | ||
} | ||
|
||
// Objector describes functionality required from given D-Bus connection. | ||
type Objector interface { | ||
Object(string, godbus.ObjectPath) godbus.BusObject | ||
} | ||
|
||
// Caller describes required functionality from D-Bus object. | ||
type Caller interface { | ||
CallWithContext(ctx context.Context, method string, flags godbus.Flags, args ...interface{}) *godbus.Call | ||
} | ||
|
||
type rebooter struct { | ||
caller Caller | ||
} | ||
|
||
// New creates new login1 client using given D-Bus connection. | ||
func New(objector Objector) (Client, error) { | ||
if objector == nil { | ||
return nil, fmt.Errorf("no objector given") | ||
} | ||
|
||
return &rebooter{ | ||
caller: objector.Object(DBusDest, DBusPath), | ||
}, nil | ||
} | ||
|
||
// Reboot reboots machine on which it's called. | ||
func (r *rebooter) Reboot(ctx context.Context) error { | ||
if call := r.caller.CallWithContext(ctx, DBusInterface+"."+DBusMethodNameReboot, 0, false); call.Err != nil { | ||
return fmt.Errorf("calling reboot: %w", call.Err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package login1_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
godbus "github.com/godbus/dbus/v5" | ||
|
||
"github.com/flatcar-linux/flatcar-linux-update-operator/pkg/dbus" | ||
"github.com/flatcar-linux/flatcar-linux-update-operator/pkg/login1" | ||
) | ||
|
||
func Test_Creating_new_client_returns_error_when_no_objector_is_given(t *testing.T) { | ||
t.Parallel() | ||
|
||
client, err := login1.New(nil) | ||
if err == nil { | ||
t.Fatalf("Expected error creating client with no connector") | ||
} | ||
|
||
if client != nil { | ||
t.Fatalf("Expected client to be nil when New returns error") | ||
} | ||
} | ||
|
||
func Test_Rebooting(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("use_given_context_for_D-Bus_call", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
testKey := struct{}{} | ||
expectedValue := "bar" | ||
|
||
ctx := context.WithValue(context.Background(), testKey, expectedValue) | ||
|
||
connectionWithContextCheck := &dbus.MockConnection{ | ||
ObjectF: func(string, godbus.ObjectPath) godbus.BusObject { | ||
return &dbus.MockObject{ | ||
CallWithContextF: func(ctx context.Context, method string, flags godbus.Flags, args ...interface{}) *godbus.Call { | ||
if val := ctx.Value(testKey); val != expectedValue { | ||
t.Fatalf("Got unexpected context on call") | ||
} | ||
|
||
return &godbus.Call{} | ||
}, | ||
} | ||
}, | ||
} | ||
|
||
client, err := login1.New(connectionWithContextCheck) | ||
if err != nil { | ||
t.Fatalf("Unexpected error creating client: %v", err) | ||
} | ||
|
||
if err := client.Reboot(ctx); err != nil { | ||
t.Fatalf("Unexpected error rebooting: %v", err) | ||
} | ||
}) | ||
|
||
t.Run("returns_error_when_D-Bus_call_fails", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
expectedError := fmt.Errorf("reboot error") | ||
|
||
connectionWithFailingObjectCall := &dbus.MockConnection{ | ||
ObjectF: func(string, godbus.ObjectPath) godbus.BusObject { | ||
return &dbus.MockObject{ | ||
CallWithContextF: func(ctx context.Context, method string, flags godbus.Flags, args ...interface{}) *godbus.Call { | ||
return &godbus.Call{ | ||
Err: expectedError, | ||
} | ||
}, | ||
} | ||
}, | ||
} | ||
|
||
client, err := login1.New(connectionWithFailingObjectCall) | ||
if err != nil { | ||
t.Fatalf("Unexpected error creating client: %v", err) | ||
} | ||
|
||
if err := client.Reboot(context.Background()); !errors.Is(err, expectedError) { | ||
t.Fatalf("Unexpected error rebooting: %v", err) | ||
} | ||
}) | ||
} |