diff --git a/test/e2e/build/build_test.go b/test/e2e/build/build_test.go index 4dfbcff6f2..956c7e7bc7 100644 --- a/test/e2e/build/build_test.go +++ b/test/e2e/build/build_test.go @@ -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")), )) @@ -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")), )) diff --git a/test/e2e/kube-run/_fixtures/state0/werf.yaml b/test/e2e/kube-run/_fixtures/state0/werf.yaml new file mode 100644 index 0000000000..966e68daa3 --- /dev/null +++ b/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 diff --git a/test/e2e/kube-run/kube_run_test.go b/test/e2e/kube-run/kube_run_test.go new file mode 100644 index 0000000000..5daa6f90d6 --- /dev/null +++ b/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")) + }, + ), + ) +}) diff --git a/test/e2e/kube-run/suite_test.go b/test/e2e/kube-run/suite_test.go new file mode 100644 index 0000000000..f0aa97ed72 --- /dev/null +++ b/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)) +) diff --git a/test/pkg/werf/project.go b/test/pkg/werf/project.go index 1f7e15646e..1e8b8dec7d 100644 --- a/test/pkg/werf/project.go +++ b/test/pkg/werf/project.go @@ -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) }