forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.go
87 lines (76 loc) · 2.54 KB
/
helper.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package image_ecosystem
import (
"fmt"
"regexp"
"strings"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
g "github.com/onsi/ginkgo"
exutil "github.com/openshift/origin/test/extended/util"
)
// RunInPodContainer will run provided command in the specified pod container.
func RunInPodContainer(oc *exutil.CLI, selector labels.Selector, cmd []string) error {
pods, err := exutil.WaitForPods(oc.KubeClient().CoreV1().Pods(oc.Namespace()), selector, exutil.CheckPodIsRunning, 1, 4*time.Minute)
if err != nil {
return err
}
if len(pods) != 1 {
return fmt.Errorf("Got %d pods for selector %v, expected 1", len(pods), selector)
}
pod, err := oc.KubeClient().CoreV1().Pods(oc.Namespace()).Get(pods[0], metav1.GetOptions{})
if err != nil {
return err
}
args := []string{pod.Name, "-c", pod.Spec.Containers[0].Name, "--"}
args = append(args, cmd...)
output, err := oc.Run("exec").Args(args...).Output()
if err == nil {
fmt.Fprintf(g.GinkgoWriter, "RunInPodContainer exec output: %s\n", output)
}
return err
}
// CheckPageContains makes a http request for an example application and checks
// that the result contains given string
func CheckPageContains(oc *exutil.CLI, endpoint, path, contents string) (bool, error) {
address, err := exutil.GetEndpointAddress(oc, endpoint)
if err != nil {
return false, err
}
response, err := exutil.FetchURL(fmt.Sprintf("http://%s/%s", address, path), 3*time.Minute)
if err != nil {
return false, err
}
success := strings.Contains(response, contents)
if !success {
fmt.Fprintf(g.GinkgoWriter, "CheckPageContains was looking for %s but got %s\n", contents, response)
}
return success, nil
}
// CheckPageRegexp makes a http request for an example application and checks
// that the result satisfies a given regexp; it will also return the submatch array entry
// present at index for possible comparisons
func CheckPageRegexp(oc *exutil.CLI, endpoint, path, regex string, index int) (bool, string, error) {
address, err := exutil.GetEndpointAddress(oc, endpoint)
if err != nil {
return false, "", err
}
response, err := exutil.FetchURL(fmt.Sprintf("http://%s/%s", address, path), 3*time.Minute)
if err != nil {
return false, "", err
}
val := ""
r, _ := regexp.Compile(regex)
parts := r.FindStringSubmatch(response)
success := len(parts) > 0
if !success {
fmt.Fprintf(g.GinkgoWriter, "CheckPageContains was looking for %s but got %s\n", regex, response)
} else {
for i, part := range parts {
if i == index {
val = part
}
}
}
return success, val, nil
}