Skip to content

Commit

Permalink
Merge 4cc05c0 into a25735f
Browse files Browse the repository at this point in the history
  • Loading branch information
bpineau committed Apr 13, 2018
2 parents a25735f + 4cc05c0 commit af14860
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 11 deletions.
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)

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)

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)

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")
}
}

0 comments on commit af14860

Please sign in to comment.