Skip to content

Commit

Permalink
refactor: error checks and nil pointer check
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimanyu003 committed Oct 8, 2024
1 parent 6c3410d commit 1453684
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 14 deletions.
2 changes: 1 addition & 1 deletion operator/apis/mlops/v1alpha1/experiment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (es *ExperimentStatus) IsConditionReady(t apis.ConditionType) bool {
func (es *ExperimentStatus) SetCondition(conditionType apis.ConditionType, condition *apis.Condition) {
switch {
case condition == nil:
experimentConditionSet.Manage(es).MarkUnknown(conditionType, "", "")
return
case condition.Status == v1.ConditionUnknown:
experimentConditionSet.Manage(es).MarkUnknown(conditionType, condition.Reason, condition.Message)
case condition.Status == v1.ConditionTrue:
Expand Down
69 changes: 69 additions & 0 deletions operator/apis/mlops/v1alpha1/experiment_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ the Change License after the Change Date as each is defined in accordance with t
package v1alpha1

import (
"knative.dev/pkg/ptr"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -210,3 +211,71 @@ func TestExperimentStatusPrintColumns(t *testing.T) {
})
}
}

func TestExperimentStatusSetCondition(t *testing.T) {
type args struct {
conditionType apis.ConditionType
condition *apis.Condition
}
tests := []struct {
name string
args args
want *v1.ConditionStatus
}{
{
name: "should not panic if condition is nil",
args: args{
conditionType: ExperimentReady,
condition: nil,
},
want: nil,
},
{
name: "ConditionUnknown",
args: args{
conditionType: ExperimentReady,
condition: &apis.Condition{
Status: "Unknown",
},
},
want: (*v1.ConditionStatus)(ptr.String("Unknown")),
},
{
name: "ConditionTrue",
args: args{
conditionType: ExperimentReady,
condition: &apis.Condition{
Status: "True",
},
},
want: (*v1.ConditionStatus)(ptr.String("True")),
},
{
name: "ConditionFalse",
args: args{
conditionType: ExperimentReady,
condition: &apis.Condition{
Status: "False",
},
},
want: (*v1.ConditionStatus)(ptr.String("False")),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
es := &ExperimentStatus{}
es.SetCondition(tt.args.conditionType, tt.args.condition)
if tt.want == nil {
if es.GetCondition(tt.args.conditionType) != nil {
t.Errorf("want %v : got %v", tt.want, es.GetCondition(tt.args.conditionType))
}
}
if tt.want != nil {
got := es.GetCondition(tt.args.conditionType).Status
if *tt.want != got {
t.Errorf("want %v : got %v", *tt.want, got)
}
}
})
}
}
2 changes: 1 addition & 1 deletion operator/apis/mlops/v1alpha1/server_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (ss *ServerStatus) IsConditionReady(t apis.ConditionType) bool {
func (ss *ServerStatus) SetCondition(condition *apis.Condition) {
switch {
case condition == nil:
serverConditionSet.Manage(ss).MarkUnknown(condition.Type, "", "")
return
case condition.Status == v1.ConditionUnknown:
serverConditionSet.Manage(ss).MarkUnknown(condition.Type, condition.Reason, condition.Message)
case condition.Status == v1.ConditionTrue:
Expand Down
66 changes: 66 additions & 0 deletions operator/apis/mlops/v1alpha1/server_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ package v1alpha1

import (
"encoding/json"
v1 "k8s.io/api/core/v1"
"knative.dev/pkg/apis"
"knative.dev/pkg/ptr"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -71,3 +74,66 @@ func TestServerStatusPrintColumns(t *testing.T) {
})
}
}

func TestServerStatusSetCondition(t *testing.T) {
type args struct {
condition *apis.Condition
}
tests := []struct {
name string
args args
want *v1.ConditionStatus
}{
{
name: "should not panic if condition is nil",
args: args{
condition: nil,
},
want: nil,
},
{
name: "ConditionUnknown",
args: args{
condition: &apis.Condition{
Status: "Unknown",
},
},
want: (*v1.ConditionStatus)(ptr.String("Unknown")),
},
{
name: "ConditionTrue",
args: args{
condition: &apis.Condition{
Status: "True",
},
},
want: (*v1.ConditionStatus)(ptr.String("True")),
},
{
name: "ConditionFalse",
args: args{
condition: &apis.Condition{
Status: "False",
},
},
want: (*v1.ConditionStatus)(ptr.String("False")),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ss := &ServerStatus{}
ss.SetCondition(tt.args.condition)
if tt.want == nil {
if ss.GetCondition("") != nil {
t.Errorf("want %v : got %v", tt.want, ss.GetCondition(""))
}
}
if tt.want != nil {
got := ss.GetCondition("").Status
if *tt.want != got {
t.Errorf("want %v : got %v", *tt.want, got)
}
}
})
}
}
3 changes: 0 additions & 3 deletions operator/pkg/cli/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,6 @@ func (sc *SchedulerClient) LoadPipeline(data []byte) (*scheduler.LoadPipelineRes
return nil, err
}
schPipeline := pipeline.AsSchedulerPipeline()
if err != nil {
return nil, err
}
req := &scheduler.LoadPipelineRequest{Pipeline: schPipeline}
if sc.verbose {
printProto(req)
Expand Down
4 changes: 0 additions & 4 deletions scheduler/pkg/kafka/gateway/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,7 @@ type mockGRPCMLServer struct {
}

func (m *mockGRPCMLServer) setup() error {
var err error
m.listener = bufconn.Listen(bufSize)
if err != nil {
return err
}
opts := []grpc.ServerOption{}
m.server = grpc.NewServer(opts...)
v2.RegisterGRPCInferenceServiceServer(m.server, m)
Expand Down
3 changes: 0 additions & 3 deletions scheduler/pkg/kafka/pipeline/status/model_rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ func NewModelRestStatusCaller(logger logrus.FieldLogger, envoyHost string, envoy
return nil, err
}
httpClient := util.GetHttpClientFromTLSOptions(tlsOptions)
if err != nil {
return nil, err
}
return &ModelRestCaller{
envoyHost: envoyHost,
envoyPort: envoyPort,
Expand Down
4 changes: 2 additions & 2 deletions scheduler/pkg/proxy/chainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ func (pc *ProxyChainer) SubscribePipelineUpdates(
Version: 1,
Uid: "1234",
Updates: []*chainer.PipelineStepUpdate{
&chainer.PipelineStepUpdate{
{
Sources: []*chainer.PipelineTopic{{PipelineName: "some-pipeline", TopicName: chainerInputTopic1}},
Sink: &chainer.PipelineTopic{PipelineName: "some-pipeline", TopicName: chainerOutputTopic1},
InputJoinTy: chainer.PipelineStepUpdate_Inner,
},
&chainer.PipelineStepUpdate{
{
Sources: []*chainer.PipelineTopic{{PipelineName: "some-pipeline", TopicName: chainerInputTopic2}},
Sink: &chainer.PipelineTopic{PipelineName: "some-pipeline", TopicName: chainerOutputTopic2},
InputJoinTy: chainer.PipelineStepUpdate_Inner,
Expand Down

0 comments on commit 1453684

Please sign in to comment.