config_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package config
  2. import (
  3. "testing"
  4. yaml "github.com/cloudfoundry-incubator/candiedyaml"
  5. "github.com/rancher/os/config/cmdline"
  6. "github.com/rancher/os/util"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestFilterKey(t *testing.T) {
  10. assert := require.New(t)
  11. data := map[interface{}]interface{}{
  12. "ssh_authorized_keys": []string{"pubk1", "pubk2"},
  13. "hostname": "ros-test",
  14. "rancher": map[interface{}]interface{}{
  15. "ssh": map[interface{}]interface{}{
  16. "keys": map[interface{}]interface{}{
  17. "dsa": "dsa-test1",
  18. "dsa-pub": "dsa-test2",
  19. },
  20. },
  21. "docker": map[interface{}]interface{}{
  22. "ca_key": "ca_key-test3",
  23. "ca_cert": "ca_cert-test4",
  24. "args": []string{"args_test5"},
  25. },
  26. },
  27. }
  28. expectedFiltered := map[interface{}]interface{}{
  29. "rancher": map[interface{}]interface{}{
  30. "ssh": map[interface{}]interface{}{
  31. "keys": map[interface{}]interface{}{
  32. "dsa": "dsa-test1",
  33. "dsa-pub": "dsa-test2",
  34. },
  35. },
  36. },
  37. }
  38. expectedRest := map[interface{}]interface{}{
  39. "ssh_authorized_keys": []string{"pubk1", "pubk2"},
  40. "hostname": "ros-test",
  41. "rancher": map[interface{}]interface{}{
  42. "docker": map[interface{}]interface{}{
  43. "ca_key": "ca_key-test3",
  44. "ca_cert": "ca_cert-test4",
  45. "args": []string{"args_test5"},
  46. },
  47. },
  48. }
  49. filtered, rest := filterKey(data, []string{"rancher", "ssh"})
  50. assert.Equal(expectedFiltered, filtered)
  51. assert.Equal(expectedRest, rest)
  52. }
  53. func TestUnmarshalOrReturnString(t *testing.T) {
  54. assert := require.New(t)
  55. assert.Equal("ab", cmdline.UnmarshalOrReturnString("ab"))
  56. assert.Equal("a\nb", cmdline.UnmarshalOrReturnString("a\nb"))
  57. assert.Equal("a\n", cmdline.UnmarshalOrReturnString("a\n"))
  58. assert.Equal("\nb", cmdline.UnmarshalOrReturnString("\nb"))
  59. assert.Equal("a,b", cmdline.UnmarshalOrReturnString("a,b"))
  60. assert.Equal("a,", cmdline.UnmarshalOrReturnString("a,"))
  61. assert.Equal(",b", cmdline.UnmarshalOrReturnString(",b"))
  62. assert.Equal(int64(10), cmdline.UnmarshalOrReturnString("10"))
  63. assert.Equal(true, cmdline.UnmarshalOrReturnString("true"))
  64. assert.Equal(false, cmdline.UnmarshalOrReturnString("false"))
  65. assert.Equal([]interface{}{"a"}, cmdline.UnmarshalOrReturnString("[a]"))
  66. assert.Equal([]interface{}{"a"}, cmdline.UnmarshalOrReturnString("[\"a\"]"))
  67. assert.Equal([]interface{}{"a,"}, cmdline.UnmarshalOrReturnString("[\"a,\"]"))
  68. assert.Equal([]interface{}{" a, "}, cmdline.UnmarshalOrReturnString("[\" a, \"]"))
  69. assert.Equal([]interface{}{",a"}, cmdline.UnmarshalOrReturnString("[\",a\"]"))
  70. assert.Equal([]interface{}{" ,a "}, cmdline.UnmarshalOrReturnString("[\" ,a \"]"))
  71. assert.Equal([]interface{}{"a\n"}, cmdline.UnmarshalOrReturnString("[\"a\n\"]"))
  72. assert.Equal([]interface{}{" a\n "}, cmdline.UnmarshalOrReturnString("[\" a\n \"]"))
  73. assert.Equal([]interface{}{"\na"}, cmdline.UnmarshalOrReturnString("[\"\na\"]"))
  74. assert.Equal([]interface{}{" \na "}, cmdline.UnmarshalOrReturnString("[\" \na \"]"))
  75. assert.Equal([]interface{}{"a", "b"}, cmdline.UnmarshalOrReturnString("[a,b]"))
  76. assert.Equal([]interface{}{"a", "b"}, cmdline.UnmarshalOrReturnString("[\"a\",\"b\"]"))
  77. assert.Equal([]interface{}{"a,", "b"}, cmdline.UnmarshalOrReturnString("[\"a,\",b]"))
  78. assert.Equal([]interface{}{"a", ",b"}, cmdline.UnmarshalOrReturnString("[a,\",b\"]"))
  79. assert.Equal([]interface{}{" a, ", " ,b "}, cmdline.UnmarshalOrReturnString("[\" a, \",\" ,b \"]"))
  80. assert.Equal([]interface{}{"a\n", "b"}, cmdline.UnmarshalOrReturnString("[\"a\n\",b]"))
  81. assert.Equal([]interface{}{"a", "\nb"}, cmdline.UnmarshalOrReturnString("[a,\"\nb\"]"))
  82. assert.Equal([]interface{}{" a\n ", " \nb "}, cmdline.UnmarshalOrReturnString("[\" a\n \",\" \nb \"]"))
  83. assert.Equal([]interface{}{"a", int64(10)}, cmdline.UnmarshalOrReturnString("[a,10]"))
  84. assert.Equal([]interface{}{int64(10), "a"}, cmdline.UnmarshalOrReturnString("[10,a]"))
  85. assert.Equal([]interface{}{"a", true}, cmdline.UnmarshalOrReturnString("[a,true]"))
  86. assert.Equal([]interface{}{false, "a"}, cmdline.UnmarshalOrReturnString("[false,a]"))
  87. }
  88. func TestCmdlineParse(t *testing.T) {
  89. assert := require.New(t)
  90. assert.Equal(map[interface{}]interface{}{
  91. "rancher": map[interface{}]interface{}{
  92. "key1": "value1",
  93. "key2": "value2",
  94. },
  95. }, cmdline.Parse("a b rancher.key1=value1 c rancher.key2=value2", false), false)
  96. assert.Equal(map[interface{}]interface{}{
  97. "rancher": map[interface{}]interface{}{
  98. "key": "a,b",
  99. },
  100. }, cmdline.Parse("rancher.key=a,b", false), false)
  101. assert.Equal(map[interface{}]interface{}{
  102. "rancher": map[interface{}]interface{}{
  103. "key": "a\nb",
  104. },
  105. }, cmdline.Parse("rancher.key=a\nb", false), false)
  106. assert.Equal(map[interface{}]interface{}{
  107. "rancher": map[interface{}]interface{}{
  108. "key": "a:b",
  109. },
  110. }, cmdline.Parse("rancher.key=a:b", false), false)
  111. assert.Equal(map[interface{}]interface{}{
  112. "rancher": map[interface{}]interface{}{
  113. "key": int64(5),
  114. },
  115. }, cmdline.Parse("rancher.key=5", false), false)
  116. assert.Equal(map[interface{}]interface{}{
  117. "rancher": map[interface{}]interface{}{
  118. "rescue": true,
  119. },
  120. }, cmdline.Parse("rancher.rescue", false), false)
  121. assert.Equal(map[interface{}]interface{}{
  122. "rancher": map[interface{}]interface{}{
  123. "keyArray": []interface{}{int64(1), int64(2)},
  124. },
  125. }, cmdline.Parse("rancher.keyArray=[1,2]", false), false)
  126. assert.Equal(map[interface{}]interface{}{
  127. "rancher": map[interface{}]interface{}{
  128. "strArray": []interface{}{"url:http://192.168.1.100/cloud-config?a=b"},
  129. },
  130. }, cmdline.Parse("rancher.strArray=[\"url:http://192.168.1.100/cloud-config?a=b\"]", false), false)
  131. assert.Equal(map[interface{}]interface{}{
  132. "rancher": map[interface{}]interface{}{
  133. "strArray": []interface{}{"url:http://192.168.1.100/cloud-config?a=b"},
  134. },
  135. }, cmdline.Parse("rancher.strArray=[url:http://192.168.1.100/cloud-config?a=b]", false), false)
  136. }
  137. func TestGet(t *testing.T) {
  138. assert := require.New(t)
  139. data := map[interface{}]interface{}{
  140. "key": "value",
  141. "rancher": map[interface{}]interface{}{
  142. "key2": map[interface{}]interface{}{
  143. "subkey": "subvalue",
  144. "subnum": 42,
  145. },
  146. },
  147. }
  148. tests := map[string]interface{}{
  149. "key": "value",
  150. "rancher.key2.subkey": "subvalue",
  151. "rancher.key2.subnum": 42,
  152. "rancher.key2.subkey2": "",
  153. "foo": "",
  154. }
  155. for k, v := range tests {
  156. val, _ := cmdline.GetOrSetVal(k, data, nil)
  157. assert.Equal(v, val)
  158. }
  159. }
  160. func TestSet(t *testing.T) {
  161. assert := require.New(t)
  162. data := map[interface{}]interface{}{
  163. "key": "value",
  164. "rancher": map[interface{}]interface{}{
  165. "key2": map[interface{}]interface{}{
  166. "subkey": "subvalue",
  167. "subnum": 42,
  168. },
  169. },
  170. }
  171. expected := map[interface{}]interface{}{
  172. "key": "value2",
  173. "rancher": map[interface{}]interface{}{
  174. "key2": map[interface{}]interface{}{
  175. "subkey": "subvalue2",
  176. "subkey2": "value",
  177. "subkey3": 43,
  178. "subnum": 42,
  179. },
  180. "key3": map[interface{}]interface{}{
  181. "subkey3": 44,
  182. },
  183. },
  184. "key4": "value4",
  185. }
  186. tests := map[string]interface{}{
  187. "key": "value2",
  188. "rancher.key2.subkey": "subvalue2",
  189. "rancher.key2.subkey2": "value",
  190. "rancher.key2.subkey3": 43,
  191. "rancher.key3.subkey3": 44,
  192. "key4": "value4",
  193. }
  194. for k, v := range tests {
  195. _, tData := cmdline.GetOrSetVal(k, data, v)
  196. val, _ := cmdline.GetOrSetVal(k, tData, nil)
  197. data = tData
  198. assert.Equal(v, val)
  199. }
  200. assert.Equal(expected, data)
  201. }
  202. type OuterData struct {
  203. One Data `yaml:"one"`
  204. }
  205. type Data struct {
  206. Two bool `yaml:"two"`
  207. Three bool `yaml:"three"`
  208. }
  209. func TestMapMerge(t *testing.T) {
  210. assert := require.New(t)
  211. one := `
  212. one:
  213. two: true`
  214. two := `
  215. one:
  216. three: true`
  217. data := map[string]map[string]bool{}
  218. yaml.Unmarshal([]byte(one), &data)
  219. yaml.Unmarshal([]byte(two), &data)
  220. assert.NotNil(data["one"])
  221. assert.True(data["one"]["three"])
  222. assert.False(data["one"]["two"])
  223. data2 := &OuterData{}
  224. yaml.Unmarshal([]byte(one), data2)
  225. yaml.Unmarshal([]byte(two), data2)
  226. assert.True(data2.One.Three)
  227. assert.True(data2.One.Two)
  228. }
  229. func TestUserDocker(t *testing.T) {
  230. assert := require.New(t)
  231. config := &CloudConfig{
  232. Rancher: RancherConfig{
  233. Docker: DockerConfig{
  234. TLS: true,
  235. },
  236. },
  237. }
  238. bytes, err := yaml.Marshal(config)
  239. assert.Nil(err)
  240. config = &CloudConfig{}
  241. assert.False(config.Rancher.Docker.TLS)
  242. err = yaml.Unmarshal(bytes, config)
  243. assert.Nil(err)
  244. assert.True(config.Rancher.Docker.TLS)
  245. data := map[interface{}]interface{}{}
  246. err = util.Convert(config, &data)
  247. assert.Nil(err)
  248. val, ok := data["rancher"].(map[interface{}]interface{})["docker"]
  249. assert.True(ok)
  250. m, ok := val.(map[interface{}]interface{})
  251. assert.True(ok)
  252. v, ok := m["tls"]
  253. assert.True(ok)
  254. assert.True(v.(bool))
  255. }