metadata_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 metadata
  15. import (
  16. "bytes"
  17. "fmt"
  18. "testing"
  19. "github.com/rancher/os/config/cloudinit/datasource/metadata/test"
  20. "github.com/rancher/os/config/cloudinit/pkg"
  21. )
  22. func TestAvailabilityChanges(t *testing.T) {
  23. want := true
  24. if ac := (Service{}).AvailabilityChanges(); ac != want {
  25. t.Fatalf("bad AvailabilityChanges: want %t, got %t", want, ac)
  26. }
  27. }
  28. func TestIsAvailable(t *testing.T) {
  29. for _, tt := range []struct {
  30. root string
  31. apiVersion string
  32. resources map[string]string
  33. expect bool
  34. }{
  35. {
  36. root: "/",
  37. apiVersion: "2009-04-04",
  38. resources: map[string]string{
  39. "/2009-04-04": "",
  40. },
  41. expect: true,
  42. },
  43. {
  44. root: "/",
  45. resources: map[string]string{},
  46. expect: false,
  47. },
  48. } {
  49. service := &Service{
  50. Root: tt.root,
  51. Client: &test.HTTPClient{Resources: tt.resources, Err: nil},
  52. APIVersion: tt.apiVersion,
  53. }
  54. if a := service.IsAvailable(); a != tt.expect {
  55. t.Fatalf("bad isAvailable (%q): want %t, got %t", tt.resources, tt.expect, a)
  56. }
  57. }
  58. }
  59. func TestFetchUserdata(t *testing.T) {
  60. for _, tt := range []struct {
  61. root string
  62. userdataPath string
  63. resources map[string]string
  64. userdata []byte
  65. clientErr error
  66. expectErr error
  67. }{
  68. {
  69. root: "/",
  70. userdataPath: "2009-04-04/user-data",
  71. resources: map[string]string{
  72. "/2009-04-04/user-data": "hello",
  73. },
  74. userdata: []byte("hello"),
  75. },
  76. {
  77. root: "/",
  78. clientErr: pkg.ErrNotFound{Err: fmt.Errorf("test not found error")},
  79. userdata: []byte{},
  80. },
  81. {
  82. root: "/",
  83. clientErr: pkg.ErrTimeout{Err: fmt.Errorf("test timeout error")},
  84. expectErr: pkg.ErrTimeout{Err: fmt.Errorf("test timeout error")},
  85. },
  86. } {
  87. service := &Service{
  88. Root: tt.root,
  89. Client: &test.HTTPClient{Resources: tt.resources, Err: tt.clientErr},
  90. UserdataPath: tt.userdataPath,
  91. }
  92. data, err := service.FetchUserdata()
  93. if Error(err) != Error(tt.expectErr) {
  94. t.Fatalf("bad error (%q): want %q, got %q", tt.resources, tt.expectErr, err)
  95. }
  96. if !bytes.Equal(data, tt.userdata) {
  97. t.Fatalf("bad userdata (%q): want %q, got %q", tt.resources, tt.userdata, data)
  98. }
  99. }
  100. }
  101. func TestURLs(t *testing.T) {
  102. for _, tt := range []struct {
  103. root string
  104. userdataPath string
  105. metadataPath string
  106. expectRoot string
  107. userdata string
  108. metadata string
  109. }{
  110. {
  111. root: "/",
  112. userdataPath: "2009-04-04/user-data",
  113. metadataPath: "2009-04-04/meta-data",
  114. expectRoot: "/",
  115. userdata: "/2009-04-04/user-data",
  116. metadata: "/2009-04-04/meta-data",
  117. },
  118. {
  119. root: "http://169.254.169.254/",
  120. userdataPath: "2009-04-04/user-data",
  121. metadataPath: "2009-04-04/meta-data",
  122. expectRoot: "http://169.254.169.254/",
  123. userdata: "http://169.254.169.254/2009-04-04/user-data",
  124. metadata: "http://169.254.169.254/2009-04-04/meta-data",
  125. },
  126. } {
  127. service := &Service{
  128. Root: tt.root,
  129. UserdataPath: tt.userdataPath,
  130. MetadataPath: tt.metadataPath,
  131. }
  132. if url := service.UserdataURL(); url != tt.userdata {
  133. t.Fatalf("bad url (%q): want %q, got %q", tt.root, tt.userdata, url)
  134. }
  135. if url := service.MetadataURL(); url != tt.metadata {
  136. t.Fatalf("bad url (%q): want %q, got %q", tt.root, tt.metadata, url)
  137. }
  138. if url := service.ConfigRoot(); url != tt.expectRoot {
  139. t.Fatalf("bad url (%q): want %q, got %q", tt.root, tt.expectRoot, url)
  140. }
  141. }
  142. }
  143. func TestNewDatasource(t *testing.T) {
  144. for _, tt := range []struct {
  145. root string
  146. expectRoot string
  147. }{
  148. {
  149. root: "",
  150. expectRoot: "/",
  151. },
  152. {
  153. root: "/",
  154. expectRoot: "/",
  155. },
  156. {
  157. root: "http://169.254.169.254",
  158. expectRoot: "http://169.254.169.254/",
  159. },
  160. {
  161. root: "http://169.254.169.254/",
  162. expectRoot: "http://169.254.169.254/",
  163. },
  164. } {
  165. service := NewDatasource(tt.root, "", "", "", nil)
  166. if service.Root != tt.expectRoot {
  167. t.Fatalf("bad root (%q): want %q, got %q", tt.root, tt.expectRoot, service.Root)
  168. }
  169. }
  170. }
  171. func Error(err error) string {
  172. if err != nil {
  173. return err.Error()
  174. }
  175. return ""
  176. }