metadata_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 digitalocean
  15. import (
  16. "fmt"
  17. "net"
  18. "reflect"
  19. "testing"
  20. "github.com/rancher/os/netconf"
  21. "github.com/rancher/os/config/cloudinit/datasource"
  22. "github.com/rancher/os/config/cloudinit/datasource/metadata"
  23. "github.com/rancher/os/config/cloudinit/datasource/metadata/test"
  24. "github.com/rancher/os/config/cloudinit/pkg"
  25. )
  26. func TestType(t *testing.T) {
  27. want := "digitalocean-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: "v1.json",
  44. resources: map[string]string{
  45. "/v1.json": "bad",
  46. },
  47. expectErr: fmt.Errorf("invalid character 'b' looking for beginning of value"),
  48. },
  49. {
  50. root: "/",
  51. metadataPath: "v1.json",
  52. resources: map[string]string{
  53. "/v1.json": `{
  54. "droplet_id": 1,
  55. "user_data": "hello",
  56. "vendor_data": "hello",
  57. "public_keys": [
  58. "publickey1",
  59. "publickey2"
  60. ],
  61. "region": "nyc2",
  62. "interfaces": {
  63. "public": [
  64. {
  65. "ipv4": {
  66. "ip_address": "192.168.1.2",
  67. "netmask": "255.255.255.0",
  68. "gateway": "192.168.1.1"
  69. },
  70. "ipv6": {
  71. "ip_address": "fe00::",
  72. "cidr": 126,
  73. "gateway": "fe00::"
  74. },
  75. "mac": "ab:cd:ef:gh:ij",
  76. "type": "public"
  77. }
  78. ]
  79. }
  80. }`,
  81. },
  82. expect: datasource.Metadata{
  83. PublicIPv4: net.ParseIP("192.168.1.2"),
  84. PublicIPv6: net.ParseIP("fe00::"),
  85. SSHPublicKeys: map[string]string{
  86. "0": "publickey1",
  87. "1": "publickey2",
  88. },
  89. NetworkConfig: netconf.NetworkConfig{
  90. Interfaces: map[string]netconf.InterfaceConfig{
  91. "eth0": netconf.InterfaceConfig{
  92. Addresses: []string{
  93. "192.168.1.2/24",
  94. "fe00::/126",
  95. },
  96. //Netmask: "255.255.255.0",
  97. Gateway: "192.168.1.1",
  98. //Cidr: 126,
  99. GatewayIpv6: "fe00::",
  100. //MAC: "ab:cd:ef:gh:ij",
  101. //Type: "public",
  102. },
  103. },
  104. //PublicKeys: []string{"publickey1", "publickey2"},
  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{
  114. Service: metadata.Service{
  115. Root: tt.root,
  116. Client: &test.HTTPClient{Resources: tt.resources, Err: tt.clientErr},
  117. MetadataPath: tt.metadataPath,
  118. },
  119. }
  120. metadata, err := service.FetchMetadata()
  121. if Error(err) != Error(tt.expectErr) {
  122. t.Fatalf("bad error (%q): \nwant %#v,\n got %#v", tt.resources, tt.expectErr, err)
  123. }
  124. if !reflect.DeepEqual(tt.expect, metadata) {
  125. t.Fatalf("bad fetch (%q): \nwant %#v,\n got %#v", tt.resources, tt.expect, metadata)
  126. }
  127. }
  128. }
  129. func Error(err error) string {
  130. if err != nil {
  131. return err.Error()
  132. }
  133. return ""
  134. }