github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/configuration/core/config_patch_option_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 core
    21  
    22  import "testing"
    23  
    24  type testType struct {
    25  	name string
    26  }
    27  
    28  func TestTypeMatch(t *testing.T) {
    29  	type args struct {
    30  		expected interface{}
    31  		values   []interface{}
    32  	}
    33  	tests := []struct {
    34  		name string
    35  		args args
    36  		want bool
    37  	}{{
    38  		"string_type_test",
    39  		args{
    40  			expected: "",
    41  			values:   []interface{}{"", "xxxx"},
    42  		},
    43  		true,
    44  	}, {
    45  		"byte_type_test_failed",
    46  		args{
    47  			expected: []byte{},
    48  			values:   []interface{}{[]byte("abcd")},
    49  		},
    50  		true,
    51  	}, {
    52  		"byte_type_test_failed_without_match",
    53  		args{
    54  			expected: []byte{},
    55  			values:   []interface{}{"abcd"},
    56  		},
    57  		false,
    58  	}, {
    59  		"byte_type_test_failed_with_null",
    60  		args{
    61  			expected: []byte{},
    62  			values:   []interface{}{nil},
    63  		},
    64  		false,
    65  	}, {
    66  		"byte_type_test_failed_with_null",
    67  		args{
    68  			expected: nil,
    69  			values:   []interface{}{nil},
    70  		},
    71  		false,
    72  	}, {
    73  		"byte_type_test_failed_with_null2",
    74  		args{
    75  			expected: nil,
    76  			values:   []interface{}{[]byte("abcd")},
    77  		},
    78  		false,
    79  	}, {
    80  		"custom_type",
    81  		args{
    82  			expected: testType{},
    83  			values:   []interface{}{testType{name: "abcd"}},
    84  		},
    85  		true,
    86  	}, {
    87  		"custom_type2",
    88  		args{
    89  			expected: &testType{},
    90  			values:   []interface{}{testType{name: "abcd"}},
    91  		},
    92  		false,
    93  	}, {
    94  		"custom_type_with_pointer",
    95  		args{
    96  			expected: &testType{},
    97  			values:   []interface{}{&testType{name: "abcd"}},
    98  		},
    99  		true,
   100  	}}
   101  	for _, tt := range tests {
   102  		t.Run(tt.name, func(t *testing.T) {
   103  			if got := typeMatch(tt.args.expected, tt.args.values...); got != tt.want {
   104  				t.Errorf("typeMatch() = %v, want %v", got, tt.want)
   105  			}
   106  		})
   107  	}
   108  }
   109  
   110  func TestCompareWithConfig(t *testing.T) {
   111  	type args struct {
   112  		left   interface{}
   113  		right  interface{}
   114  		option CfgOption
   115  	}
   116  	tests := []struct {
   117  		name    string
   118  		args    args
   119  		want    bool
   120  		wantErr bool
   121  	}{{
   122  		name: "raw_type_test",
   123  		args: args{
   124  			left:   []byte("byte"),
   125  			right:  "string",
   126  			option: CfgOption{Type: CfgRawType},
   127  		},
   128  		wantErr: true,
   129  	}, {
   130  		name: "raw_type_test",
   131  		args: args{
   132  			left:   []byte("byte"),
   133  			right:  []byte("test"),
   134  			option: CfgOption{Type: CfgRawType},
   135  		},
   136  		want: false,
   137  	}, {
   138  		name: "raw_type_test",
   139  		args: args{
   140  			left:   []byte("byte"),
   141  			right:  []byte("byte"),
   142  			option: CfgOption{Type: CfgRawType},
   143  		},
   144  		want: true,
   145  	}, {
   146  		name: "localfile_type_test",
   147  		args: args{
   148  			left:   []byte("byte"),
   149  			right:  "string",
   150  			option: CfgOption{Type: CfgLocalType},
   151  		},
   152  		wantErr: true,
   153  	}, {
   154  		name: "localfile_type_test",
   155  		args: args{
   156  			left:   "byte",
   157  			right:  "string",
   158  			option: CfgOption{Type: CfgLocalType},
   159  		},
   160  		want: false,
   161  	}, {
   162  		name: "tpl_type_test",
   163  		args: args{
   164  			left:   &ConfigResource{},
   165  			right:  "string",
   166  			option: CfgOption{Type: CfgCmType},
   167  		},
   168  		wantErr: true,
   169  	}, {
   170  		name: "tpl_type_test",
   171  		args: args{
   172  			option: CfgOption{Type: "not_support"},
   173  		},
   174  		wantErr: true,
   175  	}}
   176  	for _, tt := range tests {
   177  		t.Run(tt.name, func(t *testing.T) {
   178  			got, err := compareWithConfig(tt.args.left, tt.args.right, tt.args.option)
   179  			if (err != nil) != tt.wantErr {
   180  				t.Errorf("compareWithConfig() error = %v, wantErr %v", err, tt.wantErr)
   181  				return
   182  			}
   183  			if got != tt.want {
   184  				t.Errorf("compareWithConfig() got = %v, want %v", got, tt.want)
   185  			}
   186  		})
   187  	}
   188  }