etcd-operator/test/e2e/e2e_test.go

353 строки
9.5 KiB
Go
Исходник Обычный вид История

2016-10-14 02:19:41 +03:00
// Copyright 2016 The kube-etcd-controller 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.
2016-09-12 21:59:44 +03:00
package e2e
import (
"bytes"
"encoding/json"
"fmt"
2016-10-05 07:39:43 +03:00
"io"
2016-09-12 21:59:44 +03:00
"net/http"
"testing"
"time"
"github.com/coreos/kube-etcd-controller/pkg/spec"
"github.com/coreos/kube-etcd-controller/pkg/util/k8sutil"
2016-09-12 21:59:44 +03:00
"github.com/coreos/kube-etcd-controller/test/e2e/framework"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
2016-10-05 07:39:43 +03:00
k8sclient "k8s.io/kubernetes/pkg/client/unversioned"
2016-09-12 21:59:44 +03:00
"k8s.io/kubernetes/pkg/util/wait"
)
func TestCreateCluster(t *testing.T) {
f := framework.Global
2016-10-12 01:18:06 +03:00
testEtcd, err := createEtcdCluster(f, makeEtcdCluster("test-etcd-", 3))
2016-09-24 22:54:11 +03:00
if err != nil {
2016-09-12 21:59:44 +03:00
t.Fatal(err)
}
defer func() {
2016-09-24 22:54:11 +03:00
if err := deleteEtcdCluster(f, testEtcd.Name); err != nil {
2016-09-12 21:59:44 +03:00
t.Fatal(err)
}
}()
2016-09-27 00:29:11 +03:00
if _, err := waitUntilSizeReached(f, testEtcd.Name, 3, 60); err != nil {
2016-10-02 20:06:36 +03:00
t.Fatalf("failed to create 3 members etcd cluster: %v", err)
2016-09-12 21:59:44 +03:00
}
}
2016-09-24 03:48:50 +03:00
func TestResizeCluster3to5(t *testing.T) {
f := framework.Global
2016-10-12 01:18:06 +03:00
testEtcd, err := createEtcdCluster(f, makeEtcdCluster("test-etcd-", 3))
2016-09-24 22:54:11 +03:00
if err != nil {
2016-09-24 03:48:50 +03:00
t.Fatal(err)
}
defer func() {
2016-09-24 22:54:11 +03:00
if err := deleteEtcdCluster(f, testEtcd.Name); err != nil {
2016-09-24 03:48:50 +03:00
t.Fatal(err)
}
}()
2016-09-27 00:29:11 +03:00
if _, err := waitUntilSizeReached(f, testEtcd.Name, 3, 60); err != nil {
2016-10-02 20:06:36 +03:00
t.Fatalf("failed to create 3 members etcd cluster: %v", err)
2016-09-27 00:02:27 +03:00
return
2016-09-24 03:48:50 +03:00
}
2016-09-27 01:35:47 +03:00
fmt.Println("reached to 3 members cluster")
2016-09-24 22:54:11 +03:00
testEtcd.Spec.Size = 5
if err := updateEtcdCluster(f, testEtcd); err != nil {
t.Fatal(err)
}
2016-09-24 03:48:50 +03:00
2016-09-27 00:29:11 +03:00
if _, err := waitUntilSizeReached(f, testEtcd.Name, 5, 60); err != nil {
2016-10-02 20:06:36 +03:00
t.Fatalf("failed to resize to 5 members etcd cluster: %v", err)
2016-09-24 22:54:11 +03:00
}
}
func TestResizeCluster5to3(t *testing.T) {
f := framework.Global
2016-10-12 01:18:06 +03:00
testEtcd, err := createEtcdCluster(f, makeEtcdCluster("test-etcd-", 5))
2016-09-24 22:54:11 +03:00
if err != nil {
2016-09-24 03:48:50 +03:00
t.Fatal(err)
}
2016-09-24 22:54:11 +03:00
defer func() {
if err := deleteEtcdCluster(f, testEtcd.Name); err != nil {
t.Fatal(err)
}
}()
2016-09-27 00:29:11 +03:00
if _, err := waitUntilSizeReached(f, testEtcd.Name, 5, 90); err != nil {
2016-10-02 20:06:36 +03:00
t.Fatalf("failed to create 5 members etcd cluster: %v", err)
2016-09-27 00:02:27 +03:00
return
2016-09-24 03:48:50 +03:00
}
2016-09-27 01:35:47 +03:00
fmt.Println("reached to 5 members cluster")
2016-09-24 22:54:11 +03:00
testEtcd.Spec.Size = 3
if err := updateEtcdCluster(f, testEtcd); err != nil {
t.Fatal(err)
}
2016-09-27 00:29:11 +03:00
if _, err := waitUntilSizeReached(f, testEtcd.Name, 3, 60); err != nil {
2016-10-02 20:06:36 +03:00
t.Fatalf("failed to resize to 3 members etcd cluster: %v", err)
2016-09-24 22:54:11 +03:00
}
2016-09-24 20:22:20 +03:00
}
2016-09-24 03:48:50 +03:00
2016-09-27 00:02:27 +03:00
func TestOneMemberRecovery(t *testing.T) {
f := framework.Global
2016-10-12 01:18:06 +03:00
testEtcd, err := createEtcdCluster(f, makeEtcdCluster("test-etcd-", 3))
2016-09-27 00:02:27 +03:00
if err != nil {
t.Fatal(err)
}
defer func() {
if err := deleteEtcdCluster(f, testEtcd.Name); err != nil {
t.Fatal(err)
}
}()
2016-09-27 00:29:11 +03:00
names, err := waitUntilSizeReached(f, testEtcd.Name, 3, 60)
2016-09-27 00:02:27 +03:00
if err != nil {
2016-10-02 20:06:36 +03:00
t.Fatalf("failed to create 3 members etcd cluster: %v", err)
2016-09-27 00:02:27 +03:00
return
}
2016-09-27 01:35:47 +03:00
fmt.Println("reached to 3 members cluster")
2016-09-27 00:02:27 +03:00
2016-09-27 00:29:11 +03:00
if err := killMembers(f, names[0]); err != nil {
2016-09-27 00:02:27 +03:00
t.Fatal(err)
}
2016-09-27 00:29:11 +03:00
if _, err := waitUntilSizeReached(f, testEtcd.Name, 3, 60); err != nil {
2016-10-02 20:06:36 +03:00
t.Fatalf("failed to resize to 3 members etcd cluster: %v", err)
2016-09-27 00:02:27 +03:00
}
}
2016-09-27 00:29:11 +03:00
func TestDisasterRecovery(t *testing.T) {
f := framework.Global
backupPolicy := &spec.BackupPolicy{
2016-09-27 00:29:11 +03:00
SnapshotIntervalInSecond: 120,
MaxSnapshot: 5,
VolumeSizeInMB: 512,
StorageType: spec.BackupStorageTypePersistentVolume,
CleanupBackupIfDeleted: true,
2016-09-27 00:29:11 +03:00
}
2016-10-12 01:18:06 +03:00
origEtcd := makeEtcdCluster("test-etcd-", 3)
origEtcd = etcdClusterWithBackup(origEtcd, backupPolicy)
testEtcd, err := createEtcdCluster(f, origEtcd)
2016-09-27 00:29:11 +03:00
if err != nil {
t.Fatal(err)
}
defer func() {
if err := deleteEtcdCluster(f, testEtcd.Name); err != nil {
t.Fatal(err)
}
// TODO: add checking of removal of backup pod
}()
names, err := waitUntilSizeReached(f, testEtcd.Name, 3, 60)
if err != nil {
2016-10-02 20:06:36 +03:00
t.Fatalf("failed to create 3 members etcd cluster: %v", err)
2016-09-27 00:29:11 +03:00
return
}
2016-09-27 01:35:47 +03:00
fmt.Println("reached to 3 members cluster")
// TODO: There might be race that controller will recover members between
// these members are deleted individually.
2016-09-27 00:29:11 +03:00
if err := killMembers(f, names[0], names[1]); err != nil {
t.Fatal(err)
}
if _, err := waitUntilSizeReached(f, testEtcd.Name, 3, 120); err != nil {
2016-10-02 20:06:36 +03:00
t.Fatalf("failed to resize to 3 members etcd cluster: %v", err)
2016-09-27 00:29:11 +03:00
}
// TODO: add checking of data in etcd
}
2016-10-10 04:40:18 +03:00
func waitUntilSizeReached(f *framework.Framework, clusterName string, size, timeout int) ([]string, error) {
return waitSizeReachedWithFilter(f, clusterName, size, timeout, func(*api.Pod) bool { return true })
}
func waitSizeReachedWithFilter(f *framework.Framework, clusterName string, size, timeout int, filterPod func(*api.Pod) bool) ([]string, error) {
2016-09-27 00:02:27 +03:00
var names []string
2016-09-27 00:29:11 +03:00
err := wait.Poll(5*time.Second, time.Duration(timeout)*time.Second, func() (done bool, err error) {
podList, err := f.KubeClient.Pods(f.Namespace.Name).List(k8sutil.EtcdPodListOpt(clusterName))
2016-09-24 03:48:50 +03:00
if err != nil {
return false, err
}
names = nil
for i := range podList.Items {
pod := &podList.Items[i]
if pod.Status.Phase == api.PodRunning {
names = append(names, pod.Name)
2016-10-10 04:40:18 +03:00
}
}
fmt.Printf("waiting size (%d), etcd pods: %v\n", size, names)
if len(names) != size {
2016-09-24 03:48:50 +03:00
return false, nil
}
// TODO: check etcd member membership
2016-09-24 03:48:50 +03:00
return true, nil
})
2016-09-27 00:02:27 +03:00
if err != nil {
return nil, err
}
return names, nil
2016-09-24 03:48:50 +03:00
}
2016-09-27 00:29:11 +03:00
func killMembers(f *framework.Framework, names ...string) error {
for _, name := range names {
err := f.KubeClient.Pods(f.Namespace.Name).Delete(name, nil)
if err != nil {
return err
}
}
return nil
}
2016-10-12 01:18:06 +03:00
func makeEtcdCluster(genName string, size int) *spec.EtcdCluster {
return &spec.EtcdCluster{
2016-09-24 20:22:20 +03:00
TypeMeta: unversioned.TypeMeta{
Kind: "EtcdCluster",
APIVersion: "coreos.com/v1",
},
ObjectMeta: api.ObjectMeta{
2016-09-24 22:54:11 +03:00
GenerateName: genName,
2016-09-24 20:22:20 +03:00
},
Spec: spec.ClusterSpec{
2016-10-12 01:18:06 +03:00
Size: size,
2016-09-24 20:22:20 +03:00
},
}
}
2016-10-12 01:18:06 +03:00
func etcdClusterWithBackup(ec *spec.EtcdCluster, backupPolicy *spec.BackupPolicy) *spec.EtcdCluster {
ec.Spec.Backup = backupPolicy
return ec
}
2016-10-10 04:40:18 +03:00
func etcdClusterWithVersion(ec *spec.EtcdCluster, version string) *spec.EtcdCluster {
ec.Spec.Version = version
return ec
}
func createEtcdCluster(f *framework.Framework, e *spec.EtcdCluster) (*spec.EtcdCluster, error) {
2016-09-24 20:22:20 +03:00
b, err := json.Marshal(e)
if err != nil {
2016-09-24 22:54:11 +03:00
return nil, err
2016-09-24 20:22:20 +03:00
}
2016-09-12 21:59:44 +03:00
resp, err := f.KubeClient.Client.Post(
fmt.Sprintf("%s/apis/coreos.com/v1/namespaces/%s/etcdclusters", f.MasterHost, f.Namespace.Name),
2016-09-24 20:22:20 +03:00
"application/json", bytes.NewReader(b))
2016-09-12 21:59:44 +03:00
if err != nil {
2016-09-24 22:54:11 +03:00
return nil, err
2016-09-12 21:59:44 +03:00
}
2016-09-24 20:22:20 +03:00
defer resp.Body.Close()
2016-09-12 21:59:44 +03:00
if resp.StatusCode != http.StatusCreated {
2016-09-24 22:54:11 +03:00
return nil, fmt.Errorf("unexpected status: %v", resp.Status)
2016-09-12 21:59:44 +03:00
}
2016-09-24 22:54:11 +03:00
decoder := json.NewDecoder(resp.Body)
res := &spec.EtcdCluster{}
2016-09-24 22:54:11 +03:00
if err := decoder.Decode(res); err != nil {
return nil, err
}
2016-10-05 07:39:43 +03:00
fmt.Printf("created etcd cluster: %v\n", res.Name)
2016-09-24 22:54:11 +03:00
return res, nil
2016-09-12 21:59:44 +03:00
}
func updateEtcdCluster(f *framework.Framework, e *spec.EtcdCluster) error {
2016-09-24 20:22:20 +03:00
b, err := json.Marshal(e)
if err != nil {
return err
}
2016-09-24 03:48:50 +03:00
req, err := http.NewRequest("PUT",
2016-09-24 22:54:11 +03:00
fmt.Sprintf("%s/apis/coreos.com/v1/namespaces/%s/etcdclusters/%s", f.MasterHost, f.Namespace.Name, e.Name),
2016-09-24 20:22:20 +03:00
bytes.NewReader(b))
2016-09-24 03:48:50 +03:00
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := f.KubeClient.Client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status: %v", resp.Status)
}
return nil
}
2016-09-24 20:22:20 +03:00
func deleteEtcdCluster(f *framework.Framework, name string) error {
2016-10-05 07:39:43 +03:00
fmt.Printf("deleting etcd cluster: %v\n", name)
podList, err := f.KubeClient.Pods(f.Namespace.Name).List(k8sutil.EtcdPodListOpt(name))
if err != nil {
return err
}
fmt.Println("etcd pods ======")
for i := range podList.Items {
pod := &podList.Items[i]
fmt.Printf("pod (%v): status (%v)\n", pod.Name, pod.Status.Phase)
buf := bytes.NewBuffer(nil)
if pod.Status.Phase == api.PodFailed {
if err := getLogs(f.KubeClient, f.Namespace.Name, pod.Name, "etcd", buf); err != nil {
return err
}
fmt.Println(pod.Name, "logs ===")
fmt.Println(buf.String())
fmt.Println(pod.Name, "logs END ===")
}
}
2016-10-05 07:39:43 +03:00
buf := bytes.NewBuffer(nil)
2016-10-14 06:49:26 +03:00
if err := getLogs(f.KubeClient, f.Namespace.Name, "kube-etcd-controller", "kube-etcd-controller", buf); err != nil {
2016-10-05 07:39:43 +03:00
return err
}
fmt.Println("kube-etcd-controller logs ===")
fmt.Println(buf.String())
fmt.Println("kube-etcd-controller logs END ===")
2016-09-12 21:59:44 +03:00
req, err := http.NewRequest("DELETE",
2016-09-24 20:22:20 +03:00
fmt.Sprintf("%s/apis/coreos.com/v1/namespaces/%s/etcdclusters/%s", f.MasterHost, f.Namespace.Name, name), nil)
2016-09-12 21:59:44 +03:00
if err != nil {
return err
}
resp, err := f.KubeClient.Client.Do(req)
if err != nil {
return err
}
2016-09-24 20:22:20 +03:00
defer resp.Body.Close()
2016-09-12 21:59:44 +03:00
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status: %v", resp.Status)
}
return nil
}
2016-10-05 07:39:43 +03:00
2016-10-21 00:54:25 +03:00
func getLogs(kubecli *k8sclient.Client, ns, p, c string, out io.Writer) error {
2016-10-05 07:39:43 +03:00
req := kubecli.RESTClient.Get().
Namespace(ns).
Resource("pods").
2016-10-21 00:54:25 +03:00
Name(p).
2016-10-05 07:39:43 +03:00
SubResource("log").
2016-10-21 00:54:25 +03:00
Param("container", c).
2016-10-05 07:39:43 +03:00
Param("tailLines", "20")
readCloser, err := req.Stream()
if err != nil {
return err
}
defer readCloser.Close()
_, err = io.Copy(out, readCloser)
return err
}