env_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package initialize
  15. import (
  16. "io/ioutil"
  17. "net"
  18. "os"
  19. "path"
  20. "testing"
  21. "github.com/rancher/os/config/cloudinit/datasource"
  22. "github.com/rancher/os/config/cloudinit/system"
  23. )
  24. func TestEnvironmentApply(t *testing.T) {
  25. os.Setenv("COREOS_PUBLIC_IPV4", "1.2.3.4")
  26. os.Setenv("COREOS_PRIVATE_IPV4", "5.6.7.8")
  27. os.Setenv("COREOS_PUBLIC_IPV6", "1234::")
  28. os.Setenv("COREOS_PRIVATE_IPV6", "5678::")
  29. for _, tt := range []struct {
  30. metadata datasource.Metadata
  31. input string
  32. out string
  33. }{
  34. {
  35. // Substituting both values directly should always take precedence
  36. // over environment variables
  37. datasource.Metadata{
  38. PublicIPv4: net.ParseIP("192.0.2.3"),
  39. PrivateIPv4: net.ParseIP("192.0.2.203"),
  40. PublicIPv6: net.ParseIP("fe00:1234::"),
  41. PrivateIPv6: net.ParseIP("fe00:5678::"),
  42. },
  43. `[Service]
  44. ExecStart=/usr/bin/echo "$public_ipv4 $public_ipv6"
  45. ExecStop=/usr/bin/echo $private_ipv4 $private_ipv6
  46. ExecStop=/usr/bin/echo $unknown`,
  47. `[Service]
  48. ExecStart=/usr/bin/echo "192.0.2.3 fe00:1234::"
  49. ExecStop=/usr/bin/echo 192.0.2.203 fe00:5678::
  50. ExecStop=/usr/bin/echo $unknown`,
  51. },
  52. {
  53. // Substituting one value directly while falling back with the other
  54. datasource.Metadata{
  55. PrivateIPv4: net.ParseIP("127.0.0.1"),
  56. },
  57. "$private_ipv4\n$public_ipv4",
  58. "127.0.0.1\n1.2.3.4",
  59. },
  60. {
  61. // Falling back to environment variables for both values
  62. datasource.Metadata{},
  63. "$private_ipv4\n$public_ipv4",
  64. "5.6.7.8\n1.2.3.4",
  65. },
  66. {
  67. // No substitutions
  68. datasource.Metadata{},
  69. "$private_ipv4\nfoobar",
  70. "5.6.7.8\nfoobar",
  71. },
  72. {
  73. // Escaping substitutions
  74. datasource.Metadata{
  75. PrivateIPv4: net.ParseIP("127.0.0.1"),
  76. },
  77. `\$private_ipv4
  78. $private_ipv4
  79. addr: \$private_ipv4
  80. \\$private_ipv4`,
  81. `$private_ipv4
  82. 127.0.0.1
  83. addr: $private_ipv4
  84. \$private_ipv4`,
  85. },
  86. {
  87. // No substitutions with escaping
  88. datasource.Metadata{},
  89. "\\$test\n$test",
  90. "\\$test\n$test",
  91. },
  92. } {
  93. env := NewEnvironment("./", "./", "./", "", tt.metadata)
  94. got := env.Apply(tt.input)
  95. if got != tt.out {
  96. t.Fatalf("Environment incorrectly applied.\ngot:\n%s\nwant:\n%s", got, tt.out)
  97. }
  98. }
  99. }
  100. func TestEnvironmentFile(t *testing.T) {
  101. metadata := datasource.Metadata{
  102. PublicIPv4: net.ParseIP("1.2.3.4"),
  103. PrivateIPv4: net.ParseIP("5.6.7.8"),
  104. PublicIPv6: net.ParseIP("1234::"),
  105. PrivateIPv6: net.ParseIP("5678::"),
  106. }
  107. expect := "COREOS_PRIVATE_IPV4=5.6.7.8\nCOREOS_PRIVATE_IPV6=5678::\nCOREOS_PUBLIC_IPV4=1.2.3.4\nCOREOS_PUBLIC_IPV6=1234::\n"
  108. dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
  109. if err != nil {
  110. t.Fatalf("Unable to create tempdir: %v", err)
  111. }
  112. defer os.RemoveAll(dir)
  113. env := NewEnvironment("./", "./", "./", "", metadata)
  114. ef := env.DefaultEnvironmentFile()
  115. err = system.WriteEnvFile(ef, dir)
  116. if err != nil {
  117. t.Fatalf("WriteEnvFile failed: %v", err)
  118. }
  119. fullPath := path.Join(dir, "etc", "environment")
  120. contents, err := ioutil.ReadFile(fullPath)
  121. if err != nil {
  122. t.Fatalf("Unable to read expected file: %v", err)
  123. }
  124. if string(contents) != expect {
  125. t.Fatalf("File has incorrect contents: %q", contents)
  126. }
  127. }
  128. func TestEnvironmentFileNil(t *testing.T) {
  129. os.Clearenv()
  130. metadata := datasource.Metadata{}
  131. env := NewEnvironment("./", "./", "./", "", metadata)
  132. ef := env.DefaultEnvironmentFile()
  133. if ef != nil {
  134. t.Fatalf("Environment file not nil: %v", ef)
  135. }
  136. }