Merge pull request #80469 from draveness/feature/add-unit-tests-to-framework-status

fix: return empty string when status is nil
This commit is contained in:
Kubernetes Prow Robot 2019-07-24 18:08:05 -07:00 committed by GitHub
commit dc515bb12e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 5 deletions

View File

@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
@ -36,3 +36,9 @@ filegroup(
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = ["interface_test.go"],
embed = [":go_default_library"],
)

View File

@ -71,15 +71,15 @@ func (s *Status) Code() Code {
// Message returns message of the Status.
func (s *Status) Message() string {
if s == nil {
return ""
}
return s.message
}
// IsSuccess returns true if and only if "Status" is nil or Code is "Success".
func (s *Status) IsSuccess() bool {
if s == nil || s.code == Success {
return true
}
return false
return s.Code() == Success
}
// AsError returns an "error" object with the same message as that of the Status.

View File

@ -0,0 +1,76 @@
/*
Copyright 2019 The Kubernetes 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 (
"errors"
"testing"
)
func TestStatus(t *testing.T) {
tests := []struct {
status *Status
expectedCode Code
expectedMessage string
expectedIsSuccess bool
expectedAsError error
}{
{
status: NewStatus(Success, ""),
expectedCode: Success,
expectedMessage: "",
expectedIsSuccess: true,
expectedAsError: nil,
},
{
status: NewStatus(Error, "unknown error"),
expectedCode: Error,
expectedMessage: "unknown error",
expectedIsSuccess: false,
expectedAsError: errors.New("unknown error"),
},
{
status: nil,
expectedCode: Success,
expectedMessage: "",
expectedIsSuccess: true,
expectedAsError: nil,
},
}
for i, test := range tests {
if test.status.Code() != test.expectedCode {
t.Errorf("test #%v, expect status.Code() returns %v, but %v", i, test.expectedCode, test.status.Code())
}
if test.status.Message() != test.expectedMessage {
t.Errorf("test #%v, expect status.Message() returns %v, but %v", i, test.expectedMessage, test.status.Message())
}
if test.status.IsSuccess() != test.expectedIsSuccess {
t.Errorf("test #%v, expect status.IsSuccess() returns %v, but %v", i, test.expectedIsSuccess, test.status.IsSuccess())
}
if test.status.AsError() == test.expectedAsError {
continue
}
if test.status.AsError().Error() != test.expectedAsError.Error() {
t.Errorf("test #%v, expect status.AsError() returns %v, but %v", i, test.expectedAsError, test.status.AsError())
}
}
}