Skip to content

Commit

Permalink
Fix push with template (pingcap#1049)
Browse files Browse the repository at this point in the history
* Fix push with template

* Reflect

* Fix test
  • Loading branch information
lucklove authored Jan 8, 2021
1 parent 65f061d commit df76e55
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
19 changes: 17 additions & 2 deletions pkg/cluster/manager/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"bytes"
"fmt"
"html/template"
"reflect"
"strings"

"github.com/google/uuid"
Expand Down Expand Up @@ -116,7 +117,7 @@ func renderInstanceSpec(t string, inst spec.Instance) ([]string, error) {
tf := inst
tfs, ok := tf.(*spec.TiFlashInstance).InstanceSpec.(spec.TiFlashSpec)
if !ok {
return result, fmt.Errorf("instance type mismatch for %v", inst)
return result, perrs.Errorf("instance type mismatch for %v", inst)
}
tfs.DataDir = d
key := inst.ID() + d + uuid.New().String()
Expand All @@ -127,14 +128,28 @@ func renderInstanceSpec(t string, inst spec.Instance) ([]string, error) {
default:
s, err := renderSpec(t, inst, inst.ID())
if err != nil {
return result, fmt.Errorf("error rendering path for instance %v", inst)
return result, perrs.Errorf("error rendering path for instance %v", inst)
}
result = append(result, s)
}
return result, nil
}

func renderSpec(t string, s interface{}, id string) (string, error) {
// Only apply on *spec.TiDBInstance and *spec.PDInstance etc.
if v := reflect.ValueOf(s); v.Kind() == reflect.Ptr {
if v = v.Elem(); !v.IsValid() {
return "", perrs.Errorf("invalid spec")
}
if v = v.FieldByName("BaseInstance"); !v.IsValid() {
return "", perrs.Errorf("field BaseInstance not found")
}
if v = v.FieldByName("InstanceSpec"); !v.IsValid() {
return "", perrs.Errorf("field InstanceSpec not found")
}
s = v.Interface()
}

tpl, err := template.New(id).Option("missingkey=error").Parse(t)
if err != nil {
return "", err
Expand Down
56 changes: 56 additions & 0 deletions pkg/cluster/manager/transfer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2020 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package manager

import (
"testing"

"github.com/pingcap/tiup/pkg/cluster/spec"
"github.com/stretchr/testify/assert"
)

func TestRenderSpec(t *testing.T) {
var s spec.Instance = &spec.TiDBInstance{BaseInstance: spec.BaseInstance{
InstanceSpec: spec.TiDBSpec{
Host: "172.16.5.140",
SSHPort: 22,
Imported: false,
Port: 4000,
StatusPort: 10080,
DeployDir: "/home/test/deploy/tidb-4000",
Arch: "amd64",
OS: "linux",
},
}}
dir, err := renderSpec("{{.DataDir}}", s, "test-tidb")
assert.NotNil(t, err)
assert.Empty(t, dir)

s = &spec.PDInstance{BaseInstance: spec.BaseInstance{
InstanceSpec: spec.PDSpec{
Host: "172.16.5.140",
SSHPort: 22,
Imported: false,
Name: "pd-1",
ClientPort: 2379,
PeerPort: 2380,
DeployDir: "/home/test/deploy/pd-2379",
DataDir: "/home/test/deploy/pd-2379/data",
},
}}
//s.BaseInstance.InstanceSpec
dir, err = renderSpec("{{.DataDir}}", s, "test-pd")
assert.Nil(t, err)
assert.NotEmpty(t, dir)
}
4 changes: 2 additions & 2 deletions tests/tiup-cluster/script/cmd_subtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ function cmd_subtest() {

# Test push and pull
echo "test_transfer $name $RANDOM `date`" > test_transfer_1.txt
tiup-cluster $client push $name test_transfer_1.txt "{{ .LogDir }}/test_transfer.txt" -R grafana
tiup-cluster $client pull $name "{{ .LogDir }}/test_transfer.txt" test_transfer_2.txt -R grafana
tiup-cluster $client push $name test_transfer_1.txt "{{ .DeployDir }}/test_transfer.txt" -R grafana
tiup-cluster $client pull $name "{{ .DeployDir }}/test_transfer.txt" test_transfer_2.txt -R grafana
diff test_transfer_1.txt test_transfer_2.txt

echo "checking cleanup data and log"
Expand Down

0 comments on commit df76e55

Please sign in to comment.