PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/engine/env_test.go

https://github.com/dotcloud/docker
Go | 312 lines | 281 code | 28 blank | 3 comment | 85 complexity | cf6f40035b9f5c6e4b82bc5fae4a2276 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-2-Clause, CC-BY-4.0, 0BSD, CC-BY-SA-4.0, GPL-2.0, BSD-3-Clause, MIT
  1. package engine
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "testing"
  6. "github.com/dotcloud/docker/pkg/testutils"
  7. )
  8. func TestEnvLenZero(t *testing.T) {
  9. env := &Env{}
  10. if env.Len() != 0 {
  11. t.Fatalf("%d", env.Len())
  12. }
  13. }
  14. func TestEnvLenNotZero(t *testing.T) {
  15. env := &Env{}
  16. env.Set("foo", "bar")
  17. env.Set("ga", "bu")
  18. if env.Len() != 2 {
  19. t.Fatalf("%d", env.Len())
  20. }
  21. }
  22. func TestEnvLenDup(t *testing.T) {
  23. env := &Env{
  24. "foo=bar",
  25. "foo=baz",
  26. "a=b",
  27. }
  28. // len(env) != env.Len()
  29. if env.Len() != 2 {
  30. t.Fatalf("%d", env.Len())
  31. }
  32. }
  33. func TestNewJob(t *testing.T) {
  34. job := mkJob(t, "dummy", "--level=awesome")
  35. if job.Name != "dummy" {
  36. t.Fatalf("Wrong job name: %s", job.Name)
  37. }
  38. if len(job.Args) != 1 {
  39. t.Fatalf("Wrong number of job arguments: %d", len(job.Args))
  40. }
  41. if job.Args[0] != "--level=awesome" {
  42. t.Fatalf("Wrong job arguments: %s", job.Args[0])
  43. }
  44. }
  45. func TestSetenv(t *testing.T) {
  46. job := mkJob(t, "dummy")
  47. job.Setenv("foo", "bar")
  48. if val := job.Getenv("foo"); val != "bar" {
  49. t.Fatalf("Getenv returns incorrect value: %s", val)
  50. }
  51. job.Setenv("bar", "")
  52. if val := job.Getenv("bar"); val != "" {
  53. t.Fatalf("Getenv returns incorrect value: %s", val)
  54. }
  55. if val := job.Getenv("nonexistent"); val != "" {
  56. t.Fatalf("Getenv returns incorrect value: %s", val)
  57. }
  58. }
  59. func TestSetenvBool(t *testing.T) {
  60. job := mkJob(t, "dummy")
  61. job.SetenvBool("foo", true)
  62. if val := job.GetenvBool("foo"); !val {
  63. t.Fatalf("GetenvBool returns incorrect value: %t", val)
  64. }
  65. job.SetenvBool("bar", false)
  66. if val := job.GetenvBool("bar"); val {
  67. t.Fatalf("GetenvBool returns incorrect value: %t", val)
  68. }
  69. if val := job.GetenvBool("nonexistent"); val {
  70. t.Fatalf("GetenvBool returns incorrect value: %t", val)
  71. }
  72. }
  73. func TestSetenvInt(t *testing.T) {
  74. job := mkJob(t, "dummy")
  75. job.SetenvInt("foo", -42)
  76. if val := job.GetenvInt("foo"); val != -42 {
  77. t.Fatalf("GetenvInt returns incorrect value: %d", val)
  78. }
  79. job.SetenvInt("bar", 42)
  80. if val := job.GetenvInt("bar"); val != 42 {
  81. t.Fatalf("GetenvInt returns incorrect value: %d", val)
  82. }
  83. if val := job.GetenvInt("nonexistent"); val != 0 {
  84. t.Fatalf("GetenvInt returns incorrect value: %d", val)
  85. }
  86. }
  87. func TestSetenvList(t *testing.T) {
  88. job := mkJob(t, "dummy")
  89. job.SetenvList("foo", []string{"bar"})
  90. if val := job.GetenvList("foo"); len(val) != 1 || val[0] != "bar" {
  91. t.Fatalf("GetenvList returns incorrect value: %v", val)
  92. }
  93. job.SetenvList("bar", nil)
  94. if val := job.GetenvList("bar"); val != nil {
  95. t.Fatalf("GetenvList returns incorrect value: %v", val)
  96. }
  97. if val := job.GetenvList("nonexistent"); val != nil {
  98. t.Fatalf("GetenvList returns incorrect value: %v", val)
  99. }
  100. }
  101. func TestEnviron(t *testing.T) {
  102. job := mkJob(t, "dummy")
  103. job.Setenv("foo", "bar")
  104. val, exists := job.Environ()["foo"]
  105. if !exists {
  106. t.Fatalf("foo not found in the environ")
  107. }
  108. if val != "bar" {
  109. t.Fatalf("bar not found in the environ")
  110. }
  111. }
  112. func TestMultiMap(t *testing.T) {
  113. e := &Env{}
  114. e.Set("foo", "bar")
  115. e.Set("bar", "baz")
  116. e.Set("hello", "world")
  117. m := e.MultiMap()
  118. e2 := &Env{}
  119. e2.Set("old_key", "something something something")
  120. e2.InitMultiMap(m)
  121. if v := e2.Get("old_key"); v != "" {
  122. t.Fatalf("%#v", v)
  123. }
  124. if v := e2.Get("bar"); v != "baz" {
  125. t.Fatalf("%#v", v)
  126. }
  127. if v := e2.Get("hello"); v != "world" {
  128. t.Fatalf("%#v", v)
  129. }
  130. }
  131. func testMap(l int) [][2]string {
  132. res := make([][2]string, l)
  133. for i := 0; i < l; i++ {
  134. t := [2]string{testutils.RandomString(5), testutils.RandomString(20)}
  135. res[i] = t
  136. }
  137. return res
  138. }
  139. func BenchmarkSet(b *testing.B) {
  140. fix := testMap(100)
  141. b.ResetTimer()
  142. for i := 0; i < b.N; i++ {
  143. env := &Env{}
  144. for _, kv := range fix {
  145. env.Set(kv[0], kv[1])
  146. }
  147. }
  148. }
  149. func BenchmarkSetJson(b *testing.B) {
  150. fix := testMap(100)
  151. type X struct {
  152. f string
  153. }
  154. b.ResetTimer()
  155. for i := 0; i < b.N; i++ {
  156. env := &Env{}
  157. for _, kv := range fix {
  158. if err := env.SetJson(kv[0], X{kv[1]}); err != nil {
  159. b.Fatal(err)
  160. }
  161. }
  162. }
  163. }
  164. func BenchmarkGet(b *testing.B) {
  165. fix := testMap(100)
  166. env := &Env{}
  167. for _, kv := range fix {
  168. env.Set(kv[0], kv[1])
  169. }
  170. b.ResetTimer()
  171. for i := 0; i < b.N; i++ {
  172. for _, kv := range fix {
  173. env.Get(kv[0])
  174. }
  175. }
  176. }
  177. func BenchmarkGetJson(b *testing.B) {
  178. fix := testMap(100)
  179. env := &Env{}
  180. type X struct {
  181. f string
  182. }
  183. for _, kv := range fix {
  184. env.SetJson(kv[0], X{kv[1]})
  185. }
  186. b.ResetTimer()
  187. for i := 0; i < b.N; i++ {
  188. for _, kv := range fix {
  189. if err := env.GetJson(kv[0], &X{}); err != nil {
  190. b.Fatal(err)
  191. }
  192. }
  193. }
  194. }
  195. func BenchmarkEncode(b *testing.B) {
  196. fix := testMap(100)
  197. env := &Env{}
  198. type X struct {
  199. f string
  200. }
  201. // half a json
  202. for i, kv := range fix {
  203. if i%2 != 0 {
  204. if err := env.SetJson(kv[0], X{kv[1]}); err != nil {
  205. b.Fatal(err)
  206. }
  207. continue
  208. }
  209. env.Set(kv[0], kv[1])
  210. }
  211. var writer bytes.Buffer
  212. b.ResetTimer()
  213. for i := 0; i < b.N; i++ {
  214. env.Encode(&writer)
  215. writer.Reset()
  216. }
  217. }
  218. func BenchmarkDecode(b *testing.B) {
  219. fix := testMap(100)
  220. env := &Env{}
  221. type X struct {
  222. f string
  223. }
  224. // half a json
  225. for i, kv := range fix {
  226. if i%2 != 0 {
  227. if err := env.SetJson(kv[0], X{kv[1]}); err != nil {
  228. b.Fatal(err)
  229. }
  230. continue
  231. }
  232. env.Set(kv[0], kv[1])
  233. }
  234. var writer bytes.Buffer
  235. env.Encode(&writer)
  236. denv := &Env{}
  237. reader := bytes.NewReader(writer.Bytes())
  238. b.ResetTimer()
  239. for i := 0; i < b.N; i++ {
  240. err := denv.Decode(reader)
  241. if err != nil {
  242. b.Fatal(err)
  243. }
  244. reader.Seek(0, 0)
  245. }
  246. }
  247. func TestLongNumbers(t *testing.T) {
  248. type T struct {
  249. TestNum int64
  250. }
  251. v := T{67108864}
  252. var buf bytes.Buffer
  253. e := &Env{}
  254. e.SetJson("Test", v)
  255. if err := e.Encode(&buf); err != nil {
  256. t.Fatal(err)
  257. }
  258. res := make(map[string]T)
  259. if err := json.Unmarshal(buf.Bytes(), &res); err != nil {
  260. t.Fatal(err)
  261. }
  262. if res["Test"].TestNum != v.TestNum {
  263. t.Fatalf("TestNum %d, expected %d", res["Test"].TestNum, v.TestNum)
  264. }
  265. }
  266. func TestLongNumbersArray(t *testing.T) {
  267. type T struct {
  268. TestNum []int64
  269. }
  270. v := T{[]int64{67108864}}
  271. var buf bytes.Buffer
  272. e := &Env{}
  273. e.SetJson("Test", v)
  274. if err := e.Encode(&buf); err != nil {
  275. t.Fatal(err)
  276. }
  277. res := make(map[string]T)
  278. if err := json.Unmarshal(buf.Bytes(), &res); err != nil {
  279. t.Fatal(err)
  280. }
  281. if res["Test"].TestNum[0] != v.TestNum[0] {
  282. t.Fatalf("TestNum %d, expected %d", res["Test"].TestNum, v.TestNum)
  283. }
  284. }