Skip to content

Commit

Permalink
Merge pull request #4259 from werf/test-kube-run
Browse files Browse the repository at this point in the history
test: new command `werf kube-run`, refactor
  • Loading branch information
ilya-lesikov committed Mar 18, 2022
2 parents 057314f + bb1a4ff commit 8292567
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 14 deletions.
8 changes: 4 additions & 4 deletions test/e2e/build/build_test.go
Expand Up @@ -35,12 +35,12 @@ var _ = Describe("Build", func() {

By("state0: building images")
werfProject := werf.NewProject(SuiteData.WerfBinPath, SuiteData.GetTestRepoPath(repoDirname))
buildOut, buildReport := werfProject.BuildWithReport(SuiteData.GetBuildReportPath(buildReportName))
buildOut, buildReport := werfProject.BuildWithReport(SuiteData.GetBuildReportPath(buildReportName), nil)
Expect(buildOut).To(ContainSubstring("Building stage"))
Expect(buildOut).NotTo(ContainSubstring("Use cache image"))

By("state0: rebuilding same images")
Expect(werfProject.Build()).To(And(
Expect(werfProject.Build(nil)).To(And(
ContainSubstring("Use cache image"),
Not(ContainSubstring("Building stage")),
))
Expand Down Expand Up @@ -90,11 +90,11 @@ var _ = Describe("Build", func() {

By("state1: building images")
werfProject := werf.NewProject(SuiteData.WerfBinPath, SuiteData.GetTestRepoPath(repoDirname))
buildOut, buildReport := werfProject.BuildWithReport(SuiteData.GetBuildReportPath(buildReportName))
buildOut, buildReport := werfProject.BuildWithReport(SuiteData.GetBuildReportPath(buildReportName), nil)
Expect(buildOut).To(ContainSubstring("Building stage"))

By("state1: rebuilding same images")
Expect(werfProject.Build()).To(And(
Expect(werfProject.Build(nil)).To(And(
ContainSubstring("Use cache image"),
Not(ContainSubstring("Building stage")),
))
Expand Down
6 changes: 6 additions & 0 deletions test/e2e/kube-run/_fixtures/state0/werf.yaml
@@ -0,0 +1,6 @@
project: werf-e2e-kube-run-test
configVersion: 1

---
image: main
from: alpine:3
76 changes: 76 additions & 0 deletions test/e2e/kube-run/kube_run_test.go
@@ -0,0 +1,76 @@
package e2e_kube_run_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"

"github.com/werf/werf/test/pkg/werf"
)

var _ = Describe("kube-run", func() {
DescribeTable("should succeed/fail and produce expected output",
func(kubeRunOpts *werf.KubeRunOptions, outputExpectationsFunc func(out string)) {
repoDirname := "repo0"
fixtureRelPath := "state0"

By("preparing test repo")
SuiteData.InitTestRepo(repoDirname, fixtureRelPath)
werfProject := werf.NewProject(SuiteData.WerfBinPath, SuiteData.GetTestRepoPath(repoDirname))

By("execute kube-run")
combinedOut := werfProject.KubeRun(kubeRunOpts)
outputExpectationsFunc(combinedOut)
},
Entry(
"show output and succeed, running non-interactively",
&werf.KubeRunOptions{
Command: `sh -c "cat /etc/os-release"`,
Image: "main",
},
func(out string) {
Expect(out).To(ContainSubstring("ID=alpine"))
},
),
Entry(
"show output and succeed, running interactively with TTY",
&werf.KubeRunOptions{
Command: `sh -c "cat /etc/os-release"`,
Image: "main",
CommonOptions: werf.CommonOptions{
ExtraArgs: []string{"-i", "-t"},
},
},
func(out string) {
Expect(out).To(ContainSubstring("ID=alpine"))
},
),
Entry(
"show output and fail, running non-interactively",
&werf.KubeRunOptions{
Command: `sh -c "cat /etc/os-release; exit 1"`,
Image: "main",
CommonOptions: werf.CommonOptions{
ShouldFail: true,
},
},
func(out string) {
Expect(out).To(ContainSubstring("ID=alpine"))
},
),
Entry(
"show output and fail, running interactively with TTY",
&werf.KubeRunOptions{
Command: `sh -c "cat /etc/os-release; exit 1"`,
Image: "main",
CommonOptions: werf.CommonOptions{
ShouldFail: true,
ExtraArgs: []string{"-i", "-t"},
},
},
func(out string) {
Expect(out).To(ContainSubstring("ID=alpine"))
},
),
)
})
29 changes: 29 additions & 0 deletions test/e2e/kube-run/suite_test.go
@@ -0,0 +1,29 @@
package e2e_kube_run_test

import (
"testing"

"github.com/werf/werf/test/pkg/suite_init"
)

func TestSuite(t *testing.T) {
suite_init.MakeTestSuiteEntrypointFunc("E2E kube-run suite", suite_init.TestSuiteEntrypointFuncOptions{
RequiredSuiteTools: []string{"docker", "git"},
RequiredSuiteEnvs: []string{
"WERF_TEST_K8S_DOCKER_REGISTRY",
"WERF_TEST_K8S_DOCKER_REGISTRY_USERNAME",
"WERF_TEST_K8S_DOCKER_REGISTRY_PASSWORD",
},
})(t)
}

var SuiteData suite_init.SuiteData

var (
_ = SuiteData.SetupStubs(suite_init.NewStubsData())
_ = SuiteData.SetupSynchronizedSuiteCallbacks(suite_init.NewSynchronizedSuiteCallbacksData())
_ = SuiteData.SetupWerfBinary(suite_init.NewWerfBinaryData(SuiteData.SynchronizedSuiteCallbacksData))
_ = SuiteData.SetupProjectName(suite_init.NewProjectNameData(SuiteData.StubsData))
_ = SuiteData.SetupTmp(suite_init.NewTmpDirData())
_ = SuiteData.SetupK8sDockerRegistry(suite_init.NewK8sDockerRegistryData(SuiteData.ProjectNameData, SuiteData.StubsData))
)
104 changes: 94 additions & 10 deletions test/pkg/werf/project.go
Expand Up @@ -10,30 +10,114 @@ import (
iutils "github.com/werf/werf/test/pkg/utils"
)

func NewProject(werfBinPath, repoPath string) *Project {
func NewProject(werfBinPath, gitRepoPath string) *Project {
return &Project{
WerfBinPath: werfBinPath,
RepoPath: repoPath,
GitRepoPath: gitRepoPath,
}
}

type Project struct {
RepoPath string
GitRepoPath string
WerfBinPath string
}

func (p *Project) Build(optArgs ...string) (combinedOut string) {
optArgs = append([]string{"build", "--debug"}, optArgs...)
return iutils.SucceedCommandOutputString(p.RepoPath, p.WerfBinPath, optArgs...)
type CommonOptions struct {
ShouldFail bool
ExtraArgs []string
}

func (p *Project) BuildWithReport(buildReportPath string, optsArgs ...string) (combinedOut string, buildReport build.ImagesReport) {
optsArgs = append([]string{"build", "--debug", "--report-path", buildReportPath}, optsArgs...)
combinedOut = iutils.SucceedCommandOutputString(p.RepoPath, p.WerfBinPath, optsArgs...)
type BuildOptions struct {
CommonOptions
}

type BuildWithReportOptions struct {
CommonOptions
}

type KubeRunOptions struct {
CommonOptions
Command string
Image string
}

type KubeCtlOptions struct {
CommonOptions
}

type runCommandOptions struct {
ShouldFail bool
Args []string
}

func (p *Project) Build(opts *BuildOptions) (combinedOut string) {
if opts == nil {
opts = &BuildOptions{}
}

args := append([]string{"build"}, opts.ExtraArgs...)
outb, err := iutils.RunCommand(p.GitRepoPath, p.WerfBinPath, args...)
if opts.ShouldFail {
Expect(err).To(HaveOccurred())
} else {
Expect(err).NotTo(HaveOccurred())
}

return string(outb)
}

func (p *Project) BuildWithReport(buildReportPath string, opts *BuildWithReportOptions) (string, build.ImagesReport) {
if opts == nil {
opts = &BuildWithReportOptions{}
}

args := append([]string{"build", "--report-path", buildReportPath}, opts.ExtraArgs...)
out := p.runCommand(runCommandOptions{Args: args, ShouldFail: opts.ShouldFail})

buildReportRaw, err := os.ReadFile(buildReportPath)
Expect(err).NotTo(HaveOccurred())

var buildReport build.ImagesReport
Expect(json.Unmarshal(buildReportRaw, &buildReport)).To(Succeed())

return combinedOut, buildReport
return out, buildReport
}

func (p *Project) KubeRun(opts *KubeRunOptions) string {
if opts == nil {
opts = &KubeRunOptions{}
}

args := append([]string{"kube-run"}, opts.ExtraArgs...)

if opts.Image != "" {
args = append(args, opts.Image)
}

if opts.Command != "" {
args = append(args, "--", opts.Command)
}

return p.runCommand(runCommandOptions{Args: args, ShouldFail: opts.ShouldFail})
}

func (p *Project) KubeCtl(opts *KubeCtlOptions) string {
if opts == nil {
opts = &KubeCtlOptions{}
}

args := append([]string{"kubectl"}, opts.ExtraArgs...)

return p.runCommand(runCommandOptions{Args: args, ShouldFail: opts.ShouldFail})
}

func (p *Project) runCommand(opts runCommandOptions) string {
outb, err := iutils.RunCommand(p.GitRepoPath, p.WerfBinPath, opts.Args...)
if opts.ShouldFail {
Expect(err).To(HaveOccurred())
} else {
Expect(err).NotTo(HaveOccurred())
}

return string(outb)
}

0 comments on commit 8292567

Please sign in to comment.