File: registry_test.go

package info (click to toggle)
gitlab-ci-multi-runner 14.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 31,248 kB
  • sloc: sh: 1,694; makefile: 384; asm: 79; ruby: 68
file content (95 lines) | stat: -rw-r--r-- 2,185 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
//go:build !integration
// +build !integration

package secret_engines

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"gitlab.com/gitlab-org/gitlab-runner/helpers/vault"
	"gitlab.com/gitlab-org/gitlab-runner/helpers/vault/internal/registry"
)

func TestMustRegisterFactory(t *testing.T) {
	factory := func(client vault.Client, path string) vault.SecretEngine {
		return new(vault.MockSecretEngine)
	}

	tests := map[string]struct {
		register      func()
		panicExpected bool
	}{
		"duplicate factory registration": {
			register: func() {
				MustRegisterFactory("test-engine", factory)
				MustRegisterFactory("test-engine", factory)
			},
			panicExpected: true,
		},
		"successful factory registration": {
			register: func() {
				MustRegisterFactory("test-engine", factory)
				MustRegisterFactory("test-engine-2", factory)
			},
		},
	}

	for tn, tt := range tests {
		t.Run(tn, func(t *testing.T) {
			oldFactoriesRegistry := factoriesRegistry
			defer func() {
				factoriesRegistry = oldFactoriesRegistry
			}()
			factoriesRegistry = registry.New("fake registry")

			if tt.panicExpected {
				assert.Panics(t, tt.register)
				return
			}
			assert.NotPanics(t, tt.register)
		})
	}
}

func TestGetFactory(t *testing.T) {
	oldFactoriesRegistry := factoriesRegistry
	defer func() {
		factoriesRegistry = oldFactoriesRegistry
	}()
	factoriesRegistry = registry.New("fake registry")

	require.NotPanics(t, func() {
		MustRegisterFactory("test-engine", func(client vault.Client, path string) vault.SecretEngine {
			return new(vault.MockSecretEngine)
		})
	})

	tests := map[string]struct {
		engineName    string
		expectedError error
	}{
		"factory found": {
			engineName:    "not-existing-engine",
			expectedError: new(registry.FactoryNotRegisteredError),
		},
		"factory not found": {
			engineName: "test-engine",
		},
	}

	for tn, tt := range tests {
		t.Run(tn, func(t *testing.T) {
			factory, err := GetFactory(tt.engineName)
			if tt.expectedError != nil {
				assert.ErrorAs(t, err, &tt.expectedError)
				assert.Nil(t, factory)
				return
			}
			assert.NoError(t, err)
			assert.NotNil(t, factory)
		})
	}
}