config_test.go 8.0 KB

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