@@ -132,6 +132,19 @@ func runStopUnit(t *testing.T, conn *Conn, trTarget TrUnitProp) error {
132132 return nil
133133}
134134
135+ func getJobStatusIfExists (jobs []JobStatus , jobName string ) * JobStatus {
136+ for _ , j := range jobs {
137+ if j .Unit == jobName {
138+ return & j
139+ }
140+ }
141+ return nil
142+ }
143+
144+ func isJobStatusEmpty (job JobStatus ) bool {
145+ return job .Id == 0 && job .Unit == "" && job .JobType == "" && job .Status == "" && job .JobPath == "" && job .UnitPath == ""
146+ }
147+
135148// Ensure that basic unit starting and stopping works.
136149func TestStartStopUnit (t * testing.T ) {
137150 target := "start-stop.service"
@@ -642,6 +655,54 @@ func TestEnableDisableUnit(t *testing.T) {
642655 }
643656}
644657
658+ // Ensure that ListJobs works.
659+ func TestListJobs (t * testing.T ) {
660+ service := "oneshot.service"
661+
662+ conn := setupConn (t )
663+
664+ setupUnit (service , conn , t )
665+ linkUnit (service , conn , t )
666+
667+ _ , err := conn .StartUnit (service , "replace" , nil )
668+ if err != nil {
669+ t .Fatal (err )
670+ }
671+
672+ jobs , err := conn .ListJobs ()
673+ if err != nil {
674+ t .Skip (err )
675+ }
676+
677+ found := getJobStatusIfExists (jobs , service )
678+ if found == nil {
679+ t .Fatalf ("%s job not found in list" , service )
680+ }
681+
682+ if isJobStatusEmpty (* found ) {
683+ t .Fatalf ("empty %s job found in list" , service )
684+ }
685+
686+ reschan := make (chan string )
687+ _ , err = conn .StopUnit (service , "replace" , reschan )
688+ if err != nil {
689+ t .Fatal (err )
690+ }
691+
692+ <- reschan
693+
694+ jobs , err = conn .ListJobs ()
695+
696+ found = getJobStatusIfExists (jobs , service )
697+ if err != nil {
698+ t .Fatal (err )
699+ }
700+
701+ if found != nil {
702+ t .Fatalf ("%s job found in list when it shouldn't" , service )
703+ }
704+ }
705+
645706// TestSystemState tests if system state is one of the valid states
646707func TestSystemState (t * testing.T ) {
647708 conn := setupConn (t )
0 commit comments