github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/configuration/validate/config_validate_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 validate
    21  
    22  import (
    23  	"errors"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  
    28  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    29  	"github.com/1aal/kubeblocks/test/testdata"
    30  )
    31  
    32  var fromTestData = func(fileName string) string {
    33  	content, err := testdata.GetTestDataFileContent(fileName)
    34  	if err != nil {
    35  		panic(err)
    36  	}
    37  	return string(content)
    38  }
    39  
    40  var newFakeConfConstraint = func(cueFile string, cfgFormatter appsv1alpha1.CfgFileFormat) *appsv1alpha1.ConfigConstraintSpec {
    41  	return &appsv1alpha1.ConfigConstraintSpec{
    42  		ConfigurationSchema: &appsv1alpha1.CustomParametersValidation{
    43  			CUE: fromTestData(cueFile),
    44  		},
    45  		FormatterConfig: &appsv1alpha1.FormatterConfig{
    46  			Format: cfgFormatter,
    47  		},
    48  	}
    49  }
    50  
    51  func TestSchemaValidatorWithCue(t *testing.T) {
    52  	type args struct {
    53  		cueFile    string
    54  		configFile string
    55  		format     appsv1alpha1.CfgFileFormat
    56  		options    []ValidatorOptions
    57  	}
    58  	tests := []struct {
    59  		name string
    60  		args args
    61  		err  error
    62  	}{{
    63  		name: "mongod_test",
    64  		args: args{
    65  			cueFile:    "cue_testdata/mongod.cue",
    66  			configFile: "cue_testdata/mongod.conf",
    67  			format:     appsv1alpha1.YAML,
    68  		},
    69  		err: nil,
    70  	}, {
    71  		name: "test_wesql",
    72  		args: args{
    73  			cueFile:    "cue_testdata/wesql.cue",
    74  			configFile: "cue_testdata/wesql.cnf",
    75  			format:     appsv1alpha1.Ini,
    76  		},
    77  		err: nil,
    78  	}, {
    79  		name: "test_pg14",
    80  		args: args{
    81  			cueFile:    "cue_testdata/pg14.cue",
    82  			configFile: "cue_testdata/pg14.conf",
    83  			format:     appsv1alpha1.Properties,
    84  		},
    85  		err: nil,
    86  	}, {
    87  		name: "test_ck",
    88  		args: args{
    89  			cueFile:    "cue_testdata/clickhouse.cue",
    90  			configFile: "cue_testdata/clickhouse.xml",
    91  			format:     appsv1alpha1.XML,
    92  		},
    93  		err: nil,
    94  	}, {
    95  		name: "test_mysql",
    96  		args: args{
    97  			cueFile:    "cue_testdata/mysql.cue",
    98  			configFile: "cue_testdata/mysql.cnf",
    99  			format:     appsv1alpha1.Ini,
   100  		},
   101  		err: nil,
   102  	}, {
   103  		name: "test_failed",
   104  		args: args{
   105  			cueFile:    "cue_testdata/mysql.cue",
   106  			configFile: "cue_testdata/mysql_err.cnf",
   107  			format:     appsv1alpha1.Ini,
   108  		},
   109  		err: errors.New(`failed to render cue template configure: [mysqld.innodb_autoinc_lock_mode: 3 errors in empty disjunction:
   110  mysqld.innodb_autoinc_lock_mode: conflicting values 0 and 100:
   111      31:35
   112  mysqld.innodb_autoinc_lock_mode: conflicting values 1 and 100:
   113      31:39
   114  mysqld.innodb_autoinc_lock_mode: conflicting values 2 and 100:
   115      31:43]`),
   116  	}, {
   117  		name: "configmap_key_filter",
   118  		args: args{
   119  			cueFile:    "cue_testdata/mysql.cue",
   120  			configFile: "cue_testdata/mysql_err.cnf",
   121  			format:     appsv1alpha1.Ini,
   122  			options:    []ValidatorOptions{WithKeySelector([]string{"key2", "key3"})},
   123  		},
   124  	}}
   125  
   126  	for _, tt := range tests {
   127  		t.Run(tt.name, func(t *testing.T) {
   128  			validator := NewConfigValidator(newFakeConfConstraint(tt.args.cueFile, tt.args.format), tt.args.options...)
   129  			require.NotNil(t, validator)
   130  			require.Equal(t, tt.err, validator.Validate(
   131  				map[string]string{
   132  					"key": fromTestData(tt.args.configFile),
   133  				}))
   134  		})
   135  	}
   136  }
   137  
   138  func TestSchemaValidatorWithSelector(t *testing.T) {
   139  	validator := NewConfigValidator(newFakeConfConstraint("cue_testdata/mysql.cue", appsv1alpha1.Ini))
   140  	require.NotNil(t, validator)
   141  	require.ErrorContains(t, validator.Validate(
   142  		map[string]string{
   143  			"normal_key":   fromTestData("cue_testdata/mysql.cnf"),
   144  			"abnormal_key": fromTestData("cue_testdata/mysql_err.cnf"),
   145  		}), "[mysqld.innodb_autoinc_lock_mode: 3 errors in empty disjunction")
   146  
   147  	validator = NewConfigValidator(newFakeConfConstraint("cue_testdata/mysql.cue", appsv1alpha1.Ini), WithKeySelector([]string{}))
   148  	require.NotNil(t, validator)
   149  	require.ErrorContains(t, validator.Validate(
   150  		map[string]string{
   151  			"normal_key":   fromTestData("cue_testdata/mysql.cnf"),
   152  			"abnormal_key": fromTestData("cue_testdata/mysql_err.cnf"),
   153  		}), "[mysqld.innodb_autoinc_lock_mode: 3 errors in empty disjunction")
   154  
   155  	validator = NewConfigValidator(newFakeConfConstraint("cue_testdata/mysql.cue", appsv1alpha1.Ini), WithKeySelector([]string{"normal_key"}))
   156  	require.NotNil(t, validator)
   157  	require.Nil(t, validator.Validate(
   158  		map[string]string{
   159  			"normal_key":   fromTestData("cue_testdata/mysql.cnf"),
   160  			"abnormal_key": fromTestData("cue_testdata/mysql_err.cnf"),
   161  		}))
   162  }
   163  
   164  func TestSchemaValidatorWithOpenSchema(t *testing.T) {
   165  	type args struct {
   166  		cueFile        string
   167  		configFile     string
   168  		format         appsv1alpha1.CfgFileFormat
   169  		SchemaTypeName string
   170  	}
   171  	tests := []struct {
   172  		name string
   173  		args args
   174  		err  error
   175  	}{{
   176  		name: "test_wesql",
   177  		args: args{
   178  			cueFile:    "cue_testdata/mysql.cue",
   179  			configFile: "cue_testdata/mysql.cnf",
   180  			format:     appsv1alpha1.Ini,
   181  		},
   182  		err: nil,
   183  	}}
   184  
   185  	for _, tt := range tests {
   186  		t.Run(tt.name, func(t *testing.T) {
   187  			tplConstraint := newFakeConfConstraint(tt.args.cueFile, tt.args.format)
   188  			validator := &schemaValidator{
   189  				typeName: tt.args.SchemaTypeName,
   190  				cfgType:  tplConstraint.FormatterConfig.Format,
   191  				schema:   tplConstraint.ConfigurationSchema.Schema,
   192  			}
   193  			require.Equal(t, tt.err, validator.Validate(
   194  				map[string]string{
   195  					"key": fromTestData(tt.args.configFile),
   196  				}))
   197  		})
   198  	}
   199  }