github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/dataprotection/errors/errors_test.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package errors
    21  
    22  import (
    23  	"fmt"
    24  	"reflect"
    25  	"testing"
    26  
    27  	"github.com/pkg/errors"
    28  
    29  	intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil"
    30  )
    31  
    32  func TestNerError(t *testing.T) {
    33  	err1 := intctrlutil.NewError(ErrorTypeBackupNotCompleted, "test c2")
    34  	if err1.Error() != "test c2" {
    35  		t.Error("NewErrorf failed")
    36  	}
    37  }
    38  
    39  func TestNerErrorf(t *testing.T) {
    40  	err1 := intctrlutil.NewErrorf(ErrorTypeBackupNotCompleted, "test %s %s", "c1", "c2")
    41  	if err1.Error() != "test c1 c2" {
    42  		t.Error("NewErrorf failed")
    43  	}
    44  	testError := fmt.Errorf("test: %w", err1)
    45  	if !errors.Is(testError, err1) {
    46  		t.Error("errors.Is failed")
    47  	}
    48  
    49  	var target *intctrlutil.Error
    50  	if !errors.As(testError, &target) {
    51  		t.Error("errors.As failed")
    52  	}
    53  }
    54  
    55  func TestNewErrors(t *testing.T) {
    56  	backupNotSupported := NewBackupNotSupported("datafile", "policy-test")
    57  	if !intctrlutil.IsTargetError(backupNotSupported, ErrorTypeBackupNotSupported) {
    58  		t.Error("should be error of BackupNotSupported")
    59  	}
    60  	pvTemplateNotFound := NewBackupPVTemplateNotFound("configName", "default")
    61  	if !intctrlutil.IsTargetError(pvTemplateNotFound, ErrorTypeBackupPVTemplateNotFound) {
    62  		t.Error("should be error of BackupPVTemplateNotFound")
    63  	}
    64  	pvcIsEmpty := NewBackupPVCNameIsEmpty("datafile", "policy-test1")
    65  	if !intctrlutil.IsTargetError(pvcIsEmpty, ErrorTypeBackupPVCNameIsEmpty) {
    66  		t.Error("should be error of BackupPVCNameIsEmpty")
    67  	}
    68  	repoIsNotReady := NewBackupRepoIsNotReady("repo")
    69  	if !intctrlutil.IsTargetError(repoIsNotReady, ErrorTypeBackupRepoIsNotReady) {
    70  		t.Error("should be error of BackupRepoIsNotReady")
    71  	}
    72  	toolConfigSecretNameIsEmpty := NewToolConfigSecretNameIsEmpty("repo")
    73  	if !intctrlutil.IsTargetError(toolConfigSecretNameIsEmpty, ErrorTypeToolConfigSecretNameIsEmpty) {
    74  		t.Error("should be error of ToolConfigSecretNameIsEmpty")
    75  	}
    76  	jobFailed := NewBackupJobFailed("jobName")
    77  	if !intctrlutil.IsTargetError(jobFailed, ErrorTypeBackupJobFailed) {
    78  		t.Error("should be error of BackupJobFailed")
    79  	}
    80  }
    81  
    82  func TestUnwrapControllerError(t *testing.T) {
    83  	backupNotSupported := NewBackupNotSupported("datafile", "policy-test")
    84  	newErr := intctrlutil.UnwrapControllerError(backupNotSupported)
    85  	if newErr == nil {
    86  		t.Error("should unwrap a controller error, but got nil")
    87  	}
    88  	err := errors.New("test error")
    89  	newErr = intctrlutil.UnwrapControllerError(err)
    90  	if newErr != nil {
    91  		t.Errorf("should not unwrap a controller error, but got: %v", newErr)
    92  	}
    93  }
    94  
    95  func TestNewBackupJobFailed(t *testing.T) {
    96  	type args struct {
    97  		jobName string
    98  	}
    99  	tests := []struct {
   100  		name string
   101  		args args
   102  		want *intctrlutil.Error
   103  	}{
   104  		// TODO: Add test cases.
   105  	}
   106  	for _, tt := range tests {
   107  		t.Run(tt.name, func(t *testing.T) {
   108  			if got := NewBackupJobFailed(tt.args.jobName); !reflect.DeepEqual(got, tt.want) {
   109  				t.Errorf("NewBackupJobFailed() = %v, want %v", got, tt.want)
   110  			}
   111  		})
   112  	}
   113  }
   114  
   115  func TestNewBackupLogfileScheduleDisabled(t *testing.T) {
   116  	type args struct {
   117  		backupToolName string
   118  	}
   119  	tests := []struct {
   120  		name string
   121  		args args
   122  		want *intctrlutil.Error
   123  	}{
   124  		// TODO: Add test cases.
   125  	}
   126  	for _, tt := range tests {
   127  		t.Run(tt.name, func(t *testing.T) {
   128  			if got := NewBackupLogfileScheduleDisabled(tt.args.backupToolName); !reflect.DeepEqual(got, tt.want) {
   129  				t.Errorf("NewBackupLogfileScheduleDisabled() = %v, want %v", got, tt.want)
   130  			}
   131  		})
   132  	}
   133  }
   134  
   135  func TestNewBackupNotSupported(t *testing.T) {
   136  	type args struct {
   137  		backupType       string
   138  		backupPolicyName string
   139  	}
   140  	tests := []struct {
   141  		name string
   142  		args args
   143  		want *intctrlutil.Error
   144  	}{
   145  		// TODO: Add test cases.
   146  	}
   147  	for _, tt := range tests {
   148  		t.Run(tt.name, func(t *testing.T) {
   149  			if got := NewBackupNotSupported(tt.args.backupType, tt.args.backupPolicyName); !reflect.DeepEqual(got, tt.want) {
   150  				t.Errorf("NewBackupNotSupported() = %v, want %v", got, tt.want)
   151  			}
   152  		})
   153  	}
   154  }
   155  
   156  func TestNewBackupPVCNameIsEmpty(t *testing.T) {
   157  	type args struct {
   158  		backupRepo       string
   159  		backupPolicyName string
   160  	}
   161  	tests := []struct {
   162  		name string
   163  		args args
   164  		want *intctrlutil.Error
   165  	}{
   166  		// TODO: Add test cases.
   167  	}
   168  	for _, tt := range tests {
   169  		t.Run(tt.name, func(t *testing.T) {
   170  			if got := NewBackupPVCNameIsEmpty(tt.args.backupRepo, tt.args.backupPolicyName); !reflect.DeepEqual(got, tt.want) {
   171  				t.Errorf("NewBackupPVCNameIsEmpty() = %v, want %v", got, tt.want)
   172  			}
   173  		})
   174  	}
   175  }
   176  
   177  func TestNewBackupPVTemplateNotFound(t *testing.T) {
   178  	type args struct {
   179  		cmName      string
   180  		cmNamespace string
   181  	}
   182  	tests := []struct {
   183  		name string
   184  		args args
   185  		want *intctrlutil.Error
   186  	}{
   187  		// TODO: Add test cases.
   188  	}
   189  	for _, tt := range tests {
   190  		t.Run(tt.name, func(t *testing.T) {
   191  			if got := NewBackupPVTemplateNotFound(tt.args.cmName, tt.args.cmNamespace); !reflect.DeepEqual(got, tt.want) {
   192  				t.Errorf("NewBackupPVTemplateNotFound() = %v, want %v", got, tt.want)
   193  			}
   194  		})
   195  	}
   196  }
   197  
   198  func TestNewBackupScheduleDisabled(t *testing.T) {
   199  	type args struct {
   200  		backupType       string
   201  		backupPolicyName string
   202  	}
   203  	tests := []struct {
   204  		name string
   205  		args args
   206  		want *intctrlutil.Error
   207  	}{
   208  		// TODO: Add test cases.
   209  	}
   210  	for _, tt := range tests {
   211  		t.Run(tt.name, func(t *testing.T) {
   212  			if got := NewBackupScheduleDisabled(tt.args.backupType, tt.args.backupPolicyName); !reflect.DeepEqual(got, tt.want) {
   213  				t.Errorf("NewBackupScheduleDisabled() = %v, want %v", got, tt.want)
   214  			}
   215  		})
   216  	}
   217  }
   218  
   219  func TestNewInvalidLogfileBackupName(t *testing.T) {
   220  	type args struct {
   221  		backupPolicyName string
   222  	}
   223  	tests := []struct {
   224  		name string
   225  		args args
   226  		want *intctrlutil.Error
   227  	}{
   228  		// TODO: Add test cases.
   229  	}
   230  	for _, tt := range tests {
   231  		t.Run(tt.name, func(t *testing.T) {
   232  			if got := NewInvalidLogfileBackupName(tt.args.backupPolicyName); !reflect.DeepEqual(got, tt.want) {
   233  				t.Errorf("NewInvalidLogfileBackupName() = %v, want %v", got, tt.want)
   234  			}
   235  		})
   236  	}
   237  }