systemd_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package system_service
  15. import (
  16. "testing"
  17. )
  18. func TestParseSystemd(t *testing.T) {
  19. cases := []struct {
  20. in string
  21. inIsEnabled string
  22. name string
  23. wantLoaded bool
  24. wantActive bool
  25. wantEnabled bool
  26. }{
  27. {
  28. in: `openvswitch.service - LSB: Open vSwitch switch
  29. Loaded: loaded (/etc/rc.d/init.d/openvswitch; bad; vendor preset: disabled)
  30. Active: active (running) since Wed 2019-04-10 15:11:23 CST; 4 days ago
  31. Docs: man:systemd-sysv-generator(8)
  32. Process: 3686 ExecStart=/etc/rc.d/init.d/openvswitch start (code=exited, status=0/SUCCESS)
  33. CGroup: /system.slice/openvswitch.service
  34. ├─3722 ovsdb-server: monitoring pid 3723 (healthy)
  35. ├─3723 ovsdb-server /etc/openvswitch/conf.db -vconsole:emer -vsysl...
  36. ├─3774 ovs-vswitchd: monitoring pid 3775 (healthy)
  37. └─3775 ovs-vswitchd unix:/var/run/openvswitch/db.sock -vconsole:em...`,
  38. name: "openvswitch",
  39. wantLoaded: true,
  40. wantActive: true,
  41. },
  42. {
  43. in: "Unit yunion-host.service could not be found.",
  44. name: "yunion-host",
  45. wantLoaded: false,
  46. wantActive: false,
  47. },
  48. {
  49. name: "enabled service",
  50. inIsEnabled: "a\nb\nenabled\n",
  51. wantEnabled: true,
  52. },
  53. {
  54. name: "enabled-runtime service",
  55. inIsEnabled: "a\nb\nenabled-runtime\n",
  56. wantEnabled: true,
  57. },
  58. }
  59. for _, c := range cases {
  60. status := parseSystemdStatus(c.in, c.inIsEnabled, c.name)
  61. if status.Loaded != c.wantLoaded {
  62. t.Errorf("parseSystemdStatus %s Loaded: want %v got %v", c.name, c.wantLoaded, status.Loaded)
  63. }
  64. if status.Active != c.wantActive {
  65. t.Errorf("parseSystemdStatus %s Active: want %v got %v", c.name, c.wantActive, status.Active)
  66. }
  67. if status.Enabled != c.wantEnabled {
  68. t.Errorf("parseSystemdStatus %s Enabled: want %v got %v", c.name, c.wantActive, status.Enabled)
  69. }
  70. }
  71. }