-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unittest for vineyardctl (#1499)
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
Showing
22 changed files
with
4,528 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
62
k8s/cmd/commands/delete/delete_vineyard_deployment_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} |
Oops, something went wrong.