github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/unstructured/xml_config_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 unstructured
    21  
    22  import (
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  
    27  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    28  )
    29  
    30  func TestXMLFormat(t *testing.T) {
    31  	const xmlContext = `
    32  <!-- Settings profiles -->
    33  <profiles>
    34      <!-- Default settings -->
    35      <default>
    36          <!-- The maximum number of threads when running a single query. -->
    37          <max_threads>8</max_threads>
    38      </default>
    39  
    40      <!-- Settings for quries from the user interface -->
    41      <web>
    42          <max_execution_time>600</max_execution_time>
    43          <min_execution_speed>1000000</min_execution_speed>
    44          <timeout_before_checking_execution_speed>15</timeout_before_checking_execution_speed>
    45  
    46          <readonly>1</readonly>
    47      </web>
    48  </profiles>
    49  `
    50  	xmlConfigObj, err := LoadConfig("xml_test", xmlContext, appsv1alpha1.XML)
    51  	assert.Nil(t, err)
    52  
    53  	assert.EqualValues(t, xmlConfigObj.Get("profiles.default.max_threads"), 8)
    54  	assert.EqualValues(t, xmlConfigObj.Get("profiles.web.min_execution_speed"), 1000000)
    55  	assert.EqualValues(t, xmlConfigObj.Get("profiles.web.max_threads"), nil)
    56  
    57  	v, _ := xmlConfigObj.GetString("profiles.default.max_threads")
    58  	assert.EqualValues(t, v, "8")
    59  	v, _ = xmlConfigObj.GetString("profiles.web.min_execution_speed")
    60  	assert.EqualValues(t, v, "1000000")
    61  
    62  	_, err = xmlConfigObj.GetString("profiles.web.xxxx")
    63  	assert.Nil(t, err)
    64  
    65  	dumpContext, err := xmlConfigObj.Marshal()
    66  	assert.Nil(t, err)
    67  	newObj, err := LoadConfig("xml_test", dumpContext, appsv1alpha1.XML)
    68  	assert.Nil(t, err)
    69  	assert.EqualValues(t, newObj.GetAllParameters(), xmlConfigObj.GetAllParameters())
    70  
    71  	assert.Nil(t, xmlConfigObj.Update("profiles.web.timeout_before_checking_execution_speed", 200))
    72  	assert.EqualValues(t, xmlConfigObj.Get("profiles.web.timeout_before_checking_execution_speed"), 200)
    73  
    74  	assert.Nil(t, xmlConfigObj.RemoveKey("profiles.web.timeout_before_checking_execution_speed.sksds"))
    75  	assert.Nil(t, xmlConfigObj.RemoveKey("profiles.web.timeout_before_checking_execution_speed"))
    76  	assert.EqualValues(t, xmlConfigObj.Get("profiles.web.timeout_before_checking_execution_speed"), nil)
    77  }