本文整理汇总了Golang中k8s/io/kubernetes/pkg/api/errors.IsInvalid函数的典型用法代码示例。如果您正苦于以下问题:Golang IsInvalid函数的具体用法?Golang IsInvalid怎么用?Golang IsInvalid使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了IsInvalid函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: testCreateValidatesNames
func (t *Tester) testCreateValidatesNames(valid runtime.Object) {
for _, invalidName := range validation.NameMayNotBe {
objCopy := copyOrDie(valid)
objCopyMeta := t.getObjectMetaOrFail(objCopy)
objCopyMeta.Name = invalidName
ctx := t.TestContext()
_, err := t.storage.(rest.Creater).Create(ctx, objCopy)
if !errors.IsInvalid(err) {
t.Errorf("%s: Expected to get an invalid resource error, got %v", invalidName, err)
}
}
for _, invalidSuffix := range validation.NameMayNotContain {
objCopy := copyOrDie(valid)
objCopyMeta := t.getObjectMetaOrFail(objCopy)
objCopyMeta.Name += invalidSuffix
ctx := t.TestContext()
_, err := t.storage.(rest.Creater).Create(ctx, objCopy)
if !errors.IsInvalid(err) {
t.Errorf("%s: Expected to get an invalid resource error, got %v", invalidSuffix, err)
}
}
}
示例2: TestEtcdCreateBinding
func TestEtcdCreateBinding(t *testing.T) {
ctx := api.NewDefaultContext()
testCases := map[string]struct {
binding api.Binding
errOK func(error) bool
}{
"noName": {
binding: api.Binding{
ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"},
Target: api.ObjectReference{},
},
errOK: func(err error) bool { return errors.IsInvalid(err) },
},
"badKind": {
binding: api.Binding{
ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"},
Target: api.ObjectReference{Name: "machine1", Kind: "unknown"},
},
errOK: func(err error) bool { return errors.IsInvalid(err) },
},
"emptyKind": {
binding: api.Binding{
ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"},
Target: api.ObjectReference{Name: "machine2"},
},
errOK: func(err error) bool { return err == nil },
},
"kindNode": {
binding: api.Binding{
ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"},
Target: api.ObjectReference{Name: "machine3", Kind: "Node"},
},
errOK: func(err error) bool { return err == nil },
},
}
for k, test := range testCases {
storage, bindingStorage, _, server := newStorage(t)
key, _ := storage.KeyFunc(ctx, "foo")
key = etcdtest.AddPrefix(key)
if _, err := storage.Create(ctx, validNewPod()); err != nil {
t.Fatalf("%s: unexpected error: %v", k, err)
}
if _, err := bindingStorage.Create(ctx, &test.binding); !test.errOK(err) {
t.Errorf("%s: unexpected error: %v", k, err)
} else if err == nil {
// If bind succeeded, verify Host field in pod's Spec.
pod, err := storage.Get(ctx, validNewPod().ObjectMeta.Name)
if err != nil {
t.Errorf("%s: unexpected error: %v", k, err)
} else if pod.(*api.Pod).Spec.NodeName != test.binding.Target.Name {
t.Errorf("%s: expected: %v, got: %v", k, pod.(*api.Pod).Spec.NodeName, test.binding.Target.Name)
}
}
server.Terminate(t)
}
}
示例3: handlePodUpdateError
// handlePodUpdateError prints a more useful error to the end user when mutating a pod.
func handlePodUpdateError(out io.Writer, err error, resource string) {
if statusError, ok := err.(*errors.StatusError); ok && errors.IsInvalid(err) {
errorDetails := statusError.Status().Details
if errorDetails.Kind == "Pod" {
all, match := true, false
for _, cause := range errorDetails.Causes {
if cause.Field == "spec" && strings.Contains(cause.Message, "may not update fields other than") {
fmt.Fprintf(out, "error: may not update %s in pod %q directly\n", resource, errorDetails.Name)
match = true
} else {
all = false
}
}
if all && match {
return
}
} else {
if ok := kcmdutil.PrintErrorWithCauses(err, out); ok {
return
}
}
}
fmt.Fprintf(out, "error: %v\n", err)
}
示例4: checkCustomErr
func checkCustomErr(customPrefix string, err error, handleErr func(string)) {
if err == nil {
return
}
if errors.IsInvalid(err) {
details := err.(*errors.StatusError).Status().Details
for i := range details.Causes {
c := &details.Causes[i]
s := strings.Split(c.Message, "}': ")
if len(s) == 2 {
c.Message =
s[1]
c.Field = ""
}
}
prefix := fmt.Sprintf("%s", customPrefix)
errs := statusCausesToAggrError(details.Causes)
handleErr(MultilineError(prefix, errs))
}
// handle multiline errors
if clientcmd.IsConfigurationInvalid(err) {
handleErr(MultilineError("Error in configuration: ", err))
}
if agg, ok := err.(utilerrors.Aggregate); ok && len(agg.Errors()) > 0 {
handleErr(MultipleErrors("", agg.Errors()))
}
msg, ok := StandardErrorMessage(err)
if !ok {
msg = fmt.Sprintf("error: %s\n", err.Error())
}
handleErr(msg)
}
示例5: TestEtcdControllerValidatesUpdate
func TestEtcdControllerValidatesUpdate(t *testing.T) {
ctx := api.NewDefaultContext()
storage, _ := newStorage(t)
updateController, err := createController(storage, validController, t)
if err != nil {
t.Errorf("Failed to create controller, cannot proceed with test.")
}
updaters := []func(rc api.ReplicationController) (runtime.Object, bool, error){
func(rc api.ReplicationController) (runtime.Object, bool, error) {
rc.UID = "newUID"
return storage.Update(ctx, &rc)
},
func(rc api.ReplicationController) (runtime.Object, bool, error) {
rc.Name = ""
return storage.Update(ctx, &rc)
},
func(rc api.ReplicationController) (runtime.Object, bool, error) {
rc.Spec.Selector = map[string]string{}
return storage.Update(ctx, &rc)
},
}
for _, u := range updaters {
c, updated, err := u(updateController)
if c != nil || updated {
t.Errorf("Expected nil object and not created")
}
if !errors.IsInvalid(err) && !errors.IsBadRequest(err) {
t.Errorf("Expected invalid or bad request error, got %v of type %T", err, err)
}
}
}
示例6: NewCommandStartEtcdServer
// NewCommandStartEtcdServer starts only the etcd server
func NewCommandStartEtcdServer(name, basename string, out io.Writer) (*cobra.Command, *EtcdOptions) {
options := &EtcdOptions{Output: out}
cmd := &cobra.Command{
Use: name,
Short: "Launch etcd server",
Long: fmt.Sprintf(etcdLong, basename, name),
Run: func(c *cobra.Command, args []string) {
kcmdutil.CheckErr(options.Validate())
startProfiler()
if err := options.StartEtcdServer(); err != nil {
if kerrors.IsInvalid(err) {
if details := err.(*kerrors.StatusError).ErrStatus.Details; details != nil {
fmt.Fprintf(c.OutOrStderr(), "Invalid %s %s\n", details.Kind, details.Name)
for _, cause := range details.Causes {
fmt.Fprintf(c.OutOrStderr(), " %s: %s\n", cause.Field, cause.Message)
}
os.Exit(255)
}
}
glog.Fatal(err)
}
},
}
flags := cmd.Flags()
// This command only supports reading from config
flags.StringVar(&options.ConfigFile, "config", "", "Location of the master configuration file to run from.")
cmd.MarkFlagFilename("config", "yaml", "yml")
cmd.MarkFlagRequired("config")
return cmd, options
}
示例7: TestUpdateWithEmptyResourceVersion
func TestUpdateWithEmptyResourceVersion(t *testing.T) {
// Starting conditions
associatedUser1, associatedIdentity1User1 := makeAssociated()
unassociatedUser2 := makeUser()
// Finishing conditions
_, unassociatedIdentity1 := disassociate(associatedUser1, associatedIdentity1User1)
expectedActions := []test.Action{
// Existing mapping lookup
{"GetIdentity", associatedIdentity1User1.Name},
{"GetUser", associatedUser1.Name},
}
mapping := &api.UserIdentityMapping{
Identity: kapi.ObjectReference{Name: unassociatedIdentity1.Name},
User: kapi.ObjectReference{Name: unassociatedUser2.Name},
}
actions, _, _, rest := setupRegistries(associatedIdentity1User1, associatedUser1, unassociatedUser2)
_, _, err := rest.Update(kapi.NewContext(), mapping.Name, kapirest.DefaultUpdatedObjectInfo(mapping, kapi.Scheme))
if err == nil {
t.Errorf("Expected error")
}
if !kerrs.IsInvalid(err) {
t.Errorf("Unexpected error: %v", err)
}
verifyActions(expectedActions, *actions, t)
}
示例8: checkErr
func checkErr(err error, handleErr func(string)) {
if err == nil {
return
}
if errors.IsInvalid(err) {
details := err.(*errors.StatusError).Status().Details
prefix := fmt.Sprintf("The %s %q is invalid:", details.Kind, details.Name)
errs := statusCausesToAggrError(details.Causes)
handleErr(MultilineError(prefix, errs))
}
// handle multiline errors
if clientcmd.IsConfigurationInvalid(err) {
handleErr(MultilineError("Error in configuration: ", err))
}
if agg, ok := err.(utilerrors.Aggregate); ok && len(agg.Errors()) > 0 {
handleErr(MultipleErrors("", agg.Errors()))
}
msg, ok := StandardErrorMessage(err)
if !ok {
msg = fmt.Sprintf("error: %s\n", err.Error())
}
handleErr(msg)
}
示例9: TestEtcdParseWatchResourceVersion
func TestEtcdParseWatchResourceVersion(t *testing.T) {
testCases := []struct {
Version string
Kind string
ExpectVersion uint64
Err bool
}{
{Version: "", ExpectVersion: 0},
{Version: "a", Err: true},
{Version: " ", Err: true},
{Version: "1", ExpectVersion: 2},
{Version: "10", ExpectVersion: 11},
}
for _, testCase := range testCases {
version, err := ParseWatchResourceVersion(testCase.Version, testCase.Kind)
switch {
case testCase.Err:
if err == nil {
t.Errorf("%s: unexpected non-error", testCase.Version)
continue
}
if !errors.IsInvalid(err) {
t.Errorf("%s: unexpected error: %v", testCase.Version, err)
continue
}
case !testCase.Err && err != nil:
t.Errorf("%s: unexpected error: %v", testCase.Version, err)
continue
}
if version != testCase.ExpectVersion {
t.Errorf("%s: expected version %d but was %d", testCase.Version, testCase.ExpectVersion, version)
}
}
}
示例10: TestUpdateMissingUser
func TestUpdateMissingUser(t *testing.T) {
// Starting conditions
associatedUser1, associatedIdentity1User1 := makeAssociated()
unassociatedUser2 := makeUser()
// Finishing conditions
_, unassociatedIdentity1 := disassociate(associatedUser1, associatedIdentity1User1)
expectedActions := []test.Action{
// Existing mapping lookup
{"GetIdentity", associatedIdentity1User1.Name},
{"GetUser", associatedUser1.Name},
// New user lookup
{"GetUser", unassociatedUser2.Name},
}
mapping := &api.UserIdentityMapping{
ObjectMeta: kapi.ObjectMeta{ResourceVersion: unassociatedIdentity1.ResourceVersion},
Identity: kapi.ObjectReference{Name: unassociatedIdentity1.Name},
User: kapi.ObjectReference{Name: unassociatedUser2.Name},
}
actions, _, _, rest := setupRegistries(associatedIdentity1User1, associatedUser1)
_, _, err := rest.Update(kapi.NewContext(), mapping)
if err == nil {
t.Errorf("Expected error: %v", err)
}
if !kerrs.IsInvalid(err) {
t.Errorf("Unexpected error: %v", err)
}
verifyActions(expectedActions, *actions, t)
}
示例11: testCreateInvokesValidation
func (t *Tester) testCreateInvokesValidation(invalid ...runtime.Object) {
for i, obj := range invalid {
ctx := t.TestContext()
_, err := t.storage.(rest.Creater).Create(ctx, obj)
if !errors.IsInvalid(err) {
t.Errorf("%d: Expected to get an invalid resource error, got %v", i, err)
}
}
}
示例12: IsErrorLimitExceeded
// IsErrorLimitExceeded returns true if the given error is a limit error.
func IsErrorLimitExceeded(err error) bool {
if isForbidden := apierrs.IsForbidden(err); isForbidden || apierrs.IsInvalid(err) {
lowered := strings.ToLower(err.Error())
// the limit error message can be accompanied only by Invalid reason
if strings.Contains(lowered, errLimitsMessageString) {
return true
}
}
return false
}
示例13: isInvalidTriggerError
// isInvalidTriggerError returns true if the given error is
// a validation error that contains 'invalid trigger type' in its
// error message. This error is returned from older servers that
// consider the presence of unknown trigger types to be an error.
func isInvalidTriggerError(err error) bool {
if !kapierrors.IsInvalid(err) {
return false
}
statusErr, ok := err.(*kapierrors.StatusError)
if !ok {
return false
}
return strings.Contains(statusErr.Status().Message, "invalid trigger type")
}
示例14: TestDeleteInvokesValidation
func (t *Tester) TestDeleteInvokesValidation(invalid ...runtime.Object) {
for i, obj := range invalid {
objectMeta := t.getObjectMetaOrFail(obj)
ctx := t.TestContext()
_, err := t.storage.(rest.GracefulDeleter).Delete(ctx, objectMeta.Name, nil)
if !errors.IsInvalid(err) {
t.Errorf("%d: Expected to get an invalid resource error, got %v", i, err)
}
}
}
示例15: TestCreateInvalidProject
func TestCreateInvalidProject(t *testing.T) {
mockClient := &testclient.Fake{}
storage := NewREST(mockClient.Namespaces(), &mockLister{})
_, err := storage.Create(nil, &api.Project{
ObjectMeta: kapi.ObjectMeta{
Annotations: map[string]string{"openshift.io/display-name": "h\t\ni"},
},
})
if !errors.IsInvalid(err) {
t.Errorf("Expected 'invalid' error, got %v", err)
}
}