k8s.io/kubernetes@v1.29.3/pkg/volume/util/types/types_test.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package types
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"k8s.io/mount-utils"
    24  )
    25  
    26  func TestErrorTypes(t *testing.T) {
    27  	tests := []struct {
    28  		name           string
    29  		realError      error
    30  		errorCheckFunc func(error) bool
    31  		expectError    bool
    32  	}{
    33  		{
    34  			"when mount error has File system mismatch errors",
    35  			mount.NewMountError(mount.FilesystemMismatch, "filesystem mismatch"),
    36  			IsFilesystemMismatchError,
    37  			true,
    38  		},
    39  		{
    40  			"when mount error has other error",
    41  			mount.NewMountError(mount.FormatFailed, "filesystem mismatch"),
    42  			IsFilesystemMismatchError,
    43  			false,
    44  		},
    45  		{
    46  			"when mount error wraps filesystem mismatch error",
    47  			fmt.Errorf("mount failed %w", mount.NewMountError(mount.FilesystemMismatch, "filesystem mismatch")),
    48  			IsFilesystemMismatchError,
    49  			true,
    50  		},
    51  		{
    52  			"when error has no failedPrecondition error",
    53  			fmt.Errorf("some other error"),
    54  			IsFailedPreconditionError,
    55  			false,
    56  		},
    57  		{
    58  			"when error has failedPrecondition error",
    59  			NewFailedPreconditionError("volume-in-use"),
    60  			IsFailedPreconditionError,
    61  			true,
    62  		},
    63  		{
    64  			"when error wraps failedPrecondition error",
    65  			fmt.Errorf("volume readonly %w", NewFailedPreconditionError("volume-in-use-error")),
    66  			IsFailedPreconditionError,
    67  			true,
    68  		},
    69  	}
    70  
    71  	for _, test := range tests {
    72  		ok := test.errorCheckFunc(test.realError)
    73  		if ok != test.expectError {
    74  			t.Errorf("for %s: expected error to be %v but got %v", test.name, test.expectError, ok)
    75  		}
    76  	}
    77  }