Skip to content

Commit 8f5a75c

Browse files
gwenyakolyshkin
authored andcommitted
dbus: add StartTransientUnitAux for starting transient units with auxiliary units
the existing StartTransientUnit methods pass an empty slice to the list of auxiliary units, this adds a new method for specifying those auxiliary units. Fixes #488
1 parent 9211a7b commit 8f5a75c

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

dbus/methods.go

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

199205
// Deprecated: use [Conn.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+
[]PropertyCollection{{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()

0 commit comments

Comments
 (0)