Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more unit tests in Katib #1071

Merged
merged 5 commits into from
Feb 29, 2020
Merged
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
38 changes: 38 additions & 0 deletions cmd/db-manager/v1alpha3/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/golang/mock/gomock"

health_pb "github.com/kubeflow/katib/pkg/apis/manager/health"
api_pb "github.com/kubeflow/katib/pkg/apis/manager/v1alpha3"
mockdb "github.com/kubeflow/katib/pkg/mock/v1alpha3/db"
)
Expand Down Expand Up @@ -131,3 +132,40 @@ func TestDeleteObservationLog(t *testing.T) {
t.Fatalf("DeleteExperiment Error %v", err)
}
}

func TestCheck(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
s := &server{}
mockDB := mockdb.NewMockKatibDBInterface(ctrl)
dbIf = mockDB
testCases := []struct {
Request *health_pb.HealthCheckRequest
ExpectedStatus health_pb.HealthCheckResponse_ServingStatus
Name string
}{
{
Request: &health_pb.HealthCheckRequest{
Service: "grpc.health.v1.Health",
},
ExpectedStatus: health_pb.HealthCheckResponse_SERVING,
Name: "Valid Request",
},
{
Request: &health_pb.HealthCheckRequest{
Service: "grpc.health.v1.1.Health",
},
ExpectedStatus: health_pb.HealthCheckResponse_UNKNOWN,
Name: "Invalid service name",
},
}

mockDB.EXPECT().SelectOne().Return(nil)

for _, tc := range testCases {
response, _ := s.Check(context.Background(), tc.Request)
if response.Status != tc.ExpectedStatus {
t.Fatalf("Case %v failed. ExpectedStatus %v, got %v", tc.Name, tc.ExpectedStatus, response.Status)
}
}
}
26 changes: 24 additions & 2 deletions pkg/db/v1alpha3/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ func TestGetObservationLog(t *testing.T) {
)
obsLog, err := dbInterface.GetObservationLog(
"test1_trial1",
"",
"loss",
"2016-12-31T21:01:05.123456Z",
"",
"2016-12-31T22:10:20.123456Z",
)
if err != nil {
t.Errorf("GetObservationLog failed %v", err)
Expand All @@ -123,3 +123,25 @@ func TestGetObservationLog(t *testing.T) {
}

}

func TestDeleteObservationLog(t *testing.T) {
trialName := "test1_trial1"

mock.ExpectExec(
"DELETE FROM observation_logs",
).WithArgs(trialName).WillReturnResult(sqlmock.NewResult(1, 1))

err := dbInterface.DeleteObservationLog(trialName)
if err != nil {
t.Errorf("DeleteObservationLog failed: %v", err)
}
}

func TestGetDbName(t *testing.T) {
dbName := "root:@tcp(katib-mysql:3306)/katib?timeout=5s"

if getDbName() != dbName {
t.Errorf("getDbName returns wrong value %v", getDbName())
}

}
2 changes: 1 addition & 1 deletion pkg/webhook/v1alpha3/experiment/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (g *DefaultValidator) validateMetricsCollector(inst *experimentsv1alpha3.Ex
continue
}
if _, err := g.GetMetricsCollectorImage(mcKind); err != nil {
return fmt.Errorf("Don't support metrics collector kind %q: %v.", string(mcKind), err)
return fmt.Errorf("GetMetricsCollectorImage failed: %v.", err)
}
break
}
Expand Down
Loading