Skip to content

Commit

Permalink
Merge pull request #7 from WanderaOrg/test-upload
Browse files Browse the repository at this point in the history
Unit tests of pkg/upload
  • Loading branch information
coufalja authored May 21, 2018
2 parents e314469 + d9e2814 commit c9d6143
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 8 deletions.
33 changes: 32 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/upload/testdata/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"test": 1
}
2 changes: 2 additions & 0 deletions pkg/upload/testdata/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test:
some: 1
19 changes: 12 additions & 7 deletions pkg/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@ const (
Upsert MergeType = "upsert"
)

// FileIter provides an iterator for the files in a tree.
type FileIter interface {
ForEach(cb func(*object.File) error) error
}

// Uploader uploading data to target
type Uploader interface {
// Upload files into config map tagged by commitID
Upload(commitID string, iter *object.FileIter) error
Upload(commitID string, iter FileIter) error
}

type uploader struct {
restconfig *rest.Config
clientset *kubernetes.Clientset
clientset kubernetes.Interface
namespace string
name string
mergeType MergeType
Expand Down Expand Up @@ -121,7 +126,7 @@ func NewConfigMapUploader(o *UploaderOptions) (Uploader, error) {
}, nil
}

func (u *configmapUploader) Upload(commitID string, iter *object.FileIter) error {
func (u *configmapUploader) Upload(commitID string, iter FileIter) error {
configMaps := u.clientset.CoreV1().ConfigMaps(u.namespace)

data, err := u.iterToConfigMapData(iter)
Expand Down Expand Up @@ -220,7 +225,7 @@ func (u *configmapUploader) createConfigMap(configMaps typedcore.ConfigMapInterf
return nil
}

func (u *configmapUploader) iterToConfigMapData(iter *object.FileIter) (map[string]string, error) {
func (u *configmapUploader) iterToConfigMapData(iter FileIter) (map[string]string, error) {
var data = make(map[string]string)
err := iter.ForEach(func(file *object.File) error {
if filterFile(file, u.includes, u.excludes) {
Expand Down Expand Up @@ -283,7 +288,7 @@ func NewSecretUploader(o *UploaderOptions) (Uploader, error) {
}, nil
}

func (u *secretUploader) Upload(commitID string, iter *object.FileIter) error {
func (u *secretUploader) Upload(commitID string, iter FileIter) error {
secrets := u.clientset.CoreV1().Secrets(u.namespace)

data, err := u.iterToSecretData(iter)
Expand Down Expand Up @@ -382,7 +387,7 @@ func (u *secretUploader) createSecret(secrets typedcore.SecretInterface, data ma
return nil
}

func (u *secretUploader) iterToSecretData(iter *object.FileIter) (map[string][]byte, error) {
func (u *secretUploader) iterToSecretData(iter FileIter) (map[string][]byte, error) {
var data = make(map[string][]byte)
err := iter.ForEach(func(file *object.File) error {
if filterFile(file, u.includes, u.excludes) {
Expand Down Expand Up @@ -434,7 +439,7 @@ func NewFolderUploader(o *UploaderOptions) (Uploader, error) {
}, nil
}

func (u *folderUploader) Upload(commitID string, iter *object.FileIter) error {
func (u *folderUploader) Upload(commitID string, iter FileIter) error {
err := iter.ForEach(func(file *object.File) error {
if filterFile(file, u.includes, u.excludes) {
src := path.Join(u.sourcePath, file.Name)
Expand Down
Loading

0 comments on commit c9d6143

Please sign in to comment.