keel/tests/helpers.go

136 lines
2.9 KiB
Go
Raw Normal View History

2018-11-13 20:22:47 +00:00
package tests
import (
"context"
"flag"
2018-11-13 23:40:27 +00:00
"fmt"
2018-11-13 20:22:47 +00:00
"os"
"os/exec"
"path/filepath"
2018-11-30 22:56:52 +00:00
"strings"
2018-11-13 23:40:27 +00:00
"time"
2018-11-13 20:22:47 +00:00
2019-04-16 20:54:51 +00:00
v1 "k8s.io/api/core/v1"
2018-11-13 23:40:27 +00:00
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2018-11-13 20:22:47 +00:00
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
// load the gcp plugin (required to authenticate against GKE clusters).
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
log "github.com/sirupsen/logrus"
)
var kubeConfig = flag.String("kubeconfig", "", "Path to Kubernetes config file")
func getKubeConfig() string {
if *kubeConfig != "" {
return *kubeConfig
}
return filepath.Join(os.Getenv("HOME"), ".kube", "config")
}
func getKubernetesClient() (*rest.Config, *kubernetes.Clientset) {
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", getKubeConfig())
if err != nil {
panic(err.Error())
}
// create the clientset
clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
return config, clientSet
}
func createNamespaceForTest() string {
_, clientset := getKubernetesClient()
ns := &v1.Namespace{
2018-11-13 23:40:27 +00:00
ObjectMeta: meta_v1.ObjectMeta{
2018-11-13 20:22:47 +00:00
GenerateName: "keel-e2e-test-",
},
}
createOptions := meta_v1.CreateOptions{}
cns, err := clientset.CoreV1().Namespaces().Create(context.Background(), ns, createOptions)
2018-11-13 20:22:47 +00:00
if err != nil {
panic(err)
}
log.Infof("test namespace '%s' created", cns.Name)
return cns.Name
}
func deleteTestNamespace(namespace string) error {
defer log.Infof("test namespace '%s' deleted", namespace)
_, clientset := getKubernetesClient()
2018-11-13 23:40:27 +00:00
deleteOptions := meta_v1.DeleteOptions{}
return clientset.CoreV1().Namespaces().Delete(context.Background(), namespace, deleteOptions)
2018-11-13 20:22:47 +00:00
}
2018-11-14 20:58:34 +00:00
type KeelCmd struct {
cmd *exec.Cmd
2019-04-16 20:54:51 +00:00
env []string
2018-11-14 20:58:34 +00:00
}
func (kc *KeelCmd) Start(ctx context.Context) error {
2018-11-13 20:22:47 +00:00
log.Info("keel started")
cmd := "keel"
args := []string{"--no-incluster", "--kubeconfig", getKubeConfig()}
c := exec.CommandContext(ctx, cmd, args...)
2018-12-01 22:18:15 +00:00
c.Env = []string{
"DEBUG=true",
}
2019-04-16 20:54:51 +00:00
c.Env = append(c.Env, kc.env...)
2018-11-13 20:22:47 +00:00
c.Stdout = os.Stdout
c.Stderr = os.Stderr
2018-11-14 20:58:34 +00:00
kc.cmd = c
2018-11-13 20:22:47 +00:00
2018-11-30 22:56:52 +00:00
err := c.Run()
if err != nil {
if strings.Contains(err.Error(), "killed") {
return nil
}
}
return err
2018-11-13 20:22:47 +00:00
}
2018-11-13 23:40:27 +00:00
2018-11-14 20:58:34 +00:00
func (kc *KeelCmd) Stop() error {
defer log.Info("keel stopped")
return kc.cmd.Process.Kill()
}
2018-11-13 23:40:27 +00:00
func waitFor(ctx context.Context, kcs *kubernetes.Clientset, namespace, name string, desired string) error {
last := ""
for {
select {
case <-ctx.Done():
return fmt.Errorf("expected '%s', got: '%s'", desired, last)
default:
updated, err := kcs.AppsV1().Deployments(namespace).Get(context.Background(), name, meta_v1.GetOptions{})
2018-11-13 23:40:27 +00:00
if err != nil {
time.Sleep(500 * time.Millisecond)
continue
}
if updated.Spec.Template.Spec.Containers[0].Image != desired {
time.Sleep(500 * time.Millisecond)
last = updated.Spec.Template.Spec.Containers[0].Image
continue
}
return nil
}
}
}