utils.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. Copyright 2014 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 integration
  14. import (
  15. "testing"
  16. "time"
  17. "k8s.io/apimachinery/pkg/api/errors"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/util/wait"
  20. "k8s.io/apiserver/pkg/storage/storagebackend"
  21. clientset "k8s.io/client-go/kubernetes"
  22. coreclient "k8s.io/client-go/kubernetes/typed/core/v1"
  23. "github.com/coreos/etcd/clientv3"
  24. "github.com/coreos/etcd/pkg/transport"
  25. )
  26. // DeletePodOrErrorf deletes a pod or fails with a call to t.Errorf.
  27. func DeletePodOrErrorf(t *testing.T, c clientset.Interface, ns, name string) {
  28. if err := c.CoreV1().Pods(ns).Delete(name, nil); err != nil {
  29. t.Errorf("unable to delete pod %v: %v", name, err)
  30. }
  31. }
  32. // Requests to try. Each one should be forbidden or not forbidden
  33. // depending on the authentication and authorization setup of the master.
  34. var (
  35. Code200 = map[int]bool{200: true}
  36. Code201 = map[int]bool{201: true}
  37. Code400 = map[int]bool{400: true}
  38. Code401 = map[int]bool{401: true}
  39. Code403 = map[int]bool{403: true}
  40. Code404 = map[int]bool{404: true}
  41. Code405 = map[int]bool{405: true}
  42. Code409 = map[int]bool{409: true}
  43. Code422 = map[int]bool{422: true}
  44. Code500 = map[int]bool{500: true}
  45. Code503 = map[int]bool{503: true}
  46. )
  47. // WaitForPodToDisappear polls the API server if the pod has been deleted.
  48. func WaitForPodToDisappear(podClient coreclient.PodInterface, podName string, interval, timeout time.Duration) error {
  49. return wait.PollImmediate(interval, timeout, func() (bool, error) {
  50. _, err := podClient.Get(podName, metav1.GetOptions{})
  51. if err == nil {
  52. return false, nil
  53. }
  54. if errors.IsNotFound(err) {
  55. return true, nil
  56. }
  57. return false, err
  58. })
  59. }
  60. // GetEtcdClients returns an initialized clientv3.Client and clientv3.KV.
  61. func GetEtcdClients(config storagebackend.TransportConfig) (*clientv3.Client, clientv3.KV, error) {
  62. tlsInfo := transport.TLSInfo{
  63. CertFile: config.CertFile,
  64. KeyFile: config.KeyFile,
  65. CAFile: config.CAFile,
  66. }
  67. tlsConfig, err := tlsInfo.ClientConfig()
  68. if err != nil {
  69. return nil, nil, err
  70. }
  71. cfg := clientv3.Config{
  72. Endpoints: config.ServerList,
  73. TLS: tlsConfig,
  74. }
  75. c, err := clientv3.New(cfg)
  76. if err != nil {
  77. return nil, nil, err
  78. }
  79. return c, clientv3.NewKV(c), nil
  80. }