metadata_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package openstack
  14. import (
  15. "strings"
  16. "testing"
  17. )
  18. var FakeMetadata = Metadata{
  19. UUID: "83679162-1378-4288-a2d4-70e13ec132aa",
  20. Name: "test",
  21. AvailabilityZone: "nova",
  22. }
  23. func SetMetadataFixture(value *Metadata) {
  24. metadataCache = value
  25. }
  26. func ClearMetadata() {
  27. metadataCache = nil
  28. }
  29. func TestParseMetadata(t *testing.T) {
  30. _, err := parseMetadata(strings.NewReader("bogus"))
  31. if err == nil {
  32. t.Errorf("Should fail when bad data is provided: %s", err)
  33. }
  34. data := strings.NewReader(`
  35. {
  36. "availability_zone": "nova",
  37. "files": [
  38. {
  39. "content_path": "/content/0000",
  40. "path": "/etc/network/interfaces"
  41. },
  42. {
  43. "content_path": "/content/0001",
  44. "path": "known_hosts"
  45. }
  46. ],
  47. "hostname": "test.novalocal",
  48. "launch_index": 0,
  49. "name": "test",
  50. "meta": {
  51. "role": "webservers",
  52. "essential": "false"
  53. },
  54. "public_keys": {
  55. "mykey": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDBqUfVvCSez0/Wfpd8dLLgZXV9GtXQ7hnMN+Z0OWQUyebVEHey1CXuin0uY1cAJMhUq8j98SiW+cU0sU4J3x5l2+xi1bodDm1BtFWVeLIOQINpfV1n8fKjHB+ynPpe1F6tMDvrFGUlJs44t30BrujMXBe8Rq44cCk6wqyjATA3rQ== Generated by Nova\n"
  56. },
  57. "uuid": "83679162-1378-4288-a2d4-70e13ec132aa",
  58. "devices": [
  59. {
  60. "bus": "scsi",
  61. "serial": "6df1888b-f373-41cf-b960-3786e60a28ef",
  62. "tags": ["fake_tag"],
  63. "type": "disk",
  64. "address": "0:0:0:0"
  65. }
  66. ]
  67. }
  68. `)
  69. md, err := parseMetadata(data)
  70. if err != nil {
  71. t.Fatalf("Should succeed when provided with valid data: %s", err)
  72. }
  73. if md.Name != "test" {
  74. t.Errorf("incorrect name: %s", md.Name)
  75. }
  76. if md.UUID != "83679162-1378-4288-a2d4-70e13ec132aa" {
  77. t.Errorf("incorrect uuid: %s", md.UUID)
  78. }
  79. if md.AvailabilityZone != "nova" {
  80. t.Errorf("incorrect az: %s", md.AvailabilityZone)
  81. }
  82. if len(md.Devices) != 1 {
  83. t.Errorf("expecting to find 1 device, found %d", len(md.Devices))
  84. }
  85. if md.Devices[0].Bus != "scsi" {
  86. t.Errorf("incorrect disk bus: %s", md.Devices[0].Bus)
  87. }
  88. if md.Devices[0].Address != "0:0:0:0" {
  89. t.Errorf("incorrect disk address: %s", md.Devices[0].Address)
  90. }
  91. if md.Devices[0].Type != "disk" {
  92. t.Errorf("incorrect device type: %s", md.Devices[0].Type)
  93. }
  94. if md.Devices[0].Serial != "6df1888b-f373-41cf-b960-3786e60a28ef" {
  95. t.Errorf("incorrect device serial: %s", md.Devices[0].Serial)
  96. }
  97. }