You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ke...@apache.org on 2021/02/07 06:59:49 UTC

[skywalking-infra-e2e] branch main updated: Add http action to trigger command (#4)

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

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


The following commit(s) were added to refs/heads/main by this push:
     new a945745  Add http action to trigger command (#4)
a945745 is described below

commit a945745b9e1ff3ae179e0c242fd1100b9f02d474
Author: 何延龙 <ya...@gmail.com>
AuthorDate: Sun Feb 7 14:59:41 2021 +0800

    Add http action to trigger command (#4)
---
 commands/trigger/{trigger.go => action.go}         | 18 ++---
 commands/trigger/http.go                           | 78 ++++++++++++++++++++++
 commands/trigger/trigger.go                        | 22 +++++-
 .../trigger.go => internal/constant/trigget.go     | 19 ++----
 {commands/trigger => internal/flags}/trigger.go    | 22 +++---
 5 files changed, 115 insertions(+), 44 deletions(-)

diff --git a/commands/trigger/trigger.go b/commands/trigger/action.go
similarity index 79%
copy from commands/trigger/trigger.go
copy to commands/trigger/action.go
index 4c39d2c..125a966 100644
--- a/commands/trigger/trigger.go
+++ b/commands/trigger/action.go
@@ -1,4 +1,3 @@
-//
 // 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
@@ -15,19 +14,10 @@
 // KIND, either express or implied.  See the License for the
 // specific language governing permissions and limitations
 // under the License.
-package trigger
-
-import (
-	"fmt"
+//
 
-	"github.com/spf13/cobra"
-)
+package trigger
 
-var Trigger = &cobra.Command{
-	Use:   "trigger",
-	Short: "",
-	RunE: func(cmd *cobra.Command, args []string) error {
-		fmt.Println("Not implemented.")
-		return nil
-	},
+type Action interface {
+	Do() error
 }
diff --git a/commands/trigger/http.go b/commands/trigger/http.go
new file mode 100644
index 0000000..4e9fee2
--- /dev/null
+++ b/commands/trigger/http.go
@@ -0,0 +1,78 @@
+//
+// 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 trigger
+
+import (
+	"net/http"
+	"strings"
+	"time"
+
+	"github.com/apache/skywalking-infra-e2e/internal/flags"
+	"github.com/apache/skywalking-infra-e2e/internal/logger"
+)
+
+type httpAction struct {
+	interval time.Duration
+	times    int
+	url      string
+	method   string
+}
+
+func NewHTTPAction() Action {
+	interval, err := time.ParseDuration(flags.Interval)
+	if err != nil {
+		interval = time.Second
+	}
+	return &httpAction{
+		interval: interval,
+		times:    flags.Times,
+		url:      flags.URL,
+		method:   strings.ToUpper(flags.Method),
+	}
+}
+
+func (h *httpAction) Do() error {
+	t := time.NewTicker(h.interval)
+	c := 1
+	client := &http.Client{}
+	request, err := http.NewRequest(h.method, h.url, nil)
+	if err != nil {
+		logger.Log.Errorf("new request error %v", err)
+		return err
+	}
+
+	for range t.C {
+		response, err := client.Do(request)
+		if err != nil {
+			logger.Log.Errorf("do request error %v", err)
+			return err
+		}
+		response.Body.Close()
+
+		logger.Log.Infof("do request %v response http code %v", h.url, response.StatusCode)
+
+		if h.times > 0 {
+			if h.times <= c {
+				break
+			}
+			c++
+		}
+	}
+
+	return nil
+}
diff --git a/commands/trigger/trigger.go b/commands/trigger/trigger.go
index 4c39d2c..ce266cd 100644
--- a/commands/trigger/trigger.go
+++ b/commands/trigger/trigger.go
@@ -19,15 +19,33 @@ package trigger
 
 import (
 	"fmt"
+	"strings"
 
 	"github.com/spf13/cobra"
+
+	"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 {
-		fmt.Println("Not implemented.")
-		return nil
+		var action Action
+		if strings.EqualFold(flags.Action, constant.ActionHTTP) {
+			action = NewHTTPAction()
+		}
+		if action == nil {
+			return fmt.Errorf("no such action for args")
+		}
+		return action.Do()
 	},
 }
diff --git a/commands/trigger/trigger.go b/internal/constant/trigget.go
similarity index 78%
copy from commands/trigger/trigger.go
copy to internal/constant/trigget.go
index 4c39d2c..7fcbc79 100644
--- a/commands/trigger/trigger.go
+++ b/internal/constant/trigget.go
@@ -1,4 +1,3 @@
-//
 // 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
@@ -15,19 +14,11 @@
 // KIND, either express or implied.  See the License for the
 // specific language governing permissions and limitations
 // under the License.
-package trigger
+//
 
-import (
-	"fmt"
+package constant
 
-	"github.com/spf13/cobra"
+const (
+	ActionHTTP = "http"
+	ActionCMD  = "cmd"
 )
-
-var Trigger = &cobra.Command{
-	Use:   "trigger",
-	Short: "",
-	RunE: func(cmd *cobra.Command, args []string) error {
-		fmt.Println("Not implemented.")
-		return nil
-	},
-}
diff --git a/commands/trigger/trigger.go b/internal/flags/trigger.go
similarity index 78%
copy from commands/trigger/trigger.go
copy to internal/flags/trigger.go
index 4c39d2c..05865d7 100644
--- a/commands/trigger/trigger.go
+++ b/internal/flags/trigger.go
@@ -1,4 +1,3 @@
-//
 // 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
@@ -15,19 +14,14 @@
 // KIND, either express or implied.  See the License for the
 // specific language governing permissions and limitations
 // under the License.
-package trigger
+//
 
-import (
-	"fmt"
+package flags
 
-	"github.com/spf13/cobra"
+var (
+	Interval string
+	Times    int
+	Action   string
+	URL      string
+	Method   string
 )
-
-var Trigger = &cobra.Command{
-	Use:   "trigger",
-	Short: "",
-	RunE: func(cmd *cobra.Command, args []string) error {
-		fmt.Println("Not implemented.")
-		return nil
-	},
-}