util.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. Copyright 2019 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 service
  14. import (
  15. "time"
  16. "k8s.io/apimachinery/pkg/util/wait"
  17. "k8s.io/kubernetes/test/e2e/framework"
  18. e2enetwork "k8s.io/kubernetes/test/e2e/framework/network"
  19. )
  20. // TestReachableHTTP tests that the given host serves HTTP on the given port.
  21. func TestReachableHTTP(host string, port int, timeout time.Duration) {
  22. TestReachableHTTPWithRetriableErrorCodes(host, port, []int{}, timeout)
  23. }
  24. // TestReachableHTTPWithRetriableErrorCodes tests that the given host serves HTTP on the given port with the given retriableErrCodes.
  25. func TestReachableHTTPWithRetriableErrorCodes(host string, port int, retriableErrCodes []int, timeout time.Duration) {
  26. pollfn := func() (bool, error) {
  27. result := e2enetwork.PokeHTTP(host, port, "/echo?msg=hello",
  28. &e2enetwork.HTTPPokeParams{
  29. BodyContains: "hello",
  30. RetriableCodes: retriableErrCodes,
  31. })
  32. if result.Status == e2enetwork.HTTPSuccess {
  33. return true, nil
  34. }
  35. return false, nil // caller can retry
  36. }
  37. if err := wait.PollImmediate(framework.Poll, timeout, pollfn); err != nil {
  38. if err == wait.ErrWaitTimeout {
  39. framework.Failf("Could not reach HTTP service through %v:%v after %v", host, port, timeout)
  40. } else {
  41. framework.Failf("Failed to reach HTTP service through %v:%v: %v", host, port, err)
  42. }
  43. }
  44. }