github.com/argoproj/argo-cd/v3@v3.2.1/applicationset/services/repo_service_test.go (about)

     1  package services
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"k8s.io/client-go/kubernetes/fake"
    11  
    12  	"github.com/argoproj/argo-cd/v3/reposerver/apiclient"
    13  	repo_mocks "github.com/argoproj/argo-cd/v3/reposerver/apiclient/mocks"
    14  	"github.com/argoproj/argo-cd/v3/util/db"
    15  	"github.com/argoproj/argo-cd/v3/util/settings"
    16  
    17  	"github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
    18  )
    19  
    20  func TestGetDirectories(t *testing.T) {
    21  	type fields struct {
    22  		submoduleEnabled  bool
    23  		getRepository     func(ctx context.Context, url, project string) (*v1alpha1.Repository, error)
    24  		getGitDirectories func(ctx context.Context, req *apiclient.GitDirectoriesRequest) (*apiclient.GitDirectoriesResponse, error)
    25  	}
    26  	type args struct {
    27  		ctx             context.Context
    28  		repoURL         string
    29  		revision        string
    30  		noRevisionCache bool
    31  		verifyCommit    bool
    32  	}
    33  	tests := []struct {
    34  		name    string
    35  		fields  fields
    36  		args    args
    37  		want    []string
    38  		wantErr assert.ErrorAssertionFunc
    39  	}{
    40  		{name: "ErrorGettingRepos", fields: fields{
    41  			getRepository: func(_ context.Context, _, _ string) (*v1alpha1.Repository, error) {
    42  				return nil, errors.New("unable to get repos")
    43  			},
    44  		}, args: args{}, want: nil, wantErr: assert.Error},
    45  		{name: "ErrorGettingDirs", fields: fields{
    46  			getRepository: func(_ context.Context, _, _ string) (*v1alpha1.Repository, error) {
    47  				return &v1alpha1.Repository{}, nil
    48  			},
    49  			getGitDirectories: func(_ context.Context, _ *apiclient.GitDirectoriesRequest) (*apiclient.GitDirectoriesResponse, error) {
    50  				return nil, errors.New("unable to get dirs")
    51  			},
    52  		}, args: args{}, want: nil, wantErr: assert.Error},
    53  		{name: "HappyCase", fields: fields{
    54  			getRepository: func(_ context.Context, _, _ string) (*v1alpha1.Repository, error) {
    55  				return &v1alpha1.Repository{
    56  					Repo: "foo",
    57  				}, nil
    58  			},
    59  			getGitDirectories: func(_ context.Context, _ *apiclient.GitDirectoriesRequest) (*apiclient.GitDirectoriesResponse, error) {
    60  				return &apiclient.GitDirectoriesResponse{
    61  					Paths: []string{"foo", "foo/bar", "bar/foo"},
    62  				}, nil
    63  			},
    64  		}, args: args{
    65  			repoURL: "foo",
    66  		}, want: []string{"foo", "foo/bar", "bar/foo"}, wantErr: assert.NoError},
    67  		{name: "ErrorVerifyingCommit", fields: fields{
    68  			getRepository: func(_ context.Context, _, _ string) (*v1alpha1.Repository, error) {
    69  				return &v1alpha1.Repository{}, nil
    70  			},
    71  			getGitDirectories: func(_ context.Context, _ *apiclient.GitDirectoriesRequest) (*apiclient.GitDirectoriesResponse, error) {
    72  				return nil, errors.New("revision HEAD is not signed")
    73  			},
    74  		}, args: args{}, want: nil, wantErr: assert.Error},
    75  	}
    76  	for _, tt := range tests {
    77  		t.Run(tt.name, func(t *testing.T) {
    78  			a := &argoCDService{
    79  				getRepository:                   tt.fields.getRepository,
    80  				submoduleEnabled:                tt.fields.submoduleEnabled,
    81  				getGitDirectoriesFromRepoServer: tt.fields.getGitDirectories,
    82  			}
    83  			got, err := a.GetDirectories(tt.args.ctx, tt.args.repoURL, tt.args.revision, "", tt.args.noRevisionCache, tt.args.verifyCommit)
    84  			if !tt.wantErr(t, err, fmt.Sprintf("GetDirectories(%v, %v, %v, %v)", tt.args.ctx, tt.args.repoURL, tt.args.revision, tt.args.noRevisionCache)) {
    85  				return
    86  			}
    87  			assert.Equalf(t, tt.want, got, "GetDirectories(%v, %v, %v, %v)", tt.args.ctx, tt.args.repoURL, tt.args.revision, tt.args.noRevisionCache)
    88  		})
    89  	}
    90  }
    91  
    92  func TestGetFiles(t *testing.T) {
    93  	type fields struct {
    94  		submoduleEnabled bool
    95  		getRepository    func(ctx context.Context, url, project string) (*v1alpha1.Repository, error)
    96  		getGitFiles      func(ctx context.Context, req *apiclient.GitFilesRequest) (*apiclient.GitFilesResponse, error)
    97  	}
    98  	type args struct {
    99  		ctx             context.Context
   100  		repoURL         string
   101  		revision        string
   102  		pattern         string
   103  		noRevisionCache bool
   104  		verifyCommit    bool
   105  	}
   106  	tests := []struct {
   107  		name    string
   108  		fields  fields
   109  		args    args
   110  		want    map[string][]byte
   111  		wantErr assert.ErrorAssertionFunc
   112  	}{
   113  		{name: "ErrorGettingRepos", fields: fields{
   114  			getRepository: func(_ context.Context, _, _ string) (*v1alpha1.Repository, error) {
   115  				return nil, errors.New("unable to get repos")
   116  			},
   117  		}, args: args{}, want: nil, wantErr: assert.Error},
   118  		{name: "ErrorGettingFiles", fields: fields{
   119  			getRepository: func(_ context.Context, _, _ string) (*v1alpha1.Repository, error) {
   120  				return &v1alpha1.Repository{}, nil
   121  			},
   122  			getGitFiles: func(_ context.Context, _ *apiclient.GitFilesRequest) (*apiclient.GitFilesResponse, error) {
   123  				return nil, errors.New("unable to get files")
   124  			},
   125  		}, args: args{}, want: nil, wantErr: assert.Error},
   126  		{name: "HappyCase", fields: fields{
   127  			getRepository: func(_ context.Context, _, _ string) (*v1alpha1.Repository, error) {
   128  				return &v1alpha1.Repository{
   129  					Repo: "foo",
   130  				}, nil
   131  			},
   132  			getGitFiles: func(_ context.Context, _ *apiclient.GitFilesRequest) (*apiclient.GitFilesResponse, error) {
   133  				return &apiclient.GitFilesResponse{
   134  					Map: map[string][]byte{
   135  						"foo.json": []byte("hello: world!"),
   136  						"bar.yaml": []byte("yay: appsets"),
   137  					},
   138  				}, nil
   139  			},
   140  		}, args: args{
   141  			repoURL: "foo",
   142  		}, want: map[string][]byte{
   143  			"foo.json": []byte("hello: world!"),
   144  			"bar.yaml": []byte("yay: appsets"),
   145  		}, wantErr: assert.NoError},
   146  		{name: "ErrorVerifyingCommit", fields: fields{
   147  			getRepository: func(_ context.Context, _, _ string) (*v1alpha1.Repository, error) {
   148  				return &v1alpha1.Repository{}, nil
   149  			},
   150  			getGitFiles: func(_ context.Context, _ *apiclient.GitFilesRequest) (*apiclient.GitFilesResponse, error) {
   151  				return nil, errors.New("revision HEAD is not signed")
   152  			},
   153  		}, args: args{}, want: nil, wantErr: assert.Error},
   154  	}
   155  	for _, tt := range tests {
   156  		t.Run(tt.name, func(t *testing.T) {
   157  			a := &argoCDService{
   158  				getRepository:             tt.fields.getRepository,
   159  				submoduleEnabled:          tt.fields.submoduleEnabled,
   160  				getGitFilesFromRepoServer: tt.fields.getGitFiles,
   161  			}
   162  			got, err := a.GetFiles(tt.args.ctx, tt.args.repoURL, tt.args.revision, tt.args.pattern, "", tt.args.noRevisionCache, tt.args.verifyCommit)
   163  			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)) {
   164  				return
   165  			}
   166  			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)
   167  		})
   168  	}
   169  }
   170  
   171  func TestNewArgoCDService(t *testing.T) {
   172  	testNamespace := "test"
   173  	clientset := fake.NewClientset()
   174  	testDB := db.NewDB(testNamespace, settings.NewSettingsManager(t.Context(), clientset, testNamespace), clientset)
   175  	service := NewArgoCDService(testDB, false, &repo_mocks.Clientset{}, false)
   176  	assert.NotNil(t, service)
   177  }