crossplane

Форк
0
207 строк · 5.2 Кб
1
/*
2
Copyright 2023 The Crossplane Authors.
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
    http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
package credhelper
18

19
import (
20
	"testing"
21

22
	"github.com/docker/docker-credential-helpers/credentials"
23
	"github.com/google/go-cmp/cmp"
24

25
	"github.com/crossplane/crossplane-runtime/pkg/errors"
26
	"github.com/crossplane/crossplane-runtime/pkg/test"
27

28
	"github.com/crossplane/crossplane/internal/xpkg/upbound/config"
29
)
30

31
// TODO(hasheddan): these tests are testing through to the underlying config
32
// package more than we would like. We should consider refactoring the config
33
// package to make it more mockable.
34

35
var _ credentials.Helper = &Helper{}
36

37
func TestGet(t *testing.T) {
38
	testServer := "xpkg.upbound.io"
39
	testProfile := "test"
40
	testSecret := "supersecretvalue"
41
	errBoom := errors.New("boom")
42
	type args struct {
43
		server string
44
	}
45
	type want struct {
46
		user   string
47
		secret string
48
		err    error
49
	}
50
	cases := map[string]struct {
51
		reason string
52
		args   args
53
		opts   []Opt
54
		want   want
55
	}{
56
		"ErrorUnsupportedDomain": {
57
			reason: "Should return error if domain is not supported.",
58
			args: args{
59
				server: testServer,
60
			},
61
			opts: []Opt{
62
				WithDomain("registry.upbound.io"),
63
			},
64
			want: want{
65
				err: errors.New(errUnsupportedDomain),
66
			},
67
		},
68
		"ErrorInitializeSource": {
69
			reason: "Should return error if we fail to initialize source.",
70
			args: args{
71
				server: testServer,
72
			},
73
			opts: []Opt{
74
				WithSource(&config.MockSource{
75
					InitializeFn: func() error {
76
						return errBoom
77
					},
78
				}),
79
			},
80
			want: want{
81
				err: errors.Wrap(errBoom, errInitializeSource),
82
			},
83
		},
84
		"ErrorExtractConfig": {
85
			reason: "Should return error if we fail to extract config.",
86
			args: args{
87
				server: testServer,
88
			},
89
			opts: []Opt{
90
				WithSource(&config.MockSource{
91
					InitializeFn: func() error {
92
						return nil
93
					},
94
					GetConfigFn: func() (*config.Config, error) {
95
						return nil, errBoom
96
					},
97
				}),
98
			},
99
			want: want{
100
				err: errors.Wrap(errBoom, errExtractConfig),
101
			},
102
		},
103
		"ErrorGetDefault": {
104
			reason: "If no profile is specified and we fail to get default return error.",
105
			args: args{
106
				server: testServer,
107
			},
108
			opts: []Opt{
109
				WithSource(&config.MockSource{
110
					InitializeFn: func() error {
111
						return nil
112
					},
113
					GetConfigFn: func() (*config.Config, error) {
114
						return &config.Config{}, nil
115
					},
116
				}),
117
			},
118
			want: want{
119
				err: errors.Wrap(errors.New("no default profile specified"), errGetDefaultProfile),
120
			},
121
		},
122
		"ErrorGetProfile": {
123
			reason: "If we fail to get the specified profile return error.",
124
			args: args{
125
				server: testServer,
126
			},
127
			opts: []Opt{
128
				WithProfile(testProfile),
129
				WithSource(&config.MockSource{
130
					InitializeFn: func() error {
131
						return nil
132
					},
133
					GetConfigFn: func() (*config.Config, error) {
134
						return &config.Config{}, nil
135
					},
136
				}),
137
			},
138
			want: want{
139
				err: errors.Wrap(errors.Errorf("profile not found with identifier: %s", testProfile), errGetProfile),
140
			},
141
		},
142
		"Success": {
143
			reason: "If we successfully get profile return credentials.",
144
			args: args{
145
				server: testServer,
146
			},
147
			opts: []Opt{
148
				WithProfile(testProfile),
149
				WithSource(&config.MockSource{
150
					InitializeFn: func() error {
151
						return nil
152
					},
153
					GetConfigFn: func() (*config.Config, error) {
154
						return &config.Config{
155
							Upbound: config.Upbound{
156
								Profiles: map[string]config.Profile{
157
									testProfile: {
158
										Session: testSecret,
159
									},
160
								},
161
							},
162
						}, nil
163
					},
164
				}),
165
			},
166
			want: want{
167
				user:   defaultDockerUser,
168
				secret: testSecret,
169
			},
170
		},
171
	}
172
	for name, tc := range cases {
173
		t.Run(name, func(t *testing.T) {
174
			user, secret, err := New(tc.opts...).Get(tc.args.server)
175
			if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
176
				t.Errorf("\n%s\nGet(...): -want error, +got error:\n%s", tc.reason, diff)
177
			}
178
			if diff := cmp.Diff(tc.want.user, user); diff != "" {
179
				t.Errorf("\n%s\nGet(...): -want user, +got user:\n%s", tc.reason, diff)
180
			}
181
			if diff := cmp.Diff(tc.want.secret, secret); diff != "" {
182
				t.Errorf("\n%s\nGet(...): -want secret, +got secret:\n%s", tc.reason, diff)
183
			}
184
		})
185
	}
186
}
187

188
func TestAdd(t *testing.T) {
189
	err := New().Add(nil)
190
	if diff := cmp.Diff(errors.New(errUnimplemented), err, test.EquateErrors()); diff != "" {
191
		t.Errorf("\nAdd(...): -want error, +got error:\n%s", diff)
192
	}
193
}
194

195
func TestDelete(t *testing.T) {
196
	err := New().Delete("")
197
	if diff := cmp.Diff(errors.New(errUnimplemented), err, test.EquateErrors()); diff != "" {
198
		t.Errorf("\nDelete(...): -want error, +got error:\n%s", diff)
199
	}
200
}
201

202
func TestList(t *testing.T) {
203
	_, err := New().List()
204
	if diff := cmp.Diff(errors.New(errUnimplemented), err, test.EquateErrors()); diff != "" {
205
		t.Errorf("\nList(...): -want error, +got error:\n%s", diff)
206
	}
207
}
208

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.