mirror of
https://github.com/hanzoai/cd.git
synced 2026-10-30 14:56:45 +00:00
Rename test names, comments, fixture data, and mock GitHub/webhook responses that still said Argo/ArgoCD/argocd/argoproj in our own voice, matching the hanzocd-* binary/configmap names and the apps.hanzo.ai CRD/annotation domain already established elsewhere in the fork. Also drop the dated argov1alpha1/argoprojiov1alpha1 import aliases for our own v1alpha1 API package. Restore the CRD groups a prior blind rename had mangled on a vendored third-party Argo Workflows example manifest back to the real argoproj.io, and fix several places where an earlier half-finished rename had gone silently wrong instead of just being cosmetic: stale CD_BINARY_NAME values and a stale --argocd-repo-server flag no longer recognized by the binary, cert filenames and an ssh_known_hosts alias that didn't match what's actually on disk, a namespace and a kustomize image substitution that were silent no-ops, and a test assertion regex that could never match its own fixture. External lookup keys we don't control are left byte-exact: quay.io/argoprojlabs images, the docker.io/argoproj CI builder image, live argoproj/argo-workflows release URLs, the generated ArgoprojV1alpha1() clientset method, and the Makefile-coupled /tmp/argo-e2e and GOPATH mount conventions.
183 lines
6.7 KiB
Go
183 lines
6.7 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"k8s.io/client-go/kubernetes/fake"
|
|
|
|
"github.com/hanzoai/cd/reposerver/apiclient"
|
|
repo_mocks "github.com/hanzoai/cd/reposerver/apiclient/mocks"
|
|
"github.com/hanzoai/cd/util/db"
|
|
"github.com/hanzoai/cd/util/settings"
|
|
|
|
"github.com/hanzoai/cd/pkg/apis/application/v1alpha1"
|
|
)
|
|
|
|
func TestGetDirectories(t *testing.T) {
|
|
t.Parallel()
|
|
type fields struct {
|
|
submoduleEnabled bool
|
|
getRepository func(ctx context.Context, url, project string) (*v1alpha1.Repository, error)
|
|
getGitDirectories func(ctx context.Context, req *apiclient.GitDirectoriesRequest) (*apiclient.GitDirectoriesResponse, error)
|
|
}
|
|
type args struct {
|
|
ctx context.Context
|
|
repoURL string
|
|
revision string
|
|
noRevisionCache bool
|
|
sourceIntegrity *v1alpha1.SourceIntegrity
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
want []string
|
|
wantErr assert.ErrorAssertionFunc
|
|
}{
|
|
{name: "ErrorGettingRepos", fields: fields{
|
|
getRepository: func(_ context.Context, _, _ string) (*v1alpha1.Repository, error) {
|
|
return nil, errors.New("unable to get repos")
|
|
},
|
|
}, args: args{}, want: nil, wantErr: assert.Error},
|
|
{name: "ErrorGettingDirs", fields: fields{
|
|
getRepository: func(_ context.Context, _, _ string) (*v1alpha1.Repository, error) {
|
|
return &v1alpha1.Repository{}, nil
|
|
},
|
|
getGitDirectories: func(_ context.Context, _ *apiclient.GitDirectoriesRequest) (*apiclient.GitDirectoriesResponse, error) {
|
|
return nil, errors.New("unable to get dirs")
|
|
},
|
|
}, args: args{}, want: nil, wantErr: assert.Error},
|
|
{name: "HappyCase", fields: fields{
|
|
getRepository: func(_ context.Context, _, _ string) (*v1alpha1.Repository, error) {
|
|
return &v1alpha1.Repository{
|
|
Repo: "foo",
|
|
}, nil
|
|
},
|
|
getGitDirectories: func(_ context.Context, _ *apiclient.GitDirectoriesRequest) (*apiclient.GitDirectoriesResponse, error) {
|
|
return &apiclient.GitDirectoriesResponse{
|
|
Paths: []string{"foo", "foo/bar", "bar/foo"},
|
|
}, nil
|
|
},
|
|
}, args: args{
|
|
repoURL: "foo",
|
|
}, want: []string{"foo", "foo/bar", "bar/foo"}, wantErr: assert.NoError},
|
|
{name: "ErrorVerifyingCommit", fields: fields{
|
|
getRepository: func(_ context.Context, _, _ string) (*v1alpha1.Repository, error) {
|
|
return &v1alpha1.Repository{}, nil
|
|
},
|
|
getGitDirectories: func(_ context.Context, _ *apiclient.GitDirectoriesRequest) (*apiclient.GitDirectoriesResponse, error) {
|
|
return nil, errors.New("revision HEAD is not signed")
|
|
},
|
|
}, args: args{}, want: nil, wantErr: assert.Error},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
a := &repoService{
|
|
getRepository: tt.fields.getRepository,
|
|
submoduleEnabled: tt.fields.submoduleEnabled,
|
|
getGitDirectoriesFromRepoServer: tt.fields.getGitDirectories,
|
|
}
|
|
got, err := a.GetDirectories(tt.args.ctx, tt.args.repoURL, tt.args.revision, "", tt.args.noRevisionCache, tt.args.sourceIntegrity)
|
|
if !tt.wantErr(t, err, fmt.Sprintf("GetDirectories(%v, %v, %v, %v)", tt.args.ctx, tt.args.repoURL, tt.args.revision, tt.args.noRevisionCache)) {
|
|
return
|
|
}
|
|
assert.Equalf(t, tt.want, got, "GetDirectories(%v, %v, %v, %v)", tt.args.ctx, tt.args.repoURL, tt.args.revision, tt.args.noRevisionCache)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetFiles(t *testing.T) {
|
|
t.Parallel()
|
|
type fields struct {
|
|
submoduleEnabled bool
|
|
getRepository func(ctx context.Context, url, project string) (*v1alpha1.Repository, error)
|
|
getGitFiles func(ctx context.Context, req *apiclient.GitFilesRequest) (*apiclient.GitFilesResponse, error)
|
|
}
|
|
type args struct {
|
|
ctx context.Context
|
|
repoURL string
|
|
revision string
|
|
pattern string
|
|
noRevisionCache bool
|
|
sourceIntegrity *v1alpha1.SourceIntegrity
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
want map[string][]byte
|
|
wantErr assert.ErrorAssertionFunc
|
|
}{
|
|
{name: "ErrorGettingRepos", fields: fields{
|
|
getRepository: func(_ context.Context, _, _ string) (*v1alpha1.Repository, error) {
|
|
return nil, errors.New("unable to get repos")
|
|
},
|
|
}, args: args{}, want: nil, wantErr: assert.Error},
|
|
{name: "ErrorGettingFiles", fields: fields{
|
|
getRepository: func(_ context.Context, _, _ string) (*v1alpha1.Repository, error) {
|
|
return &v1alpha1.Repository{}, nil
|
|
},
|
|
getGitFiles: func(_ context.Context, _ *apiclient.GitFilesRequest) (*apiclient.GitFilesResponse, error) {
|
|
return nil, errors.New("unable to get files")
|
|
},
|
|
}, args: args{}, want: nil, wantErr: assert.Error},
|
|
{name: "HappyCase", fields: fields{
|
|
getRepository: func(_ context.Context, _, _ string) (*v1alpha1.Repository, error) {
|
|
return &v1alpha1.Repository{
|
|
Repo: "foo",
|
|
}, nil
|
|
},
|
|
getGitFiles: func(_ context.Context, _ *apiclient.GitFilesRequest) (*apiclient.GitFilesResponse, error) {
|
|
return &apiclient.GitFilesResponse{
|
|
Map: map[string][]byte{
|
|
"foo.json": []byte("hello: world!"),
|
|
"bar.yaml": []byte("yay: appsets"),
|
|
},
|
|
}, nil
|
|
},
|
|
}, args: args{
|
|
repoURL: "foo",
|
|
}, want: map[string][]byte{
|
|
"foo.json": []byte("hello: world!"),
|
|
"bar.yaml": []byte("yay: appsets"),
|
|
}, wantErr: assert.NoError},
|
|
{name: "ErrorVerifyingCommit", fields: fields{
|
|
getRepository: func(_ context.Context, _, _ string) (*v1alpha1.Repository, error) {
|
|
return &v1alpha1.Repository{}, nil
|
|
},
|
|
getGitFiles: func(_ context.Context, _ *apiclient.GitFilesRequest) (*apiclient.GitFilesResponse, error) {
|
|
return nil, errors.New("revision HEAD is not signed")
|
|
},
|
|
}, args: args{}, want: nil, wantErr: assert.Error},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
a := &repoService{
|
|
getRepository: tt.fields.getRepository,
|
|
submoduleEnabled: tt.fields.submoduleEnabled,
|
|
getGitFilesFromRepoServer: tt.fields.getGitFiles,
|
|
}
|
|
got, err := a.GetFiles(tt.args.ctx, tt.args.repoURL, tt.args.revision, tt.args.pattern, "", tt.args.noRevisionCache, tt.args.sourceIntegrity)
|
|
if !tt.wantErr(t, err, fmt.Sprintf("GetFiles(%v, %v, %v, %v, %v)", tt.args.ctx, tt.args.repoURL, tt.args.revision, tt.args.pattern, tt.args.noRevisionCache)) {
|
|
return
|
|
}
|
|
assert.Equalf(t, tt.want, got, "GetFiles(%v, %v, %v, %v, %v)", tt.args.ctx, tt.args.repoURL, tt.args.revision, tt.args.pattern, tt.args.noRevisionCache)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNewArgoCDService(t *testing.T) {
|
|
t.Parallel()
|
|
testNamespace := "test"
|
|
clientset := fake.NewClientset()
|
|
testDB := db.NewDB(testNamespace, settings.NewSettingsManager(t.Context(), clientset, testNamespace), clientset)
|
|
service := NewArgoCDService(testDB, false, &repo_mocks.Clientset{}, false)
|
|
assert.NotNil(t, service)
|
|
}
|