From 13582d1a7ba28d616fba30cf0a4f11a8ab92bae8 Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Thu, 21 Nov 2024 10:29:04 +0800 Subject: [PATCH] test: add unit test cases --- internal/deployer/factory.go | 35 ++++++++++ internal/notify/factory.go | 4 +- internal/pkg/core/deployer/logger.go | 12 +++- internal/pkg/core/deployer/logger_test.go | 56 ++++++++++++++++ .../providers/aliyun-alb/aliyun_alb_test.go | 2 +- .../providers/aliyun-cdn/aliyun_cdn_test.go | 2 +- .../providers/aliyun-clb/aliyun_clb_test.go | 2 +- .../providers/aliyun-dcdn/aliyun_dcdn_test.go | 2 +- .../providers/aliyun-nlb/aliyun_nlb_test.go | 2 +- .../providers/aliyun-oss/aliyun_oss_test.go | 2 +- .../baiducloud-cdn/baiducloud_cdn_test.go | 2 +- .../byteplus-cdn/byteplus_cdn_test.go | 2 +- .../dogecloud-cdn/dogecloud_cdn_test.go | 2 +- .../huaweicloud-cdn/huaweicloud_cdn_test.go | 2 +- .../huaweicloud-elb/huaweicloud_elb_test.go | 2 +- .../providers/k8s-secret/k8s_secret_test.go | 2 +- .../deployer/providers/local/local_test.go | 2 +- .../providers/qiniu-cdn/qiniu_cdn_test.go | 2 +- .../core/deployer/providers/ssh/ssh_test.go | 2 +- .../tencentcloud-cdn/tencentcloud_cdn_test.go | 2 +- .../tencentcloud-clb/tencentcloud_clb_test.go | 2 +- .../tencentcloud-cos/tencentcloud_cos_test.go | 2 +- .../tencentcloud_ecdn_test.go | 2 +- .../tencentcloud-teo/tencentcloud_teo_test.go | 2 +- .../volcengine-cdn/volcengine_cdn_test.go | 2 +- .../volcengine-live/volcengine_live_test.go | 2 +- .../providers/webhook/webhook_test.go | 2 +- .../core/notifier/providers/bark/bark_test.go | 64 +++++++++++++++++++ .../providers/dingtalk/dingtalk_test.go | 63 ++++++++++++++++++ .../notifier/providers/email/email_test.go | 2 +- .../core/notifier/providers/lark/lark_test.go | 57 +++++++++++++++++ .../providers/serverchan/serverchan_test.go | 57 +++++++++++++++++ .../providers/telegram/telegram_test.go | 64 +++++++++++++++++++ .../providers/webhook/webhook_test.go | 2 +- 34 files changed, 434 insertions(+), 28 deletions(-) create mode 100644 internal/deployer/factory.go create mode 100644 internal/pkg/core/deployer/logger_test.go create mode 100644 internal/pkg/core/notifier/providers/bark/bark_test.go create mode 100644 internal/pkg/core/notifier/providers/dingtalk/dingtalk_test.go create mode 100644 internal/pkg/core/notifier/providers/lark/lark_test.go create mode 100644 internal/pkg/core/notifier/providers/serverchan/serverchan_test.go create mode 100644 internal/pkg/core/notifier/providers/telegram/telegram_test.go diff --git a/internal/deployer/factory.go b/internal/deployer/factory.go new file mode 100644 index 00000000..ed18a7a5 --- /dev/null +++ b/internal/deployer/factory.go @@ -0,0 +1,35 @@ +package deployer + +import ( + "encoding/json" + "fmt" + + "github.com/usual2970/certimate/internal/domain" + "github.com/usual2970/certimate/internal/pkg/core/deployer" + providerAliyunOss "github.com/usual2970/certimate/internal/pkg/core/deployer/providers/aliyun-oss" + "github.com/usual2970/certimate/internal/pkg/utils/maps" +) + +// TODO: 该方法目前未实际使用,将在后续迭代中替换 +func createDeployer(target string, accessConfig string, deployConfig map[string]any) (deployer.Deployer, deployer.Logger, error) { + logger := deployer.NewDefaultLogger() + + switch target { + case targetAliyunOSS: + access := &domain.AliyunAccess{} + if err := json.Unmarshal([]byte(accessConfig), access); err != nil { + return nil, nil, err + } + + deployer, err := providerAliyunOss.NewWithLogger(&providerAliyunOss.AliyunOSSDeployerConfig{ + AccessKeyId: access.AccessKeyId, + AccessKeySecret: access.AccessKeySecret, + Region: maps.GetValueAsString(deployConfig, "region"), + Bucket: maps.GetValueAsString(deployConfig, "bucket"), + Domain: maps.GetValueAsString(deployConfig, "domain"), + }, logger) + return deployer, logger, err + } + + return nil, nil, fmt.Errorf("unsupported deployer target: %s", target) +} diff --git a/internal/notify/factory.go b/internal/notify/factory.go index 9ed0e3f4..e244b071 100644 --- a/internal/notify/factory.go +++ b/internal/notify/factory.go @@ -1,7 +1,7 @@ package notify import ( - "errors" + "fmt" "github.com/usual2970/certimate/internal/domain" "github.com/usual2970/certimate/internal/pkg/core/notifier" @@ -62,5 +62,5 @@ func createNotifier(channel string, channelConfig map[string]any) (notifier.Noti }) } - return nil, errors.New("unsupported notifier channel") + return nil, fmt.Errorf("unsupported notifier channel: %s", channelConfig) } diff --git a/internal/pkg/core/deployer/logger.go b/internal/pkg/core/deployer/logger.go index a97800b1..433b86ac 100644 --- a/internal/pkg/core/deployer/logger.go +++ b/internal/pkg/core/deployer/logger.go @@ -27,6 +27,9 @@ type Logger interface { // 获取所有日志记录。 GetRecords() []string + + // 清空。 + Flush() } // 表示默认的日志记录器类型。 @@ -43,7 +46,9 @@ func (l *DefaultLogger) Appendt(tag string, data ...any) { temp[0] = tag for i, v := range data { s := "" - if v != nil { + if v == nil { + s = "" + } else { switch reflect.ValueOf(v).Kind() { case reflect.String: s = v.(string) @@ -78,6 +83,10 @@ func (l *DefaultLogger) GetRecords() []string { return temp } +func (l *DefaultLogger) Flush() { + l.records = make([]string, 0) +} + func (l *DefaultLogger) ensureInitialized() { if l.records == nil { l.records = make([]string, 0) @@ -101,6 +110,7 @@ func (l *NilLogger) Appendf(string, ...any) {} func (l *NilLogger) GetRecords() []string { return make([]string, 0) } +func (l *NilLogger) Flush() {} func NewNilLogger() *NilLogger { return &NilLogger{} diff --git a/internal/pkg/core/deployer/logger_test.go b/internal/pkg/core/deployer/logger_test.go new file mode 100644 index 00000000..5e55df5c --- /dev/null +++ b/internal/pkg/core/deployer/logger_test.go @@ -0,0 +1,56 @@ +package deployer_test + +import ( + "testing" + + "github.com/usual2970/certimate/internal/pkg/core/deployer" +) + +/* +Shell command to run this test: + + go test -v logger_test.go +*/ +func TestLogger(t *testing.T) { + t.Run("Logger_Appendt", func(t *testing.T) { + logger := deployer.NewDefaultLogger() + + logger.Appendt("test") + logger.Appendt("test_nil", nil) + logger.Appendt("test_int", 1024) + logger.Appendt("test_string", "certimate") + logger.Appendt("test_map", map[string]interface{}{"key": "value"}) + logger.Appendt("test_struct", struct{ Name string }{Name: "certimate"}) + logger.Appendt("test_slice", []string{"certimate"}) + t.Log(logger.GetRecords()) + if len(logger.GetRecords()) != 7 { + t.Errorf("expected 7 records, got %d", len(logger.GetRecords())) + } + + logger.Flush() + if len(logger.GetRecords()) != 0 { + t.Errorf("expected 0 records, got %d", len(logger.GetRecords())) + } + }) + + t.Run("Logger_Appendf", func(t *testing.T) { + logger := deployer.NewDefaultLogger() + + logger.Appendf("test") + logger.Appendf("test_nil: %v", nil) + logger.Appendf("test_int: %v", 1024) + logger.Appendf("test_string: %v", "certimate") + logger.Appendf("test_map: %v", map[string]interface{}{"key": "value"}) + logger.Appendf("test_struct: %v", struct{ Name string }{Name: "certimate"}) + logger.Appendf("test_slice: %v", []string{"certimate"}) + t.Log(logger.GetRecords()) + if len(logger.GetRecords()) != 7 { + t.Errorf("expected 7 records, got %d", len(logger.GetRecords())) + } + + logger.Flush() + if len(logger.GetRecords()) != 0 { + t.Errorf("expected 0 records, got %d", len(logger.GetRecords())) + } + }) +} diff --git a/internal/pkg/core/deployer/providers/aliyun-alb/aliyun_alb_test.go b/internal/pkg/core/deployer/providers/aliyun-alb/aliyun_alb_test.go index 686e1e6c..143b65b4 100644 --- a/internal/pkg/core/deployer/providers/aliyun-alb/aliyun_alb_test.go +++ b/internal/pkg/core/deployer/providers/aliyun-alb/aliyun_alb_test.go @@ -45,7 +45,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_ALIYUNALB_LOADBALANCERID="your-alb-instance-id" \ --CERTIMATE_DEPLOYER_ALIYUNALB_LISTENERID="your-alb-listener-id" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy_ToLoadbalancer", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/aliyun-cdn/aliyun_cdn_test.go b/internal/pkg/core/deployer/providers/aliyun-cdn/aliyun_cdn_test.go index e5562603..66464aa1 100644 --- a/internal/pkg/core/deployer/providers/aliyun-cdn/aliyun_cdn_test.go +++ b/internal/pkg/core/deployer/providers/aliyun-cdn/aliyun_cdn_test.go @@ -39,7 +39,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_ALIYUNCDN_ACCESSKEYSECRET="your-access-key-secret" \ --CERTIMATE_DEPLOYER_ALIYUNCDN_DOMAIN="example.com" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/aliyun-clb/aliyun_clb_test.go b/internal/pkg/core/deployer/providers/aliyun-clb/aliyun_clb_test.go index 6a8cc45c..f9aa800d 100644 --- a/internal/pkg/core/deployer/providers/aliyun-clb/aliyun_clb_test.go +++ b/internal/pkg/core/deployer/providers/aliyun-clb/aliyun_clb_test.go @@ -45,7 +45,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_ALIYUNCLB_LOADBALANCERID="your-clb-instance-id" \ --CERTIMATE_DEPLOYER_ALIYUNCLB_LISTENERPORT=443 */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy_ToLoadbalancer", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/aliyun-dcdn/aliyun_dcdn_test.go b/internal/pkg/core/deployer/providers/aliyun-dcdn/aliyun_dcdn_test.go index c5a6abec..8fdddf80 100644 --- a/internal/pkg/core/deployer/providers/aliyun-dcdn/aliyun_dcdn_test.go +++ b/internal/pkg/core/deployer/providers/aliyun-dcdn/aliyun_dcdn_test.go @@ -39,7 +39,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_ALIYUNDCDN_ACCESSKEYSECRET="your-access-key-secret" \ --CERTIMATE_DEPLOYER_ALIYUNDCDN_DOMAIN="example.com" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/aliyun-nlb/aliyun_nlb_test.go b/internal/pkg/core/deployer/providers/aliyun-nlb/aliyun_nlb_test.go index b915c1a7..9c976dd9 100644 --- a/internal/pkg/core/deployer/providers/aliyun-nlb/aliyun_nlb_test.go +++ b/internal/pkg/core/deployer/providers/aliyun-nlb/aliyun_nlb_test.go @@ -45,7 +45,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_ALIYUNNLB_LOADBALANCERID="your-nlb-instance-id" \ --CERTIMATE_DEPLOYER_ALIYUNNLB_LISTENERID="your-nlb-listener-id" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy_ToLoadbalancer", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/aliyun-oss/aliyun_oss_test.go b/internal/pkg/core/deployer/providers/aliyun-oss/aliyun_oss_test.go index 66834e24..afb31853 100644 --- a/internal/pkg/core/deployer/providers/aliyun-oss/aliyun_oss_test.go +++ b/internal/pkg/core/deployer/providers/aliyun-oss/aliyun_oss_test.go @@ -45,7 +45,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_ALIYUNOSS_BUCKET="your-oss-bucket" \ --CERTIMATE_DEPLOYER_ALIYUNOSS_DOMAIN="example.com" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/baiducloud-cdn/baiducloud_cdn_test.go b/internal/pkg/core/deployer/providers/baiducloud-cdn/baiducloud_cdn_test.go index 31e51937..e3398505 100644 --- a/internal/pkg/core/deployer/providers/baiducloud-cdn/baiducloud_cdn_test.go +++ b/internal/pkg/core/deployer/providers/baiducloud-cdn/baiducloud_cdn_test.go @@ -39,7 +39,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_BAIDUCLOUDCDN_SECRETACCESSKEY="your-secret-access-key" \ --CERTIMATE_DEPLOYER_BAIDUCLOUDCDN_DOMAIN="example.com" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/byteplus-cdn/byteplus_cdn_test.go b/internal/pkg/core/deployer/providers/byteplus-cdn/byteplus_cdn_test.go index 4a051dd6..0c411062 100644 --- a/internal/pkg/core/deployer/providers/byteplus-cdn/byteplus_cdn_test.go +++ b/internal/pkg/core/deployer/providers/byteplus-cdn/byteplus_cdn_test.go @@ -39,7 +39,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_BYTEPLUSCDN_SECRETKEY="your-secret-key" \ --CERTIMATE_DEPLOYER_BYTEPLUSCDN_DOMAIN="example.com" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/dogecloud-cdn/dogecloud_cdn_test.go b/internal/pkg/core/deployer/providers/dogecloud-cdn/dogecloud_cdn_test.go index da08c09e..2375e372 100644 --- a/internal/pkg/core/deployer/providers/dogecloud-cdn/dogecloud_cdn_test.go +++ b/internal/pkg/core/deployer/providers/dogecloud-cdn/dogecloud_cdn_test.go @@ -39,7 +39,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_DOGECLOUDCDN_SECRETKEY="your-secret-key" \ --CERTIMATE_DEPLOYER_DOGECLOUDCDN_DOMAIN="example.com" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/huaweicloud-cdn/huaweicloud_cdn_test.go b/internal/pkg/core/deployer/providers/huaweicloud-cdn/huaweicloud_cdn_test.go index fc45129a..d50e3ea5 100644 --- a/internal/pkg/core/deployer/providers/huaweicloud-cdn/huaweicloud_cdn_test.go +++ b/internal/pkg/core/deployer/providers/huaweicloud-cdn/huaweicloud_cdn_test.go @@ -42,7 +42,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_HUAWEICLOUDCDN_REGION="cn-north-1" \ --CERTIMATE_DEPLOYER_HUAWEICLOUDCDN_DOMAIN="example.com" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/huaweicloud-elb/huaweicloud_elb_test.go b/internal/pkg/core/deployer/providers/huaweicloud-elb/huaweicloud_elb_test.go index 33e8afb6..f2e573ca 100644 --- a/internal/pkg/core/deployer/providers/huaweicloud-elb/huaweicloud_elb_test.go +++ b/internal/pkg/core/deployer/providers/huaweicloud-elb/huaweicloud_elb_test.go @@ -48,7 +48,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_HUAWEICLOUDELB_LOADBALANCERID="your-elb-loadbalancer-id" \ --CERTIMATE_DEPLOYER_HUAWEICLOUDELB_LISTENERID="your-elb-listener-id" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy_ToCertificate", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/k8s-secret/k8s_secret_test.go b/internal/pkg/core/deployer/providers/k8s-secret/k8s_secret_test.go index 8e6992f9..ac47deba 100644 --- a/internal/pkg/core/deployer/providers/k8s-secret/k8s_secret_test.go +++ b/internal/pkg/core/deployer/providers/k8s-secret/k8s_secret_test.go @@ -42,7 +42,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_K8SSECRET_SECRETDATAKEYFORCRT="tls.crt" \ --CERTIMATE_DEPLOYER_K8SSECRET_SECRETDATAKEYFORKEY="tls.key" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/local/local_test.go b/internal/pkg/core/deployer/providers/local/local_test.go index e14f7b59..18838c63 100644 --- a/internal/pkg/core/deployer/providers/local/local_test.go +++ b/internal/pkg/core/deployer/providers/local/local_test.go @@ -48,7 +48,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_LOCAL_JKSKEYPASS="your-jks-keypass" \ --CERTIMATE_DEPLOYER_LOCAL_JKSSTOREPASS="your-jks-storepass" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy_PEM", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/qiniu-cdn/qiniu_cdn_test.go b/internal/pkg/core/deployer/providers/qiniu-cdn/qiniu_cdn_test.go index 290dc93d..df4ee008 100644 --- a/internal/pkg/core/deployer/providers/qiniu-cdn/qiniu_cdn_test.go +++ b/internal/pkg/core/deployer/providers/qiniu-cdn/qiniu_cdn_test.go @@ -39,7 +39,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_QINIUCDN_SECRETKEY="your-secret-key" \ --CERTIMATE_DEPLOYER_QINIUCDN_DOMAIN="example.com" \ */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/ssh/ssh_test.go b/internal/pkg/core/deployer/providers/ssh/ssh_test.go index 041feb65..f1c25e63 100644 --- a/internal/pkg/core/deployer/providers/ssh/ssh_test.go +++ b/internal/pkg/core/deployer/providers/ssh/ssh_test.go @@ -48,7 +48,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_SSH_OUTPUTCERTPATH="/path/to/your-output-cert.pem" \ --CERTIMATE_DEPLOYER_SSH_OUTPUTKEYPATH="/path/to/your-output-key.pem" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/tencentcloud-cdn/tencentcloud_cdn_test.go b/internal/pkg/core/deployer/providers/tencentcloud-cdn/tencentcloud_cdn_test.go index 343a2d56..70b3c59d 100644 --- a/internal/pkg/core/deployer/providers/tencentcloud-cdn/tencentcloud_cdn_test.go +++ b/internal/pkg/core/deployer/providers/tencentcloud-cdn/tencentcloud_cdn_test.go @@ -39,7 +39,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_TENCENTCLOUDCDN_SECRETKEY="your-secret-key" \ --CERTIMATE_DEPLOYER_TENCENTCLOUDCDN_DOMAIN="example.com" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/tencentcloud-clb/tencentcloud_clb_test.go b/internal/pkg/core/deployer/providers/tencentcloud-clb/tencentcloud_clb_test.go index 1409c240..fe0cf023 100644 --- a/internal/pkg/core/deployer/providers/tencentcloud-clb/tencentcloud_clb_test.go +++ b/internal/pkg/core/deployer/providers/tencentcloud-clb/tencentcloud_clb_test.go @@ -48,7 +48,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_TENCENTCLOUDCLB_LISTENERID="your-clb-lbl-id" \ --CERTIMATE_DEPLOYER_TENCENTCLOUDCLB_DOMAIN="example.com" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy_UseSslDeploy", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/tencentcloud-cos/tencentcloud_cos_test.go b/internal/pkg/core/deployer/providers/tencentcloud-cos/tencentcloud_cos_test.go index 4043305d..5f105efe 100644 --- a/internal/pkg/core/deployer/providers/tencentcloud-cos/tencentcloud_cos_test.go +++ b/internal/pkg/core/deployer/providers/tencentcloud-cos/tencentcloud_cos_test.go @@ -45,7 +45,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_TENCENTCLOUDCOS_BUCKET="your-cos-bucket" \ --CERTIMATE_DEPLOYER_TENCENTCLOUDCOS_DOMAIN="example.com" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/tencentcloud-ecdn/tencentcloud_ecdn_test.go b/internal/pkg/core/deployer/providers/tencentcloud-ecdn/tencentcloud_ecdn_test.go index 4322d977..95d617cf 100644 --- a/internal/pkg/core/deployer/providers/tencentcloud-ecdn/tencentcloud_ecdn_test.go +++ b/internal/pkg/core/deployer/providers/tencentcloud-ecdn/tencentcloud_ecdn_test.go @@ -39,7 +39,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_TENCENTCLOUDECDN_SECRETKEY="your-secret-key" \ --CERTIMATE_DEPLOYER_TENCENTCLOUDECDN_DOMAIN="example.com" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/tencentcloud-teo/tencentcloud_teo_test.go b/internal/pkg/core/deployer/providers/tencentcloud-teo/tencentcloud_teo_test.go index 5cf4cc08..8d875e55 100644 --- a/internal/pkg/core/deployer/providers/tencentcloud-teo/tencentcloud_teo_test.go +++ b/internal/pkg/core/deployer/providers/tencentcloud-teo/tencentcloud_teo_test.go @@ -42,7 +42,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_TENCENTCLOUDETEO_ZONEID="your-zone-id" \ --CERTIMATE_DEPLOYER_TENCENTCLOUDETEO_DOMAIN="example.com" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/volcengine-cdn/volcengine_cdn_test.go b/internal/pkg/core/deployer/providers/volcengine-cdn/volcengine_cdn_test.go index fad06e94..e24740be 100644 --- a/internal/pkg/core/deployer/providers/volcengine-cdn/volcengine_cdn_test.go +++ b/internal/pkg/core/deployer/providers/volcengine-cdn/volcengine_cdn_test.go @@ -39,7 +39,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_VOLCENGINECDN_SECRETKEY="your-secret-key" \ --CERTIMATE_DEPLOYER_VOLCENGINECDN_DOMAIN="example.com" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/volcengine-live/volcengine_live_test.go b/internal/pkg/core/deployer/providers/volcengine-live/volcengine_live_test.go index 76735426..0ad66385 100644 --- a/internal/pkg/core/deployer/providers/volcengine-live/volcengine_live_test.go +++ b/internal/pkg/core/deployer/providers/volcengine-live/volcengine_live_test.go @@ -39,7 +39,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_VOLCENGINELIVE_SECRETKEY="your-secret-key" \ --CERTIMATE_DEPLOYER_VOLCENGINELIVE_DOMAIN="example.com" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy", func(t *testing.T) { diff --git a/internal/pkg/core/deployer/providers/webhook/webhook_test.go b/internal/pkg/core/deployer/providers/webhook/webhook_test.go index e3a9b3e7..a3b36dda 100644 --- a/internal/pkg/core/deployer/providers/webhook/webhook_test.go +++ b/internal/pkg/core/deployer/providers/webhook/webhook_test.go @@ -33,7 +33,7 @@ Shell command to run this test: --CERTIMATE_DEPLOYER_WEBHOOK_INPUTKEYPATH="/path/to/your-input-key.pem" \ --CERTIMATE_DEPLOYER_WEBHOOK_URL="https://example.com/your-webhook-url" */ -func Test(t *testing.T) { +func TestDeploy(t *testing.T) { flag.Parse() t.Run("Deploy", func(t *testing.T) { diff --git a/internal/pkg/core/notifier/providers/bark/bark_test.go b/internal/pkg/core/notifier/providers/bark/bark_test.go new file mode 100644 index 00000000..3b9bef2d --- /dev/null +++ b/internal/pkg/core/notifier/providers/bark/bark_test.go @@ -0,0 +1,64 @@ +package bark_test + +import ( + "context" + "flag" + "fmt" + "strings" + "testing" + + provider "github.com/usual2970/certimate/internal/pkg/core/notifier/providers/bark" +) + +const ( + mockSubject = "test_subject" + mockMessage = "test_message" +) + +var ( + fServerUrl string + fDeviceKey string +) + +func init() { + argsPrefix := "CERTIMATE_NOTIFIER_BARK_" + + flag.StringVar(&fServerUrl, argsPrefix+"SERVERURL", "", "") + flag.StringVar(&fDeviceKey, argsPrefix+"DEVICEKEY", "", "") +} + +/* +Shell command to run this test: + + go test -v bark_test.go -args \ + --CERTIMATE_NOTIFIER_BARK_SERVERURL="https://example.com/your-server-url" \ + --CERTIMATE_NOTIFIER_BARK_DEVICEKEY="your-device-key" +*/ +func TestNotify(t *testing.T) { + flag.Parse() + + t.Run("Notify", func(t *testing.T) { + t.Log(strings.Join([]string{ + "args:", + fmt.Sprintf("SERVERURL: %v", fServerUrl), + fmt.Sprintf("DEVICEKEY: %v", fDeviceKey), + }, "\n")) + + notifier, err := provider.New(&provider.BarkNotifierConfig{ + ServerUrl: fServerUrl, + DeviceKey: fDeviceKey, + }) + if err != nil { + t.Errorf("err: %+v", err) + return + } + + res, err := notifier.Notify(context.Background(), mockSubject, mockMessage) + if err != nil { + t.Errorf("err: %+v", err) + return + } + + t.Logf("ok: %v", res) + }) +} diff --git a/internal/pkg/core/notifier/providers/dingtalk/dingtalk_test.go b/internal/pkg/core/notifier/providers/dingtalk/dingtalk_test.go new file mode 100644 index 00000000..ea2c9bc8 --- /dev/null +++ b/internal/pkg/core/notifier/providers/dingtalk/dingtalk_test.go @@ -0,0 +1,63 @@ +package dingtalk_test + +import ( + "context" + "flag" + "fmt" + "strings" + "testing" + + provider "github.com/usual2970/certimate/internal/pkg/core/notifier/providers/dingtalk" +) + +const ( + mockSubject = "test_subject" + mockMessage = "test_message" +) + +var ( + fAccessToken string + fSecret string +) + +func init() { + argsPrefix := "CERTIMATE_NOTIFIER_DINGTALK_" + + flag.StringVar(&fAccessToken, argsPrefix+"ACCESSTOKEN", "", "") + flag.StringVar(&fSecret, argsPrefix+"SECRET", "", "") +} + +/* +Shell command to run this test: + + go test -v dingtalk_test.go -args \ + --CERTIMATE_NOTIFIER_DINGTALK_URL="https://example.com/your-webhook-url" +*/ +func TestNotify(t *testing.T) { + flag.Parse() + + t.Run("Notify", func(t *testing.T) { + t.Log(strings.Join([]string{ + "args:", + fmt.Sprintf("ACCESSTOKEN: %v", fAccessToken), + fmt.Sprintf("SECRET: %v", fSecret), + }, "\n")) + + notifier, err := provider.New(&provider.DingTalkNotifierConfig{ + AccessToken: fAccessToken, + Secret: fSecret, + }) + if err != nil { + t.Errorf("err: %+v", err) + return + } + + res, err := notifier.Notify(context.Background(), mockSubject, mockMessage) + if err != nil { + t.Errorf("err: %+v", err) + return + } + + t.Logf("ok: %v", res) + }) +} diff --git a/internal/pkg/core/notifier/providers/email/email_test.go b/internal/pkg/core/notifier/providers/email/email_test.go index 5f1fcd9a..34303d06 100644 --- a/internal/pkg/core/notifier/providers/email/email_test.go +++ b/internal/pkg/core/notifier/providers/email/email_test.go @@ -49,7 +49,7 @@ Shell command to run this test: --CERTIMATE_NOTIFIER_EMAIL_SENDERADDRESS="sender@example.com" \ --CERTIMATE_NOTIFIER_EMAIL_RECEIVERADDRESS="receiver@example.com" */ -func Test(t *testing.T) { +func TestNotify(t *testing.T) { flag.Parse() t.Run("Notify", func(t *testing.T) { diff --git a/internal/pkg/core/notifier/providers/lark/lark_test.go b/internal/pkg/core/notifier/providers/lark/lark_test.go new file mode 100644 index 00000000..b93d3606 --- /dev/null +++ b/internal/pkg/core/notifier/providers/lark/lark_test.go @@ -0,0 +1,57 @@ +package lark_test + +import ( + "context" + "flag" + "fmt" + "strings" + "testing" + + provider "github.com/usual2970/certimate/internal/pkg/core/notifier/providers/lark" +) + +const ( + mockSubject = "test_subject" + mockMessage = "test_message" +) + +var fWebhookUrl string + +func init() { + argsPrefix := "CERTIMATE_NOTIFIER_LARK_" + + flag.StringVar(&fWebhookUrl, argsPrefix+"WEBHOOKURL", "", "") +} + +/* +Shell command to run this test: + + go test -v lark_test.go -args \ + --CERTIMATE_NOTIFIER_LARK_WEBHOOKURL="https://example.com/your-webhook-url" +*/ +func TestNotify(t *testing.T) { + flag.Parse() + + t.Run("Notify", func(t *testing.T) { + t.Log(strings.Join([]string{ + "args:", + fmt.Sprintf("WEBHOOKURL: %v", fWebhookUrl), + }, "\n")) + + notifier, err := provider.New(&provider.LarkNotifierConfig{ + WebhookUrl: fWebhookUrl, + }) + if err != nil { + t.Errorf("err: %+v", err) + return + } + + res, err := notifier.Notify(context.Background(), mockSubject, mockMessage) + if err != nil { + t.Errorf("err: %+v", err) + return + } + + t.Logf("ok: %v", res) + }) +} diff --git a/internal/pkg/core/notifier/providers/serverchan/serverchan_test.go b/internal/pkg/core/notifier/providers/serverchan/serverchan_test.go new file mode 100644 index 00000000..63407379 --- /dev/null +++ b/internal/pkg/core/notifier/providers/serverchan/serverchan_test.go @@ -0,0 +1,57 @@ +package serverchan_test + +import ( + "context" + "flag" + "fmt" + "strings" + "testing" + + provider "github.com/usual2970/certimate/internal/pkg/core/notifier/providers/serverchan" +) + +const ( + mockSubject = "test_subject" + mockMessage = "test_message" +) + +var fUrl string + +func init() { + argsPrefix := "CERTIMATE_NOTIFIER_SERVERCHAN_" + + flag.StringVar(&fUrl, argsPrefix+"URL", "", "") +} + +/* +Shell command to run this test: + + go test -v serverchan_test.go -args \ + --CERTIMATE_NOTIFIER_SERVERCHAN_URL="https://example.com/your-webhook-url" \ +*/ +func TestNotify(t *testing.T) { + flag.Parse() + + t.Run("Notify", func(t *testing.T) { + t.Log(strings.Join([]string{ + "args:", + fmt.Sprintf("URL: %v", fUrl), + }, "\n")) + + notifier, err := provider.New(&provider.ServerChanNotifierConfig{ + Url: fUrl, + }) + if err != nil { + t.Errorf("err: %+v", err) + return + } + + res, err := notifier.Notify(context.Background(), mockSubject, mockMessage) + if err != nil { + t.Errorf("err: %+v", err) + return + } + + t.Logf("ok: %v", res) + }) +} diff --git a/internal/pkg/core/notifier/providers/telegram/telegram_test.go b/internal/pkg/core/notifier/providers/telegram/telegram_test.go new file mode 100644 index 00000000..062e2642 --- /dev/null +++ b/internal/pkg/core/notifier/providers/telegram/telegram_test.go @@ -0,0 +1,64 @@ +package telegram_test + +import ( + "context" + "flag" + "fmt" + "strings" + "testing" + + provider "github.com/usual2970/certimate/internal/pkg/core/notifier/providers/telegram" +) + +const ( + mockSubject = "test_subject" + mockMessage = "test_message" +) + +var ( + fApiToken string + fChartId int64 +) + +func init() { + argsPrefix := "CERTIMATE_NOTIFIER_TELEGRAM_" + + flag.StringVar(&fApiToken, argsPrefix+"APITOKEN", "", "") + flag.Int64Var(&fChartId, argsPrefix+"CHATID", 0, "") +} + +/* +Shell command to run this test: + + go test -v telegram_test.go -args \ + --CERTIMATE_NOTIFIER_TELEGRAM_APITOKEN="your-api-token" \ + --CERTIMATE_NOTIFIER_TELEGRAM_CHATID=123456 +*/ +func TestNotify(t *testing.T) { + flag.Parse() + + t.Run("Notify", func(t *testing.T) { + t.Log(strings.Join([]string{ + "args:", + fmt.Sprintf("APITOKEN: %v", fApiToken), + fmt.Sprintf("CHATID: %v", fChartId), + }, "\n")) + + notifier, err := provider.New(&provider.TelegramNotifierConfig{ + ApiToken: fApiToken, + ChatId: fChartId, + }) + if err != nil { + t.Errorf("err: %+v", err) + return + } + + res, err := notifier.Notify(context.Background(), mockSubject, mockMessage) + if err != nil { + t.Errorf("err: %+v", err) + return + } + + t.Logf("ok: %v", res) + }) +} diff --git a/internal/pkg/core/notifier/providers/webhook/webhook_test.go b/internal/pkg/core/notifier/providers/webhook/webhook_test.go index 294c60e8..2f83f8e0 100644 --- a/internal/pkg/core/notifier/providers/webhook/webhook_test.go +++ b/internal/pkg/core/notifier/providers/webhook/webhook_test.go @@ -29,7 +29,7 @@ Shell command to run this test: go test -v webhook_test.go -args \ --CERTIMATE_NOTIFIER_WEBHOOK_URL="https://example.com/your-webhook-url" */ -func Test(t *testing.T) { +func TestNotify(t *testing.T) { flag.Parse() t.Run("Notify", func(t *testing.T) {