generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 995
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update juicefsruntime unit test (#19)
* add jucefsruntime ut * fix test * update juicefsruntime test Signed-off-by: zwwhdls <zww@hdls.me>
- Loading branch information
Showing
7 changed files
with
1,082 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,331 @@ | ||
/* | ||
Copyright 2021 The Fluid 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 juicefs | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
|
||
datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1" | ||
"github.com/fluid-cloudnative/fluid/pkg/common" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
func TestUpdateCacheOfDataset(t *testing.T) { | ||
testDatasetInputs := []*datav1alpha1.Dataset{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "hbase", | ||
Namespace: "fluid", | ||
}, | ||
Spec: datav1alpha1.DatasetSpec{}, | ||
}, | ||
} | ||
testObjs := []runtime.Object{} | ||
for _, datasetInput := range testDatasetInputs { | ||
testObjs = append(testObjs, datasetInput.DeepCopy()) | ||
} | ||
|
||
testRuntimeInputs := []*datav1alpha1.JuiceFSRuntime{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "hbase", | ||
Namespace: "fluid", | ||
}, | ||
Spec: datav1alpha1.JuiceFSRuntimeSpec{ | ||
Replicas: 1, | ||
}, | ||
Status: datav1alpha1.JuiceFSRuntimeStatus{ | ||
CacheStates: map[common.CacheStateName]string{ | ||
common.Cached: "true", | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, runtimeInput := range testRuntimeInputs { | ||
testObjs = append(testObjs, runtimeInput.DeepCopy()) | ||
} | ||
client := fake.NewFakeClientWithScheme(testScheme, testObjs...) | ||
|
||
engine := &JuiceFSEngine{ | ||
Client: client, | ||
Log: log.NullLogger{}, | ||
name: "hbase", | ||
namespace: "fluid", | ||
runtime: testRuntimeInputs[0], | ||
} | ||
|
||
err := engine.UpdateCacheOfDataset() | ||
if err != nil { | ||
t.Errorf("fail to exec UpdateCacheOfDataset with error %v", err) | ||
return | ||
} | ||
|
||
expectedDataset := datav1alpha1.Dataset{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "hbase", | ||
Namespace: "fluid", | ||
}, | ||
Status: datav1alpha1.DatasetStatus{ | ||
CacheStates: map[common.CacheStateName]string{ | ||
common.Cached: "true", | ||
}, | ||
Runtimes: []datav1alpha1.Runtime{ | ||
{ | ||
Name: "hbase", | ||
Namespace: "fluid", | ||
Category: common.AccelerateCategory, | ||
Type: common.JuiceFSRuntime, | ||
MasterReplicas: 1, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
var datasets datav1alpha1.DatasetList | ||
err = client.List(context.TODO(), &datasets) | ||
if err != nil { | ||
t.Errorf("fail to list the datasets with error %v", err) | ||
return | ||
} | ||
if !reflect.DeepEqual(datasets.Items[0].Status, expectedDataset.Status) { | ||
t.Errorf("fail to exec the function with error %v", err) | ||
return | ||
} | ||
} | ||
|
||
func TestUpdateDatasetStatus(t *testing.T) { | ||
testDatasetInputs := []*datav1alpha1.Dataset{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "hbase", | ||
Namespace: "fluid", | ||
}, | ||
Spec: datav1alpha1.DatasetSpec{}, | ||
Status: datav1alpha1.DatasetStatus{ | ||
HCFSStatus: &datav1alpha1.HCFSStatus{ | ||
Endpoint: "test Endpoint", | ||
UnderlayerFileSystemVersion: "Underlayer HCFS Compatible Version", | ||
}, | ||
}, | ||
}, | ||
} | ||
testObjs := []runtime.Object{} | ||
for _, datasetInput := range testDatasetInputs { | ||
testObjs = append(testObjs, datasetInput.DeepCopy()) | ||
} | ||
|
||
testRuntimeInputs := []*datav1alpha1.JuiceFSRuntime{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "hbase", | ||
Namespace: "fluid", | ||
}, | ||
Spec: datav1alpha1.JuiceFSRuntimeSpec{ | ||
Replicas: 1, | ||
}, | ||
Status: datav1alpha1.JuiceFSRuntimeStatus{ | ||
CacheStates: map[common.CacheStateName]string{ | ||
common.Cached: "true", | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, runtimeInput := range testRuntimeInputs { | ||
testObjs = append(testObjs, runtimeInput.DeepCopy()) | ||
} | ||
client := fake.NewFakeClientWithScheme(testScheme, testObjs...) | ||
|
||
engine := &JuiceFSEngine{ | ||
Client: client, | ||
Log: log.NullLogger{}, | ||
name: "hbase", | ||
namespace: "fluid", | ||
runtime: testRuntimeInputs[0], | ||
} | ||
|
||
var testCase = []struct { | ||
phase datav1alpha1.DatasetPhase | ||
expectedResult datav1alpha1.Dataset | ||
}{ | ||
{ | ||
phase: datav1alpha1.BoundDatasetPhase, | ||
expectedResult: datav1alpha1.Dataset{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "hbase", | ||
Namespace: "fluid", | ||
}, | ||
Status: datav1alpha1.DatasetStatus{ | ||
Phase: datav1alpha1.BoundDatasetPhase, | ||
CacheStates: map[common.CacheStateName]string{ | ||
common.Cached: "true", | ||
}, | ||
HCFSStatus: &datav1alpha1.HCFSStatus{ | ||
Endpoint: "test Endpoint", | ||
UnderlayerFileSystemVersion: "Underlayer HCFS Compatible Version", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
phase: datav1alpha1.FailedDatasetPhase, | ||
expectedResult: datav1alpha1.Dataset{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "hbase", | ||
Namespace: "fluid", | ||
}, | ||
Status: datav1alpha1.DatasetStatus{ | ||
Phase: datav1alpha1.FailedDatasetPhase, | ||
CacheStates: map[common.CacheStateName]string{ | ||
common.Cached: "true", | ||
}, | ||
HCFSStatus: &datav1alpha1.HCFSStatus{ | ||
Endpoint: "test Endpoint", | ||
UnderlayerFileSystemVersion: "Underlayer HCFS Compatible Version", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
phase: datav1alpha1.NoneDatasetPhase, | ||
expectedResult: datav1alpha1.Dataset{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "hbase", | ||
Namespace: "fluid", | ||
}, | ||
Status: datav1alpha1.DatasetStatus{ | ||
Phase: datav1alpha1.NoneDatasetPhase, | ||
CacheStates: map[common.CacheStateName]string{ | ||
common.Cached: "true", | ||
}, | ||
HCFSStatus: &datav1alpha1.HCFSStatus{ | ||
Endpoint: "test Endpoint", | ||
UnderlayerFileSystemVersion: "Underlayer HCFS Compatible Version", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range testCase { | ||
err := engine.UpdateDatasetStatus(test.phase) | ||
if err != nil { | ||
t.Errorf("fail to exec UpdateCacheOfDataset with error %v", err) | ||
return | ||
} | ||
|
||
var datasets datav1alpha1.DatasetList | ||
err = client.List(context.TODO(), &datasets) | ||
if err != nil { | ||
t.Errorf("fail to list the datasets with error %v", err) | ||
return | ||
} | ||
if !reflect.DeepEqual(datasets.Items[0].Status.Phase, test.expectedResult.Status.Phase) || | ||
!reflect.DeepEqual(datasets.Items[0].Status.CacheStates, test.expectedResult.Status.CacheStates) || | ||
!reflect.DeepEqual(datasets.Items[0].Status.HCFSStatus, test.expectedResult.Status.HCFSStatus) { | ||
t.Errorf("fail to exec the function with error %v", err) | ||
return | ||
} | ||
} | ||
} | ||
|
||
func TestBindToDataset(t *testing.T) { | ||
testDatasetInputs := []*datav1alpha1.Dataset{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "hbase", | ||
Namespace: "fluid", | ||
}, | ||
Spec: datav1alpha1.DatasetSpec{}, | ||
Status: datav1alpha1.DatasetStatus{ | ||
HCFSStatus: &datav1alpha1.HCFSStatus{ | ||
Endpoint: "test Endpoint", | ||
UnderlayerFileSystemVersion: "Underlayer HCFS Compatible Version", | ||
}, | ||
}, | ||
}, | ||
} | ||
testObjs := []runtime.Object{} | ||
for _, datasetInput := range testDatasetInputs { | ||
testObjs = append(testObjs, datasetInput.DeepCopy()) | ||
} | ||
|
||
testRuntimeInputs := []*datav1alpha1.JuiceFSRuntime{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "hbase", | ||
Namespace: "fluid", | ||
}, | ||
Spec: datav1alpha1.JuiceFSRuntimeSpec{}, | ||
Status: datav1alpha1.JuiceFSRuntimeStatus{ | ||
CacheStates: map[common.CacheStateName]string{ | ||
common.Cached: "true", | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, runtimeInput := range testRuntimeInputs { | ||
testObjs = append(testObjs, runtimeInput.DeepCopy()) | ||
} | ||
client := fake.NewFakeClientWithScheme(testScheme, testObjs...) | ||
|
||
engine := &JuiceFSEngine{ | ||
Client: client, | ||
Log: log.NullLogger{}, | ||
name: "hbase", | ||
namespace: "fluid", | ||
runtime: testRuntimeInputs[0], | ||
} | ||
|
||
var expectedResult = datav1alpha1.Dataset{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "hbase", | ||
Namespace: "fluid", | ||
}, | ||
Status: datav1alpha1.DatasetStatus{ | ||
Phase: datav1alpha1.BoundDatasetPhase, | ||
CacheStates: map[common.CacheStateName]string{ | ||
common.Cached: "true", | ||
}, | ||
HCFSStatus: &datav1alpha1.HCFSStatus{ | ||
Endpoint: "test Endpoint", | ||
UnderlayerFileSystemVersion: "Underlayer HCFS Compatible Version", | ||
}, | ||
}, | ||
} | ||
err := engine.BindToDataset() | ||
if err != nil { | ||
t.Errorf("fail to exec UpdateCacheOfDataset with error %v", err) | ||
return | ||
} | ||
|
||
var datasets datav1alpha1.DatasetList | ||
err = client.List(context.TODO(), &datasets) | ||
if err != nil { | ||
t.Errorf("fail to list the datasets with error %v", err) | ||
return | ||
} | ||
if !reflect.DeepEqual(datasets.Items[0].Status.Phase, expectedResult.Status.Phase) || | ||
!reflect.DeepEqual(datasets.Items[0].Status.CacheStates, expectedResult.Status.CacheStates) || | ||
!reflect.DeepEqual(datasets.Items[0].Status.HCFSStatus, expectedResult.Status.HCFSStatus) { | ||
t.Errorf("fail to exec the function with error %v", err) | ||
return | ||
} | ||
} |
Oops, something went wrong.