refactor e2e test (#1093)

This commit is contained in:
chaodaiG 2019-03-27 10:41:52 -07:00 committed by Knative Prow Robot
parent 05c608e7eb
commit 40b64f27f8
5 changed files with 36 additions and 26 deletions

View File

@ -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)
})

View File

@ -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) })

View File

@ -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"

View File

@ -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)