Files
cloudpods/pkg/cloudcommon/notifyclient/notify_test.go
2019-04-15 19:54:46 +08:00

78 lines
2.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Copyright 2019 Yunion
//
// 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 notifyclient
import (
"html/template"
"strings"
"testing"
"yunion.io/x/jsonutils"
)
func TestNotifyTemplate(t *testing.T) {
cases := []struct {
template string
data interface{}
want string
}{
{
`云主机{{ .name }}创建成功`,
struct {
Name string
}{
Name: "testsrv-1",
},
`云主机testsrv-1创建成功`,
},
{
`您的云主机{{ .name }}已经创建成功服务器IP地址为{{ .ips }}{{ if .account }}初始帐号为{{ .account }}{{ end }}{{ if .keypair }}访问密钥为{{ .keypair }}{{ end }}{{ if len .password }}初始密码为{{ .password }}{{ end }}请使用{{ if .windows }}远程桌面连接器(RDC){{ else }}SSH{{ end }}或控制面板控制台访问云主机。`,
struct {
Name string
Ips string
Account string
Keypair string
Password string
Windows bool
}{
Name: "testsrv-1",
Ips: "10.168.222.23",
Account: "root",
Password: "1234567",
Windows: false,
},
`您的云主机testsrv-1已经创建成功服务器IP地址为10.168.222.23初始帐号为root初始密码为1234567请使用SSH或控制面板控制台访问云主机。`,
},
}
for _, c := range cases {
temp, err := template.New("template").Parse(c.template)
if err != nil {
t.Errorf("parse template %s fail %s", c.template, err)
} else {
strBuild := strings.Builder{}
jsonData := jsonutils.Marshal(c.data)
t.Logf("jsonData: %s", jsonData)
err = temp.Execute(&strBuild, jsonData.Interface())
if err != nil {
t.Errorf("execute template fail %s", err)
} else {
if strBuild.String() != c.want {
t.Errorf("fail: got %s want %s", strBuild.String(), c.want)
}
}
}
}
}