Skip to content

Commit

Permalink
Merge pull request #4941 from werf/test-e2e-add-converge-test
Browse files Browse the repository at this point in the history
test(e2e): add simple converge test
  • Loading branch information
ilya-lesikov committed Sep 15, 2022
2 parents e5c2cdd + 2c074d8 commit 9c30979
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 7 deletions.
4 changes: 2 additions & 2 deletions test/e2e/build/suite_test.go
Expand Up @@ -7,7 +7,7 @@ import (
. "github.com/onsi/ginkgo/v2"

"github.com/werf/werf/test/pkg/suite_init"
iutils "github.com/werf/werf/test/pkg/utils"
"github.com/werf/werf/test/pkg/utils"
"github.com/werf/werf/test/pkg/utils/docker"
)

Expand Down Expand Up @@ -45,6 +45,6 @@ var (
})

_ = AfterEach(func() {
iutils.RunSucceedCommand("", SuiteData.WerfBinPath, "host", "purge", "--force", "--project-name", SuiteData.ProjectName)
utils.RunSucceedCommand("", SuiteData.WerfBinPath, "host", "purge", "--force", "--project-name", SuiteData.ProjectName)
})
)
@@ -0,0 +1,6 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: test1
data:
key1: value1
6 changes: 6 additions & 0 deletions test/e2e/converge/_fixtures/simple/state0/werf.yaml
@@ -0,0 +1,6 @@
project: werf-test-e2e-converge-simple
configVersion: 1

---
image: main
from: ubuntu:22.04
20 changes: 20 additions & 0 deletions test/e2e/converge/common_test.go
@@ -0,0 +1,20 @@
package e2e_converge_test

import (
"os"
"strings"

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

func setupEnv() {
SuiteData.Stubs.SetEnv("WERF_REPO", strings.Join([]string{os.Getenv("WERF_TEST_K8S_DOCKER_REGISTRY"), SuiteData.ProjectName}, "/"))

if util.GetBoolEnvironmentDefaultFalse("WERF_TEST_K8S_DOCKER_REGISTRY_INSECURE") {
SuiteData.Stubs.SetEnv("WERF_INSECURE_REGISTRY", "1")
SuiteData.Stubs.SetEnv("WERF_SKIP_TLS_VERIFY_REGISTRY", "1")
} else {
SuiteData.Stubs.UnsetEnv("WERF_INSECURE_REGISTRY")
SuiteData.Stubs.UnsetEnv("WERF_SKIP_TLS_VERIFY_REGISTRY")
}
}
54 changes: 54 additions & 0 deletions test/e2e/converge/simple_test.go
@@ -0,0 +1,54 @@
package e2e_converge_test

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/werf/kubedog/pkg/kube"
"github.com/werf/werf/test/pkg/utils"
"github.com/werf/werf/test/pkg/werf"
)

var _ = Describe("Simple converge", Label("e2e", "converge", "simple"), func() {
var repoDirname string

BeforeEach(func() {
Expect(kube.Init(kube.InitOptions{})).To(Succeed())
})

AfterEach(func() {
utils.RunSucceedCommand(SuiteData.GetTestRepoPath(repoDirname), SuiteData.WerfBinPath, "dismiss", "--with-namespace")
})

It("should succeed and deploy expected resources",
func() {
By("initializing")
repoDirname = "repo0"
setupEnv()

By("state0: starting")
{
fixtureRelPath := "simple/state0"

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

By("state0: execute converge")
combinedOut := werfProject.Converge(&werf.ConvergeOptions{})

By("state0: check converge output")
Expect(combinedOut).To(ContainSubstring("STATUS: deployed"))
Expect(combinedOut).To(ContainSubstring("REVISION: 1"))

By("state0: check deployed resources in cluster")
cm, err := kube.Client.CoreV1().ConfigMaps(werfProject.Namespace()).Get(context.Background(), "test1", metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
Expect(cm.Data).To(Equal(map[string]string{"key1": "value1"}))
}
},
)
})
35 changes: 35 additions & 0 deletions test/e2e/converge/suite_test.go
@@ -0,0 +1,35 @@
package e2e_converge_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"

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

func TestSuite(t *testing.T) {
suite_init.MakeTestSuiteEntrypointFunc("E2E converge suite", suite_init.TestSuiteEntrypointFuncOptions{
RequiredSuiteTools: []string{"docker", "git"},
RequiredSuiteEnvs: []string{
"WERF_TEST_K8S_DOCKER_REGISTRY",
},
})(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))

_ = AfterEach(func() {
utils.RunSucceedCommand("", SuiteData.WerfBinPath, "host", "purge", "--force", "--project-name", SuiteData.ProjectName)
})
)
2 changes: 1 addition & 1 deletion test/e2e/kube-run/_fixtures/simple/state0/werf.yaml
@@ -1,4 +1,4 @@
project: werf-test-e2e-kube-run-complex
project: werf-test-e2e-kube-run-simple
configVersion: 1

