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

zhangke pushed a commit to branch trigger/e2e
in repository https://gitbox.apache.org/repos/asf/skywalking-infra-e2e.git

commit af8751634705ee7b757165ffc288023041f539bf
Author: HumbertZhang <[email protected]>
AuthorDate: Sun Mar 14 20:19:17 2021 +0800

    refactor trigger part. it uses e2e config file now
---
 commands/run/run.go                                | 29 ++++++++++++-
 commands/setup/setup.go                            |  4 +-
 commands/trigger/trigger.go                        | 48 ++++++++++++++--------
 examples/simple/e2e.yaml                           |  7 ++++
 .../components}/trigger/action.go                  |  0
 {commands => internal/components}/trigger/http.go  | 31 ++++++++++----
 internal/config/e2eConfig.go                       | 13 +++++-
 internal/constant/{trigget.go => trigger.go}       |  0
 internal/flags/trigger.go                          | 27 ------------
 9 files changed, 102 insertions(+), 57 deletions(-)

diff --git a/commands/run/run.go b/commands/run/run.go
index 97ec44b..5827d9d 100644
--- a/commands/run/run.go
+++ b/commands/run/run.go
@@ -18,7 +18,9 @@
 package run
 
 import (
-       "fmt"
+       "github.com/apache/skywalking-infra-e2e/commands/setup"
+       "github.com/apache/skywalking-infra-e2e/internal/config"
+       "github.com/apache/skywalking-infra-e2e/internal/logger"
 
        "github.com/spf13/cobra"
 )
@@ -27,7 +29,30 @@ var Run = &cobra.Command{
        Use:   "run",
        Short: "",
        RunE: func(cmd *cobra.Command, args []string) error {
-               fmt.Println("Not implemented.")
+               err := runAccordingE2E()
+               if err != nil {
+                       return err
+               }
+
                return nil
        },
 }
+
+func runAccordingE2E() error {
+       if config.GlobalConfig.Error != nil {
+               return config.GlobalConfig.Error
+       }
+
+       // setup part
+       err := setup.DoSetupAccordingE2E()
+       if err != nil {
+               return err
+       }
+       logger.Log.Infof("setup part finished successfully")
+
+       // trigger part
+
+       // verify part
+
+       return nil
+}
diff --git a/commands/setup/setup.go b/commands/setup/setup.go
index 3490d37..a95f8f2 100644
--- a/commands/setup/setup.go
+++ b/commands/setup/setup.go
@@ -39,14 +39,14 @@ var Setup = &cobra.Command{
                        return err
                }
 
-               if err := setupAccordingE2E(); err != nil {
+               if err := DoSetupAccordingE2E(); err != nil {
                        return fmt.Errorf("[Setup] %s", err)
                }
                return nil
        },
 }
 
