Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions pkg/cluster/manager/edit_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,28 @@ func (m *Manager) editTopo(origTopo spec.Topology, data []byte, opt EditConfigOp
decoder.KnownFields(true)
err = decoder.Decode(newTopo)
if err != nil {
// A provided file cannot be fixed interactively, so return the error.
if opt.NewTopoFile != "" {
return nil, spec.ErrTopologyParseFailed.Wrap(err, "Failed to parse topology file %s", name)
}
fmt.Print(color.RedString("New topology could not be saved: "))
m.logger.Infof("Failed to parse topology file: %v", err)
if opt.NewTopoFile == "" {
if pass, _ := tui.PromptForConfirmNo("Do you want to continue editing? [Y/n]: "); !pass {
return m.editTopo(origTopo, newData, opt, skipConfirm)
}
if pass, _ := tui.PromptForConfirmNo("Do you want to continue editing? [Y/n]: "); !pass {
return m.editTopo(origTopo, newData, opt, skipConfirm)
}
m.logger.Infof("Nothing changed.")
return nil, nil
}

// report error if immutable field has been changed
if err := utils.ValidateSpecDiff(origTopo, newTopo); err != nil {
if opt.NewTopoFile != "" {
return nil, perrs.Annotate(err, "new topology could not be saved")
}
fmt.Print(color.RedString("New topology could not be saved: "))
m.logger.Errorf("%s", err)
if opt.NewTopoFile == "" {
if pass, _ := tui.PromptForConfirmNo("Do you want to continue editing? [Y/n]: "); !pass {
return m.editTopo(origTopo, newData, opt, skipConfirm)
}
if pass, _ := tui.PromptForConfirmNo("Do you want to continue editing? [Y/n]: "); !pass {
return m.editTopo(origTopo, newData, opt, skipConfirm)
}
m.logger.Infof("Nothing changed.")
return nil, nil
Expand Down
104 changes: 104 additions & 0 deletions pkg/cluster/manager/edit_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright 2026 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package manager

import (
"os"
"path/filepath"
"testing"

"github.com/joomcode/errorx"
"github.com/pingcap/tiup/pkg/cluster/spec"
logprinter "github.com/pingcap/tiup/pkg/logger/printer"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)

// TestEditConfigTopologyFile checks that edit-config with --topology-file
// returns an error when the provided topology is not applied.
func TestEditConfigTopologyFile(t *testing.T) {
const name = "test-cluster"

t.Setenv("TIUP_COMPONENT_DATA_DIR", t.TempDir())
require.NoError(t, spec.Initialize("cluster"))
m := NewManager("tidb", spec.GetSpecManager(), logprinter.NewLogger(""))

topo := &spec.Specification{}
require.NoError(t, yaml.Unmarshal([]byte(`
global:
user: tidb
pd_servers:
- host: 10.0.0.1
tidb_servers:
- host: 10.0.0.1
tikv_servers:
- host: 10.0.0.1
`), topo))
spec.ExpandRelativeDir(topo)
require.NoError(t, m.specManager.SaveMeta(name, &spec.ClusterMeta{User: "tidb", Version: "v8.5.6", Topology: topo}))

metaFile := m.specManager.Path(name, "meta.yaml")
readMeta := func() string {
data, err := os.ReadFile(metaFile)
require.NoError(t, err)
return string(data)
}
// render returns the saved topology as a topology file after an edit.
render := func(edit func(*spec.Specification)) []byte {
saved, err := m.meta(name)
require.NoError(t, err)
current := saved.GetTopology().(*spec.Specification)
edit(current)
data, err := yaml.Marshal(current)
require.NoError(t, err)
return data
}
editWith := func(data []byte) error {
file := filepath.Join(t.TempDir(), "topology.yaml")
require.NoError(t, os.WriteFile(file, data, 0o644))
return m.EditConfig(name, EditConfigOptions{NewTopoFile: file}, true)
}

unchanged := render(func(*spec.Specification) {})
original := readMeta()

t.Run("immutable field", func(t *testing.T) {
err := editWith(render(func(s *spec.Specification) { s.PDServers[0].Host = "10.0.0.2" }))
require.ErrorContains(t, err, "new topology could not be saved")
require.ErrorContains(t, err, "immutable field changed")
require.Equal(t, original, readMeta())
})

t.Run("unknown field", func(t *testing.T) {
err := editWith(append([]byte("no_such_section: 1\n"), unchanged...))
require.True(t, errorx.IsOfType(err, spec.ErrTopologyParseFailed))
require.ErrorContains(t, err, "no_such_section")
require.Equal(t, original, readMeta())
})

t.Run("nothing changed", func(t *testing.T) {
require.NoError(t, editWith(unchanged))
require.Equal(t, original, readMeta())
})

t.Run("editable field", func(t *testing.T) {
require.NoError(t, editWith(render(func(s *spec.Specification) {
s.ServerConfigs.TiDB = map[string]any{"log.level": "warn"}
})))
saved, err := m.meta(name)
require.NoError(t, err)
require.Equal(t, map[string]any{"log.level": "warn"}, saved.GetTopology().(*spec.Specification).ServerConfigs.TiDB)
})
}
Loading