Skip to content

Commit

Permalink
Add migration E2E test
Browse files Browse the repository at this point in the history
Signed-off-by: danfengl <[email protected]>
  • Loading branch information
danfengliu committed Jul 18, 2022
1 parent 267db7a commit 9e2dcbe
Show file tree
Hide file tree
Showing 28 changed files with 470 additions and 112 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ tilt-resources/velero_v1_backupstoragelocation.yaml
tilt-resources/deployment.yaml
tilt-resources/restic.yaml
tilt-resources/cloud

test/e2e/report.xml
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ require (
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-logr/logr v0.4.0 // indirect
github.com/go-logr/zapr v0.4.0 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/gofuzz v1.2.0 // indirect
Expand Down Expand Up @@ -107,6 +108,7 @@ require (
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
golang.org/x/tools v0.1.5 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ github.com/go-openapi/swag v0.19.2/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-openapi/swag v0.19.14/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
Expand Down
13 changes: 9 additions & 4 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@ import (
"github.com/vmware-tanzu/velero/pkg/buildinfo"
)

func buildConfigFromFlags(context, kubeconfigPath string, precedence []string) (*rest.Config, error) {
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath, Precedence: precedence},
&clientcmd.ConfigOverrides{
CurrentContext: context,
}).ClientConfig()
}

// Config returns a *rest.Config, using either the kubeconfig (if specified) or an in-cluster
// configuration.
func Config(kubeconfig, kubecontext, baseName string, qps float32, burst int) (*rest.Config, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
loadingRules.ExplicitPath = kubeconfig
configOverrides := &clientcmd.ConfigOverrides{CurrentContext: kubecontext}
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)

clientConfig, err := kubeConfig.ClientConfig()
clientConfig, err := buildConfigFromFlags(kubecontext, kubeconfig, loadingRules.Precedence)
if err != nil {
return nil, errors.Wrap(err, "error finding Kubernetes API server config in --kubeconfig, $KUBECONFIG, or in-cluster configuration")
}
Expand Down
1 change: 0 additions & 1 deletion pkg/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func LoadConfig() (VeleroConfig, error) {
if err := json.NewDecoder(configFile).Decode(&config); err != nil {
return nil, errors.WithStack(err)
}

return config, nil
}

Expand Down
11 changes: 5 additions & 6 deletions pkg/client/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ type factory struct {
}

