unit_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright 2015 CoreOS, Inc.
  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
  15. import (
  16. "testing"
  17. "github.com/rancher/os/config/cloudinit/config"
  18. )
  19. func TestType(t *testing.T) {
  20. tests := []struct {
  21. name string
  22. typ string
  23. }{
  24. {},
  25. {"test.service", "service"},
  26. {"hello", ""},
  27. {"lots.of.dots", "dots"},
  28. }
  29. for _, tt := range tests {
  30. u := Unit{config.Unit{
  31. Name: tt.name,
  32. }}
  33. if typ := u.Type(); tt.typ != typ {
  34. t.Errorf("bad type (%+v): want %q, got %q", tt, tt.typ, typ)
  35. }
  36. }
  37. }
  38. func TestGroup(t *testing.T) {
  39. tests := []struct {
  40. name string
  41. group string
  42. }{
  43. {"test.service", "system"},
  44. {"test.link", "network"},
  45. {"test.network", "network"},
  46. {"test.netdev", "network"},
  47. {"test.conf", "system"},
  48. }
  49. for _, tt := range tests {
  50. u := Unit{config.Unit{
  51. Name: tt.name,
  52. }}
  53. if group := u.Group(); tt.group != group {
  54. t.Errorf("bad group (%+v): want %q, got %q", tt, tt.group, group)
  55. }
  56. }
  57. }
  58. func TestDestination(t *testing.T) {
  59. tests := []struct {
  60. root string
  61. name string
  62. runtime bool
  63. destination string
  64. }{
  65. {
  66. root: "/some/dir",
  67. name: "foobar.service",
  68. destination: "/some/dir/etc/systemd/system/foobar.service",
  69. },
  70. {
  71. root: "/some/dir",
  72. name: "foobar.service",
  73. runtime: true,
  74. destination: "/some/dir/run/systemd/system/foobar.service",
  75. },
  76. }
  77. for _, tt := range tests {
  78. u := Unit{config.Unit{
  79. Name: tt.name,
  80. Runtime: tt.runtime,
  81. }}
  82. if d := u.Destination(tt.root); tt.destination != d {
  83. t.Errorf("bad destination (%+v): want %q, got %q", tt, tt.destination, d)
  84. }
  85. }
  86. }
  87. func TestDropInDestination(t *testing.T) {
  88. tests := []struct {
  89. root string
  90. unitName string
  91. dropInName string
  92. runtime bool
  93. destination string
  94. }{
  95. {
  96. root: "/some/dir",
  97. unitName: "foo.service",
  98. dropInName: "bar.conf",
  99. destination: "/some/dir/etc/systemd/system/foo.service.d/bar.conf",
  100. },
  101. {
  102. root: "/some/dir",
  103. unitName: "foo.service",
  104. dropInName: "bar.conf",
  105. runtime: true,
  106. destination: "/some/dir/run/systemd/system/foo.service.d/bar.conf",
  107. },
  108. }
  109. for _, tt := range tests {
  110. u := Unit{config.Unit{
  111. Name: tt.unitName,
  112. Runtime: tt.runtime,
  113. DropIns: []config.UnitDropIn{{
  114. Name: tt.dropInName,
  115. }},
  116. }}
  117. if d := u.DropInDestination(tt.root, u.DropIns[0]); tt.destination != d {
  118. t.Errorf("bad destination (%+v): want %q, got %q", tt, tt.destination, d)
  119. }
  120. }
  121. }