Skip to content

Commit

Permalink
More application manager tests (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmt authored Feb 23, 2018
1 parent fe7bc15 commit dd47c74
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 10 deletions.
13 changes: 11 additions & 2 deletions Gopkg.lock

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

10 changes: 4 additions & 6 deletions application/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package application
import (
"context"

"io/ioutil"

"fmt"

"time"
Expand All @@ -13,6 +11,9 @@ import (
"github.com/argoproj/argo-cd/server/repository"
"github.com/argoproj/argo-cd/util/git"

"os"
"path"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -53,10 +54,7 @@ func (m *Manager) tryRefreshAppStatus(app *v1alpha1.Application) (*v1alpha1.Appl
return nil, err
}

appRepoPath, err := ioutil.TempDir("", app.Name)
if err != nil {
return nil, fmt.Errorf("unable to create temp repository directory for app '%s'", app.Name)
}
appRepoPath := path.Join(os.TempDir(), app.Name)

err = m.gitClient.CloneOrFetch(repo.Repo, repo.Username, repo.Password, appRepoPath)
if err != nil {
Expand Down
45 changes: 43 additions & 2 deletions application/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ import (

"time"

"context"

"github.com/argoproj/argo-cd/application"
appMocks "github.com/argoproj/argo-cd/application/mocks"
"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/server/repository"
repoMocks "github.com/argoproj/argo-cd/server/repository/mock"
gitMocks "github.com/argoproj/argo-cd/util/git/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -20,10 +27,12 @@ func TestManager(t *testing.T) {
TargetRevision: "master",
RepoURL: "http://my-git-repo.git",
}
gitClientMock := gitMocks.Client{}
appComparatorMock := appMocks.AppComparator{}
repoServiceMock := repoMocks.RepositoryServiceServer{}
manager := application.NewAppManager(&gitClientMock, &repoServiceMock, &appComparatorMock, refreshTimeout)

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},
Expand Down Expand Up @@ -70,4 +79,36 @@ func TestManager(t *testing.T) {
assert.True(t, needRefresh)
})
})

t.Run("RefreshAppStatus", func(t *testing.T) {
repo := v1alpha1.Repository{
Repo: "https://testRepo/repo.git",
Username: "user",
Password: "test",
}
app := v1alpha1.Application{
Spec: v1alpha1.ApplicationSpec{Source: appSource},
Status: v1alpha1.ApplicationStatus{},
}

repoServiceMock.On("Get", context.Background(), &repository.RepoQuery{
Repo: appSource.RepoURL,
}).Return(&repo, nil)
var repoPath string
gitClientMock.On("CloneOrFetch", repo.Repo, repo.Username, repo.Password, mock.MatchedBy(func(receivedRepoPath string) bool {
repoPath = receivedRepoPath
return true
})).Return(nil)
gitClientMock.On("Checkout", mock.MatchedBy(func(receivedRepoPath string) bool {
return repoPath == receivedRepoPath
}), appSource.TargetRevision).Return(nil)

appComparatorMock.On("CompareAppState", mock.MatchedBy(func(receivedRepoPath string) bool {
return repoPath == receivedRepoPath
}), &app).Return(&v1alpha1.ComparisonResult{
Status: v1alpha1.ComparisonStatusEqual,
}, nil)
updatedAppStatus := manager.RefreshAppStatus(&app)
assert.Equal(t, updatedAppStatus.ComparisonResult.Status, v1alpha1.ComparisonStatusEqual)
})
}
33 changes: 33 additions & 0 deletions application/mocks/AppComparator.go

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

150 changes: 150 additions & 0 deletions server/repository/mock/RepositoryServiceServer.go

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

37 changes: 37 additions & 0 deletions util/git/mocks/Client.go

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

0 comments on commit dd47c74

Please sign in to comment.