// NewFactory returns a Factory.
func NewFactory(baseName string, config VeleroConfig) Factory {
func NewFactory(baseName, kubecontext string, config VeleroConfig) Factory {
f := &factory{
flags: pflag.NewFlagSet("", pflag.ContinueOnError),
baseName: baseName,
flags: pflag.NewFlagSet("", pflag.ContinueOnError),
baseName: baseName,
kubecontext: kubecontext,
}

f.namespace = os.Getenv("VELERO_NAMESPACE")
Expand All @@ -96,8 +97,7 @@ func NewFactory(baseName string, config VeleroConfig) Factory {

f.flags.StringVar(&f.kubeconfig, "kubeconfig", "", "Path to the kubeconfig file to use to talk to the Kubernetes apiserver. If unset, try the environment variable KUBECONFIG, as well as in-cluster configuration")
f.flags.StringVarP(&f.namespace, "namespace", "n", f.namespace, "The namespace in which Velero should operate")
f.flags.StringVar(&f.kubecontext, "kubecontext", "", "The context to use to talk to the Kubernetes apiserver. If unset defaults to whatever your current-context is (kubectl config current-context)")

//f.flags.StringVar(&f.kubecontext, "kubecontext", "", "The context to use to talk to the Kubernetes apiserver. If unset defaults to whatever your current-context is (kubectl config current-context)")
return f
}

Expand Down Expand Up @@ -127,7 +127,6 @@ func (f *factory) KubeClient() (kubernetes.Interface, error) {
if err != nil {
return nil, err
}

kubeClient, err := kubernetes.NewForConfig(clientConfig)
if err != nil {
return nil, errors.WithStack(err)
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ func TestFactory(t *testing.T) {

// Env variable should set the namespace if no config or argument are used
os.Setenv("VELERO_NAMESPACE", "env-velero")
f := NewFactory("velero", make(map[string]interface{}))
f := NewFactory("velero", "", make(map[string]interface{}))

assert.Equal(t, "env-velero", f.Namespace())

os.Unsetenv("VELERO_NAMESPACE")

// Argument should change the namespace
f = NewFactory("velero", make(map[string]interface{}))
f = NewFactory("velero", "", make(map[string]interface{}))
s := "flag-velero"
flags := new(pflag.FlagSet)

Expand All @@ -50,7 +50,7 @@ func TestFactory(t *testing.T) {

// An argument overrides the env variable if both are set.
os.Setenv("VELERO_NAMESPACE", "env-velero")
f = NewFactory("velero", make(map[string]interface{}))
f = NewFactory("velero", "", make(map[string]interface{}))
flags = new(pflag.FlagSet)

f.BindFlags(flags)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/velero/velero.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ operations can also be performed as 'velero backup get' and 'velero schedule cre
},
}

f := client.NewFactory(name, config)
f := client.NewFactory(name, "", config)
f.BindFlags(c.PersistentFlags())

// Bind features directly to the root command so it's available to all callers.
Expand Down
2 changes: 1 addition & 1 deletion pkg/restore/prioritize_group_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func userPriorityConfigMap() (*corev1.ConfigMap, error) {
return nil, errors.Wrap(err, "reading client config file")
}

fc := client.NewFactory("APIGroupVersionsRestore", cfg)
fc := client.NewFactory("APIGroupVersionsRestore", "", cfg)

kc, err := fc.KubeClient()
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion test/e2e/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ ADDITIONAL_BSL_CONFIG ?=
FEATURES ?=
DEBUG_E2E_TEST ?= false

DEFAULT_CLUSTER ?=
STANDBY_CLUSTER ?=


.PHONY:ginkgo
ginkgo: # Make sure ginkgo is in $GOPATH/bin
go get github.com/onsi/ginkgo/ginkgo
Expand Down Expand Up @@ -123,7 +127,9 @@ run: ginkgo
-install-velero=$(INSTALL_VELERO) \
-registry-credential-file=$(REGISTRY_CREDENTIAL_FILE) \
-kibishii-directory=$(KIBISHII_DIRECTORY) \
-debug-e2e-test=$(DEBUG_E2E_TEST)
-debug-e2e-test=$(DEBUG_E2E_TEST) \
-default-cluster=$(DEFAULT_CLUSTER) \
-standby-cluster=$(STANDBY_CLUSTER)

build: ginkgo
mkdir -p $(OUTPUT_DIR)
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func BackupRestoreTest(useVolumeSnapshots bool) {
)

By("Create test client instance", func() {
client, err = NewTestClient()
Expect(err).NotTo(HaveOccurred(), "Failed to instantiate cluster client for backup tests")
client, err = NewTestClient(VeleroCfg.DefaultCluster)
//Expect(err).NotTo(HaveOccurred(), "Failed to instantiate cluster client for backup tests")
})
BeforeEach(func() {
if useVolumeSnapshots && VeleroCfg.CloudProvider == "kind" {
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/backups/deletion.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func backup_deletion_test(useVolumeSnapshots bool) {
)

By("Create test client instance", func() {
client, err = NewTestClient()
Expect(err).NotTo(HaveOccurred(), "Failed to instantiate cluster client for backup tests")
client, err = NewTestClient(VeleroCfg.DefaultCluster)
//Expect(err).NotTo(HaveOccurred(), "Failed to instantiate cluster client for backup tests")
})
BeforeEach(func() {
if useVolumeSnapshots && VeleroCfg.CloudProvider == "kind" {
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/backups/sync_backups.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ func BackupsSyncTest() {
)

By("Create test client instance", func() {
client, err = NewTestClient()
Expect(err).NotTo(HaveOccurred(), "Failed to instantiate cluster client for backup tests")
client, err = NewTestClient(VeleroCfg.DefaultCluster)
//Expect(err).NotTo(HaveOccurred(), "Failed to instantiate cluster client for backup tests")
})

Expect(err).To(Succeed(), "Failed to instantiate cluster client for backup tests")
//Expect(err).To(Succeed(), "Failed to instantiate cluster client for backup tests")

BeforeEach(func() {
flag.Parse()
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/backups/ttl.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ func (b *TTL) Init() {
func TTLTest() {
useVolumeSnapshots := true
test := new(TTL)
client, err := NewTestClient()
client, err := NewTestClient(VeleroCfg.DefaultCluster)
if err != nil {
println(err.Error())
}
Expect(err).To(Succeed(), "Failed to instantiate cluster client for backup tests")
//Expect(err).To(Succeed(), "Failed to instantiate cluster client for backup tests")

BeforeEach(func() {
flag.Parse()
Expand Down Expand Up @@ -142,7 +142,7 @@ func TTLTest() {

By(fmt.Sprintf("Restore %s", test.testNS), func() {
Expect(VeleroRestore(test.ctx, VeleroCfg.VeleroCLI,
VeleroCfg.VeleroNamespace, test.restoreName, test.backupName)).To(Succeed(), func() string {
VeleroCfg.VeleroNamespace, test.restoreName, test.backupName, "")).To(Succeed(), func() string {
RunDebug(test.ctx, VeleroCfg.VeleroCLI,
VeleroCfg.VeleroNamespace, "", test.restoreName)
return "Fail to restore workload"
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/basic/enable_api_group_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func APIGropuVersionsTest() {
)

By("Create test client instance", func() {
client, err = NewTestClient()
Expect(err).NotTo(HaveOccurred(), "Failed to instantiate cluster client for backup tests")
client, err = NewTestClient(VeleroCfg.DefaultCluster)
//Expect(err).NotTo(HaveOccurred(), "Failed to instantiate cluster client for backup tests")
})

BeforeEach(func() {
Expand Down Expand Up @@ -288,7 +288,7 @@ func runEnableAPIGroupVersionsTests(ctx context.Context, client TestClient, reso
restore := "restore-rockbands-" + UUIDgen.String() + "-" + strconv.Itoa(i)

if tc.want != nil {
if err := VeleroRestore(ctx, VeleroCfg.VeleroCLI, VeleroCfg.VeleroNamespace, restore, backup); err != nil {
if err := VeleroRestore(ctx, VeleroCfg.VeleroCLI, VeleroCfg.VeleroNamespace, restore, backup, ""); err != nil {
RunDebug(context.Background(), VeleroCfg.VeleroCLI, VeleroCfg.VeleroNamespace, "", restore)
return errors.Wrapf(err, "restore %s namespaces on target cluster", namespacesStr)
}
Expand Down Expand Up @@ -327,7 +327,7 @@ func runEnableAPIGroupVersionsTests(ctx context.Context, client TestClient, reso
} else {
// No custom resource should have been restored. Expect "no resource found"
// error during restore.
err := VeleroRestore(ctx, VeleroCfg.VeleroCLI, VeleroCfg.VeleroNamespace, restore, backup)
err := VeleroRestore(ctx, VeleroCfg.VeleroCLI, VeleroCfg.VeleroNamespace, restore, backup, "")

if err.Error() != "Unexpected restore phase got PartiallyFailed, expecting Completed" {
return errors.New("expected error but not none")
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/bsl-mgmt/deletion.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func BslDeletionTest(useVolumeSnapshots bool) {
)

By("Create test client instance", func() {
client, err = NewTestClient()
Expect(err).NotTo(HaveOccurred(), "Failed to instantiate cluster client for backup tests")
client, err = NewTestClient(VeleroCfg.DefaultCluster)
//Expect(err).NotTo(HaveOccurred(), "Failed to instantiate cluster client for backup tests")
})

less := func(a, b string) bool { return a < b }
Expand Down
Loading

0 comments on commit 9e2dcbe

Please sign in to comment.