metadata_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 gce
  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 := "gce-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. testName string
  35. root string
  36. metadataPath string
  37. resources map[string]string
  38. expect datasource.Metadata
  39. clientErr error
  40. expectErr error
  41. }{
  42. {
  43. testName: "one",
  44. root: "/",
  45. metadataPath: "computeMetadata/v1/",
  46. resources: map[string]string{},
  47. },
  48. {
  49. testName: "two",
  50. root: "/",
  51. metadataPath: "computeMetadata/v1/",
  52. resources: map[string]string{
  53. "/computeMetadata/v1/instance/hostname": "host",
  54. },
  55. expect: datasource.Metadata{
  56. Hostname: "host",
  57. },
  58. },
  59. {
  60. testName: "three",
  61. root: "/",
  62. metadataPath: "computeMetadata/v1/",
  63. resources: map[string]string{
  64. "/computeMetadata/v1/instance/hostname": "host",
  65. "/computeMetadata/v1/instance/network-interfaces/0/ip": "1.2.3.4",
  66. "/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip": "5.6.7.8",
  67. },
  68. expect: datasource.Metadata{
  69. Hostname: "host",
  70. PrivateIPv4: net.ParseIP("1.2.3.4"),
  71. PublicIPv4: net.ParseIP("5.6.7.8"),
  72. // NetworkConfig: netconf.NetworkConfig{
  73. // Interfaces: map[string]netconf.InterfaceConfig{
  74. // "eth0": netconf.InterfaceConfig{
  75. // Addresses: []string{
  76. // "5.6.7.8",
  77. // "1.2.3.4",
  78. // },
  79. // },
  80. // },
  81. // },
  82. },
  83. },
  84. {
  85. testName: "four",
  86. clientErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")},
  87. expectErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")},
  88. },
  89. } {
  90. service := &MetadataService{metadata.Service{
  91. Root: tt.root,
  92. Client: &test.HTTPClient{Resources: tt.resources, Err: tt.clientErr},
  93. MetadataPath: tt.metadataPath,
  94. }}
  95. metadata, err := service.FetchMetadata()
  96. if Error(err) != Error(tt.expectErr) {
  97. t.Fatalf("bad error (%q): want \n%q\n, got \n%q\n", tt.resources, tt.expectErr, err)
  98. }
  99. if !reflect.DeepEqual(tt.expect, metadata) {
  100. t.Fatalf("bad fetch %s(%q): want \n%#v\n, got \n%#v\n", tt.testName, tt.resources, tt.expect, metadata)
  101. }
  102. }
  103. }
  104. func Error(err error) string {
  105. if err != nil {
  106. return err.Error()
  107. }
  108. return ""
  109. }