config_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package config
  2. import (
  3. "fmt"
  4. "gopkg.in/yaml.v2"
  5. "log"
  6. "testing"
  7. "github.com/rancherio/os/util"
  8. )
  9. import "reflect"
  10. func testParseCmdline(t *testing.T) {
  11. expected := map[string]interface{}{
  12. "rescue": true,
  13. "key1": "value1",
  14. "key2": "value2",
  15. "keyArray": []string{"1", "2"},
  16. "obj1": map[string]interface{}{
  17. "key3": "3value",
  18. "obj2": map[string]interface{}{
  19. "key4": true,
  20. },
  21. },
  22. "key5": 5,
  23. }
  24. 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")
  25. ok := reflect.DeepEqual(actual, expected)
  26. if !ok {
  27. t.Fatalf("%v != %v", actual, expected)
  28. }
  29. }
  30. func TestGet(t *testing.T) {
  31. data := map[interface{}]interface{}{
  32. "key": "value",
  33. "key2": map[interface{}]interface{}{
  34. "subkey": "subvalue",
  35. "subnum": 42,
  36. },
  37. }
  38. tests := map[string]interface{}{
  39. "key": "value",
  40. "key2.subkey": "subvalue",
  41. "key2.subnum": 42,
  42. "key2.subkey2": "",
  43. "foo": "",
  44. }
  45. for k, v := range tests {
  46. if getOrSetVal(k, data, nil) != v {
  47. t.Fatalf("Expected %v, got %v, for key %s", v, getOrSetVal(k, data, nil), k)
  48. }
  49. }
  50. }
  51. func TestSet(t *testing.T) {
  52. data := map[interface{}]interface{}{
  53. "key": "value",
  54. "key2": map[interface{}]interface{}{
  55. "subkey": "subvalue",
  56. "subnum": 42,
  57. },
  58. }
  59. expected := map[interface{}]interface{}{
  60. "key": "value2",
  61. "key2": map[interface{}]interface{}{
  62. "subkey": "subvalue2",
  63. "subkey2": "value",
  64. "subkey3": 43,
  65. "subnum": 42,
  66. },
  67. "key3": map[interface{}]interface{}{
  68. "subkey3": 44,
  69. },
  70. "key4": "value4",
  71. }
  72. tests := map[string]interface{}{
  73. "key": "value2",
  74. "key2.subkey": "subvalue2",
  75. "key2.subkey2": "value",
  76. "key2.subkey3": 43,
  77. "key3.subkey3": 44,
  78. "key4": "value4",
  79. }
  80. for k, v := range tests {
  81. getOrSetVal(k, data, v)
  82. if getOrSetVal(k, data, nil) != v {
  83. t.Fatalf("Expected %v, got %v, for key %s", v, getOrSetVal(k, data, nil), k)
  84. }
  85. }
  86. if !reflect.DeepEqual(data, expected) {
  87. t.Fatalf("Expected %v, got %v", expected, data)
  88. }
  89. }
  90. type OuterData struct {
  91. One Data `"yaml:one"`
  92. }
  93. type Data struct {
  94. Two bool `"yaml:two"`
  95. Three bool `"yaml:three"`
  96. }
  97. func TestMapMerge(t *testing.T) {
  98. one := `
  99. one:
  100. two: true`
  101. two := `
  102. one:
  103. three: true`
  104. data := make(map[string]map[string]bool)
  105. yaml.Unmarshal([]byte(one), data)
  106. yaml.Unmarshal([]byte(two), data)
  107. if _, ok := data["one"]; !ok {
  108. t.Fatal("one not found")
  109. }
  110. if !data["one"]["three"] {
  111. t.Fatal("three not found")
  112. }
  113. if data["one"]["two"] {
  114. t.Fatal("two not found")
  115. }
  116. data2 := &OuterData{}
  117. yaml.Unmarshal([]byte(one), data2)
  118. yaml.Unmarshal([]byte(two), data2)
  119. if !data2.One.Three {
  120. t.Fatal("three not found")
  121. }
  122. if !data2.One.Two {
  123. t.Fatal("two not found")
  124. }
  125. }
  126. func TestUserDocker(t *testing.T) {
  127. config := &Config{
  128. UserDocker: DockerConfig{
  129. TLS: true,
  130. },
  131. }
  132. bytes, err := yaml.Marshal(config)
  133. if err != nil {
  134. log.Fatal(err)
  135. }
  136. config = NewConfig()
  137. err = yaml.Unmarshal(bytes, config)
  138. if err != nil {
  139. log.Fatal(err)
  140. }
  141. data := make(map[interface{}]interface{})
  142. util.Convert(config, data)
  143. fmt.Println(data)
  144. val, ok := data["user_docker"]
  145. if !ok {
  146. t.Fatal("Failed to find user_docker")
  147. }
  148. if m, ok := val.(map[interface{}]interface{}); ok {
  149. if v, ok := m["tls"]; ok {
  150. if v != true {
  151. t.Fatal("user_docker.tls is not true")
  152. }
  153. } else {
  154. t.Fatal("user_docker.tls is not found")
  155. }
  156. } else {
  157. t.Fatal("Bad data")
  158. }
  159. }