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

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

commit b4970a8deb369df5696160eccc97147a719dba2c
Author: kezhenxu94 <[email protected]>
AuthorDate: Thu Mar 4 23:04:51 2021 +0800

    Fix relative paths to e2e.yaml
---
 commands/root.go                    |  6 ++----
 commands/setup/setup.go             |  7 -------
 commands/verify/verify.go           |  4 ++--
 internal/components/cleanup/kind.go |  7 +++----
 internal/components/setup/kind.go   |  4 ++--
 internal/config/e2eConfig.go        | 20 +++++++++++++++++++-
 internal/config/globalConfig.go     | 36 ++++++++++++++++++++++++++++--------
 7 files changed, 56 insertions(+), 28 deletions(-)

diff --git a/commands/root.go b/commands/root.go
index d378f0c..8616080 100644
--- a/commands/root.go
+++ b/commands/root.go
@@ -29,8 +29,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 +37,7 @@ var Root = &cobra.Command{
        SilenceErrors: true,
        SilenceUsage:  true,
        PersistentPreRun: func(cmd *cobra.Command, args []string) {
-               config.ReadGlobalConfigFile(cfg)
+               config.ReadGlobalConfigFile()
        },
 }
 
@@ -52,7 +50,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(&config.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/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..4c7a715 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,7 +187,7 @@ 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")
                return err
diff --git a/internal/config/e2eConfig.go b/internal/config/e2eConfig.go
index c242947..d2daed6 100644
--- a/internal/config/e2eConfig.go
+++ b/internal/config/e2eConfig.go
@@ -33,11 +33,19 @@ type Setup struct {
        Timeout int   `yaml:"timeout"`
 }
 
+func (s *Setup) GetFile() string {
+       return ResolveAbs(s.File)
+}
+
 type Manifest struct {
-       Path  string `yaml:"path"`
+       path  string `yaml:"path"`
        Waits []Wait `yaml:"wait"`
 }
 
+func (m Manifest) GetPath() string {
+       return ResolveAbs(m.path)
+}
+
 type Run struct {
        Command string `yaml:"command"`
        Waits   []Wait `yaml:"wait"`
@@ -55,3 +63,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 ResolveAbs(v.Actual)
+}
+
+// GetExpected resolves the absolute file path of the expected data file.
+func (v *VerifyCase) GetExpected() string {
+       return ResolveAbs(v.Expected)
+}
diff --git a/internal/config/globalConfig.go b/internal/config/globalConfig.go
index c796962..316c54c 100644
--- a/internal/config/globalConfig.go
+++ b/internal/config/globalConfig.go
@@ -21,6 +21,8 @@ package config
 import (
        "fmt"
        "io/ioutil"
+       "path"
+       "path/filepath"
 
        "github.com/apache/skywalking-infra-e2e/internal/logger"
        "github.com/apache/skywalking-infra-e2e/internal/util"
@@ -35,24 +37,23 @@ type GlobalE2EConfig struct {
 }
 
 var GlobalConfig GlobalE2EConfig
+var CfgFile string
 
-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(CfgFile) {
+               GlobalConfig.Error = fmt.Errorf("e2e config file %s not exist", 
CfgFile)
                return
        }
 
-       data, err := ioutil.ReadFile(e2eFile)
+       data, err := ioutil.ReadFile(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", 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", CfgFile, err)
                return
        }
 
@@ -60,3 +61,22 @@ func ReadGlobalConfigFile(configFilePath string) {
        GlobalConfig.Error = nil
        logger.Log.Info("load the e2e config successfully")
 }
+
+// ResolveAbs resolves the relative path (relative to CfgFile) to an absolute 
file path.
+func ResolveAbs(p string) string {
+       if p == "" {
+               return p
+       }
+
+       if path.IsAbs(p) {
+               return p
+       }
+
+       abs, err := filepath.Abs(CfgFile)
+       if err != nil {
+               logger.Log.Warnf("failed to resolve the absolute file path of 
%v\n", CfgFile)
+               return p
+       }
+
+       return filepath.Join(filepath.Dir(abs), p)
+}

Reply via email to