update_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. "io"
  17. "reflect"
  18. "strings"
  19. "testing"
  20. "github.com/rancher/os/config/cloudinit/config"
  21. )
  22. func testReadConfig(config string) func() (io.Reader, error) {
  23. return func() (io.Reader, error) {
  24. return strings.NewReader(config), nil
  25. }
  26. }
  27. func TestUpdateUnits(t *testing.T) {
  28. for _, tt := range []struct {
  29. config config.Update
  30. units []Unit
  31. err error
  32. }{
  33. {
  34. config: config.Update{},
  35. },
  36. {
  37. config: config.Update{Group: "master", Server: "http://foo.com"},
  38. units: []Unit{{config.Unit{
  39. Name: "update-engine.service",
  40. Command: "restart",
  41. }}},
  42. },
  43. {
  44. config: config.Update{RebootStrategy: "best-effort"},
  45. units: []Unit{{config.Unit{
  46. Name: "locksmithd.service",
  47. Command: "restart",
  48. Runtime: true,
  49. }}},
  50. },
  51. {
  52. config: config.Update{RebootStrategy: "etcd-lock"},
  53. units: []Unit{{config.Unit{
  54. Name: "locksmithd.service",
  55. Command: "restart",
  56. Runtime: true,
  57. }}},
  58. },
  59. {
  60. config: config.Update{RebootStrategy: "reboot"},
  61. units: []Unit{{config.Unit{
  62. Name: "locksmithd.service",
  63. Command: "restart",
  64. Runtime: true,
  65. }}},
  66. },
  67. {
  68. config: config.Update{RebootStrategy: "off"},
  69. units: []Unit{{config.Unit{
  70. Name: "locksmithd.service",
  71. Command: "stop",
  72. Runtime: true,
  73. Mask: true,
  74. }}},
  75. },
  76. } {
  77. units := Update{Update: tt.config, ReadConfig: testReadConfig("")}.Units()
  78. if !reflect.DeepEqual(tt.units, units) {
  79. t.Errorf("bad units (%q): want %#v, got %#v", tt.config, tt.units, units)
  80. }
  81. }
  82. }
  83. func TestUpdateFile(t *testing.T) {
  84. for _, tt := range []struct {
  85. config config.Update
  86. orig string
  87. file *File
  88. err error
  89. }{
  90. {
  91. config: config.Update{},
  92. },
  93. {
  94. config: config.Update{RebootStrategy: "wizzlewazzle"},
  95. err: &config.ErrorValid{Value: "wizzlewazzle", Field: "RebootStrategy", Valid: "^(best-effort|etcd-lock|reboot|off)$"},
  96. },
  97. {
  98. config: config.Update{Group: "master", Server: "http://foo.com"},
  99. file: &File{config.File{
  100. Content: "GROUP=master\nSERVER=http://foo.com\n",
  101. Path: "etc/coreos/update.conf",
  102. RawFilePermissions: "0644",
  103. }},
  104. },
  105. {
  106. config: config.Update{RebootStrategy: "best-effort"},
  107. file: &File{config.File{
  108. Content: "REBOOT_STRATEGY=best-effort\n",
  109. Path: "etc/coreos/update.conf",
  110. RawFilePermissions: "0644",
  111. }},
  112. },
  113. {
  114. config: config.Update{RebootStrategy: "etcd-lock"},
  115. file: &File{config.File{
  116. Content: "REBOOT_STRATEGY=etcd-lock\n",
  117. Path: "etc/coreos/update.conf",
  118. RawFilePermissions: "0644",
  119. }},
  120. },
  121. {
  122. config: config.Update{RebootStrategy: "reboot"},
  123. file: &File{config.File{
  124. Content: "REBOOT_STRATEGY=reboot\n",
  125. Path: "etc/coreos/update.conf",
  126. RawFilePermissions: "0644",
  127. }},
  128. },
  129. {
  130. config: config.Update{RebootStrategy: "off"},
  131. file: &File{config.File{
  132. Content: "REBOOT_STRATEGY=off\n",
  133. Path: "etc/coreos/update.conf",
  134. RawFilePermissions: "0644",
  135. }},
  136. },
  137. {
  138. config: config.Update{RebootStrategy: "etcd-lock"},
  139. orig: "SERVER=https://example.com\nGROUP=thegroupc\nREBOOT_STRATEGY=awesome",
  140. file: &File{config.File{
  141. Content: "SERVER=https://example.com\nGROUP=thegroupc\nREBOOT_STRATEGY=etcd-lock\n",
  142. Path: "etc/coreos/update.conf",
  143. RawFilePermissions: "0644",
  144. }},
  145. },
  146. } {
  147. file, err := Update{Update: tt.config, ReadConfig: testReadConfig(tt.orig)}.File()
  148. if !reflect.DeepEqual(tt.err, err) {
  149. t.Errorf("bad error (%q): want %q, got %q", tt.config, tt.err, err)
  150. }
  151. if !reflect.DeepEqual(tt.file, file) {
  152. t.Errorf("bad units (%q): want %#v, got %#v", tt.config, tt.file, file)
  153. }
  154. }
  155. }