token_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright 2017 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 token
  14. import (
  15. "testing"
  16. "time"
  17. "github.com/pkg/errors"
  18. clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
  19. )
  20. func TestFetchKubeConfigWithTimeout(t *testing.T) {
  21. const testAPIEndpoint = "sample-endpoint:1234"
  22. tests := []struct {
  23. name string
  24. discoveryTimeout time.Duration
  25. shouldFail bool
  26. }{
  27. {
  28. name: "Timeout if value is not returned on time",
  29. discoveryTimeout: 1 * time.Second,
  30. shouldFail: true,
  31. },
  32. {
  33. name: "Don't timeout if value is returned on time",
  34. discoveryTimeout: 5 * time.Second,
  35. shouldFail: false,
  36. },
  37. }
  38. for _, test := range tests {
  39. t.Run(test.name, func(t *testing.T) {
  40. cfg, err := fetchKubeConfigWithTimeout(testAPIEndpoint, test.discoveryTimeout, func(apiEndpoint string) (*clientcmdapi.Config, error) {
  41. if apiEndpoint != testAPIEndpoint {
  42. return nil, errors.Errorf("unexpected API server endpoint:\n\texpected: %q\n\tgot: %q", testAPIEndpoint, apiEndpoint)
  43. }
  44. time.Sleep(3 * time.Second)
  45. return &clientcmdapi.Config{}, nil
  46. })
  47. if test.shouldFail {
  48. if err == nil {
  49. t.Fatal("unexpected success")
  50. }
  51. } else {
  52. if err != nil {
  53. t.Fatalf("unexpected failure: %v", err)
  54. }
  55. if cfg == nil {
  56. t.Fatal("cfg is nil")
  57. }
  58. }
  59. })
  60. }
  61. }