Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/v1alpha1/alluxioruntime_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,15 @@ func init() {

// Replicas gets the replicas of runtime worker
func (runtime *AlluxioRuntime) Replicas() int32 {
if runtime == nil {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a brief comment explaining the nil-receiver return value, e.g.: // nil AlluxioRuntime defaults to 0 replicas. This makes the intent explicit and prevents future readers from questioning whether the nil check is intentional or just defensive.

return 0
}
return runtime.Spec.Replicas
}

func (runtime *AlluxioRuntime) GetStatus() *RuntimeStatus {
if runtime == nil {
return &RuntimeStatus{}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nil-receiver guard returns &RuntimeStatus{} — a freshly allocated empty struct. For non-nil receivers, GetStatus() returns a pointer to the same underlying Status (mutations propagate back). But on nil receiver, mutations to the returned pointer are silently discarded since they operate on a disconnected temporary. This asymmetry could surprise callers. Consider adding a comment: // nil AlluxioRuntime returns empty, disconnected status.

}
return &runtime.Status
}
87 changes: 87 additions & 0 deletions api/v1alpha1/alluxioruntime_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Copyright 2026 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 v1alpha1

import "testing"

func TestAlluxioRuntime_Replicas(t *testing.T) {
testCases := map[string]struct {
runtime *AlluxioRuntime
want int32
}{
"nil runtime defaults to zero": {
runtime: nil,
want: 0,
},
"returns configured replicas": {
runtime: &AlluxioRuntime{
Spec: AlluxioRuntimeSpec{
Replicas: 3,
},
},
want: 3,
},
"returns zero replicas when not configured": {
runtime: &AlluxioRuntime{},
want: 0,
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got := tc.runtime.Replicas()
if got != tc.want {
t.Fatalf("Replicas() = %d, want %d", got, tc.want)
}
})
}
}

func TestAlluxioRuntime_GetStatus(t *testing.T) {
t.Run("nil runtime returns empty status pointer", func(t *testing.T) {
var runtime *AlluxioRuntime
got := runtime.GetStatus()
if got == nil {
t.Fatalf("GetStatus() returned nil")
}
if got.MasterPhase != RuntimePhaseNone {
t.Fatalf("GetStatus().MasterPhase = %q, want %q", got.MasterPhase, RuntimePhaseNone)
}
})

t.Run("returns pointer to runtime status", func(t *testing.T) {
runtime := &AlluxioRuntime{
Status: RuntimeStatus{
MasterPhase: RuntimePhaseReady,
WorkerPhase: RuntimePhasePartialReady,
},
}

got := runtime.GetStatus()
if got == nil {
t.Fatalf("GetStatus() returned nil")
}
if got.MasterPhase != RuntimePhaseReady {
t.Fatalf("GetStatus().MasterPhase = %q, want %q", got.MasterPhase, RuntimePhaseReady)
}

got.FusePhase = RuntimePhaseNotReady
if runtime.Status.FusePhase != RuntimePhaseNotReady {
t.Fatalf("GetStatus() should return underlying status pointer")
}
})
}
Loading