PageRenderTime 35ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Godeps/_workspace/src/github.com/docker/docker/engine/env_test.go

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