cloudstack_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 cloudstack
  14. import (
  15. "context"
  16. "os"
  17. "strconv"
  18. "strings"
  19. "testing"
  20. "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. )
  23. const testClusterName = "testCluster"
  24. func TestReadConfig(t *testing.T) {
  25. _, err := readConfig(nil)
  26. if err != nil {
  27. t.Fatalf("Should not return an error when no config is provided: %v", err)
  28. }
  29. cfg, err := readConfig(strings.NewReader(`
  30. [Global]
  31. api-url = https://cloudstack.url
  32. api-key = a-valid-api-key
  33. secret-key = a-valid-secret-key
  34. ssl-no-verify = true
  35. project-id = a-valid-project-id
  36. `))
  37. if err != nil {
  38. t.Fatalf("Should succeed when a valid config is provided: %v", err)
  39. }
  40. if cfg.Global.APIURL != "https://cloudstack.url" {
  41. t.Errorf("incorrect api-url: %s", cfg.Global.APIURL)
  42. }
  43. if cfg.Global.APIKey != "a-valid-api-key" {
  44. t.Errorf("incorrect api-key: %s", cfg.Global.APIKey)
  45. }
  46. if cfg.Global.SecretKey != "a-valid-secret-key" {
  47. t.Errorf("incorrect secret-key: %s", cfg.Global.SecretKey)
  48. }
  49. if !cfg.Global.SSLNoVerify {
  50. t.Errorf("incorrect ssl-no-verify: %t", cfg.Global.SSLNoVerify)
  51. }
  52. }
  53. // This allows acceptance testing against an existing CloudStack environment.
  54. func configFromEnv() (*CSConfig, bool) {
  55. cfg := &CSConfig{}
  56. cfg.Global.APIURL = os.Getenv("CS_API_URL")
  57. cfg.Global.APIKey = os.Getenv("CS_API_KEY")
  58. cfg.Global.SecretKey = os.Getenv("CS_SECRET_KEY")
  59. cfg.Global.ProjectID = os.Getenv("CS_PROJECT_ID")
  60. // It is save to ignore the error here. If the input cannot be parsed SSLNoVerify
  61. // will still be a bool with its zero value (false) which is the expected default.
  62. cfg.Global.SSLNoVerify, _ = strconv.ParseBool(os.Getenv("CS_SSL_NO_VERIFY"))
  63. // Check if we have the minimum required info to be able to connect to CloudStack.
  64. ok := cfg.Global.APIURL != "" && cfg.Global.APIKey != "" && cfg.Global.SecretKey != ""
  65. return cfg, ok
  66. }
  67. func TestNewCSCloud(t *testing.T) {
  68. cfg, ok := configFromEnv()
  69. if !ok {
  70. t.Skipf("No config found in environment")
  71. }
  72. _, err := newCSCloud(cfg)
  73. if err != nil {
  74. t.Fatalf("Failed to construct/authenticate CloudStack: %v", err)
  75. }
  76. }
  77. func TestLoadBalancer(t *testing.T) {
  78. cfg, ok := configFromEnv()
  79. if !ok {
  80. t.Skipf("No config found in environment")
  81. }
  82. cs, err := newCSCloud(cfg)
  83. if err != nil {
  84. t.Fatalf("Failed to construct/authenticate CloudStack: %v", err)
  85. }
  86. lb, ok := cs.LoadBalancer()
  87. if !ok {
  88. t.Fatalf("LoadBalancer() returned false")
  89. }
  90. _, exists, err := lb.GetLoadBalancer(context.TODO(), testClusterName, &v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "noexist"}})
  91. if err != nil {
  92. t.Fatalf("GetLoadBalancer(\"noexist\") returned error: %s", err)
  93. }
  94. if exists {
  95. t.Fatalf("GetLoadBalancer(\"noexist\") returned exists")
  96. }
  97. }