-func setupAccordingE2E() error {
+func DoSetupAccordingE2E() error {
        if config.GlobalConfig.Error != nil {
                return config.GlobalConfig.Error
        }
diff --git a/commands/trigger/trigger.go b/commands/trigger/trigger.go
index ce266cd..7d86776 100644
--- a/commands/trigger/trigger.go
+++ b/commands/trigger/trigger.go
@@ -19,33 +19,49 @@ package trigger
 
 import (
        "fmt"
-       "strings"
 
        "github.com/spf13/cobra"
 
+       "github.com/apache/skywalking-infra-e2e/internal/components/trigger"
+       "github.com/apache/skywalking-infra-e2e/internal/config"
+
        "github.com/apache/skywalking-infra-e2e/internal/constant"
-       "github.com/apache/skywalking-infra-e2e/internal/flags"
 )
 
-func init() {
-       Trigger.Flags().StringVar(&flags.Interval, "interval", "3s", "trigger 
the action every N seconds")
-       Trigger.Flags().IntVar(&flags.Times, "times", 0, "how many times to 
trigger the action, 0=infinite")
-       Trigger.Flags().StringVar(&flags.Action, "action", "", "the action of 
the trigger")
-       Trigger.Flags().StringVar(&flags.URL, "url", "", "the url of the http 
action")
-       Trigger.Flags().StringVar(&flags.Method, "method", "get", "the method 
of the http action")
-}
-
 var Trigger = &cobra.Command{
        Use:   "trigger",
        Short: "",
        RunE: func(cmd *cobra.Command, args []string) error {
-               var action Action
-               if strings.EqualFold(flags.Action, constant.ActionHTTP) {
-                       action = NewHTTPAction()
+               if err := DoActionAccordingE2E(); err != nil {
+                       return fmt.Errorf("[Trigger] %s", err)
                }
+
+               return nil
+       },
+}
+
+func DoActionAccordingE2E() error {
+       if config.GlobalConfig.Error != nil {
+               return config.GlobalConfig.Error
+       }
+
+       e2eConfig := config.GlobalConfig.E2EConfig
+       if e2eConfig.Trigger.Action == constant.ActionHTTP {
+               action := trigger.NewHTTPAction(e2eConfig.Trigger.Interval,
+                       e2eConfig.Trigger.Times,
+                       e2eConfig.Trigger.URL,
+                       e2eConfig.Trigger.Method)
                if action == nil {
-                       return fmt.Errorf("no such action for args")
+                       return fmt.Errorf("trigger [%+v] parse error", 
e2eConfig.Trigger)
                }
-               return action.Do()
-       },
+
+               err := action.Do()
+               if err != nil {
+                       return err
+               }
+       } else {
+               return fmt.Errorf("no such action for trigger: %s", 
e2eConfig.Trigger.Action)
+       }
+
+       return nil
 }
diff --git a/examples/simple/e2e.yaml b/examples/simple/e2e.yaml
index 778718e..e486887 100644
--- a/examples/simple/e2e.yaml
+++ b/examples/simple/e2e.yaml
@@ -51,6 +51,13 @@ setup:
           for: condition=Available
   timeout: 600
 
+trigger:
+  action: http
+  interval: 3s
+  times: 5
+  url: http://localhost:9090/user
+  method: GET
+
 verify:
   - actual: ../../test/verify/1.actual.yaml
     expected: ../../test/verify/1.expected.yaml
diff --git a/commands/trigger/action.go b/internal/components/trigger/action.go
similarity index 100%
rename from commands/trigger/action.go
rename to internal/components/trigger/action.go
diff --git a/commands/trigger/http.go b/internal/components/trigger/http.go
similarity index 69%
rename from commands/trigger/http.go
rename to internal/components/trigger/http.go
index 4e9fee2..b3948cc 100644
--- a/commands/trigger/http.go
+++ b/internal/components/trigger/http.go
@@ -18,11 +18,11 @@
 package trigger
 
 import (
+       "fmt"
        "net/http"
        "strings"
        "time"
 
-       "github.com/apache/skywalking-infra-e2e/internal/flags"
        "github.com/apache/skywalking-infra-e2e/internal/logger"
 )
 
@@ -33,16 +33,23 @@ type httpAction struct {
        method   string
 }
 
-func NewHTTPAction() Action {
-       interval, err := time.ParseDuration(flags.Interval)
+func NewHTTPAction(intervalStr string, times int, url, method string) Action {
+       interval, err := time.ParseDuration(intervalStr)
        if err != nil {
-               interval = time.Second
+               logger.Log.Errorf("interval [%s] parse error: %s.", 
intervalStr, err)
+               return nil
        }
+
+       if interval <= 0 {
+               logger.Log.Errorf("interval [%s] is not positive")
+               return nil
+       }
+
        return &httpAction{
                interval: interval,
-               times:    flags.Times,
-               url:      flags.URL,
-               method:   strings.ToUpper(flags.Method),
+               times:    times,
+               url:      url,
+               method:   strings.ToUpper(method),
        }
 }
 
@@ -56,7 +63,11 @@ func (h *httpAction) Do() error {
                return err
        }
 
+       logger.Log.Infof("Trigger will request URL %s %d times, %s seconds 
apart each time.", h.url, h.times, h.interval)
+
        for range t.C {
+               logger.Log.Debugf("request URL %s the %d time.", h.url, c)
+
                response, err := client.Do(request)
                if err != nil {
                        logger.Log.Errorf("do request error %v", err)
@@ -65,10 +76,14 @@ func (h *httpAction) Do() error {
                response.Body.Close()
 
                logger.Log.Infof("do request %v response http code %v", h.url, 
response.StatusCode)
+               if response.StatusCode == http.StatusOK {
+                       logger.Log.Debugf("do http action %+v success.", *h)
+                       break
+               }
 
                if h.times > 0 {
                        if h.times <= c {
-                               break
+                               return fmt.Errorf("do request %d times, but 
still failed", c)
                        }
                        c++
                }
diff --git a/internal/config/e2eConfig.go b/internal/config/e2eConfig.go
index 9ccbe98..af90953 100644
--- a/internal/config/e2eConfig.go
+++ b/internal/config/e2eConfig.go
@@ -22,8 +22,9 @@ import "github.com/apache/skywalking-infra-e2e/internal/util"
 
 // E2EConfig corresponds to configuration file e2e.yaml.
 type E2EConfig struct {
-       Setup  Setup        `yaml:"setup"`
-       Verify []VerifyCase `yaml:"verify"`
+       Setup   Setup        `yaml:"setup"`
+       Trigger Trigger      `yaml:"trigger"`
+       Verify  []VerifyCase `yaml:"verify"`
 }
 
 type Setup struct {
@@ -59,6 +60,14 @@ type Wait struct {
        For           string `yaml:"for"`
 }
 
+type Trigger struct {
+       Action   string `yaml:"action"`
+       Interval string `yaml:"interval"`
+       Times    int    `yaml:"times"`
+       URL      string `yaml:"url"`
+       Method   string `yaml:"method"`
+}
+
 type VerifyCase struct {
        Query    string `yaml:"query"`
        Actual   string `yaml:"actual"`
diff --git a/internal/constant/trigget.go b/internal/constant/trigger.go
similarity index 100%
rename from internal/constant/trigget.go
rename to internal/constant/trigger.go
diff --git a/internal/flags/trigger.go b/internal/flags/trigger.go
deleted file mode 100644
index 05865d7..0000000
--- a/internal/flags/trigger.go
+++ /dev/null
@@ -1,27 +0,0 @@
-// Licensed to Apache Software Foundation (ASF) under one or more contributor
-// license agreements. See the NOTICE file distributed with
-// this work for additional information regarding copyright
-// ownership. Apache Software Foundation (ASF) licenses this file to you 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 flags
-
-var (
-       Interval string
-       Times    int
-       Action   string
-       URL      string
-       Method   string
-)

Reply via email to