configdrive_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 configdrive
  15. import (
  16. "reflect"
  17. "testing"
  18. "github.com/rancher/os/config/cloudinit/datasource"
  19. "github.com/rancher/os/config/cloudinit/datasource/test"
  20. )
  21. func TestFetchMetadata(t *testing.T) {
  22. for _, tt := range []struct {
  23. root string
  24. files test.MockFilesystem
  25. metadata datasource.Metadata
  26. }{
  27. {
  28. root: "/",
  29. files: test.NewMockFilesystem(test.File{Path: "/openstack/latest/meta_data.json", Contents: ""}),
  30. },
  31. {
  32. root: "/",
  33. files: test.NewMockFilesystem(test.File{Path: "/openstack/latest/meta_data.json", Contents: `{"ignore": "me"}`}),
  34. },
  35. {
  36. root: "/",
  37. files: test.NewMockFilesystem(test.File{Path: "/openstack/latest/meta_data.json", Contents: `{"hostname": "host"}`}),
  38. metadata: datasource.Metadata{Hostname: "host"},
  39. },
  40. {
  41. root: "/media/configdrive",
  42. files: test.NewMockFilesystem(test.File{Path: "/media/configdrive/openstack/latest/meta_data.json", Contents: `{"hostname": "host", "network_config": {"content_path": "config_file.json"}, "public_keys":{"1": "key1", "2": "key2"}}`},
  43. test.File{Path: "/media/configdrive/openstack/config_file.json", Contents: "make it work"},
  44. ),
  45. metadata: datasource.Metadata{
  46. Hostname: "host",
  47. SSHPublicKeys: map[string]string{
  48. "1": "key1",
  49. "2": "key2",
  50. },
  51. },
  52. },
  53. } {
  54. cd := ConfigDrive{tt.root, tt.files.ReadFile, nil, true}
  55. metadata, err := cd.FetchMetadata()
  56. if err != nil {
  57. t.Fatalf("bad error for %+v: want %v, got %q", tt, nil, err)
  58. }
  59. if !reflect.DeepEqual(tt.metadata, metadata) {
  60. t.Fatalf("bad metadata for %+v: want %#v, got %#v", tt, tt.metadata, metadata)
  61. }
  62. }
  63. }
  64. func TestFetchUserdata(t *testing.T) {
  65. for _, tt := range []struct {
  66. root string
  67. files test.MockFilesystem
  68. userdata string
  69. }{
  70. {
  71. "/",
  72. test.NewMockFilesystem(),
  73. "",
  74. },
  75. {
  76. "/",
  77. test.NewMockFilesystem(test.File{Path: "/openstack/latest/user_data", Contents: "userdata"}),
  78. "userdata",
  79. },
  80. {
  81. "/media/configdrive",
  82. test.NewMockFilesystem(test.File{Path: "/media/configdrive/openstack/latest/user_data", Contents: "userdata"}),
  83. "userdata",
  84. },
  85. } {
  86. cd := ConfigDrive{tt.root, tt.files.ReadFile, nil, true}
  87. userdata, err := cd.FetchUserdata()
  88. if err != nil {
  89. t.Fatalf("bad error for %+v: want %v, got %q", tt, nil, err)
  90. }
  91. if string(userdata) != tt.userdata {
  92. t.Fatalf("bad userdata for %+v: want %q, got %q", tt, tt.userdata, userdata)
  93. }
  94. }
  95. }
  96. func TestConfigRoot(t *testing.T) {
  97. for _, tt := range []struct {
  98. root string
  99. configRoot string
  100. }{
  101. {
  102. "/",
  103. "/openstack",
  104. },
  105. {
  106. "/media/configdrive",
  107. "/media/configdrive/openstack",
  108. },
  109. } {
  110. cd := ConfigDrive{tt.root, nil, nil, true}
  111. if configRoot := cd.ConfigRoot(); configRoot != tt.configRoot {
  112. t.Fatalf("bad config root for %q: want %q, got %q", tt, tt.configRoot, configRoot)
  113. }
  114. }
  115. }
  116. func TestNewDatasource(t *testing.T) {
  117. for _, tt := range []struct {
  118. root string
  119. expectRoot string
  120. }{
  121. {
  122. root: "",
  123. expectRoot: "",
  124. },
  125. {
  126. root: "/media/configdrive",
  127. expectRoot: "/media/configdrive",
  128. },
  129. } {
  130. service := NewDatasource(tt.root)
  131. if service.root != tt.expectRoot {
  132. t.Fatalf("bad root (%q): want %q, got %q", tt.root, tt.expectRoot, service.root)
  133. }
  134. }
  135. }