Skip to content

Commit 3e764f0

Browse files
authored
Implement application manager unit tests (#4)
1 parent c0233eb commit 3e764f0

File tree

6 files changed

+147
-14
lines changed

6 files changed

+147
-14
lines changed

Gopkg.lock

+24-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

+4
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ required = [
3030
[[constraint]]
3131
branch = "release-6.0"
3232
name = "k8s.io/client-go"
33+
34+
[[constraint]]
35+
name = "github.com/stretchr/testify"
36+
version = "1.2.1"

application/comparator.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package application
2+
3+
import (
4+
"time"
5+
6+
"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
7+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
)
9+
10+
// AppComparator defines methods which allow to compare application spec and actual application state.
11+
type AppComparator interface {
12+
CompareAppState(appRepoPath string, app *v1alpha1.Application) (*v1alpha1.ComparisonResult, error)
13+
}
14+
15+
// KsonnetAppComparator allows to compare application using KSonnet CLI
16+
type KsonnetAppComparator struct {
17+
}
18+
19+
// CompareAppState compares application spec and real app state using KSonnet
20+
func (ks *KsonnetAppComparator) CompareAppState(appRepoPath string, app *v1alpha1.Application) (*v1alpha1.ComparisonResult, error) {
21+
// TODO (amatyushentsev): Implement actual comparison
22+
return &v1alpha1.ComparisonResult{
23+
Status: v1alpha1.ComparisonStatusEqual,
24+
ComparedTo: app.Spec.Source,
25+
ComparedAt: metav1.Time{Time: time.Now().UTC()},
26+
}, nil
27+
}
28+
29+
// NewKsonnetAppComparator creates new instance of Ksonnet app comparator
30+
func NewKsonnetAppComparator() AppComparator {
31+
return &KsonnetAppComparator{}
32+
}

application/manager.go

+9-12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Manager struct {
2121
gitClient git.Client
2222
repoService repository.RepositoryServiceServer
2323
statusRefreshTimeout time.Duration
24+
appComparator AppComparator
2425
}
2526

2627
// NeedRefreshAppStatus answers if application status needs to be refreshed. Returns true if application never been compared, has changed or comparison result has expired.
@@ -66,7 +67,7 @@ func (m *Manager) tryRefreshAppStatus(app *v1alpha1.Application) (*v1alpha1.Appl
6667
if err != nil {
6768
return nil, err
6869
}
69-
comparisonResult, err := m.compareAppState(appRepoPath, app)
70+
comparisonResult, err := m.appComparator.CompareAppState(appRepoPath, app)
7071
if err != nil {
7172
return nil, err
7273
}
@@ -75,20 +76,16 @@ func (m *Manager) tryRefreshAppStatus(app *v1alpha1.Application) (*v1alpha1.Appl
7576
}, nil
7677
}
7778

78-
func (m *Manager) compareAppState(appRepoPath string, app *v1alpha1.Application) (*v1alpha1.ComparisonResult, error) {
79-
// TODO (amatyushentsev): Implement actual comparison
80-
return &v1alpha1.ComparisonResult{
81-
Status: v1alpha1.ComparisonStatusEqual,
82-
ComparedTo: app.Spec.Source,
83-
ComparedAt: metav1.Time{Time: time.Now().UTC()},
84-
}, nil
85-
}
86-
87-
// NewAppManager creates new instance of app.Manager
88-
func NewAppManager(gitClient git.Client, repoService repository.RepositoryServiceServer, statusRefreshTimeout time.Duration) *Manager {
79+
// NewAppManager creates new instance of app manager.
80+
func NewAppManager(
81+
gitClient git.Client,
82+
repoService repository.RepositoryServiceServer,
83+
appComparator AppComparator,
84+
statusRefreshTimeout time.Duration) *Manager {
8985
return &Manager{
9086
gitClient: gitClient,
9187
repoService: repoService,
9288
statusRefreshTimeout: statusRefreshTimeout,
89+
appComparator: appComparator,
9390
}
9491
}

application/manager_test.go

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package application_test
2+
3+
import (
4+
"testing"
5+
6+
"time"
7+
8+
"github.com/argoproj/argo-cd/application"
9+
"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
10+
"github.com/stretchr/testify/assert"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
)
13+
14+
func TestManager(t *testing.T) {
15+
16+
refreshTimeout := time.Second * 10
17+
appSource := v1alpha1.ApplicationSource{
18+
Environment: "prod/us-west-2",
19+
Path: "apps/elk",
20+
TargetRevision: "master",
21+
RepoURL: "http://my-git-repo.git",
22+
}
23+
24+
t.Run("NeedRefreshAppStatus", func(t *testing.T) {
25+
26+
manager := application.NewAppManager(nil, nil, nil, refreshTimeout)
27+
t.Run("TestReturnsTrueIfAppWasNotCompared", func(t *testing.T) {
28+
needRefresh := manager.NeedRefreshAppStatus(&v1alpha1.Application{
29+
Spec: v1alpha1.ApplicationSpec{Source: appSource},
30+
Status: v1alpha1.ApplicationStatus{
31+
ComparisonResult: v1alpha1.ComparisonResult{Status: v1alpha1.ComparisonStatusUnknown},
32+
},
33+
})
34+
assert.True(t, needRefresh)
35+
})
36+
37+
t.Run("TestReturnsFalseIfAppWasComparedBeforeRefreshTimeoutExpires", func(t *testing.T) {
38+
needRefresh := manager.NeedRefreshAppStatus(&v1alpha1.Application{
39+
Spec: v1alpha1.ApplicationSpec{Source: appSource},
40+
Status: v1alpha1.ApplicationStatus{
41+
ComparisonResult: v1alpha1.ComparisonResult{Status: v1alpha1.ComparisonStatusEqual, ComparedAt: metav1.Time{Time: time.Now()}, ComparedTo: appSource},
42+
},
43+
})
44+
assert.False(t, needRefresh)
45+
})
46+
47+
t.Run("TestReturnsTrueIfAppWasComparedAfterRefreshTimeoutExpires", func(t *testing.T) {
48+
needRefresh := manager.NeedRefreshAppStatus(&v1alpha1.Application{
49+
Spec: v1alpha1.ApplicationSpec{Source: appSource},
50+
Status: v1alpha1.ApplicationStatus{
51+
ComparisonResult: v1alpha1.ComparisonResult{
52+
Status: v1alpha1.ComparisonStatusEqual,
53+
ComparedAt: metav1.Time{Time: time.Now().Add(-(refreshTimeout + time.Second))},
54+
ComparedTo: appSource,
55+
},
56+
},
57+
})
58+
assert.True(t, needRefresh)
59+
})
60+
61+
t.Run("TestReturnsTrueApplicationSourceHasChanged", func(t *testing.T) {
62+
updatedSource := *appSource.DeepCopy()
63+
updatedSource.TargetRevision = "abc"
64+
needRefresh := manager.NeedRefreshAppStatus(&v1alpha1.Application{
65+
Spec: v1alpha1.ApplicationSpec{Source: appSource},
66+
Status: v1alpha1.ApplicationStatus{
67+
ComparisonResult: v1alpha1.ComparisonResult{Status: v1alpha1.ComparisonStatusEqual, ComparedAt: metav1.Time{Time: time.Now()}, ComparedTo: updatedSource},
68+
},
69+
})
70+
assert.True(t, needRefresh)
71+
})
72+
})
73+
}

cmd/argocd-application-controller/main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ func newCommand() *cobra.Command {
4444
namespace := "default"
4545
appResyncPeriod := time.Minute * 10
4646

47-
appManager := application.NewAppManager(nativeGitClient, repository.NewServer(namespace, kubeClient, appClient), appResyncPeriod)
47+
appManager := application.NewAppManager(
48+
nativeGitClient,
49+
repository.NewServer(namespace, kubeClient, appClient),
50+
application.NewKsonnetAppComparator(),
51+
appResyncPeriod)
4852

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

0 commit comments

Comments
 (0)