metadata_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package aliyun
  2. import (
  3. "fmt"
  4. "reflect"
  5. "testing"
  6. "github.com/rancher/os/config/cloudinit/datasource"
  7. "github.com/rancher/os/config/cloudinit/datasource/metadata"
  8. "github.com/rancher/os/config/cloudinit/datasource/metadata/test"
  9. "github.com/rancher/os/config/cloudinit/pkg"
  10. )
  11. func TestType(t *testing.T) {
  12. want := "aliyun-metadata-service"
  13. if kind := (MetadataService{}).Type(); kind != want {
  14. t.Fatalf("bad type: want %q, got %q", want, kind)
  15. }
  16. }
  17. func TestFetchMetadata(t *testing.T) {
  18. for _, tt := range []struct {
  19. root string
  20. metadataPath string
  21. resources map[string]string
  22. expect datasource.Metadata
  23. clientErr error
  24. expectErr error
  25. }{
  26. {
  27. root: "/",
  28. metadataPath: "2016-01-01/meta-data/",
  29. resources: map[string]string{
  30. "/2016-01-01/meta-data/": "hostname\n",
  31. },
  32. expectErr: fmt.Errorf("The public-keys should be enable in aliyun-metadata-service"),
  33. },
  34. {
  35. root: "/",
  36. metadataPath: "2016-01-01/meta-data/",
  37. resources: map[string]string{
  38. "/2016-01-01/meta-data/": "hostname\npublic-keys/\n",
  39. "/2016-01-01/meta-data/hostname": "host",
  40. "/2016-01-01/meta-data/public-keys/": "xx/",
  41. "/2016-01-01/meta-data/public-keys/xx/": "openssh-key",
  42. "/2016-01-01/meta-data/public-keys/xx/openssh-key": "key",
  43. },
  44. expect: datasource.Metadata{
  45. Hostname: "host",
  46. SSHPublicKeys: map[string]string{"xx": "key"},
  47. },
  48. },
  49. {
  50. clientErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")},
  51. expectErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")},
  52. },
  53. } {
  54. service := &MetadataService{metadata.Service{
  55. Root: tt.root,
  56. Client: &test.HTTPClient{Resources: tt.resources, Err: tt.clientErr},
  57. MetadataPath: tt.metadataPath,
  58. }}
  59. metadata, err := service.FetchMetadata()
  60. if Error(err) != Error(tt.expectErr) {
  61. t.Fatalf("bad error (%q): \nwant %q, \ngot %q\n", tt.resources, tt.expectErr, err)
  62. }
  63. if !reflect.DeepEqual(tt.expect, metadata) {
  64. t.Fatalf("bad fetch (%q): \nwant %#v, \ngot %#v\n", tt.resources, tt.expect, metadata)
  65. }
  66. }
  67. }
  68. func Error(err error) string {
  69. if err != nil {
  70. return err.Error()
  71. }
  72. return ""
  73. }