k3s/test/e2e/util.go

148 lines
4.8 KiB
Go
Raw Normal View History

2015-01-13 02:11:27 +00:00
/*
Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e
import (
"fmt"
"math/rand"
2015-01-13 02:11:27 +00:00
"path/filepath"
"strconv"
2015-01-13 02:11:27 +00:00
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/clientauth"
"github.com/golang/glog"
. "github.com/onsi/ginkgo"
2015-01-13 02:11:27 +00:00
)
type testContextType struct {
authConfig string
certDir string
host string
repoRoot string
provider string
2015-01-13 02:11:27 +00:00
}
var testContext testContextType
func waitForPodRunning(c *client.Client, id string, tryFor time.Duration) error {
trySecs := int(tryFor.Seconds())
for i := 0; i <= trySecs; i += 5 {
2015-01-13 02:11:27 +00:00
time.Sleep(5 * time.Second)
pod, err := c.Pods(api.NamespaceDefault).Get(id)
if err != nil {
return fmt.Errorf("Get pod %s failed: %v", id, err.Error())
2015-01-13 02:11:27 +00:00
}
if pod.Status.Phase == api.PodRunning {
return nil
2015-01-13 02:11:27 +00:00
}
By(fmt.Sprintf("Waiting for pod %s status to be %q (found %q) (%d secs)", id, api.PodRunning, pod.Status.Phase, i))
2015-01-13 02:11:27 +00:00
}
return fmt.Errorf("Gave up waiting for pod %s to be running after %d seconds", id, trySecs)
2015-01-13 02:11:27 +00:00
}
// waitForPodNotPending returns false if it took too long for the pod to go out of pending state.
func waitForPodNotPending(c *client.Client, ns, podName string, tryFor time.Duration) error {
trySecs := int(tryFor.Seconds())
for i := 0; i <= trySecs; i += 5 {
if i > 0 {
time.Sleep(5 * time.Second)
}
pod, err := c.Pods(ns).Get(podName)
if err != nil {
By(fmt.Sprintf("Get pod %s in namespace %s failed, ignoring for 5s: %v", podName, ns, err))
continue
}
if pod.Status.Phase != api.PodPending {
By(fmt.Sprintf("Saw pod %s in namespace %s out of pending state (found %q)", podName, ns, pod.Status.Phase))
return nil
}
By(fmt.Sprintf("Waiting for status of pod %s in namespace %s to be !%q (found %q) (%v secs)", podName, ns, api.PodPending, pod.Status.Phase, i))
}
return fmt.Errorf("Gave up waiting for status of pod %s in namespace %s to go out of pending after %d seconds", podName, ns, trySecs)
}
2015-01-13 02:11:27 +00:00
// waitForPodSuccess returns true if the pod reached state success, or false if it reached failure or ran too long.
func waitForPodSuccess(c *client.Client, podName string, contName string) bool {
for i := 0; i < 10; i++ {
if i > 0 {
time.Sleep(5 * time.Second)
}
pod, err := c.Pods(api.NamespaceDefault).Get(podName)
if err != nil {
glog.Warningf("Get pod failed: %v", err)
continue
}
// Cannot use pod.Status.Phase == api.PodSucceeded/api.PodFailed due to #2632
ci, ok := pod.Status.Info[contName]
if !ok {
glog.Infof("No Status.Info for container %s in pod %s yet", contName, podName)
} else {
if ci.State.Termination != nil {
if ci.State.Termination.ExitCode == 0 {
glog.Infof("Saw pod success")
return true
} else {
glog.Infof("Saw pod failure: %+v", ci.State.Termination)
}
glog.Infof("Waiting for pod %q status to be success or failure", podName)
} else {
glog.Infof("Nil State.Termination for container %s in pod %s so far", contName, podName)
}
}
}
glog.Warningf("Gave up waiting for pod %q status to be success or failure", podName)
return false
}
func loadClient() (*client.Client, error) {
2015-01-13 02:11:27 +00:00
config := client.Config{
Host: testContext.host,
}
info, err := clientauth.LoadFromFile(testContext.authConfig)
if err != nil {
return nil, fmt.Errorf("Error loading auth: %v", err.Error())
2015-01-13 02:11:27 +00:00
}
// If the certificate directory is provided, set the cert paths to be there.
if testContext.certDir != "" {
By(fmt.Sprintf("Expecting certs in %v.", testContext.certDir))
2015-01-13 02:11:27 +00:00
info.CAFile = filepath.Join(testContext.certDir, "ca.crt")
info.CertFile = filepath.Join(testContext.certDir, "kubecfg.crt")
info.KeyFile = filepath.Join(testContext.certDir, "kubecfg.key")
}
config, err = info.MergeWithConfig(config)
if err != nil {
return nil, fmt.Errorf("Error creating client: %v", err.Error())
2015-01-13 02:11:27 +00:00
}
c, err := client.New(&config)
if err != nil {
return nil, fmt.Errorf("Error creating client: %v", err.Error())
2015-01-13 02:11:27 +00:00
}
return c, nil
2015-01-13 02:11:27 +00:00
}
// TODO: Allow service names to have the same form as names
// for pods and replication controllers so we don't
// need to use such a function and can instead
// use the UUID utilty function.
func randomSuffix() string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return strconv.Itoa(r.Int() % 10000)
}