metadata_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/pkg/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 TestFetchMetadata(t *testing.T) {
  33. for _, tt := range []struct {
  34. root string
  35. metadataPath string
  36. resources map[string]string
  37. expect datasource.Metadata
  38. clientErr error
  39. expectErr error
  40. }{
  41. {
  42. root: "/",
  43. metadataPath: "2009-04-04/meta-data/",
  44. resources: map[string]string{
  45. "/2009-04-04/meta-data/public-keys": "bad\n",
  46. },
  47. expectErr: fmt.Errorf("malformed public key: \"bad\""),
  48. },
  49. {
  50. root: "/",
  51. metadataPath: "2009-04-04/meta-data/",
  52. resources: map[string]string{
  53. "/2009-04-04/meta-data/hostname": "host",
  54. "/2009-04-04/meta-data/local-ipv4": "1.2.3.4",
  55. "/2009-04-04/meta-data/public-ipv4": "5.6.7.8",
  56. "/2009-04-04/meta-data/public-keys": "0=test1\n",
  57. "/2009-04-04/meta-data/public-keys/0": "openssh-key",
  58. "/2009-04-04/meta-data/public-keys/0/openssh-key": "key",
  59. },
  60. expect: datasource.Metadata{
  61. Hostname: "host",
  62. PrivateIPv4: net.ParseIP("1.2.3.4"),
  63. PublicIPv4: net.ParseIP("5.6.7.8"),
  64. SSHPublicKeys: map[string]string{"test1": "key"},
  65. RootDisk: "/dev/xvda",
  66. NetworkConfig: netconf.NetworkConfig{
  67. Interfaces: map[string]netconf.InterfaceConfig{
  68. /* "eth0": netconf.InterfaceConfig{
  69. Addresses: []string{
  70. "1.2.3.4",
  71. "5.6.7.8",
  72. },
  73. },
  74. */},
  75. },
  76. },
  77. },
  78. {
  79. root: "/",
  80. metadataPath: "2009-04-04/meta-data/",
  81. resources: map[string]string{
  82. "/2009-04-04/meta-data/hostname": "host domain another_domain",
  83. "/2009-04-04/meta-data/local-ipv4": "21.2.3.4",
  84. "/2009-04-04/meta-data/public-ipv4": "25.6.7.8",
  85. "/2009-04-04/meta-data/public-keys": "0=test1\n",
  86. "/2009-04-04/meta-data/public-keys/0": "openssh-key",
  87. "/2009-04-04/meta-data/public-keys/0/openssh-key": "key",
  88. "/2009-04-04/meta-data/instance-type": "m5.large",
  89. },
  90. expect: datasource.Metadata{
  91. Hostname: "host",
  92. PrivateIPv4: net.ParseIP("21.2.3.4"),
  93. PublicIPv4: net.ParseIP("25.6.7.8"),
  94. SSHPublicKeys: map[string]string{"test1": "key"},
  95. RootDisk: "/dev/nvme0n1",
  96. NetworkConfig: netconf.NetworkConfig{
  97. Interfaces: map[string]netconf.InterfaceConfig{
  98. /* "eth0": netconf.InterfaceConfig{
  99. Addresses: []string{
  100. "1.2.3.4",
  101. "5.6.7.8",
  102. },
  103. },
  104. */},
  105. },
  106. },
  107. },
  108. {
  109. clientErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")},
  110. expectErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")},
  111. },
  112. } {
  113. service := &MetadataService{metadata.Service{
  114. Root: tt.root,
  115. Client: &test.HTTPClient{Resources: tt.resources, Err: tt.clientErr},
  116. MetadataPath: tt.metadataPath,
  117. }}
  118. metadata, err := service.FetchMetadata()
  119. if Error(err) != Error(tt.expectErr) {
  120. t.Fatalf("bad error (%q): \nwant %q, \ngot %q\n", tt.resources, tt.expectErr, err)
  121. }
  122. if !reflect.DeepEqual(tt.expect, metadata) {
  123. t.Fatalf("bad fetch (%q): \nwant %#v, \ngot %#v\n", tt.resources, tt.expect, metadata)
  124. }
  125. }
  126. }
  127. func Error(err error) string {
  128. if err != nil {
  129. return err.Error()
  130. }
  131. return ""
  132. }