Skip to content

Unit tests for the git store #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ lint:
--enable=misspell \
--enable=gas \
--enable=goimports \
--enable=gocyclo \
./...

fmt:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# katafygio

[![Build Status](https://travis-ci.org/bpineau/katafygio.svg?branch=master)](https://travis-ci.org/bpineau/katafygio)
[![Coverage Status](https://coveralls.io/repos/github/bpineau/katafygio/badge.svg?branch=master)](https://coveralls.io/github/bpineau/katafygio?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/bpineau/katafygio)](https://goreportcard.com/report/github.com/bpineau/katafygio)

**katafygio** discovers Kubernetes objects (deployments, services, ...),
Expand Down
21 changes: 11 additions & 10 deletions pkg/store/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ package git
import (
"context"
"fmt"
"os"
"os/exec"
"time"

"github.com/spf13/afero"

"github.com/bpineau/katafygio/config"
"github.com/sirupsen/logrus"
)
Expand All @@ -25,6 +26,8 @@ const (
checkInterval = 10 * time.Second
)

var appFs = afero.NewOsFs()

// Store will maintain a git repository off dumped kube objects
type Store struct {
Logger *logrus.Logger
Expand Down Expand Up @@ -131,15 +134,13 @@ func (s *Store) Status() (changed bool, err error) {
return false, nil
}

// Clone does git clone, or git init (when there's no GiURL to clone from)
func (s *Store) Clone() error {
if s.DryRun {
return nil
}

err := os.MkdirAll(s.LocalDir, 0700)
if err != nil {
return fmt.Errorf("failed to created %s: %v", s.LocalDir, err)
// Clone does git clone, or git init (when there's no GitURL to clone from)
func (s *Store) Clone() (err error) {
if !s.DryRun {
err = appFs.MkdirAll(s.LocalDir, 0700)
if err != nil {
return fmt.Errorf("failed to create %s: %v", s.LocalDir, err)
}
}

if s.URL == "" {
Expand Down
157 changes: 157 additions & 0 deletions pkg/store/git/git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package git

import (
"io/ioutil"
"os"
"os/exec"
"testing"

"github.com/spf13/afero"

"github.com/bpineau/katafygio/config"
"github.com/bpineau/katafygio/pkg/log"
)

var testHasGit bool

func init() {
// Thanks to Mitchell Hashimoto!
if _, err := exec.LookPath("git"); err == nil {
testHasGit = true
}
}

func TestGitDryRun(t *testing.T) {
appFs = afero.NewMemMapFs()

conf := &config.KfConfig{
DryRun: true,
Logger: log.New("info", "", "test"),
LocalDir: "/tmp/ktest", // fake dir (in memory fs provided by Afero)
}

repo, err := New(conf).Start()
if err != nil {
t.Errorf("failed to start git: %v", err)
}

_, err = repo.Status()
if err != nil {
t.Error(err)
}

repo.Stop()
}

// testing with real git repositories and commands
func TestGit(t *testing.T) {
if !testHasGit {
t.Log("git not found, skipping")
t.Skip()
}

dir, err := ioutil.TempDir("", "katafygio-tests")
if err != nil {
t.Fatal("failed to create a temp dir for tests")
}

defer os.RemoveAll(dir)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error return value is not checked


conf := &config.KfConfig{
Logger: log.New("info", "", "test"),
LocalDir: dir,
}

repo, err := New(conf).Start()
if err != nil {
t.Errorf("failed to start git: %v", err)
}

changed, err := repo.Status()
if changed || err != nil {
t.Errorf("Status should return false on empty new repos (%v)", err)
}

_ = ioutil.WriteFile(dir+"/t.yaml", []byte{42}, 0600)

changed, err = repo.Status()
if !changed || err != nil {
t.Errorf("Status should return true on non committed files (%v)", err)
}

changed, err = repo.Commit()
if !changed || err != nil {
t.Errorf("Commit should notify changes and not fail (%v)", err)
}

changed, err = repo.Status()
if changed || err != nil {
t.Errorf("Status should return false after a add+commit (%v)", err)
}

changed, err = repo.Commit()
if changed || err != nil {
t.Errorf("Commit shouldn't notify changes on unchanged repos (%v)", err)
}

// re-use the previous repos for clone tests

newdir, err := ioutil.TempDir("", "katafygio-tests")
if err != nil {
t.Fatal("failed to create a temp dir for tests")
}

defer os.RemoveAll(newdir)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error return value is not checked


repo.LocalDir = newdir
repo.URL = dir

err = repo.Clone()
if err != nil {
t.Errorf("clone failed: %v", err)
}

_ = ioutil.WriteFile(newdir+"/t2.yaml", []byte{42}, 0600)
repo.commitAndPush()

changed, err = repo.Status()
if changed || err != nil {
t.Errorf("Status should return false after a add+commit+push (%v)", err)
}

repo.Stop()

// test various failure modes

_, err = repo.Start()
if err == nil {
t.Error("Start/Clone on an existing repository should fail")
}

err = repo.Git("fortzob", "42")
if err == nil {
t.Error("Git should fail with unknown subcommands")
}

if err == nil {
t.Error("clone should fail on existing repos")
}

notrepo, err := ioutil.TempDir("", "katafygio-tests")
if err != nil {
t.Fatal("failed to create a temp dir for tests")
}

defer os.RemoveAll(notrepo)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error return value is not checked


repo.LocalDir = notrepo
_, err = repo.Status()
if err == nil {
t.Error("Status should fail on a non-repos")
}
repo.commitAndPush()
_, err = repo.Commit()
if err == nil {
t.Error("Commit should fail on a non-repos")
}
}