line_parsers_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package parser
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestParseNameValOldFormat(t *testing.T) {
  7. directive := Directive{}
  8. node, err := parseNameVal("foo bar", "LABEL", &directive)
  9. assert.NoError(t, err)
  10. expected := &Node{
  11. Value: "foo",
  12. Next: &Node{Value: "bar"},
  13. }
  14. assert.Equal(t, expected, node)
  15. }
  16. func TestParseNameValNewFormat(t *testing.T) {
  17. directive := Directive{}
  18. node, err := parseNameVal("foo=bar thing=star", "LABEL", &directive)
  19. assert.NoError(t, err)
  20. expected := &Node{
  21. Value: "foo",
  22. Next: &Node{
  23. Value: "bar",
  24. Next: &Node{
  25. Value: "thing",
  26. Next: &Node{
  27. Value: "star",
  28. },
  29. },
  30. },
  31. }
  32. assert.Equal(t, expected, node)
  33. }
  34. func TestNodeFromLabels(t *testing.T) {
  35. labels := map[string]string{
  36. "foo": "bar",
  37. "weird": "first' second",
  38. }
  39. expected := &Node{
  40. Value: "label",
  41. Original: `LABEL "foo"='bar' "weird"='first' second'`,
  42. Next: &Node{
  43. Value: "foo",
  44. Next: &Node{
  45. Value: "'bar'",
  46. Next: &Node{
  47. Value: "weird",
  48. Next: &Node{
  49. Value: "'first' second'",
  50. },
  51. },
  52. },
  53. },
  54. }
  55. node := NodeFromLabels(labels)
  56. assert.Equal(t, expected, node)
  57. }
  58. func TestParseNameValWithoutVal(t *testing.T) {
  59. directive := Directive{}
  60. // In Config.Env, a variable without `=` is removed from the environment. (#31634)
  61. // However, in Dockerfile, we don't allow "unsetting" an environment variable. (#11922)
  62. _, err := parseNameVal("foo", "ENV", &directive)
  63. assert.Error(t, err, "ENV must have two arguments")
  64. }