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 02e18db  Fix relative paths to e2e.yaml (#11)
02e18db is described below

commit 02e18dbb3d06f47c5722d01ca4ead13abce59893
Author: Zhenxu Ke <[email protected]>
AuthorDate: Fri Mar 5 15:55:52 2021 +0800

    Fix relative paths to e2e.yaml (#11)
    
    Co-authored-by: HumbertZhang <[email protected]>
---
 commands/root.go                                   |  8 ++--
 commands/setup/setup.go                            |  7 ----
 commands/verify/verify.go                          |  4 +-
 examples/simple/e2e.yaml                           |  2 +-
 examples/simple/manifests2/busybox4.yaml           | 30 ++++++++++++++
 internal/components/cleanup/kind.go                |  7 ++--
 internal/components/setup/kind.go                  |  6 +--
 internal/config/e2eConfig.go                       | 20 +++++++++
 internal/config/globalConfig.go                    | 14 +++----
 .../{config/globalConfig.go => util/config.go}     | 47 ++++++++--------------
 internal/util/k8s.go                               |  9 +++--
 11 files changed, 91 insertions(+), 63 deletions(-)

diff --git a/commands/root.go b/commands/root.go
index d378f0c..8b5fe6c 100644
--- a/commands/root.go
+++ b/commands/root.go
@@ -20,6 +20,8 @@ package commands
 import (
        "github.com/spf13/cobra"
 
+       "github.com/apache/skywalking-infra-e2e/internal/util"
+
        "github.com/apache/skywalking-infra-e2e/commands/cleanup"
        "github.com/apache/skywalking-infra-e2e/commands/run"
        "github.com/apache/skywalking-infra-e2e/commands/setup"
@@ -29,8 +31,6 @@ import (
        "github.com/apache/skywalking-infra-e2e/internal/constant"
 )
 
-var cfg string
-
 // Root represents the base command when called without any subcommands
 var Root = &cobra.Command{
        Use:           "e2e command [flags]",
@@ -39,7 +39,7 @@ var Root = &cobra.Command{
        SilenceErrors: true,
        SilenceUsage:  true,
        PersistentPreRun: func(cmd *cobra.Command, args []string) {
-               config.ReadGlobalConfigFile(cfg)
+               config.ReadGlobalConfigFile()
        },
 }
 
@@ -52,7 +52,7 @@ func Execute() error {
        Root.AddCommand(verify.Verify)
        Root.AddCommand(cleanup.Cleanup)
 
-       Root.PersistentFlags().StringVarP(&cfg, "config", "c", 
constant.E2EDefaultFile, "the config file")
+       Root.PersistentFlags().StringVarP(&util.CfgFile, "config", "c", 
constant.E2EDefaultFile, "the config file")
 
        return Root.Execute()
 }
diff --git a/commands/setup/setup.go b/commands/setup/setup.go
index a3eb3b9..146644f 100644
--- a/commands/setup/setup.go
+++ b/commands/setup/setup.go
@@ -25,7 +25,6 @@ import (
        "github.com/apache/skywalking-infra-e2e/internal/config"
        "github.com/apache/skywalking-infra-e2e/internal/constant"
        "github.com/apache/skywalking-infra-e2e/internal/logger"
-       "github.com/apache/skywalking-infra-e2e/internal/util"
 
        "github.com/spf13/cobra"
 )
@@ -33,12 +32,6 @@ import (
 var Setup = &cobra.Command{
        Use:   "setup",
        Short: "",
-       PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
-               if err := util.CheckDockerDaemon(); err != nil {
-                       return err
-               }
-               return nil
-       },
        RunE: func(cmd *cobra.Command, args []string) error {
                if err := setupAccordingE2E(); err != nil {
                        return fmt.Errorf("[Setup] %s", err)
diff --git a/commands/verify/verify.go b/commands/verify/verify.go
index 05ccbe0..0e700b9 100644
--- a/commands/verify/verify.go
+++ b/commands/verify/verify.go
@@ -93,8 +93,8 @@ func verifyAccordingConfig() error {
        e2eConfig := config.GlobalConfig.E2EConfig
 
        for _, v := range e2eConfig.Verify {
-               if v.Expected != "" {
-                       if err := verifySingleCase(v.Expected, v.Actual, 
v.Query); err != nil {
+               if v.GetExpected() != "" {
+                       if err := verifySingleCase(v.GetExpected(), 
v.GetActual(), v.Query); err != nil {
                                logger.Log.Errorf("%v", err)
                        }
                } else {
diff --git a/examples/simple/e2e.yaml b/examples/simple/e2e.yaml
index 721d446..480099b 100644
--- a/examples/simple/e2e.yaml
+++ b/examples/simple/e2e.yaml
@@ -24,7 +24,7 @@ setup:
         - namespace: default
           resource: pod/busybox1 
           for: condition=Ready
-    - path: manifests
+    - path: manifests,manifests2
       wait:
         - namespace: default
           resource: pod
diff --git a/examples/simple/manifests2/busybox4.yaml 
b/examples/simple/manifests2/busybox4.yaml
new file mode 100644
index 0000000..08e5759
--- /dev/null
+++ b/examples/simple/manifests2/busybox4.yaml
@@ -0,0 +1,30 @@
+# Licensed to the 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.
+# The 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.
+
+apiVersion: v1
+kind: Pod
+metadata:
+  name: busybox4
+  labels:
+    app: busybox4
+spec:
+  containers:
+  - image: busybox
+    command:
+      - sleep
+      - "3600"
+    imagePullPolicy: IfNotPresent
+    name: busybox
+  restartPolicy: Always
diff --git a/internal/components/cleanup/kind.go 
b/internal/components/cleanup/kind.go
index 2fc4de2..1e19072 100644
--- a/internal/components/cleanup/kind.go
+++ b/internal/components/cleanup/kind.go
@@ -39,9 +39,9 @@ type KindClusterNameConfig struct {
 }
 
 func KindCleanUp(e2eConfig *config.E2EConfig) error {
-       kindConfigFilePath := e2eConfig.Setup.File
+       kindConfigFilePath := e2eConfig.Setup.GetFile()
 
-       logger.Log.Info("deleting kind cluster...")
+       logger.Log.Infof("deleting kind cluster...\n")
        if err := cleanKindCluster(kindConfigFilePath); err != nil {
                logger.Log.Error("delete kind cluster failed")
                return err
@@ -52,8 +52,7 @@ func KindCleanUp(e2eConfig *config.E2EConfig) error {
        logger.Log.Infof("deleting k8s cluster config file:%s", kubeConfigPath)
        err := os.Remove(kubeConfigPath)
        if err != nil {
-               logger.Log.Errorf("delete k8s cluster config file failed")
-               return err
+               logger.Log.Infoln("delete k8s cluster config file failed")
        }
 
        return nil
diff --git a/internal/components/setup/kind.go 
b/internal/components/setup/kind.go
index 9f7b156..2b6c100 100644
--- a/internal/components/setup/kind.go
+++ b/internal/components/setup/kind.go
@@ -52,7 +52,7 @@ var (
 
 // KindSetup sets up environment according to e2e.yaml.
 func KindSetup(e2eConfig *config.E2EConfig) error {
-       kindConfigPath = e2eConfig.Setup.File
+       kindConfigPath = e2eConfig.Setup.GetFile()
 
        if kindConfigPath == "" {
                return fmt.Errorf("no kind config file was provided")
@@ -187,9 +187,9 @@ func createManifestsAndWait(c *kubernetes.Clientset, dc 
dynamic.Interface, manif
 }
 
 func createByManifest(c *kubernetes.Clientset, dc dynamic.Interface, manifest 
config.Manifest) error {
-       files, err := util.GetManifests(manifest.Path)
+       files, err := util.GetManifests(manifest.GetPath())
        if err != nil {
-               logger.Log.Error("get manifests from command line argument 
failed")
+               logger.Log.Error("get manifests failed")
                return err
        }
 
diff --git a/internal/config/e2eConfig.go b/internal/config/e2eConfig.go
index c242947..7b54aaf 100644
--- a/internal/config/e2eConfig.go
+++ b/internal/config/e2eConfig.go
@@ -18,6 +18,8 @@
 
 package config
 
+import "github.com/apache/skywalking-infra-e2e/internal/util"
+
 // E2EConfig corresponds to configuration file e2e.yaml.
 type E2EConfig struct {
        Setup  Setup        `yaml:"setup"`
@@ -33,11 +35,19 @@ type Setup struct {
        Timeout int   `yaml:"timeout"`
 }
 
+func (s *Setup) GetFile() string {
+       return util.ResolveAbs(s.File)
+}
+
 type Manifest struct {
        Path  string `yaml:"path"`
        Waits []Wait `yaml:"wait"`
 }
 
+func (m Manifest) GetPath() string {
+       return m.Path
+}
+
 type Run struct {
        Command string `yaml:"command"`
        Waits   []Wait `yaml:"wait"`
@@ -55,3 +65,13 @@ type VerifyCase struct {
        Actual   string `yaml:"actual"`
        Expected string `yaml:"expected"`
 }
+
+// GetActual resolves the absolute file path of the actual data file.
+func (v *VerifyCase) GetActual() string {
+       return util.ResolveAbs(v.Actual)
+}
+
+// GetExpected resolves the absolute file path of the expected data file.
+func (v *VerifyCase) GetExpected() string {
+       return util.ResolveAbs(v.Expected)
+}
diff --git a/internal/config/globalConfig.go b/internal/config/globalConfig.go
index c796962..fb72c9c 100644
--- a/internal/config/globalConfig.go
+++ b/internal/config/globalConfig.go
@@ -36,23 +36,21 @@ type GlobalE2EConfig struct {
 
 var GlobalConfig GlobalE2EConfig
 
-func ReadGlobalConfigFile(configFilePath string) {
-       e2eFile := configFilePath
-
-       if !util.PathExist(e2eFile) {
-               GlobalConfig.Error = fmt.Errorf("e2e config file %s not exist", 
e2eFile)
+func ReadGlobalConfigFile() {
+       if !util.PathExist(util.CfgFile) {
+               GlobalConfig.Error = fmt.Errorf("e2e config file %s not exist", 
util.CfgFile)
                return
        }
 
-       data, err := ioutil.ReadFile(e2eFile)
+       data, err := ioutil.ReadFile(util.CfgFile)
        if err != nil {
-               GlobalConfig.Error = fmt.Errorf("read e2e config file %s error: 
%s", e2eFile, err)
+               GlobalConfig.Error = fmt.Errorf("read e2e config file %s error: 
%s", util.CfgFile, err)
                return
        }
 
        e2eConfigObject := E2EConfig{}
        if err := yaml.Unmarshal(data, &e2eConfigObject); err != nil {
-               GlobalConfig.Error = fmt.Errorf("unmarshal e2e config file %s 
error: %s", e2eFile, err)
+               GlobalConfig.Error = fmt.Errorf("unmarshal e2e config file %s 
error: %s", util.CfgFile, err)
                return
        }
 
diff --git a/internal/config/globalConfig.go b/internal/util/config.go
similarity index 50%
copy from internal/config/globalConfig.go
copy to internal/util/config.go
index c796962..15e38e1 100644
--- a/internal/config/globalConfig.go
+++ b/internal/util/config.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,48 +14,34 @@
 // KIND, either express or implied.  See the License for the
 // specific language governing permissions and limitations
 // under the License.
+//
 
-package config
+package util
 
 import (
-       "fmt"
-       "io/ioutil"
+       "path"
+       "path/filepath"
 
        "github.com/apache/skywalking-infra-e2e/internal/logger"
-       "github.com/apache/skywalking-infra-e2e/internal/util"
-
-       "gopkg.in/yaml.v2"
 )
 
-// GlobalE2EConfig store E2EConfig which can be used globally.
-type GlobalE2EConfig struct {
-       Error     error
-       E2EConfig E2EConfig
-}
-
-var GlobalConfig GlobalE2EConfig
-
-func ReadGlobalConfigFile(configFilePath string) {
-       e2eFile := configFilePath
+var CfgFile string
 
-       if !util.PathExist(e2eFile) {
-               GlobalConfig.Error = fmt.Errorf("e2e config file %s not exist", 
e2eFile)
-               return
+// ResolveAbs resolves the relative path (relative to CfgFile) to an absolute 
file path.
+func ResolveAbs(p string) string {
+       if p == "" {
+               return p
        }
 
-       data, err := ioutil.ReadFile(e2eFile)
-       if err != nil {
-               GlobalConfig.Error = fmt.Errorf("read e2e config file %s error: 
%s", e2eFile, err)
-               return
+       if path.IsAbs(p) {
+               return p
        }
 
-       e2eConfigObject := E2EConfig{}
-       if err := yaml.Unmarshal(data, &e2eConfigObject); err != nil {
-               GlobalConfig.Error = fmt.Errorf("unmarshal e2e config file %s 
error: %s", e2eFile, err)
-               return
+       abs, err := filepath.Abs(CfgFile)
+       if err != nil {
+               logger.Log.Warnf("failed to resolve the absolute file path of 
%v\n", CfgFile)
+               return p
        }
 
-       GlobalConfig.E2EConfig = e2eConfigObject
-       GlobalConfig.Error = nil
-       logger.Log.Info("load the e2e config successfully")
+       return filepath.Join(filepath.Dir(abs), p)
 }
diff --git a/internal/util/k8s.go b/internal/util/k8s.go
index 04acf71..9a1f435 100644
--- a/internal/util/k8s.go
+++ b/internal/util/k8s.go
@@ -71,6 +71,7 @@ func GetManifests(manifests string) (files []string, err 
error) {
        files = strings.Split(manifests, ",")
        // file or directory
        for _, f := range files {
+               f = ResolveAbs(f)
                fi, err := os.Stat(f)
                if err != nil {
                        return nil, err
@@ -78,12 +79,13 @@ func GetManifests(manifests string) (files []string, err 
error) {
 
                switch mode := fi.Mode(); {
                case mode.IsDir():
-                       err := filepath.Walk(fi.Name(), func(path string, info 
os.FileInfo, err error) error {
+                       err := filepath.Walk(f, func(path string, info 
os.FileInfo, err error) error {
                                if err != nil {
                                        return err
                                }
-                               filename := info.Name()
-                               if strings.HasSuffix(filename, ".yml") || 
strings.HasSuffix(filename, ".yaml") {
+
+                               if strings.HasSuffix(path, ".yml") || 
strings.HasSuffix(path, ".yaml") {
+                                       path = ResolveAbs(path)
                                        s = append(s, path)
                                }
                                return nil
@@ -94,6 +96,7 @@ func GetManifests(manifests string) (files []string, err 
error) {
                case mode.IsRegular():
                        filename := fi.Name()
                        if strings.HasSuffix(filename, ".yml") || 
strings.HasSuffix(filename, ".yaml") {
+                               filename = ResolveAbs(filename)
                                s = append(s, filename)
                        }
                }

Reply via email to