metadata_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. "reflect"
  19. "testing"
  20. "github.com/rancher/os/config/cloudinit/datasource/metadata/test"
  21. "github.com/rancher/os/config/cloudinit/pkg"
  22. )
  23. func TestAvailabilityChanges(t *testing.T) {
  24. want := true
  25. if ac := (Service{}).AvailabilityChanges(); ac != want {
  26. t.Fatalf("bad AvailabilityChanges: want %t, got %t", want, ac)
  27. }
  28. }
  29. func TestIsAvailable(t *testing.T) {
  30. for _, tt := range []struct {
  31. root string
  32. checkPath string
  33. resources map[string]string
  34. expect bool
  35. }{
  36. {
  37. root: "/",
  38. checkPath: "2009-04-04",
  39. resources: map[string]string{
  40. "/2009-04-04": "",
  41. },
  42. expect: true,
  43. },
  44. {
  45. root: "/",
  46. resources: map[string]string{},
  47. expect: false,
  48. },
  49. } {
  50. service := &Service{
  51. Root: tt.root,
  52. Client: &test.HTTPClient{Resources: tt.resources, Err: nil},
  53. IsAvailableCheckPath: tt.checkPath,
  54. }
  55. if a := service.IsAvailable(); a != tt.expect {
  56. t.Fatalf("bad isAvailable (%q): want %t, got %t", tt.resources, tt.expect, a)
  57. }
  58. }
  59. }
  60. func TestFetchUserdata(t *testing.T) {
  61. for _, tt := range []struct {
  62. root string
  63. userdataPath string
  64. resources map[string]string
  65. userdata []byte
  66. clientErr error
  67. expectErr error
  68. }{
  69. {
  70. root: "/",
  71. userdataPath: "2009-04-04/user-data",
  72. resources: map[string]string{
  73. "/2009-04-04/user-data": "hello",
  74. },
  75. userdata: []byte("hello"),
  76. },
  77. {
  78. root: "/",
  79. clientErr: pkg.ErrNotFound{Err: fmt.Errorf("test not found error")},
  80. userdata: []byte{},
  81. },
  82. {
  83. root: "/",
  84. clientErr: pkg.ErrTimeout{Err: fmt.Errorf("test timeout error")},
  85. expectErr: pkg.ErrTimeout{Err: fmt.Errorf("test timeout error")},
  86. },
  87. } {
  88. service := &Service{
  89. Root: tt.root,
  90. Client: &test.HTTPClient{Resources: tt.resources, Err: tt.clientErr},
  91. UserdataPath: tt.userdataPath,
  92. }
  93. data, err := service.FetchUserdata()
  94. if Error(err) != Error(tt.expectErr) {
  95. t.Fatalf("bad error (%q): want %q, got %q", tt.resources, tt.expectErr, err)
  96. }
  97. if !bytes.Equal(data, tt.userdata) {
  98. t.Fatalf("bad userdata (%q): want %q, got %q", tt.resources, tt.userdata, data)
  99. }
  100. }
  101. }
  102. func TestURLs(t *testing.T) {
  103. for _, tt := range []struct {
  104. root string
  105. userdataPath string
  106. metadataPath string
  107. expectRoot string
  108. userdata string
  109. metadata string
  110. }{
  111. {
  112. root: "/",
  113. userdataPath: "2009-04-04/user-data",
  114. metadataPath: "2009-04-04/meta-data",
  115. expectRoot: "/",
  116. userdata: "/2009-04-04/user-data",
  117. metadata: "/2009-04-04/meta-data",
  118. },
  119. {
  120. root: "http://169.254.169.254/",
  121. userdataPath: "2009-04-04/user-data",
  122. metadataPath: "2009-04-04/meta-data",
  123. expectRoot: "http://169.254.169.254/",
  124. userdata: "http://169.254.169.254/2009-04-04/user-data",
  125. metadata: "http://169.254.169.254/2009-04-04/meta-data",
  126. },
  127. } {
  128. service := &Service{
  129. Root: tt.root,
  130. UserdataPath: tt.userdataPath,
  131. MetadataPath: tt.metadataPath,
  132. }
  133. if url := service.UserdataURL(); url != tt.userdata {
  134. t.Fatalf("bad url (%q): want %q, got %q", tt.root, tt.userdata, url)
  135. }
  136. if url := service.MetadataURL(); url != tt.metadata {
  137. t.Fatalf("bad url (%q): want %q, got %q", tt.root, tt.metadata, url)
  138. }
  139. if url := service.ConfigRoot(); url != tt.expectRoot {
  140. t.Fatalf("bad url (%q): want %q, got %q", tt.root, tt.expectRoot, url)
  141. }
  142. }
  143. }
  144. func TestNewDatasource(t *testing.T) {
  145. for _, tt := range []struct {
  146. root string
  147. expectRoot string
  148. }{
  149. {
  150. root: "",
  151. expectRoot: "/",
  152. },
  153. {
  154. root: "/",
  155. expectRoot: "/",
  156. },
  157. {
  158. root: "http://169.254.169.254",
  159. expectRoot: "http://169.254.169.254/",
  160. },
  161. {
  162. root: "http://169.254.169.254/",
  163. expectRoot: "http://169.254.169.254/",
  164. },
  165. } {
  166. service := NewDatasource(tt.root, "", "", "", nil)
  167. if service.Root != tt.expectRoot {
  168. t.Fatalf("bad root (%q): want %q, got %q", tt.root, tt.expectRoot, service.Root)
  169. }
  170. }
  171. }
  172. func TestFetchAttributes(t *testing.T) {
  173. for _, s := range []struct {
  174. resources map[string]string
  175. err error
  176. tests []struct {
  177. path string
  178. val []string
  179. }
  180. }{
  181. {
  182. resources: map[string]string{
  183. "/": "a\nb\nc/",
  184. "/c/": "d\ne/",
  185. "/c/e/": "f",
  186. "/a": "1",
  187. "/b": "2",
  188. "/c/d": "3",
  189. "/c/e/f": "4",
  190. },
  191. tests: []struct {
  192. path string
  193. val []string
  194. }{
  195. {"/", []string{"a", "b", "c/"}},
  196. {"/b", []string{"2"}},
  197. {"/c/d", []string{"3"}},
  198. {"/c/e/", []string{"f"}},
  199. },
  200. },
  201. {
  202. err: fmt.Errorf("test error"),
  203. tests: []struct {
  204. path string
  205. val []string
  206. }{
  207. {"", nil},
  208. },
  209. },
  210. } {
  211. service := &Service{
  212. Client: &test.HTTPClient{Resources: s.resources, Err: s.err},
  213. }
  214. for _, tt := range s.tests {
  215. attrs, err := service.FetchAttributes(tt.path)
  216. if err != s.err {
  217. t.Fatalf("bad error for %q (%q): want %q, got %q", tt.path, s.resources, s.err, err)
  218. }
  219. if !reflect.DeepEqual(attrs, tt.val) {
  220. t.Fatalf("bad fetch for %q (%q): want %q, got %q", tt.path, s.resources, tt.val, attrs)
  221. }
  222. }
  223. }
  224. }
  225. func TestFetchAttribute(t *testing.T) {
  226. for _, s := range []struct {
  227. resources map[string]string
  228. err error
  229. tests []struct {
  230. path string
  231. val string
  232. }
  233. }{
  234. {
  235. resources: map[string]string{
  236. "/": "a\nb\nc/",
  237. "/c/": "d\ne/",
  238. "/c/e/": "f",
  239. "/a": "1",
  240. "/b": "2",
  241. "/c/d": "3",
  242. "/c/e/f": "4",
  243. },
  244. tests: []struct {
  245. path string
  246. val string
  247. }{
  248. {"/a", "1"},
  249. {"/b", "2"},
  250. {"/c/d", "3"},
  251. {"/c/e/f", "4"},
  252. },
  253. },
  254. {
  255. err: fmt.Errorf("test error"),
  256. tests: []struct {
  257. path string
  258. val string
  259. }{
  260. {"", ""},
  261. },
  262. },
  263. } {
  264. service := &Service{
  265. Client: &test.HTTPClient{Resources: s.resources, Err: s.err},
  266. }
  267. for _, tt := range s.tests {
  268. attr, err := service.FetchAttribute(tt.path)
  269. if err != s.err {
  270. t.Fatalf("bad error for %q (%q): want %q, got %q", tt.path, s.resources, s.err, err)
  271. }
  272. if attr != tt.val {
  273. t.Fatalf("bad fetch for %q (%q): want %q, got %q", tt.path, s.resources, tt.val, attr)
  274. }
  275. }
  276. }
  277. }
  278. func Error(err error) string {
  279. if err != nil {
  280. return err.Error()
  281. }
  282. return ""
  283. }