Skip to content

Commit 0ffe40b

Browse files
committed
dbus: add StartTransientUnitAux and StartTransientUnitAuxContext for starting transient units with auxiliary units
the existing StartTransientUnit methods pass an empty slice to the list of auxiliary units, this adds new methods for specifying those auxiliary units. Fixes #488
1 parent d25876d commit 0ffe40b

File tree

3 files changed

+102
-2
lines changed

3 files changed

+102
-2
lines changed

dbus/methods.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,13 @@ func (c *Conn) StartTransientUnit(name string, mode string, properties []Propert
194194
// unique. mode is the same as in StartUnitContext, properties contains properties
195195
// of the unit.
196196
func (c *Conn) StartTransientUnitContext(ctx context.Context, name string, mode string, properties []Property, ch chan<- string) (int, error) {
197-
return c.startJob(ctx, ch, "org.freedesktop.systemd1.Manager.StartTransientUnit", name, mode, properties, make([]PropertyCollection, 0))
197+
return c.startJob(ctx, ch, "org.freedesktop.systemd1.Manager.StartTransientUnit", name, mode, properties, make([]AuxiliaryUnit, 0))
198+
}
199+
200+
// StartTransientUnitAux is the same as StartTransientUnitContext but allows passing
201+
// auxiliary units in the aux parameter.
202+
func (c *Conn) StartTransientUnitAux(ctx context.Context, name string, mode string, properties []Property, aux []AuxiliaryUnit, ch chan<- string) (int, error) {
203+
return c.startJob(ctx, ch, "org.freedesktop.systemd1.Manager.StartTransientUnit", name, mode, properties, aux)
198204
}
199205

200206
// Deprecated: use [KillUnitWithTarget] instead.

dbus/methods_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,28 @@ func runStartTrUnit(t *testing.T, conn *Conn, trTarget TrUnitProp) error {
128128
return nil
129129
}
130130

131+
func runStartTrUnitWithAux(t *testing.T, conn *Conn, trTarget TrUnitProp, trAux TrUnitProp) error {
132+
reschan := make(chan string)
133+
_, err := conn.StartTransientUnitAux(
134+
context.TODO(), // Switch to t.Context once go < 1.24 is not supported.
135+
trTarget.name,
136+
"replace",
137+
trTarget.props,
138+
[]AuxiliaryUnit{{Name: trAux.name, Properties: trAux.props}},
139+
reschan,
140+
)
141+
if err != nil {
142+
return err
143+
}
144+
145+
job := <-reschan
146+
if job != "done" {
147+
return fmt.Errorf("Job is not done: %s", job)
148+
}
149+
150+
return nil
151+
}
152+
131153
func runStopUnit(t *testing.T, conn *Conn, trTarget TrUnitProp) error {
132154
reschan := make(chan string)
133155
_, err := conn.StopUnit(trTarget.name, "replace", reschan)
@@ -850,6 +872,11 @@ func TestSetUnitProperties(t *testing.T) {
850872
}
851873
}
852874

875+
type TrUnitSocketListenProp struct {
876+
Type string
877+
Address string
878+
}
879+
853880
// Ensure that oneshot transient unit starting and stopping works.
854881
func TestStartStopTransientUnitAll(t *testing.T) {
855882
testCases := []struct {
@@ -991,6 +1018,29 @@ func TestStartStopTransientUnitAll(t *testing.T) {
9911018
},
9921019
checkFunc: checkTransientUnitConflicts,
9931020
},
1021+
{
1022+
trTarget: TrUnitProp{
1023+
name: "testing-aux-unit.socket",
1024+
props: []Property{
1025+
{
1026+
Name: "Listen",
1027+
Value: dbus.MakeVariant([]TrUnitSocketListenProp{
1028+
{
1029+
Type: "Stream",
1030+
Address: path.Join(t.TempDir(), "go-systemd-test.sock"),
1031+
},
1032+
}),
1033+
},
1034+
},
1035+
},
1036+
trDep: TrUnitProp{
1037+
name: "testing-aux-unit.service",
1038+
props: []Property{
1039+
PropExecStart([]string{"/bin/sleep", "400"}, false),
1040+
},
1041+
},
1042+
checkFunc: checkTransientUnitAux,
1043+
},
9941044
}
9951045

9961046
for i, tt := range testCases {
@@ -1332,6 +1382,47 @@ func checkTransientUnitConflicts(t *testing.T, trTarget TrUnitProp, trDep TrUnit
13321382
return nil
13331383
}
13341384

1385+
func checkTransientUnitAux(t *testing.T, trTarget TrUnitProp, trDep TrUnitProp) error {
1386+
conn := setupConn(t)
1387+
1388+
// Start the target unit with dep as auxiliary unit
1389+
err := runStartTrUnitWithAux(t, conn, trTarget, trDep)
1390+
if err != nil {
1391+
return err
1392+
}
1393+
1394+
target := getUnitStatusSingle(conn, trTarget.name)
1395+
if target == nil || target.ActiveState != "active" {
1396+
return fmt.Errorf("Test unit %s not active, should be active", trTarget.name)
1397+
}
1398+
1399+
aux := getUnitStatusSingle(conn, trDep.name)
1400+
if aux == nil {
1401+
return fmt.Errorf("Test unit %s not found", trDep.name)
1402+
}
1403+
1404+
if aux.ActiveState == "active" {
1405+
return fmt.Errorf("Test unit %s active, should be inactive", trDep.name)
1406+
}
1407+
1408+
err = runStopUnit(t, conn, trTarget)
1409+
if err != nil {
1410+
return err
1411+
}
1412+
1413+
target = getUnitStatusSingle(conn, trTarget.name)
1414+
if target != nil {
1415+
return fmt.Errorf("Test unit %s found in list, should be stopped", trTarget.name)
1416+
}
1417+
1418+
aux = getUnitStatusSingle(conn, trDep.name)
1419+
if aux != nil {
1420+
return fmt.Errorf("Test unit %s found in list, should be stopped", trTarget.name)
1421+
}
1422+
1423+
return nil
1424+
}
1425+
13351426
func runSleep(t *testing.T) uint32 {
13361427
cmd := exec.Command("/bin/sleep", "400")
13371428
err := cmd.Start()

dbus/properties.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@ type Property struct {
3939
Value dbus.Variant
4040
}
4141

42-
type PropertyCollection struct {
42+
type AuxiliaryUnit struct {
4343
Name string
4444
Properties []Property
4545
}
4646

47+
// PropertyCollection is deprecated: use AuxiliaryUnit instead.
48+
type PropertyCollection = AuxiliaryUnit
49+
4750
type execStart struct {
4851
Path string // the binary path to execute
4952
Args []string // an array with all arguments to pass to the executed command, starting with argument 0

0 commit comments

Comments
 (0)