upgradetest: add framework for operator deploy

This commit is contained in:
Hongchao Deng 2017-04-28 10:25:53 -07:00
Родитель fca6c88b4f
Коммит 0bc16aa6cf
4 изменённых файлов: 187 добавлений и 2 удалений

Просмотреть файл

@ -82,10 +82,9 @@ function e2e_pass {
fi
fi
TEST_PKGS=`go list ./test/e2e/... | grep -v framework`
# Run all the tests by default
E2E_TEST_SELECTOR=${E2E_TEST_SELECTOR:-.*}
go test ${TEST_PKGS} -run "$E2E_TEST_SELECTOR" -timeout 30m --race --kubeconfig $KUBECONFIG --operator-image $OPERATOR_IMAGE --namespace ${TEST_NAMESPACE}
go test "./test/e2e/" -run "$E2E_TEST_SELECTOR" -timeout 30m --race --kubeconfig $KUBECONFIG --operator-image $OPERATOR_IMAGE --namespace ${TEST_NAMESPACE}
}
function unit_pass {

Просмотреть файл

@ -0,0 +1,8 @@
# Operator Upgrade Test
## How to run
From operator root dir, run:
```
$ go run ./test/e2e/upgradetest/ --kubeconfig=... --kube-ns=... --old-image=... --new-image=...
```

Просмотреть файл

@ -0,0 +1,128 @@
// Copyright 2017 The etcd-operator Authors
//
// 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 framework
import (
"github.com/coreos/etcd-operator/pkg/util/k8sutil"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/pkg/api"
"k8s.io/client-go/pkg/api/v1"
appsv1beta1 "k8s.io/client-go/pkg/apis/apps/v1beta1"
"k8s.io/client-go/tools/clientcmd"
)
type Config struct {
// program flags
KubeConfig string
KubeNS string
OldImage string
NewImage string
}
type Framework struct {
Config
// global var
KubeCli kubernetes.Interface
}
func New(fc Config) (*Framework, error) {
kc, err := clientcmd.BuildConfigFromFlags("", fc.KubeConfig)
if err != nil {
return nil, err
}
kubecli, err := kubernetes.NewForConfig(kc)
if err != nil {
return nil, err
}
return &Framework{
Config: fc,
KubeCli: kubecli,
}, nil
}
func (f *Framework) CreateOperator() error {
cmd := []string{"/usr/local/bin/etcd-operator", "--analytics=false"}
name := "etcd-operator"
image := f.OldImage
selector := map[string]string{"name": "etcd-operator"}
d := &appsv1beta1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: f.KubeNS,
},
Spec: appsv1beta1.DeploymentSpec{
Selector: &metav1.LabelSelector{MatchLabels: selector},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: selector,
},
Spec: v1.PodSpec{
Containers: []v1.Container{{
Name: "etcd-operator",
Image: image,
Command: cmd,
Env: []v1.EnvVar{
{
Name: "MY_POD_NAMESPACE",
ValueFrom: &v1.EnvVarSource{FieldRef: &v1.ObjectFieldSelector{FieldPath: "metadata.namespace"}},
},
{
Name: "MY_POD_NAME",
ValueFrom: &v1.EnvVarSource{FieldRef: &v1.ObjectFieldSelector{FieldPath: "metadata.name"}},
},
},
}},
},
},
},
}
_, err := f.KubeCli.AppsV1beta1().Deployments(f.KubeNS).Create(d)
return err
}
func (f *Framework) DeleteOperator() error {
foreground := metav1.DeletePropagationForeground
return f.KubeCli.AppsV1beta1().Deployments(f.KubeNS).Delete("etcd-operator", &metav1.DeleteOptions{
PropagationPolicy: &foreground,
})
}
func (f *Framework) UpgradeOperator() error {
image := f.NewImage
d, err := f.KubeCli.AppsV1beta1().Deployments(f.KubeNS).Get("etcd-operator", metav1.GetOptions{})
if err != nil {
return err
}
cd := cloneDeployment(d)
cd.Spec.Template.Spec.Containers[0].Image = image
patchData, err := k8sutil.CreatePatch(d, cd, appsv1beta1.Deployment{})
if err != nil {
return err
}
_, err = f.KubeCli.AppsV1beta1().Deployments(f.KubeNS).Patch(d.Name, types.StrategicMergePatchType, patchData)
return err
}
func cloneDeployment(d *appsv1beta1.Deployment) *appsv1beta1.Deployment {
cd, err := api.Scheme.DeepCopy(d)
if err != nil {
panic("cannot deep copy pod")
}
return cd.(*appsv1beta1.Deployment)
}

Просмотреть файл

@ -0,0 +1,50 @@
// Copyright 2017 The etcd-operator Authors
//
// 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 upgradetest
import (
"flag"
"os"
"testing"
"github.com/coreos/etcd-operator/test/e2e/upgradetest/framework"
"github.com/Sirupsen/logrus"
)
var testF *framework.Framework
func TestMain(m *testing.M) {
kubeconfig := flag.String("kubeconfig", "", "kube config path, e.g. $HOME/.kube/config")
kubeNS := flag.String("kube-ns", "default", "upgrade test namespace")
oldImage := flag.String("old-image", "", "")
newImage := flag.String("new-image", "", "")
flag.Parse()
cfg := framework.Config{
KubeConfig: *kubeconfig,
KubeNS: *kubeNS,
OldImage: *oldImage,
NewImage: *newImage,
}
var err error
testF, err = framework.New(cfg)
if err != nil {
logrus.Fatalf("failed to create test framework: %v", err)
}
code := m.Run()
os.Exit(code)
}