-
Notifications
You must be signed in to change notification settings - Fork 328
dbus: add support for new ListUnits* methods #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ import ( | |
| "fmt" | ||
| "math/rand" | ||
| "os" | ||
| "path" | ||
| "path/filepath" | ||
| "reflect" | ||
| "testing" | ||
|
|
@@ -70,6 +71,24 @@ func linkUnit(target string, conn *Conn, t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func getUnitStatus(units []UnitStatus, name string) *UnitStatus { | ||
| for _, u := range units { | ||
| if u.Name == name { | ||
| return &u | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func getUnitFile(units []UnitFile, name string) *UnitFile { | ||
| for _, u := range units { | ||
| if path.Base(u.Path) == name { | ||
| return &u | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Ensure that basic unit starting and stopping works. | ||
| func TestStartStopUnit(t *testing.T) { | ||
| target := "start-stop.service" | ||
|
|
@@ -92,18 +111,11 @@ func TestStartStopUnit(t *testing.T) { | |
|
|
||
| units, err := conn.ListUnits() | ||
|
|
||
| var unit *UnitStatus | ||
| for _, u := range units { | ||
| if u.Name == target { | ||
| unit = &u | ||
| } | ||
| } | ||
| unit := getUnitStatus(units, target) | ||
|
|
||
| if unit == nil { | ||
| t.Fatalf("Test unit not found in list") | ||
| } | ||
|
|
||
| if unit.ActiveState != "active" { | ||
| } else if unit.ActiveState != "active" { | ||
| t.Fatalf("Test unit not active") | ||
| } | ||
|
|
||
|
|
@@ -118,18 +130,169 @@ func TestStartStopUnit(t *testing.T) { | |
|
|
||
| units, err = conn.ListUnits() | ||
|
|
||
| unit = nil | ||
| for _, u := range units { | ||
| if u.Name == target { | ||
| unit = &u | ||
| } | ||
| } | ||
| unit = getUnitStatus(units, target) | ||
|
|
||
| if unit != nil { | ||
| t.Fatalf("Test unit found in list, should be stopped") | ||
| } | ||
| } | ||
|
|
||
| // Ensure that ListUnitsByNames works. | ||
| func TestListUnitsByNames(t *testing.T) { | ||
| target1 := "systemd-journald.service" | ||
| target2 := "unexisting.service" | ||
|
|
||
| conn := setupConn(t) | ||
|
|
||
| units, err := conn.ListUnitsByNames([]string{target1, target2}) | ||
|
|
||
| if err != nil { | ||
| t.Skip(err) | ||
| } | ||
|
|
||
| unit := getUnitStatus(units, target1) | ||
|
|
||
| if unit == nil { | ||
| t.Fatalf("%s unit not found in list", target1) | ||
| } else if unit.ActiveState != "active" { | ||
| t.Fatalf("%s unit should be active but it is %s", target1, unit.ActiveState) | ||
| } | ||
|
|
||
| unit = getUnitStatus(units, target2) | ||
|
|
||
| if unit == nil { | ||
| t.Fatalf("Unexisting test unit not found in list") | ||
| } else if unit.ActiveState != "inactive" { | ||
| t.Fatalf("Test unit should be inactive") | ||
| } | ||
| } | ||
|
|
||
| // Ensure that ListUnitsByPatterns works. | ||
| func TestListUnitsByPatterns(t *testing.T) { | ||
| target1 := "systemd-journald.service" | ||
| target2 := "unexisting.service" | ||
|
|
||
| conn := setupConn(t) | ||
|
|
||
| units, err := conn.ListUnitsByPatterns([]string{}, []string{"systemd-journald*", target2}) | ||
|
|
||
| if err != nil { | ||
| t.Skip(err) | ||
| } | ||
|
|
||
| unit := getUnitStatus(units, target1) | ||
|
|
||
| if unit == nil { | ||
| t.Fatalf("%s unit not found in list", target1) | ||
| } else if unit.ActiveState != "active" { | ||
| t.Fatalf("Test unit should be active") | ||
| } | ||
|
|
||
| unit = getUnitStatus(units, target2) | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same thing with previous test ? have one loop maybe ? |
||
| if unit != nil { | ||
| t.Fatalf("Unexisting test unit found in list") | ||
| } | ||
| } | ||
|
|
||
| // Ensure that ListUnitsFiltered works. | ||
| func TestListUnitsFiltered(t *testing.T) { | ||
| target := "systemd-journald.service" | ||
|
|
||
| conn := setupConn(t) | ||
|
|
||
| units, err := conn.ListUnitsFiltered([]string{"active"}) | ||
|
|
||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| unit := getUnitStatus(units, target) | ||
|
|
||
| if unit == nil { | ||
| t.Fatalf("%s unit not found in list", target) | ||
| } else if unit.ActiveState != "active" { | ||
| t.Fatalf("Test unit should be active") | ||
| } | ||
|
|
||
| units, err = conn.ListUnitsFiltered([]string{"inactive"}) | ||
|
|
||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| unit = getUnitStatus(units, target) | ||
|
|
||
| if unit != nil { | ||
| t.Fatalf("Inactive unit should not be found in list") | ||
| } | ||
| } | ||
|
|
||
| // Ensure that ListUnitFilesByPatterns works. | ||
| func TestListUnitFilesByPatterns(t *testing.T) { | ||
| target1 := "systemd-journald.service" | ||
| target2 := "exit.target" | ||
|
|
||
| conn := setupConn(t) | ||
|
|
||
| units, err := conn.ListUnitFilesByPatterns([]string{"static"}, []string{"systemd-journald*", target2}) | ||
|
|
||
| if err != nil { | ||
| t.Skip(err) | ||
| } | ||
|
|
||
| unit := getUnitFile(units, target1) | ||
|
|
||
| if unit == nil { | ||
| t.Fatalf("%s unit not found in list", target1) | ||
| } else if unit.Type != "static" { | ||
| t.Fatalf("Test unit file should be static") | ||
| } | ||
|
|
||
| units, err = conn.ListUnitFilesByPatterns([]string{"disabled"}, []string{"systemd-journald*", target2}) | ||
|
|
||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| unit = getUnitFile(units, target2) | ||
|
|
||
| if unit == nil { | ||
| t.Fatalf("%s unit not found in list", target2) | ||
| } else if unit.Type != "disabled" { | ||
| t.Fatalf("%s unit file should be disabled", target2) | ||
| } | ||
| } | ||
|
|
||
| func TestListUnitFiles(t *testing.T) { | ||
| target1 := "systemd-journald.service" | ||
| target2 := "exit.target" | ||
|
|
||
| conn := setupConn(t) | ||
|
|
||
| units, err := conn.ListUnitFiles() | ||
|
|
||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| unit := getUnitFile(units, target1) | ||
|
|
||
| if unit == nil { | ||
| t.Fatalf("%s unit not found in list", target1) | ||
| } else if unit.Type != "static" { | ||
| t.Fatalf("Test unit file should be static") | ||
| } | ||
|
|
||
| unit = getUnitFile(units, target2) | ||
|
|
||
| if unit == nil { | ||
| t.Fatalf("%s unit not found in list", target2) | ||
| } else if unit.Type != "disabled" { | ||
| t.Fatalf("%s unit file should be disabled", target2) | ||
| } | ||
| } | ||
|
|
||
| // Enables a unit and then immediately tears it down | ||
| func TestEnableDisableUnit(t *testing.T) { | ||
| target := "enable-disable.service" | ||
|
|
@@ -298,18 +461,11 @@ func TestStartStopTransientUnit(t *testing.T) { | |
|
|
||
| units, err := conn.ListUnits() | ||
|
|
||
| var unit *UnitStatus | ||
| for _, u := range units { | ||
| if u.Name == target { | ||
| unit = &u | ||
| } | ||
| } | ||
| unit := getUnitStatus(units, target) | ||
|
|
||
| if unit == nil { | ||
| t.Fatalf("Test unit not found in list") | ||
| } | ||
|
|
||
| if unit.ActiveState != "active" { | ||
| } else if unit.ActiveState != "active" { | ||
| t.Fatalf("Test unit not active") | ||
| } | ||
|
|
||
|
|
@@ -324,12 +480,7 @@ func TestStartStopTransientUnit(t *testing.T) { | |
|
|
||
| units, err = conn.ListUnits() | ||
|
|
||
| unit = nil | ||
| for _, u := range units { | ||
| if u.Name == target { | ||
| unit = &u | ||
| } | ||
| } | ||
| unit = getUnitStatus(units, target) | ||
|
|
||
| if unit != nil { | ||
| t.Fatalf("Test unit found in list, should be stopped") | ||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why should it be found in the list? this is counterintuitive the way it's written
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jonboulle take a look on this comment https://github.com/coreos/go-systemd/pull/150/files#diff-b5b8384be79564335a3b50bcfed63e91R293
systemd should return unit states for non-existing units.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh... confusing
On Tue, May 31, 2016 at 5:34 PM, kayrus [email protected] wrote:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jonboulle yep I confirm it's a bit confusing, but that's how systemd works actually please check this comment systemd/systemd#3182 (comment) so basically the implementation doesn't bother it's just gives you back what you have requested setting up the right state of course.
Now this test can be updated to be more readable:
just create a target1 and target2 object which has {unit_name, activestate, a bolean found or not} , do the whole check inside the same loop, and check after the loop if the units have been found or not. The doc of ListUnitsByNames() clearly states that it may return results for even non-existent units which is good.