This is an automated email from the ASF dual-hosted git repository.

kvn pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-ingress-controller.git


The following commit(s) were added to refs/heads/master by this push:
     new 3b6199a  test: add test case for ingress command (#91)
3b6199a is described below

commit 3b6199a7f1ec73fb39b19adb864bcec4326fdfff
Author: Alex Zhang <[email protected]>
AuthorDate: Mon Dec 14 19:31:42 2020 +0800

    test: add test case for ingress command (#91)
    
    * test: add test case for ingress command
    
    * fix: fix the broken TestSignalHandler case
---
 cmd/ingress/ingress.go      | 13 ++++++--
 cmd/ingress/ingress_test.go | 76 +++++++++++++++++++++++++++++++++++++++++++--
 conf/config-default.yaml    |  2 +-
 pkg/log/logger.go           |  2 +-
 4 files changed, 86 insertions(+), 7 deletions(-)

diff --git a/cmd/ingress/ingress.go b/cmd/ingress/ingress.go
index 4180f49..0575356 100644
--- a/cmd/ingress/ingress.go
+++ b/cmd/ingress/ingress.go
@@ -15,6 +15,7 @@
 package ingress
 
 import (
+       "encoding/json"
        "fmt"
        "os"
        "os/signal"
@@ -68,7 +69,6 @@ func NewIngressCommand() *cobra.Command {
                                }
                                cfg = c
                        }
-
                        logger, err := log.NewLogger(
                                log.WithLogLevel(cfg.LogLevel),
                                log.WithOutputFile(cfg.LogOutput),
@@ -77,6 +77,13 @@ func NewIngressCommand() *cobra.Command {
                                dief("failed to initialize logging: %s", err)
                        }
                        log.DefaultLogger = logger
+                       log.Info("apisix ingress controller started")
+
+                       data, err := json.MarshalIndent(cfg, "", "\t")
+                       if err != nil {
+                               dief("failed to show configuration: %s", 
string(data))
+                       }
+                       log.Info("use configuration\n", string(data))
 
                        kubeClientSet := conf.GetKubeClient()
                        apisixClientset := conf.InitApisixClient()
@@ -120,12 +127,12 @@ func NewIngressCommand() *cobra.Command {
                        }()
 
                        waitForSignal(stop)
-                       log.Info("apisix-ingress-controller exited")
+                       log.Info("apisix ingress controller exited")
                },
        }
 
        cmd.PersistentFlags().StringVar(&configPath, "config-path", "", 
"configuration file path for apisix-ingress-controller")
-       cmd.PersistentFlags().StringVar(&cfg.LogLevel, "log-level", "warn", 
"error log level")
+       cmd.PersistentFlags().StringVar(&cfg.LogLevel, "log-level", "info", 
"error log level")
        cmd.PersistentFlags().StringVar(&cfg.LogOutput, "log-output", "stderr", 
"error log output file")
        cmd.PersistentFlags().StringVar(&cfg.HTTPListen, "http-listen", 
":8080", "the HTTP Server listen address")
        cmd.PersistentFlags().BoolVar(&cfg.EnableProfiling, "enable-profiling", 
true, "enable profiling via web interface host:port/debug/pprof")
diff --git a/cmd/ingress/ingress_test.go b/cmd/ingress/ingress_test.go
index b8b08c8..8cde8cf 100644
--- a/cmd/ingress/ingress_test.go
+++ b/cmd/ingress/ingress_test.go
@@ -15,9 +15,12 @@
 package ingress
 
 import (
+       "bufio"
        "bytes"
+       "encoding/json"
        "fmt"
        "os"
+       "strings"
        "syscall"
        "testing"
        "time"
@@ -25,7 +28,9 @@ import (
        "github.com/stretchr/testify/assert"
 
        "github.com/api7/ingress-controller/conf"
+       "github.com/api7/ingress-controller/pkg/config"
        "github.com/api7/ingress-controller/pkg/log"
+       "github.com/api7/ingress-controller/pkg/types"
 )
 
 type fakeWriteSyncer struct {
@@ -55,7 +60,7 @@ func (fws *fakeWriteSyncer) bytes() (p []byte) {
 }
 
 func TestSignalHandler(t *testing.T) {
-       // TODO remove these two lines.
+       // TODO remove these lines.
        conf.ENV = "local"
        conf.SetConfPath("./testdata/conf.json")
        conf.Init()
@@ -78,5 +83,72 @@ func TestSignalHandler(t *testing.T) {
 
        msg := string(fws.buf.Bytes())
        assert.Contains(t, msg, fmt.Sprintf("signal %d (%s) received", 
syscall.SIGINT, syscall.SIGINT.String()))
-       assert.Contains(t, msg, "apisix-ingress-controller exited")
+       assert.Contains(t, msg, "apisix ingress controller exited")
+}
+
+func TestNewIngressCommandEffectiveLog(t *testing.T) {
+       cmd := NewIngressCommand()
+       cmd.SetArgs([]string{
+               "--log-level", "debug",
+               "--log-output", "./test.log",
+               "--http-listen", "127.0.0.1:16780",
+               "--enable-profiling",
+               "--kubeconfig", "/foo/bar/baz",
+               "--resync-interval", "24h",
+               "--apisix-base-url", 
"http://apisixgw.default.cluster.local/apisix";,
+               "--apisix-admin-key", "0x123",
+       })
+       defer os.Remove("./test.log")
+
+       // TODO remove these lines.
+       conf.ENV = "local"
+       conf.SetConfPath("./testdata/conf.json")
+       conf.Init()
+
+       stopCh := make(chan struct{})
+       go func() {
+               assert.Nil(t, cmd.Execute())
+               close(stopCh)
+       }()
+
+       time.Sleep(time.Second)
+       assert.Nil(t, syscall.Kill(os.Getpid(), syscall.SIGINT))
+       <-stopCh
+
+       file, err := os.Open("./test.log")
+       assert.Nil(t, err)
+
+       buf := bufio.NewReader(file)
+       f := parseLog(t, buf)
+       assert.Contains(t, f.Message, "apisix ingress controller started")
+       assert.Equal(t, f.Level, "info")
+
+       f = parseLog(t, buf)
+       assert.Contains(t, f.Message, "use configuration")
+       assert.Equal(t, f.Level, "info")
+
+       var cfg config.Config
+       data := strings.TrimPrefix(f.Message, "use configuration\n")
+       err = json.Unmarshal([]byte(data), &cfg)
+       assert.Nil(t, err)
+
+       assert.Equal(t, cfg.LogOutput, "./test.log")
+       assert.Equal(t, cfg.LogLevel, "debug")
+       assert.Equal(t, cfg.HTTPListen, "127.0.0.1:16780")
+       assert.Equal(t, cfg.EnableProfiling, true)
+       assert.Equal(t, cfg.Kubernetes.Kubeconfig, "/foo/bar/baz")
+       assert.Equal(t, cfg.Kubernetes.ResyncInterval, types.TimeDuration{24 * 
time.Hour})
+       assert.Equal(t, cfg.APISIX.AdminKey, "0x123")
+       assert.Equal(t, cfg.APISIX.BaseURL, 
"http://apisixgw.default.cluster.local/apisix";)
+}
+
+func parseLog(t *testing.T, r *bufio.Reader) *fields {
+       line, isPrefix, err := r.ReadLine()
+       assert.False(t, isPrefix)
+       assert.Nil(t, err)
+
+       var f fields
+       err = json.Unmarshal(line, &f)
+       assert.Nil(t, err)
+       return &f
 }
diff --git a/conf/config-default.yaml b/conf/config-default.yaml
index d3af0b2..d2466f9 100644
--- a/conf/config-default.yaml
+++ b/conf/config-default.yaml
@@ -14,7 +14,7 @@
 # limitations under the License.
 
 # log options
-log_level: "warn"    # the error log level, default is warn, optional values 
are:
+log_level: "info"    # the error log level, default is info, optional values 
are:
                      # debug
                      # info
                      # warn
diff --git a/pkg/log/logger.go b/pkg/log/logger.go
index 51cf6fa..0457790 100644
--- a/pkg/log/logger.go
+++ b/pkg/log/logger.go
@@ -242,7 +242,7 @@ func NewLogger(opts ...Option) (*Logger, error) {
                } else if o.outputFile == "stderr" {
                        writer = os.Stderr
                } else {
-                       file, err := os.OpenFile(o.outputFile, 
os.O_WRONLY|os.O_APPEND, 0644)
+                       file, err := os.OpenFile(o.outputFile, 
os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
                        if err != nil {
                                return nil, err
                        }

Reply via email to