Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : add e2e test of logger #35

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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
26 changes: 26 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: E2E TEST

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:

build:
strategy:
matrix:
go-version: ['1.21']
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: set up Go 1.x
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ go 1.21

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
Expand Down
42 changes: 42 additions & 0 deletions test/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"log"

"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "test",
Short: "e2e test",
Long: `e2e test of loghook`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
discordURL, err := cmd.Flags().GetString("discord")
if err != nil {
log.Println(err)
return err
}
slackURL, err := cmd.Flags().GetString("slack")
if err != nil {
log.Println(err)
return err
}
err = eToeTest(discordURL, slackURL)
if err != nil {
log.Println(err)
return err
}
return nil
},
}

func main() {
if err := rootCmd.Execute(); err != nil {
log.Println(err)
}
}

func init() {
rootCmd.Flags().StringP("discord", "d", "", "discord webhook url")
rootCmd.Flags().StringP("slack", "s", "", "slack webhook url")
}
70 changes: 70 additions & 0 deletions test/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"context"

"github.com/seipan/loghook"
)

func eToeTest(discordWebhookURL string, slackWebhookURL string) error {
testNosend()
testDiscordSend(discordWebhookURL)
testSendSlack(slackWebhookURL)
return nil
}

func testNosend() {
logger := loghook.NewLogger("", "test", "discord", "")
logger.SetLevel(loghook.DebugLevel)
logger.SetWebhook("")

logger.NoSendDebug()
logger.Debug("test")
logger.DebugContext(context.Background(), "test")
logger.NoSendInfo()
logger.Infof("test %s", "info")
}

func testDiscordSend(discordWebhookURL string) {
logger := loghook.NewLogger("", "test", "discord", discordWebhookURL)
logger.SetLevel(loghook.DebugLevel)
logger.SetWebhook(discordWebhookURL)

logger.Debug("test")
logger.DebugContext(context.Background(), "test")
logger.Debugf("test %s", "debug")
logger.Info("test")
logger.InfoContext(context.Background(), "test")
logger.Infof("test %s", "info")
logger.Warn("test")
logger.WarnContext(context.Background(), "test")
logger.Warnf("test %s", "warn")
logger.Error("test")
logger.ErrorContext(context.Background(), "test")
logger.Errorf("test %s", "error")
logger.Fatal("test")
logger.FatalContext(context.Background(), "test")
logger.Fatalf("test %s", "fatal")
}

func testSendSlack(slackWebhookURL string) {
logger := loghook.NewLogger("", "test", "slack", slackWebhookURL)
logger.SetLevel(loghook.DebugLevel)
logger.SetWebhook("")

logger.Debug("test")
logger.DebugContext(context.Background(), "test")
logger.Debugf("test %s", "debug")
logger.Info("test")
logger.InfoContext(context.Background(), "test")
logger.Infof("test %s", "info")
logger.Warn("test")
logger.WarnContext(context.Background(), "test")
logger.Warnf("test %s", "warn")
logger.Error("test")
logger.ErrorContext(context.Background(), "test")
logger.Errorf("test %s", "error")
logger.Fatal("test")
logger.FatalContext(context.Background(), "test")
logger.Fatalf("test %s", "fatal")
}
Loading