photon_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 photon
  14. import (
  15. "context"
  16. "log"
  17. "os"
  18. "strconv"
  19. "strings"
  20. "testing"
  21. "k8s.io/apimachinery/pkg/types"
  22. "k8s.io/apimachinery/pkg/util/rand"
  23. cloudprovider "k8s.io/cloud-provider"
  24. )
  25. func configFromEnv() (TestVM string, TestFlavor string, cfg PCConfig, ok bool) {
  26. var AuthEnabled bool
  27. var OverrideIP bool
  28. var err error
  29. cfg.Global.CloudTarget = os.Getenv("PHOTON_TARGET")
  30. cfg.Global.Project = os.Getenv("PHOTON_PROJECT")
  31. cfg.Global.VMID = os.Getenv("PHOTON_VMID")
  32. if os.Getenv("PHOTON_AUTH_ENABLED") != "" {
  33. AuthEnabled, err = strconv.ParseBool(os.Getenv("PHOTON_AUTH_ENABLED"))
  34. } else {
  35. AuthEnabled = false
  36. }
  37. if err != nil {
  38. log.Fatal(err)
  39. }
  40. cfg.Global.AuthEnabled = AuthEnabled
  41. if os.Getenv("PHOTON_OVERRIDE_IP") != "" {
  42. OverrideIP, err = strconv.ParseBool(os.Getenv("PHOTON_OVERRIDE_IP"))
  43. } else {
  44. OverrideIP = false
  45. }
  46. if err != nil {
  47. log.Fatal(err)
  48. }
  49. cfg.Global.OverrideIP = OverrideIP
  50. TestVM = os.Getenv("PHOTON_TEST_VM")
  51. if os.Getenv("PHOTON_TEST_FLAVOR") != "" {
  52. TestFlavor = os.Getenv("PHOTON_TEST_FLAVOR")
  53. } else {
  54. TestFlavor = ""
  55. }
  56. if err != nil {
  57. log.Fatal(err)
  58. }
  59. ok = (cfg.Global.CloudTarget != "" &&
  60. cfg.Global.Project != "" &&
  61. cfg.Global.VMID != "" &&
  62. TestVM != "")
  63. return
  64. }
  65. func TestReadConfig(t *testing.T) {
  66. _, err := readConfig(nil)
  67. if err == nil {
  68. t.Errorf("Should fail when no config is provided: %s", err)
  69. }
  70. cfg, err := readConfig(strings.NewReader(`
  71. [Global]
  72. target = 0.0.0.0
  73. project = project
  74. overrideIP = true
  75. vmID = vmid
  76. authentication = false
  77. `))
  78. if err != nil {
  79. t.Fatalf("Should succeed when a valid config is provided: %s", err)
  80. }
  81. if cfg.Global.CloudTarget != "0.0.0.0" {
  82. t.Errorf("incorrect photon target ip: %s", cfg.Global.CloudTarget)
  83. }
  84. if cfg.Global.Project != "project" {
  85. t.Errorf("incorrect project: %s", cfg.Global.Project)
  86. }
  87. if cfg.Global.VMID != "vmid" {
  88. t.Errorf("incorrect vmid: %s", cfg.Global.VMID)
  89. }
  90. }
  91. func TestNewPCCloud(t *testing.T) {
  92. _, _, cfg, ok := configFromEnv()
  93. if !ok {
  94. t.Skipf("No config found in environment")
  95. }
  96. _, err := newPCCloud(cfg)
  97. if err != nil {
  98. t.Fatalf("Failed to create new Photon client: %s", err)
  99. }
  100. }
  101. func TestInstances(t *testing.T) {
  102. testVM, _, cfg, ok := configFromEnv()
  103. if !ok {
  104. t.Skipf("No config found in environment")
  105. }
  106. NodeName := types.NodeName(testVM)
  107. pc, err := newPCCloud(cfg)
  108. if err != nil {
  109. t.Fatalf("Failed to create new Photon client: %s", err)
  110. }
  111. i, ok := pc.Instances()
  112. if !ok {
  113. t.Fatalf("Instances() returned false")
  114. }
  115. nonExistingVM := types.NodeName(rand.String(15))
  116. instanceId, err := i.InstanceID(context.TODO(), NodeName)
  117. if err != nil {
  118. t.Fatalf("Instances.InstanceID(%s) failed: %s", testVM, err)
  119. }
  120. t.Logf("Found InstanceID(%s) = %s\n", testVM, instanceId)
  121. _, err = i.InstanceID(context.TODO(), nonExistingVM)
  122. if err == cloudprovider.InstanceNotFound {
  123. t.Logf("VM %s was not found as expected\n", nonExistingVM)
  124. } else if err == nil {
  125. t.Fatalf("Instances.InstanceID did not fail as expected, VM %s was found", nonExistingVM)
  126. } else {
  127. t.Fatalf("Instances.InstanceID did not fail as expected, err: %v", err)
  128. }
  129. addrs, err := i.NodeAddresses(context.TODO(), NodeName)
  130. if err != nil {
  131. t.Fatalf("Instances.NodeAddresses(%s) failed: %s", testVM, err)
  132. }
  133. t.Logf("Found NodeAddresses(%s) = %s\n", testVM, addrs)
  134. }
  135. func TestVolumes(t *testing.T) {
  136. testVM, testFlavor, cfg, ok := configFromEnv()
  137. if !ok {
  138. t.Skipf("No config found in environment")
  139. }
  140. pc, err := newPCCloud(cfg)
  141. if err != nil {
  142. t.Fatalf("Failed to create new Photon client: %s", err)
  143. }
  144. NodeName := types.NodeName(testVM)
  145. volumeOptions := &VolumeOptions{
  146. CapacityGB: 2,
  147. Tags: nil,
  148. Name: "kubernetes-test-volume-" + rand.String(10),
  149. Flavor: testFlavor}
  150. pdID, err := pc.CreateDisk(volumeOptions)
  151. if err != nil {
  152. t.Fatalf("Cannot create a Photon persistent disk: %v", err)
  153. }
  154. err = pc.AttachDisk(context.TODO(), pdID, NodeName)
  155. if err != nil {
  156. t.Fatalf("Cannot attach persistent disk(%s) to VM(%s): %v", pdID, testVM, err)
  157. }
  158. _, err = pc.DiskIsAttached(context.TODO(), pdID, NodeName)
  159. if err != nil {
  160. t.Fatalf("Cannot attach persistent disk(%s) to VM(%s): %v", pdID, testVM, err)
  161. }
  162. err = pc.DetachDisk(context.TODO(), pdID, NodeName)
  163. if err != nil {
  164. t.Fatalf("Cannot detach persisten disk(%s) from VM(%s): %v", pdID, testVM, err)
  165. }
  166. err = pc.DeleteDisk(pdID)
  167. if err != nil {
  168. t.Fatalf("Cannot delete persisten disk(%s): %v", pdID, err)
  169. }
  170. }