File: poller_test.go

package info (click to toggle)
gitlab-agent 16.11.5-1
  • links: PTS, VCS
  • area: contrib
  • in suites: experimental
  • size: 7,072 kB
  • sloc: makefile: 193; sh: 55; ruby: 3
file content (106 lines) | stat: -rw-r--r-- 2,900 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package gitaly

import (
	"context"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/gitaly/vendored/gitalypb"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/testing/matcher"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/testing/mock_gitaly"
	"go.uber.org/mock/gomock"
)

const (
	revision1 = "507ebc6de9bcac25628aa7afd52802a91a0685d8"
	revision2 = "28aa7afd52802a91a0685d8507ebc6de9bcac256"
)

var (
	_ PollerInterface = &Poller{}
)

func TestPoller_HappyPath(t *testing.T) {
	tests := []struct {
		name                string
		lastProcessedCommit string
		expectedInfoCommit  string
		expectedInfoUpdate  bool
	}{
		{
			name:                "same commit",
			lastProcessedCommit: revision1,
			expectedInfoCommit:  revision1,
			expectedInfoUpdate:  false,
		},
		{
			name:                "no commit",
			lastProcessedCommit: "",
			expectedInfoCommit:  revision1,
			expectedInfoUpdate:  true,
		},
		{
			name:                "another commit",
			lastProcessedCommit: revision2,
			expectedInfoCommit:  revision1,
			expectedInfoUpdate:  true,
		},
	}
	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			ctrl := gomock.NewController(t)
			r := repo()
			client := mock_gitaly.NewMockCommitServiceClient(ctrl)
			features := map[string]string{
				"f1": "true",
			}
			client.EXPECT().
				FindCommit(matcher.GRPCOutgoingCtx(features), matcher.ProtoEq(nil, &gitalypb.FindCommitRequest{
					Repository: r,
					Revision:   []byte(DefaultBranch),
				}), gomock.Any()).
				Return(&gitalypb.FindCommitResponse{
					Commit: &gitalypb.GitCommit{
						Id: revision1,
					},
				}, nil)
			p := Poller{
				Client:   client,
				Features: features,
			}
			pollInfo, err := p.Poll(context.Background(), r, tc.lastProcessedCommit, DefaultBranch)
			require.NoError(t, err)
			assert.Equal(t, tc.expectedInfoUpdate, pollInfo.UpdateAvailable)
			assert.Equal(t, tc.expectedInfoCommit, pollInfo.CommitID)
		})
	}
}

func TestPoller_RefNotFound(t *testing.T) {
	ctrl := gomock.NewController(t)
	r := repo()
	client := mock_gitaly.NewMockCommitServiceClient(ctrl)
	client.EXPECT().
		FindCommit(gomock.Any(), matcher.ProtoEq(nil, &gitalypb.FindCommitRequest{
			Repository: r,
			Revision:   []byte(DefaultBranch),
		}), gomock.Any()).
		Return(&gitalypb.FindCommitResponse{}, nil)
	p := Poller{
		Client: client,
	}
	pollInfo, err := p.Poll(context.Background(), r, "", DefaultBranch)
	require.NoError(t, err)
	assert.True(t, pollInfo.RefNotFound)
}

func repo() *gitalypb.Repository {
	return &gitalypb.Repository{
		StorageName:        "StorageName",
		RelativePath:       "RelativePath",
		GitObjectDirectory: "GitObjectDirectory",
		GlRepository:       "GlRepository",
		GlProjectPath:      "GlProjectPath",
	}
}