Skip to content

Commit

Permalink
pkg/updateengine: rewrite tests and code to use new dbus package
Browse files Browse the repository at this point in the history
This is a safe step before moving creation of D-Bus connection to agent
and eventually agent CLI code, which however requires development of
tests on the agent code itself.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
  • Loading branch information
invidian committed Dec 2, 2021
1 parent a505733 commit 8ab6f21
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 372 deletions.
3 changes: 2 additions & 1 deletion pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"k8s.io/klog/v2"

"github.com/flatcar-linux/flatcar-linux-update-operator/pkg/constants"
"github.com/flatcar-linux/flatcar-linux-update-operator/pkg/dbus"
"github.com/flatcar-linux/flatcar-linux-update-operator/pkg/k8sutil"
"github.com/flatcar-linux/flatcar-linux-update-operator/pkg/updateengine"
)
Expand Down Expand Up @@ -67,7 +68,7 @@ func New(node string, reapTimeout time.Duration) (*Klocksmith, error) {
nc := kc.CoreV1().Nodes()

// Set up update_engine client.
updateEngineClient, err := updateengine.New(updateengine.DBusSystemPrivateConnector)
updateEngineClient, err := updateengine.New(dbus.SystemPrivateConnector)
if err != nil {
return nil, fmt.Errorf("establishing connection to update_engine dbus: %w", err)
}
Expand Down
38 changes: 8 additions & 30 deletions pkg/updateengine/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ package updateengine

import (
"fmt"
"os"
"strconv"

godbus "github.com/godbus/dbus/v5"

"github.com/flatcar-linux/flatcar-linux-update-operator/pkg/dbus"
)

const (
Expand Down Expand Up @@ -52,49 +52,27 @@ type Client interface {

// DBusConnection is set of methods which client expects D-Bus connection to implement.
type DBusConnection interface {
Auth([]godbus.Auth) error
Hello() error
Close() error
AddMatchSignal(...godbus.MatchOption) error
Signal(chan<- *godbus.Signal)
Object(string, godbus.ObjectPath) godbus.BusObject
}

// DBusConnector is a constructor function providing D-Bus connection.
type DBusConnector func() (DBusConnection, error)

// DBusSystemPrivateConnector is a standard update_engine connector using system bus.
func DBusSystemPrivateConnector() (DBusConnection, error) {
return godbus.SystemBusPrivate()
type caller interface {
Call(method string, flags godbus.Flags, args ...interface{}) *godbus.Call
}

type client struct {
conn DBusConnection
object godbus.BusObject
object caller
ch chan *godbus.Signal
}

// New creates new instance of Client and initializes it.
func New(newConnection DBusConnector) (Client, error) {
conn, err := newConnection()
func New(connector dbus.Connector) (Client, error) {
conn, err := dbus.New(connector)
if err != nil {
return nil, fmt.Errorf("opening private connection to system bus: %w", err)
}

methods := []godbus.Auth{godbus.AuthExternal(strconv.Itoa(os.Getuid()))}

if err := conn.Auth(methods); err != nil {
// Best effort closing the connection.
_ = conn.Close()

return nil, fmt.Errorf("authenticating to system bus: %w", err)
}

if err := conn.Hello(); err != nil {
// Best effort closing the connection.
_ = conn.Close()

return nil, fmt.Errorf("sending hello to system bus: %w", err)
return nil, fmt.Errorf("creating D-Bus client: %w", err)
}

matchOptions := []godbus.MatchOption{
Expand Down
244 changes: 0 additions & 244 deletions pkg/updateengine/client_integration_test.go

This file was deleted.

Loading

0 comments on commit 8ab6f21

Please sign in to comment.