| // Copyright 2021 Google LLC | |
| // | |
| // Licensed under the Apache License, Version 2.0 (the "License"); | |
| // you may not use this file except in compliance with the License. | |
| // You may obtain a copy of the License at | |
| // | |
| // http://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, software | |
| // distributed under the License is distributed on an "AS IS" BASIS, | |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| // See the License for the specific language governing permissions and | |
| // limitations under the License. | |
| package systemd | |
| import ( | |
| "errors" | |
| "fmt" | |
| "testing" | |
| ) | |
| const systemctlCmd = "testdata/systemctl.sh" | |
| func TestIsUnitActiveRunning(t *testing.T) { | |
| tests := []struct { | |
| name string | |
| unitName string | |
| simulatedOutput string | |
| isActive bool | |
| cmdErr error | |
| expectActiveRunning bool | |
| expectError bool | |
| }{ | |
| { | |
| name: "UnitIsActiveRunning", | |
| unitName: "stackdriver-logging.service", | |
| simulatedOutput: "active", | |
| isActive: true, | |
| cmdErr: nil, | |
| expectActiveRunning: true, | |
| expectError: false, | |
| }, | |
| { | |
| name: "UnitIsActiveNotRunning", | |
| unitName: "node-problem-detector.service", | |
| simulatedOutput: "active", | |
| isActive: true, | |
| cmdErr: nil, | |
| expectActiveRunning: true, | |
| expectError: false, | |
| }, | |
| { | |
| name: "UnitIsInactiveNotRunning", | |
| unitName: "node-problem-detector.service", | |
| simulatedOutput: "inactive", | |
| isActive: false, | |
| cmdErr: nil, | |
| expectActiveRunning: false, | |
| expectError: false, | |
| }, | |
| { | |
| name: "UnitIsFailed", | |
| unitName: "node-problem-detector.service", | |
| simulatedOutput: "failed", | |
| isActive: false, | |
| cmdErr: nil, | |
| expectActiveRunning: false, | |
| expectError: false, | |
| }, | |
| { | |
| name: "UnitIsActivating", | |
| unitName: "node-problem-detector.service", | |
| simulatedOutput: "activating", | |
| isActive: false, | |
| cmdErr: nil, | |
| expectActiveRunning: false, | |
| expectError: false, | |
| }, | |
| { | |
| name: "NotExistUnit", | |
| unitName: "thisisnotaunit.nonunit", | |
| simulatedOutput: "unknown", | |
| isActive: false, | |
| cmdErr: nil, | |
| expectActiveRunning: false, | |
| expectError: false, | |
| }, | |
| { | |
| name: "IgnoreOutputWhenCommandFailed", | |
| unitName: "node-problem-detector.service", | |
| simulatedOutput: "active", | |
| isActive: true, | |
| cmdErr: errors.New("Some error."), | |
| expectActiveRunning: false, | |
| expectError: true, | |
| }, | |
| } | |
| for _, test := range tests { | |
| t.Run(test.name, func(t *testing.T) { | |
| systemdClient := NewSystemdClient(systemctlCmd) | |
| unitName := fmt.Sprintf("%s,%t,%v,%s", test.unitName, test.isActive, test.cmdErr, test.simulatedOutput) | |
| activeRunning, err := systemdClient.IsUnitActiveRunning(unitName) | |
| if activeRunning != test.expectActiveRunning { | |
| t.Errorf("got ActiveState %v, expect to have ActiveState %v", | |
| activeRunning, test.expectActiveRunning) | |
| } | |
| if err != nil && !test.expectError { | |
| t.Errorf("got unexpected error: %v", err) | |
| } else if err == nil && test.expectError { | |
| t.Error("didn't get expected error") | |
| } | |
| }) | |
| } | |
| } | |
| func TestStartUnit(t *testing.T) { | |
| tests := []struct { | |
| name string | |
| unitName string | |
| cmdErr error | |
| expectError bool | |
| }{ | |
| { | |
| name: "StartSuccess", | |
| unitName: "stackdriver-logging.service", | |
| cmdErr: nil, | |
| expectError: false, | |
| }, | |
| { | |
| name: "WontReportFailureWhenServiceFailedToStart", | |
| unitName: "failed-to-start.service", | |
| cmdErr: nil, | |
| expectError: false, | |
| }, | |
| { | |
| name: "UnitIsAlreadyStarted", | |
| unitName: "already-started.service", | |
| cmdErr: nil, | |
| expectError: false, | |
| }, | |
| { | |
| name: "WrongUnitName", | |
| unitName: "thisisnotaunit.nonunit", | |
| cmdErr: errors.New("exit status 5"), | |
| expectError: true, | |
| }, | |
| { | |
| name: "SystemctlError", | |
| unitName: "node-problem-detector.service", | |
| cmdErr: errors.New("Some error."), | |
| expectError: true, | |
| }, | |
| } | |
| for _, test := range tests { | |
| t.Run(test.name, func(t *testing.T) { | |
| systemdClient := NewSystemdClient(systemctlCmd) | |
| unitName := fmt.Sprintf("%v,%v", test.unitName, test.cmdErr) | |
| err := systemdClient.StartUnit(unitName) | |
| if err != nil && !test.expectError { | |
| t.Errorf("got unexpected error: %v", err) | |
| } else if err == nil && test.expectError { | |
| t.Error("didn't get expected error") | |
| } | |
| }) | |
| } | |
| } | |
| func TestStopUnit(t *testing.T) { | |
| tests := []struct { | |
| name string | |
| unitName string | |
| cmdErr error | |
| expectError bool | |
| }{ | |
| { | |
| name: "StopSuccess", | |
| unitName: "stackdriver-logging.service", | |
| cmdErr: nil, | |
| expectError: false, | |
| }, | |
| { | |
| name: "ReportFailureWhenServiceFailedToStop", | |
| unitName: "stackdriver-logging.service", | |
| cmdErr: errors.New("exit status 137"), | |
| expectError: true, | |
| }, | |
| { | |
| name: "UnitIsAlreadyStopped", | |
| unitName: "already-stopped.service", | |
| cmdErr: nil, | |
| expectError: false, | |
| }, | |
| { | |
| name: "WrongUnitName", | |
| unitName: "thisisnotaunit.nonunit", | |
| cmdErr: errors.New("exit status 5"), | |
| expectError: true, | |
| }, | |
| { | |
| name: "SystemctlError", | |
| unitName: "node-problem-detector.service", | |
| cmdErr: errors.New("Some error."), | |
| expectError: true, | |
| }, | |
| } | |
| for _, test := range tests { | |
| t.Run(test.name, func(t *testing.T) { | |
| systemdClient := NewSystemdClient(systemctlCmd) | |
| unitName := fmt.Sprintf("%v,%v", test.unitName, test.cmdErr) | |
| err := systemdClient.StopUnit(unitName) | |
| if err != nil && !test.expectError { | |
| t.Errorf("got unexpected error: %v", err) | |
| } else if err == nil && test.expectError { | |
| t.Error("didn't get expected error") | |
| } | |
| }) | |
| } | |
| } |