forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pod.go
70 lines (62 loc) · 2.36 KB
/
pod.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package k8s
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/retry"
)
// GetPod returns a Kubernetes pod resource in the provided namespace with the given name. This will
// fail the test if there is an error.
func GetPod(t *testing.T, options *KubectlOptions, podName string) *corev1.Pod {
pod, err := GetPodE(t, options, podName)
require.NoError(t, err)
return pod
}
// GetPodE returns a Kubernetes pod resource in the provided namespace with the given name.
func GetPodE(t *testing.T, options *KubectlOptions, podName string) (*corev1.Pod, error) {
clientset, err := GetKubernetesClientFromOptionsE(t, options)
if err != nil {
return nil, err
}
return clientset.CoreV1().Pods(options.Namespace).Get(podName, metav1.GetOptions{})
}
// WaitUntilPodAvailable waits until the pod is running, retrying the check for the specified amount of times, sleeping
// for the provided duration between each try. This will fail the test if there is an error or if the check times out.
func WaitUntilPodAvailable(t *testing.T, options *KubectlOptions, podName string, retries int, sleepBetweenRetries time.Duration) {
require.NoError(t, WaitUntilPodAvailableE(t, options, podName, retries, sleepBetweenRetries))
}
// WaitUntilPodAvailableE waits until the pod is running, retrying the check for the specified amount of times, sleeping
// for the provided duration between each try.
func WaitUntilPodAvailableE(t *testing.T, options *KubectlOptions, podName string, retries int, sleepBetweenRetries time.Duration) error {
statusMsg := fmt.Sprintf("Wait for pod %s to be provisioned.", podName)
message, err := retry.DoWithRetryE(
t,
statusMsg,
retries,
sleepBetweenRetries,
func() (string, error) {
pod, err := GetPodE(t, options, podName)
if err != nil {
return "", err
}
if !IsPodAvailable(pod) {
return "", NewPodNotAvailableError(pod)
}
return "Pod is now available", nil
},
)
if err != nil {
logger.Logf(t, "Timedout waiting for Pod to be provisioned: %s", err)
return err
}
logger.Logf(t, message)
return nil
}
// IsPodAvailable returns true if the pod is running.
func IsPodAvailable(pod *corev1.Pod) bool {
return pod.Status.Phase == corev1.PodRunning
}