metadata_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 ec2
  15. import (
  16. "fmt"
  17. "net"
  18. "reflect"
  19. "testing"
  20. "github.com/rancher/os/config/cloudinit/datasource"
  21. "github.com/rancher/os/config/cloudinit/datasource/metadata"
  22. "github.com/rancher/os/config/cloudinit/datasource/metadata/test"
  23. "github.com/rancher/os/config/cloudinit/pkg"
  24. "github.com/rancher/os/netconf"
  25. )
  26. func TestType(t *testing.T) {
  27. want := "ec2-metadata-service"
  28. if kind := (MetadataService{}).Type(); kind != want {
  29. t.Fatalf("bad type: want %q, got %q", want, kind)
  30. }
  31. }
  32. func TestFetchAttributes(t *testing.T) {
  33. for _, s := range []struct {
  34. resources map[string]string
  35. err error
  36. tests []struct {
  37. path string
  38. val []string
  39. }
  40. }{
  41. {
  42. resources: map[string]string{
  43. "/": "a\nb\nc/",
  44. "/c/": "d\ne/",
  45. "/c/e/": "f",
  46. "/a": "1",
  47. "/b": "2",
  48. "/c/d": "3",
  49. "/c/e/f": "4",
  50. },
  51. tests: []struct {
  52. path string
  53. val []string
  54. }{
  55. {"/", []string{"a", "b", "c/"}},
  56. {"/b", []string{"2"}},
  57. {"/c/d", []string{"3"}},
  58. {"/c/e/", []string{"f"}},
  59. },
  60. },
  61. {
  62. err: fmt.Errorf("test error"),
  63. tests: []struct {
  64. path string
  65. val []string
  66. }{
  67. {"", nil},
  68. },
  69. },
  70. } {
  71. service := MetadataService{metadata.Service{
  72. Client: &test.HTTPClient{Resources: s.resources, Err: s.err},
  73. }}
  74. for _, tt := range s.tests {
  75. attrs, err := service.fetchAttributes(tt.path)
  76. if err != s.err {
  77. t.Fatalf("bad error for %q (%q): want %q, got %q", tt.path, s.resources, s.err, err)
  78. }
  79. if !reflect.DeepEqual(attrs, tt.val) {
  80. t.Fatalf("bad fetch for %q (%q): want %q, got %q", tt.path, s.resources, tt.val, attrs)
  81. }
  82. }
  83. }
  84. }
  85. func TestFetchAttribute(t *testing.T) {
  86. for _, s := range []struct {
  87. resources map[string]string
  88. err error
  89. tests []struct {
  90. path string
  91. val string
  92. }
  93. }{
  94. {
  95. resources: map[string]string{
  96. "/": "a\nb\nc/",
  97. "/c/": "d\ne/",
  98. "/c/e/": "f",
  99. "/a": "1",
  100. "/b": "2",
  101. "/c/d": "3",
  102. "/c/e/f": "4",
  103. },
  104. tests: []struct {
  105. path string
  106. val string
  107. }{
  108. {"/a", "1"},
  109. {"/b", "2"},
  110. {"/c/d", "3"},
  111. {"/c/e/f", "4"},
  112. },
  113. },
  114. {
  115. err: fmt.Errorf("test error"),
  116. tests: []struct {
  117. path string
  118. val string
  119. }{
  120. {"", ""},
  121. },
  122. },
  123. } {
  124. service := MetadataService{metadata.Service{
  125. Client: &test.HTTPClient{Resources: s.resources, Err: s.err},
  126. }}
  127. for _, tt := range s.tests {
  128. attr, err := service.fetchAttribute(tt.path)
  129. if err != s.err {
  130. t.Fatalf("bad error for %q (%q): want %q, got %q", tt.path, s.resources, s.err, err)
  131. }
  132. if attr != tt.val {
  133. t.Fatalf("bad fetch for %q (%q): want %q, got %q", tt.path, s.resources, tt.val, attr)
  134. }
  135. }
  136. }
  137. }
  138. func TestFetchMetadata(t *testing.T) {
  139. for _, tt := range []struct {
  140. root string
  141. metadataPath string
  142. resources map[string]string
  143. expect datasource.Metadata
  144. clientErr error
  145. expectErr error
  146. }{
  147. {
  148. root: "/",
  149. metadataPath: "2009-04-04/meta-data/",
  150. resources: map[string]string{
  151. "/2009-04-04/meta-data/public-keys": "bad\n",
  152. },
  153. expectErr: fmt.Errorf("malformed public key: \"bad\""),
  154. },
  155. {
  156. root: "/",
  157. metadataPath: "2009-04-04/meta-data/",
  158. resources: map[string]string{
  159. "/2009-04-04/meta-data/hostname": "host",
  160. "/2009-04-04/meta-data/local-ipv4": "1.2.3.4",
  161. "/2009-04-04/meta-data/public-ipv4": "5.6.7.8",
  162. "/2009-04-04/meta-data/public-keys": "0=test1\n",
  163. "/2009-04-04/meta-data/public-keys/0": "openssh-key",
  164. "/2009-04-04/meta-data/public-keys/0/openssh-key": "key",
  165. },
  166. expect: datasource.Metadata{
  167. Hostname: "host",
  168. PrivateIPv4: net.ParseIP("1.2.3.4"),
  169. PublicIPv4: net.ParseIP("5.6.7.8"),
  170. SSHPublicKeys: map[string]string{"test1": "key"},
  171. NetworkConfig: netconf.NetworkConfig{
  172. Interfaces: map[string]netconf.InterfaceConfig{
  173. /* "eth0": netconf.InterfaceConfig{
  174. Addresses: []string{
  175. "1.2.3.4",
  176. "5.6.7.8",
  177. },
  178. },
  179. */},
  180. },
  181. },
  182. },
  183. {
  184. root: "/",
  185. metadataPath: "2009-04-04/meta-data/",
  186. resources: map[string]string{
  187. "/2009-04-04/meta-data/hostname": "host domain another_domain",
  188. "/2009-04-04/meta-data/local-ipv4": "21.2.3.4",
  189. "/2009-04-04/meta-data/public-ipv4": "25.6.7.8",
  190. "/2009-04-04/meta-data/public-keys": "0=test1\n",
  191. "/2009-04-04/meta-data/public-keys/0": "openssh-key",
  192. "/2009-04-04/meta-data/public-keys/0/openssh-key": "key",
  193. },
  194. expect: datasource.Metadata{
  195. Hostname: "host",
  196. PrivateIPv4: net.ParseIP("21.2.3.4"),
  197. PublicIPv4: net.ParseIP("25.6.7.8"),
  198. SSHPublicKeys: map[string]string{"test1": "key"},
  199. NetworkConfig: netconf.NetworkConfig{
  200. Interfaces: map[string]netconf.InterfaceConfig{
  201. /* "eth0": netconf.InterfaceConfig{
  202. Addresses: []string{
  203. "1.2.3.4",
  204. "5.6.7.8",
  205. },
  206. },
  207. */},
  208. },
  209. },
  210. },
  211. {
  212. clientErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")},
  213. expectErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")},
  214. },
  215. } {
  216. service := &MetadataService{metadata.Service{
  217. Root: tt.root,
  218. Client: &test.HTTPClient{Resources: tt.resources, Err: tt.clientErr},
  219. MetadataPath: tt.metadataPath,
  220. }}
  221. metadata, err := service.FetchMetadata()
  222. if Error(err) != Error(tt.expectErr) {
  223. t.Fatalf("bad error (%q): \nwant %q, \ngot %q\n", tt.resources, tt.expectErr, err)
  224. }
  225. if !reflect.DeepEqual(tt.expect, metadata) {
  226. t.Fatalf("bad fetch (%q): \nwant %#v, \ngot %#v\n", tt.resources, tt.expect, metadata)
  227. }
  228. }
  229. }
  230. func Error(err error) string {
  231. if err != nil {
  232. return err.Error()
  233. }
  234. return ""
  235. }