metadata_test.go 3.2 KB

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