File: template_test.go

package info (click to toggle)
tiup 1.16.3-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,384 kB
  • sloc: sh: 1,988; makefile: 138; sql: 16
file content (139 lines) | stat: -rw-r--r-- 4,424 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package command

import (
	"bytes"
	"io"
	"strings"
	"testing"
)

func Test_DMTemplateLocalCommandSingle(t *testing.T) {
	tests := []struct {
		optKey   string
		optVal   string
		expected string
	}{
		{"user", "ubuntu", "user: \"ubuntu\""},
		{"group", "ubuntu", "group: \"ubuntu\""},
		{"ssh-port", "2222", "ssh_port: 2222"},
		{"deploy-dir", "/path/to/deploy", "deploy_dir: \"/path/to/deploy\""},
		{"data-dir", "/path/to/data", "data_dir: \"/path/to/data\""},
		{"arch", "arm64", "arch: \"arm64\""},
		{"master-servers", "a,b,c", "master_servers:\n  - host: a\n  - host: b\n  - host: c"},
		{"worker-servers", "a,b,c", "worker_servers:\n  - host: a\n  - host: b\n  - host: c"},
		{"monitoring-servers", "a,b,c", "monitoring_servers:\n  - host: a\n  - host: b\n  - host: c"},
		{"grafana-servers", "a,b,c", "grafana_servers:\n  - host: a\n  - host: b\n  - host: c"},
		{"alertmanager-servers", "a,b,c", "alertmanager_servers:\n  - host: a\n  - host: b\n  - host: c"},
	}

	for _, test := range tests {
		cmd := newTemplateCmd()
		b := bytes.NewBufferString("")
		cmd.SetOut(b)
		_ = cmd.Flags().Set("local", "true") // add --local
		_ = cmd.Flags().Set(test.optKey, test.optVal)

		if err := cmd.Execute(); err != nil {
			t.Fatal(err)
		}

		out, err := io.ReadAll(b)
		if err != nil {
			t.Fatal(err)
		}
		if !strings.Contains(string(out), test.expected) {
			t.Fatalf("expected \"%s\", got \"%s\"", test.expected, string(out))
		}
	}
}

func Test_DMTemplateLocalCommandMulti(t *testing.T) {
	cmd := newTemplateCmd()
	b := bytes.NewBufferString("")
	cmd.SetOut(b)
	_ = cmd.Flags().Set("local", "true")                    // add --local
	_ = cmd.Flags().Set("user", "ubuntu")                   // add --user=ubuntu
	_ = cmd.Flags().Set("group", "ubuntu")                  // add --group=ubuntu
	_ = cmd.Flags().Set("master-servers", "m1,m2,m3")       // add --master-servers=m1,m2,m3
	_ = cmd.Flags().Set("worker-servers", "w1,w2,w3")       // add --worker-servers=w1,w2,w3
	_ = cmd.Flags().Set("alertmanager-servers", "a1,a2,a3") // add --alertmanager-servers=a1,a2,a3

	if err := cmd.Execute(); err != nil {
		t.Fatal(err)
	}

	out, err := io.ReadAll(b)
	if err != nil {
		t.Fatal(err)
	}

	for _, b := range []bool{
		strings.Contains(string(out), "user: \"ubuntu\""),
		strings.Contains(string(out), "group: \"ubuntu\""),
		strings.Contains(string(out), "master_servers:\n  - host: m1\n  - host: m2\n  - host: m3"),
		strings.Contains(string(out), "worker_servers:\n  - host: w1\n  - host: w2\n  - host: w3"),
		strings.Contains(string(out), "alertmanager_servers:\n  - host: a1\n  - host: a2\n  - host: a3"),
	} {
		if !b {
			t.Fatalf("unexpected output. got \"%s\"", string(out))
		}
	}
}

func Test_TemplateLocalCommandNoopt(t *testing.T) {
	cmd := newTemplateCmd()
	b := bytes.NewBufferString("")
	cmd.SetOut(b)
	_ = cmd.Flags().Set("local", "true") // add --local

	if err := cmd.Execute(); err != nil {
		t.Fatal(err)
	}

	out, err := io.ReadAll(b)
	if err != nil {
		t.Fatal(err)
	}

	// check default output
	for _, b := range []bool{
		strings.Contains(string(out), "user: \"tidb\""),
		strings.Contains(string(out), "ssh_port: 22"),
		strings.Contains(string(out), "deploy_dir: \"/tidb-deploy\""),
		strings.Contains(string(out), "data_dir: \"/tidb-data\""),
		strings.Contains(string(out), "arch: \"amd64\""),
		strings.Contains(string(out), "master_servers:\n  - host: 172.19.0.101\n  - host: 172.19.0.102\n  - host: 172.19.0.103"),
		strings.Contains(string(out), "worker_servers:\n  - host: 172.19.0.101\n  - host: 172.19.0.102\n  - host: 172.19.0.103"),
		strings.Contains(string(out), "monitoring_servers:\n  - host: 172.19.0.101"),
		strings.Contains(string(out), "grafana_servers:\n  - host: 172.19.0.101"),
		strings.Contains(string(out), "alertmanager_servers:\n  - host: 172.19.0.101"),
	} {
		if !b {
			t.Fatalf("unexpected output. got \"%s\"", string(out))
		}
	}
}

func Test_TemplateLocalCommandValidate(t *testing.T) {
	tests := []struct {
		optKey string
		optVal string
	}{
		{"arch", "i386"},
		{"master-servers", "m1,m2"},
		{"worker-servers", "w1"},
	}

	for _, test := range tests {
		cmd := newTemplateCmd()
		b := bytes.NewBufferString("")
		cmd.SetOut(b)
		_ = cmd.Flags().Set("local", "true")          // add --local
		_ = cmd.Flags().Set(test.optKey, test.optVal) // add invalid option

		// should returns err
		if err := cmd.Execute(); err == nil {
			t.Fatal(err)
		}
	}
}