Skip to content

Commit

Permalink
test: true_git package worktree verifier test
Browse files Browse the repository at this point in the history
Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Feb 18, 2022
1 parent 523eaeb commit 4d2a2d6
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Makefile
@@ -1,4 +1,4 @@
.PHONY: all werf buildah-test unit-test fmt lint docs clean
.PHONY: all werf buildah-test fmt lint docs clean

all: werf

Expand All @@ -14,6 +14,7 @@ acceptance-tests:
export WERF_TEST_K8S_DOCKER_REGISTRY_PASSWORD=""
CGO_ENABLED=1 ginkgo -r -v -compiler gc -ldflags="-linkmode external -extldflags=-static" -tags="dfrunmount dfssh containers_image_openpgp osusergo exclude_graphdriver_devicemapper netgo no_devmapper static_build" integration/suites test/e2e

.PHONY: unit-tests
unit-tests:
CGO_ENABLED=1 go test -v -compiler gc -ldflags="-linkmode external -extldflags=-static" -tags="dfrunmount dfssh containers_image_openpgp osusergo exclude_graphdriver_devicemapper netgo no_devmapper static_build" github.com/werf/werf/pkg/...

Expand Down
5 changes: 4 additions & 1 deletion pkg/true_git/work_tree.go
Expand Up @@ -2,6 +2,7 @@ package true_git

import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
Expand All @@ -16,6 +17,8 @@ import (
"github.com/werf/werf/pkg/werf"
)

var ErrInvalidDotGit = errors.New("invalid file format: expected gitdir record")

type WithWorkTreeOptions struct {
HasSubmodules bool
}
Expand Down Expand Up @@ -196,7 +199,7 @@ func resolveDotGitFile(ctx context.Context, dotGitPath string) (string, error) {
return strings.TrimSpace(strings.TrimPrefix(lines[0], "gitdir: ")), nil

InvalidDotGit:
return "", fmt.Errorf("invalid file format: expected gitdir record")
return "", ErrInvalidDotGit
}

func switchWorkTree(ctx context.Context, repoDir, workTreeDir string, commit string, withSubmodules bool) error {
Expand Down
86 changes: 86 additions & 0 deletions pkg/true_git/work_tree_test.go
@@ -0,0 +1,86 @@
package true_git

import (
"context"
"fmt"
"os"
"path/filepath"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

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

var _ = Describe("Work tree helpers", func() {
Describe("resolveDotGitFile", func() {
It("parses correctly formatted dot git link file", func() {
ctx := context.Background()
linkFile := filepath.Join(SuiteData.TestDirPath, ".git")

targetPath := "/path/to/target/git"

Expect(os.WriteFile(linkFile, []byte(fmt.Sprintf("gitdir: %s\n", targetPath)), 0o644)).To(Succeed())

resPath, err := resolveDotGitFile(ctx, linkFile)
Expect(err).To(Succeed())

Expect(resPath).To(Equal(targetPath))
})

It("fails to parse invalid dot git link file", func() {
ctx := context.Background()
linkFile := filepath.Join(SuiteData.TestDirPath, ".git")

Expect(os.WriteFile(linkFile, []byte("invalid"), 0o644)).To(Succeed())

_, err := resolveDotGitFile(ctx, linkFile)
Expect(err).To(Equal(ErrInvalidDotGit))
})
})

Describe("verifyWorkTreeConsistency", func() {
var mainWtDir, sideWtDir string
BeforeEach(func() {
mainWtDir = filepath.Join(SuiteData.TestDirPath, "main-wt")
sideWtDir = filepath.Join(SuiteData.TestDirPath, "side-wt")

Expect(os.MkdirAll(mainWtDir, os.ModePerm)).To(Succeed())

utils.RunSucceedCommand(
mainWtDir,
"git",
"-c", "init.defaultBranch=main",
"init",
)

utils.RunSucceedCommand(
mainWtDir,
"git",
"commit", "--allow-empty", "-m", "Initial commit",
)

utils.RunSucceedCommand(
mainWtDir,
"git", "worktree", "add", sideWtDir,
)
})

It("passes correct work tree", func() {
ctx := context.Background()
valid, err := verifyWorkTreeConsistency(ctx, mainWtDir, sideWtDir)
Expect(err).To((Succeed()))
Expect(valid).To(BeTrue())
})

It("detects side work tree with incorrect back dot git link", func() {
ctx := context.Background()

Expect(os.WriteFile(filepath.Join(sideWtDir, ".git"), []byte(fmt.Sprintf("gitdir: %s\n", filepath.Join(mainWtDir, ".git", "worktrees", "no-such-worktree"))), os.ModePerm)).To(Succeed())

valid, err := verifyWorkTreeConsistency(ctx, mainWtDir, sideWtDir)
Expect(err).To((Succeed()))
Expect(valid).To(BeFalse())
})
})
})

0 comments on commit 4d2a2d6

Please sign in to comment.