config_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package config
  2. import (
  3. yaml "github.com/cloudfoundry-incubator/candiedyaml"
  4. "testing"
  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. expected := map[interface{}]interface{}{
  90. "rancher": map[interface{}]interface{}{
  91. "rescue": true,
  92. "key1": "value1",
  93. "key2": "value2",
  94. "keyArray": []interface{}{int64(1), int64(2)},
  95. "obj1": map[interface{}]interface{}{
  96. "key3": "3value",
  97. "obj2": map[interface{}]interface{}{
  98. "key4": true,
  99. },
  100. },
  101. "key5": int64(5),
  102. "key6": "a,b",
  103. "key7": "a\nb",
  104. },
  105. }
  106. actual := parseCmdline("a b rancher.rescue rancher.keyArray=[1,2] rancher.key1=value1 c rancher.key2=value2 rancher.obj1.key3=3value rancher.obj1.obj2.key4 rancher.key5=5 rancher.key6=a,b rancher.key7=a\nb")
  107. assert.Equal(expected, actual)
  108. }
  109. func TestGet(t *testing.T) {
  110. assert := require.New(t)
  111. data := map[interface{}]interface{}{
  112. "key": "value",
  113. "rancher": map[interface{}]interface{}{
  114. "key2": map[interface{}]interface{}{
  115. "subkey": "subvalue",
  116. "subnum": 42,
  117. },
  118. },
  119. }
  120. tests := map[string]interface{}{
  121. "key": "value",
  122. "rancher.key2.subkey": "subvalue",
  123. "rancher.key2.subnum": 42,
  124. "rancher.key2.subkey2": "",
  125. "foo": "",
  126. }
  127. for k, v := range tests {
  128. val, _ := getOrSetVal(k, data, nil)
  129. assert.Equal(v, val)
  130. }
  131. }
  132. func TestSet(t *testing.T) {
  133. assert := require.New(t)
  134. data := map[interface{}]interface{}{
  135. "key": "value",
  136. "rancher": map[interface{}]interface{}{
  137. "key2": map[interface{}]interface{}{
  138. "subkey": "subvalue",
  139. "subnum": 42,
  140. },
  141. },
  142. }
  143. expected := map[interface{}]interface{}{
  144. "key": "value2",
  145. "rancher": map[interface{}]interface{}{
  146. "key2": map[interface{}]interface{}{
  147. "subkey": "subvalue2",
  148. "subkey2": "value",
  149. "subkey3": 43,
  150. "subnum": 42,
  151. },
  152. "key3": map[interface{}]interface{}{
  153. "subkey3": 44,
  154. },
  155. },
  156. "key4": "value4",
  157. }
  158. tests := map[string]interface{}{
  159. "key": "value2",
  160. "rancher.key2.subkey": "subvalue2",
  161. "rancher.key2.subkey2": "value",
  162. "rancher.key2.subkey3": 43,
  163. "rancher.key3.subkey3": 44,
  164. "key4": "value4",
  165. }
  166. for k, v := range tests {
  167. _, tData := getOrSetVal(k, data, v)
  168. val, _ := getOrSetVal(k, tData, nil)
  169. data = tData
  170. assert.Equal(v, val)
  171. }
  172. assert.Equal(expected, data)
  173. }
  174. type OuterData struct {
  175. One Data `"yaml:one"`
  176. }
  177. type Data struct {
  178. Two bool `"yaml:two"`
  179. Three bool `"yaml:three"`
  180. }
  181. func TestMapMerge(t *testing.T) {
  182. assert := require.New(t)
  183. one := `
  184. one:
  185. two: true`
  186. two := `
  187. one:
  188. three: true`
  189. data := map[string]map[string]bool{}
  190. yaml.Unmarshal([]byte(one), &data)
  191. yaml.Unmarshal([]byte(two), &data)
  192. assert.NotNil(data["one"])
  193. assert.True(data["one"]["three"])
  194. assert.False(data["one"]["two"])
  195. data2 := &OuterData{}
  196. yaml.Unmarshal([]byte(one), data2)
  197. yaml.Unmarshal([]byte(two), data2)
  198. assert.True(data2.One.Three)
  199. assert.True(data2.One.Two)
  200. }
  201. func TestUserDocker(t *testing.T) {
  202. assert := require.New(t)
  203. config := &CloudConfig{
  204. Rancher: RancherConfig{
  205. Docker: DockerConfig{
  206. TLS: true,
  207. },
  208. },
  209. }
  210. bytes, err := yaml.Marshal(config)
  211. assert.Nil(err)
  212. config = &CloudConfig{}
  213. assert.False(config.Rancher.Docker.TLS)
  214. err = yaml.Unmarshal(bytes, config)
  215. assert.Nil(err)
  216. assert.True(config.Rancher.Docker.TLS)
  217. data := map[interface{}]interface{}{}
  218. err = util.Convert(config, &data)
  219. assert.Nil(err)
  220. val, ok := data["rancher"].(map[interface{}]interface{})["docker"]
  221. assert.True(ok)
  222. m, ok := val.(map[interface{}]interface{})
  223. assert.True(ok)
  224. v, ok := m["tls"]
  225. assert.True(ok)
  226. assert.True(v.(bool))
  227. }