Skip to content
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

Implement application manager unit tests #4

Merged
merged 1 commit into from
Feb 22, 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
25 changes: 24 additions & 1 deletion Gopkg.lock

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

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ required = [
[[constraint]]
branch = "release-6.0"
name = "k8s.io/client-go"

[[constraint]]
name = "github.com/stretchr/testify"
version = "1.2.1"
32 changes: 32 additions & 0 deletions application/comparator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package application

import (
"time"

"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// AppComparator defines methods which allow to compare application spec and actual application state.
type AppComparator interface {
CompareAppState(appRepoPath string, app *v1alpha1.Application) (*v1alpha1.ComparisonResult, error)
}

// KsonnetAppComparator allows to compare application using KSonnet CLI
type KsonnetAppComparator struct {
}

// CompareAppState compares application spec and real app state using KSonnet
func (ks *KsonnetAppComparator) CompareAppState(appRepoPath string, app *v1alpha1.Application) (*v1alpha1.ComparisonResult, error) {
// TODO (amatyushentsev): Implement actual comparison
return &v1alpha1.ComparisonResult{
Status: v1alpha1.ComparisonStatusEqual,
ComparedTo: app.Spec.Source,
ComparedAt: metav1.Time{Time: time.Now().UTC()},
}, nil
}

// NewKsonnetAppComparator creates new instance of Ksonnet app comparator
func NewKsonnetAppComparator() AppComparator {
return &KsonnetAppComparator{}
}
21 changes: 9 additions & 12 deletions application/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Manager struct {
gitClient git.Client
repoService repository.RepositoryServiceServer
statusRefreshTimeout time.Duration
appComparator AppComparator
}

// NeedRefreshAppStatus answers if application status needs to be refreshed. Returns true if application never been compared, has changed or comparison result has expired.
Expand Down Expand Up @@ -66,7 +67,7 @@ func (m *Manager) tryRefreshAppStatus(app *v1alpha1.Application) (*v1alpha1.Appl
if err != nil {
return nil, err
}
comparisonResult, err := m.compareAppState(appRepoPath, app)
comparisonResult, err := m.appComparator.CompareAppState(appRepoPath, app)
if err != nil {
return nil, err
}
Expand All @@ -75,20 +76,16 @@ func (m *Manager) tryRefreshAppStatus(app *v1alpha1.Application) (*v1alpha1.Appl
}, nil
}

func (m *Manager) compareAppState(appRepoPath string, app *v1alpha1.Application) (*v1alpha1.ComparisonResult, error) {
// TODO (amatyushentsev): Implement actual comparison
return &v1alpha1.ComparisonResult{
Status: v1alpha1.ComparisonStatusEqual,
ComparedTo: app.Spec.Source,
ComparedAt: metav1.Time{Time: time.Now().UTC()},
}, nil
}

// NewAppManager creates new instance of app.Manager
func NewAppManager(gitClient git.Client, repoService repository.RepositoryServiceServer, statusRefreshTimeout time.Duration) *Manager {
// NewAppManager creates new instance of app manager.
func NewAppManager(
gitClient git.Client,
repoService repository.RepositoryServiceServer,
appComparator AppComparator,
statusRefreshTimeout time.Duration) *Manager {
return &Manager{
gitClient: gitClient,
repoService: repoService,
statusRefreshTimeout: statusRefreshTimeout,
appComparator: appComparator,
}
}
73 changes: 73 additions & 0 deletions application/manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package application_test

import (
"testing"

"time"

"github.com/argoproj/argo-cd/application"
"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestManager(t *testing.T) {

refreshTimeout := time.Second * 10
appSource := v1alpha1.ApplicationSource{
Environment: "prod/us-west-2",
Path: "apps/elk",
TargetRevision: "master",
RepoURL: "http://my-git-repo.git",
}

t.Run("NeedRefreshAppStatus", func(t *testing.T) {

manager := application.NewAppManager(nil, nil, nil, refreshTimeout)
t.Run("TestReturnsTrueIfAppWasNotCompared", func(t *testing.T) {
needRefresh := manager.NeedRefreshAppStatus(&v1alpha1.Application{
Spec: v1alpha1.ApplicationSpec{Source: appSource},
Status: v1alpha1.ApplicationStatus{
ComparisonResult: v1alpha1.ComparisonResult{Status: v1alpha1.ComparisonStatusUnknown},
},
})
assert.True(t, needRefresh)
})

t.Run("TestReturnsFalseIfAppWasComparedBeforeRefreshTimeoutExpires", func(t *testing.T) {
needRefresh := manager.NeedRefreshAppStatus(&v1alpha1.Application{
Spec: v1alpha1.ApplicationSpec{Source: appSource},
Status: v1alpha1.ApplicationStatus{
ComparisonResult: v1alpha1.ComparisonResult{Status: v1alpha1.ComparisonStatusEqual, ComparedAt: metav1.Time{Time: time.Now()}, ComparedTo: appSource},
},
})
assert.False(t, needRefresh)
})

t.Run("TestReturnsTrueIfAppWasComparedAfterRefreshTimeoutExpires", func(t *testing.T) {
needRefresh := manager.NeedRefreshAppStatus(&v1alpha1.Application{
Spec: v1alpha1.ApplicationSpec{Source: appSource},
Status: v1alpha1.ApplicationStatus{
ComparisonResult: v1alpha1.ComparisonResult{
Status: v1alpha1.ComparisonStatusEqual,
ComparedAt: metav1.Time{Time: time.Now().Add(-(refreshTimeout + time.Second))},
ComparedTo: appSource,
},
},
})
assert.True(t, needRefresh)
})

t.Run("TestReturnsTrueApplicationSourceHasChanged", func(t *testing.T) {
updatedSource := *appSource.DeepCopy()
updatedSource.TargetRevision = "abc"
needRefresh := manager.NeedRefreshAppStatus(&v1alpha1.Application{
Spec: v1alpha1.ApplicationSpec{Source: appSource},
Status: v1alpha1.ApplicationStatus{
ComparisonResult: v1alpha1.ComparisonResult{Status: v1alpha1.ComparisonStatusEqual, ComparedAt: metav1.Time{Time: time.Now()}, ComparedTo: updatedSource},
},
})
assert.True(t, needRefresh)
})
})
}
6 changes: 5 additions & 1 deletion cmd/argocd-application-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ func newCommand() *cobra.Command {
namespace := "default"
appResyncPeriod := time.Minute * 10

appManager := application.NewAppManager(nativeGitClient, repository.NewServer(namespace, kubeClient, appClient), appResyncPeriod)
appManager := application.NewAppManager(
nativeGitClient,
repository.NewServer(namespace, kubeClient, appClient),
application.NewKsonnetAppComparator(),
appResyncPeriod)

appController := controller.NewApplicationController(kubeClient, appClient, appManager, appResyncPeriod)

Expand Down