Skip to content

Commit 7e5d85f

Browse files
committed
test(base): add Setup error and not-ready path coverage
1 parent 9fe6aad commit 7e5d85f

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

pkg/ddc/base/setup_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
Copyright 2026 The Fluid Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package base_test
18+
19+
import (
20+
"errors"
21+
"testing"
22+
23+
datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
24+
"github.com/fluid-cloudnative/fluid/pkg/ddc/base"
25+
enginemock "github.com/fluid-cloudnative/fluid/pkg/ddc/base/mock"
26+
cruntime "github.com/fluid-cloudnative/fluid/pkg/runtime"
27+
"github.com/fluid-cloudnative/fluid/pkg/utils/fake"
28+
"github.com/golang/mock/gomock"
29+
"k8s.io/apimachinery/pkg/types"
30+
)
31+
32+
func newSetupEngine(impl *enginemock.MockImplement) (*base.TemplateEngine, cruntime.ReconcileRequestContext) {
33+
ctx := cruntime.ReconcileRequestContext{
34+
NamespacedName: types.NamespacedName{
35+
Namespace: "default",
36+
Name: "dataset-0",
37+
},
38+
Log: fake.NullLogger(),
39+
RuntimeType: "alluxio",
40+
Runtime: &datav1alpha1.AlluxioRuntime{},
41+
}
42+
43+
return base.NewTemplateEngine(impl, "setup-test", ctx), ctx
44+
}
45+
46+
func assertSetupResult(t *testing.T, ready bool, err error, expectedReady bool, expectedErr error) {
47+
t.Helper()
48+
if ready != expectedReady {
49+
t.Fatalf("unexpected ready state, got %v, want %v", ready, expectedReady)
50+
}
51+
52+
if expectedErr == nil && err != nil {
53+
t.Fatalf("unexpected error: %v", err)
54+
}
55+
if expectedErr != nil && !errors.Is(err, expectedErr) {
56+
t.Fatalf("unexpected error, got %v, want %v", err, expectedErr)
57+
}
58+
}
59+
60+
func TestSetupShouldSetupMasterError(t *testing.T) {
61+
ctrl := gomock.NewController(t)
62+
defer ctrl.Finish()
63+
64+
impl := enginemock.NewMockImplement(ctrl)
65+
engine, ctx := newSetupEngine(impl)
66+
testErr := errors.New("should-setup-master failed")
67+
68+
impl.EXPECT().ShouldSetupMaster().Return(false, testErr).Times(1)
69+
70+
ready, err := engine.Setup(ctx)
71+
assertSetupResult(t, ready, err, false, testErr)
72+
}
73+
74+
func TestSetupMasterError(t *testing.T) {
75+
ctrl := gomock.NewController(t)
76+
defer ctrl.Finish()
77+
78+
impl := enginemock.NewMockImplement(ctrl)
79+
engine, ctx := newSetupEngine(impl)
80+
testErr := errors.New("setup-master failed")
81+
82+
gomock.InOrder(
83+
impl.EXPECT().ShouldSetupMaster().Return(true, nil).Times(1),
84+
impl.EXPECT().SetupMaster().Return(testErr).Times(1),
85+
)
86+
87+
ready, err := engine.Setup(ctx)
88+
assertSetupResult(t, ready, err, false, testErr)
89+
}
90+
91+
func TestSetupMasterNotReady(t *testing.T) {
92+
ctrl := gomock.NewController(t)
93+
defer ctrl.Finish()
94+
95+
impl := enginemock.NewMockImplement(ctrl)
96+
engine, ctx := newSetupEngine(impl)
97+
98+
gomock.InOrder(
99+
impl.EXPECT().ShouldSetupMaster().Return(false, nil).Times(1),
100+
impl.EXPECT().CheckMasterReady().Return(false, nil).Times(1),
101+
)
102+
103+
ready, err := engine.Setup(ctx)
104+
assertSetupResult(t, ready, err, false, nil)
105+
}

0 commit comments

Comments
 (0)