-
Notifications
You must be signed in to change notification settings - Fork 887
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In this commit, we unit test the init and deinit workflows in the operator pkg. Signed-off-by: Mohamed Awnallah <mohamedmohey2352@gmail.com>
- Loading branch information
1 parent
8cc712f
commit 32792cf
Showing
6 changed files
with
875 additions
and
33 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,167 @@ | ||
/* | ||
Copyright 2024 The Karmada 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 karmada | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
clientset "k8s.io/client-go/kubernetes" | ||
fakeclientset "k8s.io/client-go/kubernetes/fake" | ||
"k8s.io/client-go/rest" | ||
|
||
operatorv1alpha1 "github.com/karmada-io/karmada/operator/pkg/apis/operator/v1alpha1" | ||
"github.com/karmada-io/karmada/operator/pkg/workflow" | ||
) | ||
|
||
func TestNewDeInitDataJob(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
deInitOptions *DeInitOptions | ||
tasksExpected []workflow.Task | ||
}{ | ||
{ | ||
name: "NewDeInitDataJob_WithInitTasks_AllIsSubset", | ||
deInitOptions: &DeInitOptions{ | ||
Name: "test_deinit", | ||
Namespace: "test", | ||
Kubeconfig: &rest.Config{}, | ||
HostCluster: &operatorv1alpha1.HostCluster{}, | ||
}, | ||
tasksExpected: DefaultDeInitTasks, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
deInitJob := NewDeInitDataJob(test.deInitOptions, test.tasksExpected) | ||
err := containAllTasks(deInitJob.Tasks, test.tasksExpected) | ||
if err != nil { | ||
t.Errorf("unexpected error, got: %v", err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestRunNewDeInitJob(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
deInitOptions *DeInitOptions | ||
tasksExpected []workflow.Task | ||
mockFunc func() | ||
wantErr bool | ||
errMsg string | ||
}{ | ||
{ | ||
name: "RunNewDeInitJob_EmptyNamespace_NamespaceIsEmpty", | ||
deInitOptions: &DeInitOptions{ | ||
Name: "test_deinit", | ||
Kubeconfig: &rest.Config{}, | ||
HostCluster: &operatorv1alpha1.HostCluster{}, | ||
}, | ||
tasksExpected: DefaultDeInitTasks, | ||
mockFunc: func() {}, | ||
wantErr: true, | ||
errMsg: "unexpected empty name or namespace", | ||
}, | ||
{ | ||
name: "RunNewDeInitJob_EmptyName_NameIsEmpty", | ||
deInitOptions: &DeInitOptions{ | ||
Namespace: "test", | ||
Kubeconfig: &rest.Config{}, | ||
HostCluster: &operatorv1alpha1.HostCluster{}, | ||
}, | ||
tasksExpected: DefaultDeInitTasks, | ||
mockFunc: func() {}, | ||
wantErr: true, | ||
errMsg: "unexpected empty name", | ||
}, | ||
{ | ||
name: "RunNewDeInitJob_FailedToCreateLocalClusterClient_LocalClusterClientCreationError", | ||
deInitOptions: &DeInitOptions{ | ||
Name: "test_deinit", | ||
Namespace: "test", | ||
Kubeconfig: &rest.Config{}, | ||
HostCluster: &operatorv1alpha1.HostCluster{}, | ||
}, | ||
tasksExpected: DefaultDeInitTasks, | ||
mockFunc: func() { | ||
clientFactory = func(*rest.Config) (clientset.Interface, error) { | ||
return nil, fmt.Errorf("failed to create local cluster client") | ||
} | ||
}, | ||
wantErr: true, | ||
errMsg: "failed to create local cluster client", | ||
}, | ||
{ | ||
name: "RunNewDeInitJob_FailedToCreateRemoteClusterClient_RemoteClusterClientCreationError", | ||
deInitOptions: &DeInitOptions{ | ||
Name: "test_deinit", | ||
Namespace: "test", | ||
Kubeconfig: &rest.Config{}, | ||
HostCluster: &operatorv1alpha1.HostCluster{ | ||
SecretRef: &operatorv1alpha1.LocalSecretReference{ | ||
Namespace: "test", | ||
Name: "karmada-demo", | ||
}, | ||
}, | ||
}, | ||
tasksExpected: DefaultDeInitTasks, | ||
mockFunc: func() { | ||
clientFactory = func(*rest.Config) (clientset.Interface, error) { | ||
return fakeclientset.NewSimpleClientset(), nil | ||
} | ||
buildClientFromSecretRefFactory = func(clientset.Interface, *operatorv1alpha1.LocalSecretReference) (clientset.Interface, error) { | ||
return nil, fmt.Errorf("failed to create remote cluster client") | ||
} | ||
}, | ||
wantErr: true, | ||
errMsg: "failed to create remote cluster client", | ||
}, | ||
{ | ||
name: "RunNewDeInitJob_WithDeInitTasks_RunIsSuccessful", | ||
deInitOptions: &DeInitOptions{ | ||
Name: "test_deinit", | ||
Namespace: "test", | ||
Kubeconfig: &rest.Config{}, | ||
HostCluster: &operatorv1alpha1.HostCluster{}, | ||
}, | ||
mockFunc: func() { | ||
clientFactory = func(*rest.Config) (clientset.Interface, error) { | ||
return fakeclientset.NewSimpleClientset(), nil | ||
} | ||
}, | ||
tasksExpected: DefaultDeInitTasks, | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
test.mockFunc() | ||
deInitJob := NewDeInitDataJob(test.deInitOptions, test.tasksExpected) | ||
err := deInitJob.Run() | ||
if (err != nil && !test.wantErr) || (err == nil && test.wantErr) { | ||
t.Errorf("RunNewDeInitJob() = got %v error, but want %t error", err, test.wantErr) | ||
} | ||
if (err != nil && test.wantErr) && (!strings.Contains(err.Error(), test.errMsg)) { | ||
t.Errorf("RunNewDeInitJob() = got %s, want %s", err.Error(), test.errMsg) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.