waagent_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 waagent
  15. import (
  16. "net"
  17. "reflect"
  18. "testing"
  19. "github.com/rancher/os/config/cloudinit/datasource"
  20. "github.com/rancher/os/config/cloudinit/datasource/test"
  21. )
  22. func TestFetchMetadata(t *testing.T) {
  23. for _, tt := range []struct {
  24. root string
  25. files test.MockFilesystem
  26. metadata datasource.Metadata
  27. }{
  28. {
  29. root: "/",
  30. files: test.NewMockFilesystem(),
  31. },
  32. {
  33. root: "/",
  34. files: test.NewMockFilesystem(test.File{Path: "/SharedConfig.xml", Contents: ""}),
  35. },
  36. {
  37. root: "/var/lib/Waagent",
  38. files: test.NewMockFilesystem(test.File{Path: "/var/lib/Waagent/SharedConfig.xml", Contents: ""}),
  39. },
  40. {
  41. root: "/var/lib/Waagent",
  42. files: test.NewMockFilesystem(test.File{Path: "/var/lib/Waagent/SharedConfig.xml", Contents: `<?xml version="1.0" encoding="utf-8"?>
  43. <SharedConfig version="1.0.0.0" goalStateIncarnation="1">
  44. <Deployment name="c8f9e4c9c18948e1bebf57c5685da756" guid="{1d10394f-c741-4a1a-a6bb-278f213c5a5e}" incarnation="0" isNonCancellableTopologyChangeEnabled="false">
  45. <Service name="core-test-1" guid="{00000000-0000-0000-0000-000000000000}" />
  46. <ServiceInstance name="c8f9e4c9c18948e1bebf57c5685da756.0" guid="{1e202e9a-8ffe-4915-b6ef-4118c9628fda}" />
  47. </Deployment>
  48. <Incarnation number="1" instance="core-test-1" guid="{8767eb4b-b445-4783-b1f5-6c0beaf41ea0}" />
  49. <Role guid="{53ecc81e-257f-fbc9-a53a-8cf1a0a122b4}" name="core-test-1" settleTimeSeconds="0" />
  50. <LoadBalancerSettings timeoutSeconds="0" waitLoadBalancerProbeCount="8">
  51. <Probes>
  52. <Probe name="D41D8CD98F00B204E9800998ECF8427E" />
  53. <Probe name="C9DEC1518E1158748FA4B6081A8266DD" />
  54. </Probes>
  55. </LoadBalancerSettings>
  56. <OutputEndpoints>
  57. <Endpoint name="core-test-1:openInternalEndpoint" type="SFS">
  58. <Target instance="core-test-1" endpoint="openInternalEndpoint" />
  59. </Endpoint>
  60. </OutputEndpoints>
  61. <Instances>
  62. <Instance id="core-test-1" address="100.73.202.64">
  63. <FaultDomains randomId="0" updateId="0" updateCount="0" />
  64. <InputEndpoints>
  65. <Endpoint name="openInternalEndpoint" address="100.73.202.64" protocol="any" isPublic="false" enableDirectServerReturn="false" isDirectAddress="false" disableStealthMode="false">
  66. <LocalPorts>
  67. <LocalPortSelfManaged />
  68. </LocalPorts>
  69. </Endpoint>
  70. <Endpoint name="ssh" address="100.73.202.64:22" protocol="tcp" hostName="core-test-1ContractContract" isPublic="true" loadBalancedPublicAddress="191.239.39.77:22" enableDirectServerReturn="false" isDirectAddress="false" disableStealthMode="false">
  71. <LocalPorts>
  72. <LocalPortRange from="22" to="22" />
  73. </LocalPorts>
  74. </Endpoint>
  75. </InputEndpoints>
  76. </Instance>
  77. </Instances>
  78. </SharedConfig>`}),
  79. metadata: datasource.Metadata{
  80. PrivateIPv4: net.ParseIP("100.73.202.64"),
  81. PublicIPv4: net.ParseIP("191.239.39.77"),
  82. },
  83. },
  84. } {
  85. a := Waagent{tt.root, tt.files.ReadFile, nil}
  86. metadata, err := a.FetchMetadata()
  87. if err != nil {
  88. t.Fatalf("bad error for %+v: want %v, got %q", tt, nil, err)
  89. }
  90. if !reflect.DeepEqual(tt.metadata, metadata) {
  91. t.Fatalf("bad metadata for %+v: want %#v, got %#v", tt, tt.metadata, metadata)
  92. }
  93. }
  94. }
  95. func TestFetchUserdata(t *testing.T) {
  96. for _, tt := range []struct {
  97. root string
  98. files test.MockFilesystem
  99. }{
  100. {
  101. "/",
  102. test.NewMockFilesystem(),
  103. },
  104. {
  105. "/",
  106. test.NewMockFilesystem(test.File{Path: "/CustomData", Contents: ""}),
  107. },
  108. {
  109. "/var/lib/Waagent/",
  110. test.NewMockFilesystem(test.File{Path: "/var/lib/Waagent/CustomData", Contents: ""}),
  111. },
  112. } {
  113. a := Waagent{tt.root, tt.files.ReadFile, nil}
  114. _, err := a.FetchUserdata()
  115. if err != nil {
  116. t.Fatalf("bad error for %+v: want %v, got %q", tt, nil, err)
  117. }
  118. }
  119. }
  120. func TestConfigRoot(t *testing.T) {
  121. for _, tt := range []struct {
  122. root string
  123. configRoot string
  124. }{
  125. {
  126. "/",
  127. "/",
  128. },
  129. {
  130. "/var/lib/Waagent",
  131. "/var/lib/Waagent",
  132. },
  133. } {
  134. a := Waagent{tt.root, nil, nil}
  135. if configRoot := a.ConfigRoot(); configRoot != tt.configRoot {
  136. t.Fatalf("bad config root for %q: want %q, got %q", tt, tt.configRoot, configRoot)
  137. }
  138. }
  139. }
  140. func TestNewDatasource(t *testing.T) {
  141. for _, tt := range []struct {
  142. root string
  143. expectRoot string
  144. }{
  145. {
  146. root: "",
  147. expectRoot: "",
  148. },
  149. {
  150. root: "/var/lib/Waagent",
  151. expectRoot: "/var/lib/Waagent",
  152. },
  153. } {
  154. service := NewDatasource(tt.root)
  155. if service.root != tt.expectRoot {
  156. t.Fatalf("bad root (%q): want %q, got %q", tt.root, tt.expectRoot, service.root)
  157. }
  158. }
  159. }