crossplane

Форк
0
173 строки · 4.4 Кб
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 config
18

19
import (
20
	"encoding/json"
21
	"os"
22
	"testing"
23

24
	"github.com/google/go-cmp/cmp"
25
	"github.com/spf13/afero"
26

27
	"github.com/crossplane/crossplane-runtime/pkg/test"
28
)
29

30
var _ Source = &FSSource{}
31
var _ Source = &MockSource{}
32

33
// TODO(hasheddan): a mock afero.Fs could increase test coverage here with
34
// simulated failed file opens and writes.
35

36
func TestInitialize(t *testing.T) {
37
	cases := map[string]struct {
38
		reason    string
39
		modifiers []FSSourceModifier
40
		want      error
41
	}{
42
		"SuccessNotSuppliedNotExists": {
43
			reason: "If path is not supplied use default and create if not exist.",
44
			modifiers: []FSSourceModifier{
45
				func(f *FSSource) {
46
					f.fs = afero.NewMemMapFs()
47
				},
48
			},
49
		},
50
		"SuccessSuppliedNotExists": {
51
			reason: "If path is supplied but doesn't already exist create it.",
52
			modifiers: []FSSourceModifier{
53
				func(f *FSSource) {
54
					f.path = "/.up/config.json"
55
					f.fs = afero.NewMemMapFs()
56
				},
57
			},
58
		},
59
		"SuccessSuppliedExists": {
60
			reason: "If path is supplied and already exists do not return error.",
61
			modifiers: []FSSourceModifier{
62
				func(f *FSSource) {
63
					f.path = "/.up/config.json"
64
					fs := afero.NewMemMapFs()
65
					file, _ := fs.Create("/.up/config.json")
66
					defer file.Close()
67
					f.fs = fs
68
				},
69
			},
70
		},
71
	}
72
	for name, tc := range cases {
73
		t.Run(name, func(t *testing.T) {
74
			src := NewFSSource(tc.modifiers...)
75
			err := src.Initialize()
76
			if diff := cmp.Diff(tc.want, err, test.EquateErrors()); diff != "" {
77
				t.Errorf("\n%s\nInitialize(...): -want error, +got error:\n%s", tc.reason, diff)
78
			}
79
		})
80
	}
81
}
82

83
func TestGetConfig(t *testing.T) {
84
	testConf := &Config{
85
		Upbound: Upbound{
86
			Default: "test",
87
		},
88
	}
89
	cases := map[string]struct {
90
		reason    string
91
		modifiers []FSSourceModifier
92
		want      *Config
93
		err       error
94
	}{
95
		"SuccessfulEmptyConfig": {
96
			reason: "An empty file should return an empty config.",
97
			modifiers: []FSSourceModifier{
98
				func(f *FSSource) {
99
					f.fs = afero.NewMemMapFs()
100
				},
101
			},
102
			want: &Config{},
103
		},
104
		"Successful": {
105
			reason: "If we are able to get config we should it.",
106
			modifiers: []FSSourceModifier{
107
				func(f *FSSource) {
108
					f.path = "/.up/config.json"
109
					fs := afero.NewMemMapFs()
110
					file, _ := fs.OpenFile("/.up/config.json", os.O_CREATE, 0600)
111
					defer file.Close()
112
					b, _ := json.Marshal(testConf) //nolint:errchkjson // marshalling should not fail
113
					_, _ = file.Write(b)
114
					f.fs = fs
115
				},
116
			},
117
			want: testConf,
118
		},
119
	}
120
	for name, tc := range cases {
121
		t.Run(name, func(t *testing.T) {
122
			src := NewFSSource(tc.modifiers...)
123
			conf, err := src.GetConfig()
124
			if diff := cmp.Diff(tc.err, err, test.EquateErrors()); diff != "" {
125
				t.Errorf("\n%s\nGetConfig(...): -want error, +got error:\n%s", tc.reason, diff)
126
			}
127
			if diff := cmp.Diff(tc.want, conf); diff != "" {
128
				t.Errorf("\n%s\nGetConfig(...): -want, +got:\n%s", tc.reason, diff)
129
			}
130
		})
131
	}
132
}
133

134
func TestUpdateConfig(t *testing.T) {
135
	testConf := &Config{
136
		Upbound: Upbound{
137
			Default: "test",
138
		},
139
	}
140
	cases := map[string]struct {
141
		reason    string
142
		modifiers []FSSourceModifier
143
		conf      *Config
144
		err       error
145
	}{
146
		"EmptyConfig": {
147
			reason: "Updating with empty config should not cause an error.",
148
			modifiers: []FSSourceModifier{
149
				func(f *FSSource) {
150
					f.fs = afero.NewMemMapFs()
151
				},
152
			},
153
		},
154
		"PopulatedConfig": {
155
			reason: "Updating with populated config should not cause an error.",
156
			modifiers: []FSSourceModifier{
157
				func(f *FSSource) {
158
					f.fs = afero.NewMemMapFs()
159
				},
160
			},
161
			conf: testConf,
162
		},
163
	}
164
	for name, tc := range cases {
165
		t.Run(name, func(t *testing.T) {
166
			src := NewFSSource(tc.modifiers...)
167
			err := src.UpdateConfig(tc.conf)
168
			if diff := cmp.Diff(tc.err, err, test.EquateErrors()); diff != "" {
169
				t.Errorf("\n%s\nUpdateConfig(...): -want error, +got error:\n%s", tc.reason, diff)
170
			}
171
		})
172
	}
173
}
174

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

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

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

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