Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support running stability test out of cluster #397

Merged
merged 9 commits into from
Apr 16, 2019
Merged
16 changes: 8 additions & 8 deletions tests/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ func (oi *OperatorConfig) OperatorHelmSetString(m map[string]string) string {

func (oa *operatorActions) DeployOperator(info *OperatorConfig) error {
if info.Tag != "e2e" {
if err := cloneOperatorRepo(); err != nil {
if err := oa.cloneOperatorRepo(); err != nil {
return err
}
if err := checkoutTag(info.Tag); err != nil {
if err := oa.checkoutTag(info.Tag); err != nil {
return err
}
}
Expand Down Expand Up @@ -280,7 +280,7 @@ func (oa *operatorActions) CleanOperatorOrDie(info *OperatorConfig) {
}

func (oa *operatorActions) UpgradeOperator(info *OperatorConfig) error {
if err := checkoutTag(info.Tag); err != nil {
if err := oa.checkoutTag(info.Tag); err != nil {
return err
}

Expand Down Expand Up @@ -1318,8 +1318,8 @@ func releaseIsNotFound(err error) bool {
return strings.Contains(err.Error(), "not found")
}

func cloneOperatorRepo() error {
cmd := fmt.Sprintf("git clone https://github.com/pingcap/tidb-operator.git /tidb-operator")
func (oa *operatorActions) cloneOperatorRepo() error {
cmd := fmt.Sprintf("git clone https://github.com/pingcap/tidb-operator.git %s", oa.cfg.GitRepoDir)
glog.Info(cmd)
res, err := exec.Command("/bin/sh", "-c", cmd).CombinedOutput()
if err != nil && !strings.Contains(string(res), "already exists") {
Expand All @@ -1329,15 +1329,15 @@ func cloneOperatorRepo() error {
return nil
}

func checkoutTag(tagName string) error {
cmd := fmt.Sprintf(`cd /tidb-operator &&
func (oa *operatorActions) checkoutTag(tagName string) error {
cmd := fmt.Sprintf(`cd %s &&
git stash -u &&
git checkout %s &&
mkdir -p /charts/%s &&
cp -rf charts/tidb-operator /charts/%s/tidb-operator &&
cp -rf charts/tidb-cluster /charts/%s/tidb-cluster &&
cp -rf charts/tidb-backup /charts/%s/tidb-backup`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(do not block this PR) Is it possible to copy them to temporary directories which are always writable in the future? I guess the path does not matter here. Then we can simplify the workflow (do not need to prepare /charts).

Copy link
Contributor Author

@aylei aylei Apr 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed for future work, I've tried but found that /charts is hard coded in many places😅

tagName, tagName, tagName, tagName, tagName)
oa.cfg.GitRepoDir, tagName, tagName, tagName, tagName, tagName)
glog.Info(cmd)
res, err := exec.Command("/bin/sh", "-c", cmd).CombinedOutput()
if err != nil {
Expand Down
11 changes: 9 additions & 2 deletions tests/cmd/stability/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package main

import (
"fmt"
"github.com/pingcap/tidb-operator/pkg/client/clientset/versioned"
"k8s.io/client-go/kubernetes"
"net/http"
_ "net/http/pprof"
"time"
Expand All @@ -31,13 +33,18 @@ import (
func main() {
logs.InitLogs()
defer logs.FlushLogs()

go func() {
glog.Info(http.ListenAndServe("localhost:6060", nil))
}()

conf := tests.ParseConfigOrDie()
cli, kubeCli := client.NewCliOrDie()
var cli versioned.Interface
var kubeCli kubernetes.Interface
if conf.OutOfCluster {
cli, kubeCli = client.NewOutOfClusterCliOrDie(conf.KubeConfigPath)
} else {
cli, kubeCli = client.NewCliOrDie()
}
oa := tests.NewOperatorActions(cli, kubeCli, conf)
fta := tests.NewFaultTriggerAction(cli, kubeCli, conf)
fta.CheckAndRecoverEnvOrDie()
Expand Down
23 changes: 23 additions & 0 deletions tests/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/golang/glog"
Expand All @@ -22,6 +24,11 @@ type Config struct {
Nodes []Nodes `yaml:"nodes" json:"nodes"`
ETCDs []Nodes `yaml:"etcds" json:"etcds"`
APIServers []Nodes `yaml:"apiservers" json:"apiservers"`

// For local test
GitRepoDir string `yaml:"git_repo_dir" json:"gir_repo_dir"`
OutOfCluster bool `yaml:"out_of_cluster" json:"out_of_cluster"`
KubeConfigPath string `yaml:"kube_config_path" json:"kube_config_path"`
}

// Nodes defines a series of nodes that belong to the same physical node.
Expand All @@ -39,6 +46,15 @@ func NewConfig() *Config {
flag.StringVar(&cfg.TidbVersions, "tidb-versions", "v2.1.3,v2.1.4", "tidb versions")
flag.StringVar(&cfg.OperatorTag, "operator-tag", "master", "operator tag used to choose charts")
flag.StringVar(&cfg.OperatorImage, "operator-image", "pingcap/tidb-operator:latest", "operator image")
flag.StringVar(&cfg.GitRepoDir, "git-repo-dir", "/tidb-operator", "local directory to which tidb-operator cloned")
aylei marked this conversation as resolved.
Show resolved Hide resolved
if home := homeDir(); home != "" {
flag.StringVar(&cfg.KubeConfigPath, "kubeconfig", filepath.Join(home, ".kube", "config"), "absolute path to the kubeconfig file")
} else {
// cannot get homedir, kube config file path must be provided explicitly
flag.StringVar(&cfg.KubeConfigPath, "kubeconfig", "", "absolute path to the kubeconfig file")
aylei marked this conversation as resolved.
Show resolved Hide resolved
}
flag.BoolVar(&cfg.OutOfCluster, "out-of-cluster", false, "whether stability test runs out of cluster.")
aylei marked this conversation as resolved.
Show resolved Hide resolved
flag.Parse()

return cfg
}
Expand Down Expand Up @@ -115,3 +131,10 @@ func (c *Config) GetUpgradeTidbVersionsOrDie() []string {

return versions
}

func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // for windows
}
30 changes: 22 additions & 8 deletions tests/pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@ func NewCliOrDie() (versioned.Interface, kubernetes.Interface) {
panic(err)
}

cfg.Timeout = 30 * time.Second
cli, err := versioned.NewForConfig(cfg)
if err != nil {
panic(err)
}
return buildClientsOrDie(cfg)
}

kubeCli, err := kubernetes.NewForConfig(cfg)
func NewOutOfClusterCliOrDie(kubeconfig string) (versioned.Interface, kubernetes.Interface) {
// TODO: support context selection, current context will be used now
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
panic(err)
panic(err.Error())
}

return cli, kubeCli
return buildClientsOrDie(cfg)
}

var (
Expand Down Expand Up @@ -74,3 +73,18 @@ func LoadConfig() (*rest.Config, error) {
cfg, err := clientcmd.BuildConfigFromFlags(masterUrl, kubeconfigPath)
return cfg, errors.Trace(err)
}

func buildClientsOrDie(cfg *rest.Config) (versioned.Interface, kubernetes.Interface) {
cfg.Timeout = 30 * time.Second
cli, err := versioned.NewForConfig(cfg)
if err != nil {
panic(err)
}

kubeCli, err := kubernetes.NewForConfig(cfg)
if err != nil {
panic(err)
}

return cli, kubeCli
}