From 40b64f27f8cfeccd1c7166ef98e278fecdab94a8 Mon Sep 17 00:00:00 2001 From: chaodaiG <45011425+chaodaiG@users.noreply.github.com> Date: Wed, 27 Mar 2019 10:41:52 -0700 Subject: [PATCH] refactor e2e test (#1093) --- test/e2e/sampleapp_test.go | 14 +++++++++----- test/e2e/sampleapptestbase.go | 14 +++++++++----- test/{e2e => }/flag.go | 4 +--- test/{e2e => sampleapp}/config.go | 30 ++++++++++++++++------------- test/{e2e => sampleapp}/config.yaml | 0 5 files changed, 36 insertions(+), 26 deletions(-) rename test/{e2e => }/flag.go (98%) rename test/{e2e => sampleapp}/config.go (74%) rename test/{e2e => sampleapp}/config.yaml (100%) diff --git a/test/e2e/sampleapp_test.go b/test/e2e/sampleapp_test.go index fe7dc3e9b..36c119eec 100644 --- a/test/e2e/sampleapp_test.go +++ b/test/e2e/sampleapp_test.go @@ -21,22 +21,26 @@ package e2etest import ( "strings" "testing" + + "github.com/knative/docs/test/sampleapp" + + "github.com/knative/docs/test" ) const ( - configFile = "config.yaml" + configFile = "../sampleapp/config.yaml" ) // TestSampleApp runs all sample apps from different languages func TestSampleApp(t *testing.T) { - lcs, err := getConfigs(configFile) + lcs, err := sampleapp.GetConfigs(configFile) if nil != err { t.Fatalf("Failed reading config file %s: '%v'", configFile, err) } whitelist := make(map[string]bool) - if "" != Flags.Languages { - for _, l := range strings.Split(Flags.Languages, ",") { + if "" != test.Flags.Languages { + for _, l := range strings.Split(test.Flags.Languages, ",") { whitelist[l] = true } } @@ -44,7 +48,7 @@ func TestSampleApp(t *testing.T) { if _, ok := whitelist[lc.Language]; len(whitelist) > 0 && !ok { continue } - lc.useDefaultIfNotProvided() + lc.UseDefaultIfNotProvided() t.Run(lc.Language, func(t *testing.T) { SampleAppTestBase(t, lc, lc.ExpectedOutput) }) diff --git a/test/e2e/sampleapptestbase.go b/test/e2e/sampleapptestbase.go index e3212ef49..e71ef364f 100644 --- a/test/e2e/sampleapptestbase.go +++ b/test/e2e/sampleapptestbase.go @@ -29,6 +29,10 @@ import ( "strings" "testing" "time" + + "github.com/knative/docs/test/sampleapp" + + "github.com/knative/docs/test" ) const ( @@ -73,7 +77,7 @@ func ingressAddress(gateway string, addressType string) string { "-o", fmt.Sprintf("jsonpath={.status.loadBalancer.ingress[*]['%s']}", addressType)) } -func prepareWorkDir(t *testing.T, srcDir, workDir string, preCommands []command, copies []string, postCommands []command) { +func prepareWorkDir(t *testing.T, srcDir, workDir string, preCommands []sampleapp.Command, copies []string, postCommands []sampleapp.Command) { t.Log("Prepare source project") err := os.RemoveAll(workDir) // this function returns nil if path not found if nil == err { @@ -86,7 +90,7 @@ func prepareWorkDir(t *testing.T, srcDir, workDir string, preCommands []command, } for _, c := range preCommands { - c.run(t) + c.Run(t) } for _, f := range copies { src := path.Join(srcDir, f) @@ -98,7 +102,7 @@ func prepareWorkDir(t *testing.T, srcDir, workDir string, preCommands []command, } } for _, c := range postCommands { - c.run(t) + c.Run(t) } } @@ -175,9 +179,9 @@ func checkDeployment(t *testing.T, appName, expectedOutput string) { } // SampleAppTestBase tests individual sample app -func SampleAppTestBase(t *testing.T, lc languageConfig, expectedOutput string) { +func SampleAppTestBase(t *testing.T, lc sampleapp.LanguageConfig, expectedOutput string) { t.Parallel() - imagePath := ImagePath(lc.AppName) + imagePath := test.ImagePath(lc.AppName) yamlFilePath := path.Join(lc.WorkDir, "service.yaml") CleanupOnInterrupt(func() { cleanup(yamlFilePath, lc.WorkDir) }) diff --git a/test/e2e/flag.go b/test/flag.go similarity index 98% rename from test/e2e/flag.go rename to test/flag.go index f65730c4b..5db7e49ee 100644 --- a/test/e2e/flag.go +++ b/test/flag.go @@ -1,5 +1,3 @@ -// +build e2e - /* Copyright 2019 The Knative Authors @@ -16,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package e2etest +package test import ( "flag" diff --git a/test/e2e/config.go b/test/sampleapp/config.go similarity index 74% rename from test/e2e/config.go rename to test/sampleapp/config.go index 684c13ce8..a5a6f675e 100644 --- a/test/e2e/config.go +++ b/test/sampleapp/config.go @@ -1,5 +1,3 @@ -// +build e2e - /* Copyright 2019 The Knative Authors @@ -16,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package e2etest +package sampleapp import ( "fmt" @@ -36,28 +34,32 @@ const ( defaultYamlImagePlaceHolder = "docker.io/{username}/helloworld-%s" ) -type allConfigs struct { - Languages []languageConfig `yaml:"languages` +// AllConfigs contains all LanguageConfig +type AllConfigs struct { + Languages []LanguageConfig `yaml:"languages` } -type languageConfig struct { +// LanguageConfig contains all information for building/deploying an app +type LanguageConfig struct { Language string `yaml:"language"` ExpectedOutput string `yaml:"expectedOutput"` SrcDir string `yaml:"srcDir"` // Directory contains sample code WorkDir string `yaml:"workDir"` // Temp work directory AppName string `yaml:"appName"` YamlImagePlaceholder string `yaml:"yamlImagePlaceholder"` // Token to be replaced by real docker image URI - PreCommands []command `yaml:"preCommands"` // Commands to be ran before copying + PreCommands []Command `yaml:"preCommands"` // Commands to be ran before copying Copies []string `yaml:"copies"` // Files to be copied from SrcDir to WorkDir - PostCommands []command `yaml:"postCommands"` // Commands to be ran after copying + PostCommands []Command `yaml:"postCommands"` // Commands to be ran after copying } -type command struct { +// Command contains shell commands +type Command struct { Exec string `yaml:"exec"` Args string `yaml:"args"` } -func (lc *languageConfig) useDefaultIfNotProvided() { +// UseDefaultIfNotProvided sets default value to SrcDir, WorkDir, AppName, and YamlImagePlaceholder if not provided +func (lc *LanguageConfig) UseDefaultIfNotProvided() { if "" == lc.SrcDir { lc.SrcDir = fmt.Sprintf(defaultSrcDir, lc.Language) } @@ -72,15 +74,17 @@ func (lc *languageConfig) useDefaultIfNotProvided() { } } -func (c *command) run(t *testing.T) { +// Run runs command and fail if it failed +func (c *Command) Run(t *testing.T) { args := strings.Split(c.Args, " ") if output, err := exec.Command(c.Exec, args...).CombinedOutput(); err != nil { t.Fatalf("Error executing: '%s' '%s' -err: '%v'", c.Exec, c.Args, strings.TrimSpace(string(output))) } } -func getConfigs(configPath string) (allConfigs, error) { - var lcs allConfigs +// GetConfigs parses a config yaml file and return AllConfigs struct +func GetConfigs(configPath string) (AllConfigs, error) { + var lcs AllConfigs content, err := ioutil.ReadFile(configPath) if nil == err { err = yaml.Unmarshal(content, &lcs) diff --git a/test/e2e/config.yaml b/test/sampleapp/config.yaml similarity index 100% rename from test/e2e/config.yaml rename to test/sampleapp/config.yaml