metadata_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 cloudstack
  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 := "cloudstack-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. root string
  34. metadataPath string
  35. resources map[string]string
  36. expect datasource.Metadata
  37. clientErr error
  38. expectErr error
  39. }{
  40. {
  41. root: "/",
  42. metadataPath: "latest/meta-data/",
  43. resources: map[string]string{
  44. "/latest/meta-data/local-hostname": "host",
  45. "/latest/meta-data/local-ipv4": "1.2.3.4",
  46. "/latest/meta-data/public-ipv4": "5.6.7.8",
  47. "/latest/meta-data/public-keys": "key\n",
  48. },
  49. expect: datasource.Metadata{
  50. Hostname: "host",
  51. PrivateIPv4: net.ParseIP("1.2.3.4"),
  52. PublicIPv4: net.ParseIP("5.6.7.8"),
  53. SSHPublicKeys: map[string]string{"0": "key"},
  54. },
  55. },
  56. {
  57. root: "/",
  58. metadataPath: "latest/meta-data/",
  59. resources: map[string]string{
  60. "/latest/meta-data/local-hostname": "host domain another_domain",
  61. "/latest/meta-data/local-ipv4": "21.2.3.4",
  62. "/latest/meta-data/public-ipv4": "25.6.7.8",
  63. "/latest/meta-data/public-keys": "key\n",
  64. },
  65. expect: datasource.Metadata{
  66. Hostname: "host",
  67. PrivateIPv4: net.ParseIP("21.2.3.4"),
  68. PublicIPv4: net.ParseIP("25.6.7.8"),
  69. SSHPublicKeys: map[string]string{"0": "key"},
  70. },
  71. },
  72. {
  73. clientErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")},
  74. expectErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")},
  75. },
  76. } {
  77. service := &MetadataService{metadata.Service{
  78. Root: tt.root,
  79. Client: &test.HTTPClient{Resources: tt.resources, Err: tt.clientErr},
  80. MetadataPath: tt.metadataPath,
  81. }}
  82. metadata, err := service.FetchMetadata()
  83. if Error(err) != Error(tt.expectErr) {
  84. t.Fatalf("bad error (%q): \nwant %q, \ngot %q\n", tt.resources, tt.expectErr, err)
  85. }
  86. if !reflect.DeepEqual(tt.expect, metadata) {
  87. t.Fatalf("bad fetch (%q): \nwant %#v, \ngot %#v\n", tt.resources, tt.expect, metadata)
  88. }
  89. }
  90. }
  91. func Error(err error) string {
  92. if err != nil {
  93. return err.Error()
  94. }
  95. return ""
  96. }