Skip to content

Commit

Permalink
Add unittest for vineyardctl (#1499)
Browse files Browse the repository at this point in the history
Fixes #1301

Signed-off-by: zhuyi1159 <1159751291@qq.com>
Signed-off-by: Ye Cao <caoye.cao@alibaba-inc.com>
Co-authored-by: Ye Cao <caoye.cao@alibaba-inc.com>
  • Loading branch information
zhuyi1159 and dashanji authored Aug 29, 2023
1 parent 8d5969a commit 89b3b17
Show file tree
Hide file tree
Showing 22 changed files with 4,528 additions and 13 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/vineyard-operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ jobs:
matrix:
job:
- release
- unit-tests
- e2e-tests-airflow-integration
- e2e-tests-assembly-local
- e2e-tests-assembly-distributed
Expand Down Expand Up @@ -159,6 +160,11 @@ jobs:
run: |
make -C k8s/test/e2e build-base-images
- name: unit-tests
if: ${{ matrix.job == 'unit-tests' }}
run: |
make -C k8s/test/e2e unittest
- name: Prepare directories for logs
run: |
# create log directories for all tasks to prevent the "Path to upload does not exist" warnings
Expand All @@ -177,7 +183,8 @@ jobs:
e2e-tests-workflow \
e2e-tests-mars-examples \
e2e-tests-vineyardctl \
e2e-tests-deploy-raw-backup-and-recover; do
e2e-tests-deploy-raw-backup-and-recover \
unit-tests; do
mkdir -p $job-logs
chmod a+rwx $job-logs
done
Expand Down Expand Up @@ -343,4 +350,4 @@ jobs:
;;
esac
echo "::notice:: ${{ matrix.job }}: https://www.stoat.dev/file-viewer?root=https://v6d-io--v6d--$short_sha--e2e-tests-$signed_url_key.stoat.page"
echo "::notice:: ${{ matrix.job }}: https://www.stoat.dev/file-viewer?root=https://v6d-io--v6d--$short_sha--e2e-tests-$signed_url_key.stoat.page"
5 changes: 0 additions & 5 deletions k8s/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ all: vineyardctl
.PHONY: check
check: lint e2e-test

# install the cert-manager and wait for ready
.PHONY: install-cert-manager
install-cert-manager:
@go run cmd/main.go deploy cert-manager

# Build vineyardctl binary
.PHONY: vineyardctl
vineyardctl: generate fmt
Expand Down
82 changes: 82 additions & 0 deletions k8s/cmd/commands/create/create_backup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2020-2023 Alibaba Group Holding Limited.
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 create

import (
"fmt"
"os"
"reflect"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/v6d-io/v6d/k8s/apis/k8s/v1alpha1"
"github.com/v6d-io/v6d/k8s/cmd/commands/flags"
"github.com/v6d-io/v6d/k8s/cmd/commands/util"
)

func Test_BuildBackup(t *testing.T) {
flags.KubeConfig = os.Getenv("KUBECONFIG")
flags.BackupName = "test-backup"
flags.Namespace = "test_backup"
flags.BackupOpts.BackupPath = "backup/path/to/test"
c := util.KubernetesClient()

type args struct {
c client.Client
args []string
}
tests := []struct {
name string
args args
want *v1alpha1.Backup
wantErr bool
}{
{
name: "Valid JSON from stdin",
args: args{
c: c,
args: []string{},
},
want: &v1alpha1.Backup{
// Expected Backup CR based on the provided JSON.
ObjectMeta: metav1.ObjectMeta{
Name: "test-backup",
Namespace: "test_backup",
},
Spec: v1alpha1.BackupSpec{
BackupPath: "backup/path/to/test",
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := BuildBackup(tt.args.c, tt.args.args)
if (err != nil) != tt.wantErr {
t.Errorf("BuildBackup() error = %v, wantErr %v", err, tt.wantErr)
return
}
a := fmt.Sprint(got)
b := fmt.Sprint(tt.want)
if !reflect.DeepEqual(a, b) {
t.Errorf("BuildBackup() = %v, want %v", got, tt.want)
}
})
}
}
57 changes: 57 additions & 0 deletions k8s/cmd/commands/create/create_operation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2020-2023 Alibaba Group Holding Limited.
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 create

import (
"reflect"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/v6d-io/v6d/k8s/apis/k8s/v1alpha1"
"github.com/v6d-io/v6d/k8s/cmd/commands/flags"
)

func Test_BuildOperation(t *testing.T) {
flags.OperationName = "test-operation"
flags.OperationOpts.Name = "test-operation-opts"
tests := []struct {
name string
want *v1alpha1.Operation
}{
// Add test cases.
{
name: "Test Case 1",
want: &v1alpha1.Operation{
ObjectMeta: metav1.ObjectMeta{
Name: "test-operation",
Namespace: flags.GetDefaultVineyardNamespace(),
},
Spec: v1alpha1.OperationSpec{
Name: "test-operation-opts",
TimeoutSeconds: int64(600),
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := buildOperation(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("buildOperation() = %v, want %v", got, tt.want)
}
})
}
}
65 changes: 65 additions & 0 deletions k8s/cmd/commands/create/create_recover_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2020-2023 Alibaba Group Holding Limited.
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 create

import (
"reflect"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/v6d-io/v6d/k8s/apis/k8s/v1alpha1"
"github.com/v6d-io/v6d/k8s/cmd/commands/flags"
)

func Test_BuildV1alphaRecoverCR(t *testing.T) {
flags.RecoverName = "test-recover"
flags.BackupName = "test-backup"

tests := []struct {
name string
want *v1alpha1.Recover
wantErr bool
}{
// Add test cases.
{
name: "Test Case 1",
want: &v1alpha1.Recover{
ObjectMeta: metav1.ObjectMeta{
Name: "test-recover",
Namespace: flags.GetDefaultVineyardNamespace(),
},
Spec: v1alpha1.RecoverSpec{
BackupName: "test-backup",
BackupNamespace: flags.GetDefaultVineyardNamespace(),
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := BuildV1alphaRecoverCR()
if (err != nil) != tt.wantErr {
t.Errorf("BuildV1alphaRecoverCR() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("BuildV1alphaRecoverCR() = %v, want %v", got, tt.want)
}
})
}
}
62 changes: 62 additions & 0 deletions k8s/cmd/commands/delete/delete_vineyard_deployment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2020-2023 Alibaba Group Holding Limited.
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 delete

import (
"context"
"os"
"testing"

"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/v6d-io/v6d/k8s/cmd/commands/deploy"
"github.com/v6d-io/v6d/k8s/cmd/commands/flags"
"github.com/v6d-io/v6d/k8s/cmd/commands/util"
"github.com/v6d-io/v6d/k8s/pkg/log"
apierrors "k8s.io/apimachinery/pkg/api/errors"
)

func TestDeleteVineyardDeploymentCmd(t *testing.T) {
// deploy a vineyardd for later delete operation
flags.KubeConfig = os.Getenv("KUBECONFIG")
flags.VineyarddName = "test-vineyardd-deployment-name"
flags.Namespace = "vineyard-system"
flags.VineyarddOpts.Replicas = 3
flags.VineyarddOpts.EtcdReplicas = 1
flags.VineyarddOpts.Vineyard.Image = "vineyardcloudnative/vineyardd:latest"
flags.VineyarddOpts.Vineyard.CPU = ""
flags.VineyarddOpts.Vineyard.Memory = ""
flags.VineyarddOpts.Service.Port = 9600
flags.VineyarddOpts.Service.Type = "ClusterIP"
flags.VineyarddOpts.Volume.PvcName = ""
flags.VineyarddOpts.Vineyard.Size = "256Mi"
c := util.KubernetesClient()
deployVineyardDeploymentCmd := deploy.NewDeployVineyardDeploymentCmd()
deployVineyardDeploymentCmd.Run(deployVineyardDeploymentCmd, []string{})

// delete operation
deleteVineyardDeploymentCmd.Run(deleteVineyardDeploymentCmd, []string{})

objects, _ := deploy.GetVineyardDeploymentObjectsFromTemplate()

// test if the vineyardd has been deleted
for _, obj := range objects {
err := c.Get(context.TODO(), client.ObjectKeyFromObject(obj), obj)
if (err == nil) || !apierrors.IsNotFound(err) {
log.Error(err, "failed to deleted object")
}
}
}
Loading

0 comments on commit 89b3b17

Please sign in to comment.