---
Expand Down
Expand Up @@ -13,7 +13,7 @@ import (

var _ = Describe("Simple kube-run", Label("e2e", "kube-run", "simple"), func() {
DescribeTable("should",
func(kubeRunOpts *werf.KubeRunOptions, outputExpectationsFunc func(out string)) {
func(kubeRunOpts *werf.KubeRunOptions, expectOutFn func(out string)) {
By("initializing")
setupEnv()
repoDirname := "repo0"
Expand All @@ -25,7 +25,7 @@ var _ = Describe("Simple kube-run", Label("e2e", "kube-run", "simple"), func() {

By("state0: execute kube-run")
combinedOut := werfProject.KubeRun(kubeRunOpts)
outputExpectationsFunc(combinedOut)
expectOutFn(combinedOut)
},
Entry(
"succeed and produce expected output, running non-interactively",
Expand Down
2 changes: 0 additions & 2 deletions test/e2e/kube-run/suite_test.go
Expand Up @@ -11,8 +11,6 @@ func TestSuite(t *testing.T) {
RequiredSuiteTools: []string{"docker", "git"},
RequiredSuiteEnvs: []string{
"WERF_TEST_K8S_DOCKER_REGISTRY",
"WERF_TEST_K8S_DOCKER_REGISTRY_USERNAME",
"WERF_TEST_K8S_DOCKER_REGISTRY_PASSWORD",
},
})(t)
}
Expand Down
25 changes: 25 additions & 0 deletions test/pkg/werf/project.go
Expand Up @@ -3,6 +3,7 @@ package werf
import (
"encoding/json"
"os"
"strings"

. "github.com/onsi/gomega"

Expand All @@ -20,6 +21,7 @@ func NewProject(werfBinPath, gitRepoPath string) *Project {
type Project struct {
GitRepoPath string
WerfBinPath string
namespace string
}

type CommonOptions struct {
Expand All @@ -35,6 +37,10 @@ type BuildWithReportOptions struct {
CommonOptions
}

type ConvergeOptions struct {
CommonOptions
}

type KubeRunOptions struct {
CommonOptions
Command []string
Expand Down Expand Up @@ -78,6 +84,17 @@ func (p *Project) BuildWithReport(buildReportPath string, opts *BuildWithReportO
return out, buildReport
}

func (p *Project) Converge(opts *ConvergeOptions) (combinedOut string) {
if opts == nil {
opts = &ConvergeOptions{}
}

args := append([]string{"converge"}, opts.ExtraArgs...)
outb := p.runCommand(runCommandOptions{Args: args, ShouldFail: opts.ShouldFail})

return string(outb)
}

func (p *Project) KubeRun(opts *KubeRunOptions) string {
if opts == nil {
opts = &KubeRunOptions{}
Expand Down Expand Up @@ -107,6 +124,14 @@ func (p *Project) KubeCtl(opts *KubeCtlOptions) string {
return p.runCommand(runCommandOptions{Args: args, ShouldFail: opts.ShouldFail})
}

func (p *Project) Namespace() string {
if p.namespace == "" {
p.namespace = strings.TrimSpace(p.runCommand(runCommandOptions{Args: []string{"helm", "get-namespace"}}))
}

return p.namespace
}

func (p *Project) runCommand(opts runCommandOptions) string {
outb, err := iutils.RunCommand(p.GitRepoPath, p.WerfBinPath, iutils.WerfBinArgs(opts.Args...)...)
if opts.ShouldFail {
Expand Down

0 comments on commit 9c30979

Please sign in to comment.