cloudinit_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2015 CoreOS, Inc.
  2. // Copyright 2015 Rancher Labs, Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. package cloudinit
  16. import (
  17. "net"
  18. "testing"
  19. "github.com/coreos/coreos-cloudinit/datasource"
  20. )
  21. func TestSubstituteUserDataVars(t *testing.T) {
  22. for _, tt := range []struct {
  23. metadata datasource.Metadata
  24. input string
  25. out string
  26. }{
  27. {
  28. // Userdata with docker-compose syntax
  29. datasource.Metadata{
  30. PublicIPv4: net.ParseIP("192.0.2.3"),
  31. PrivateIPv4: net.ParseIP("192.0.2.203"),
  32. PublicIPv6: net.ParseIP("fe00:1234::"),
  33. PrivateIPv6: net.ParseIP("fe00:5678::"),
  34. },
  35. `servicexyz:
  36. image: rancher/servicexyz:v0.3.1
  37. ports:
  38. - "$public_ipv4:8001:8001"
  39. - "$public_ipv6:8001:8001"
  40. - "$private_ipv4:8001:8001"
  41. - "$private_ipv6:8001:8001"`,
  42. `servicexyz:
  43. image: rancher/servicexyz:v0.3.1
  44. ports:
  45. - "192.0.2.3:8001:8001"
  46. - "fe00:1234:::8001:8001"
  47. - "192.0.2.203:8001:8001"
  48. - "fe00:5678:::8001:8001"`,
  49. },
  50. {
  51. // Userdata with cloud-config/rancher syntax
  52. datasource.Metadata{
  53. PublicIPv4: net.ParseIP("192.0.2.3"),
  54. PrivateIPv4: net.ParseIP("192.0.2.203"),
  55. PublicIPv6: net.ParseIP("fe00:1234::"),
  56. PrivateIPv6: net.ParseIP("fe00:5678::"),
  57. },
  58. `write_files:
  59. - path: /etc/environment
  60. content: |
  61. PRIVATE_IPV6=$private_ipv6
  62. PUBLIC_IPV6=$public_ipv6
  63. rancher:
  64. network:
  65. interfaces:
  66. eth1:
  67. address: $private_ipv4/16
  68. user_docker:
  69. tls_args: ['-H=$public_ipv4:2376']`,
  70. `write_files:
  71. - path: /etc/environment
  72. content: |
  73. PRIVATE_IPV6=fe00:5678::
  74. PUBLIC_IPV6=fe00:1234::
  75. rancher:
  76. network:
  77. interfaces:
  78. eth1:
  79. address: 192.0.2.203/16
  80. user_docker:
  81. tls_args: ['-H=192.0.2.3:2376']`,
  82. },
  83. {
  84. // no metadata
  85. datasource.Metadata{},
  86. "address: $private_ipv4",
  87. "address: ",
  88. },
  89. } {
  90. got := substituteUserDataVars([]byte(tt.input), tt.metadata)
  91. if string(got) != tt.out {
  92. t.Fatalf("Userdata substitution incorrectly applied.\ngot:\n%s\nwant:\n%s", got, tt.out)
  93. }
  94. }
  95. }