crossplane
173 строки · 4.4 Кб
1/*
2Copyright 2023 The Crossplane Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package config
18
19import (
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
30var _ Source = &FSSource{}
31var _ Source = &MockSource{}
32
33// TODO(hasheddan): a mock afero.Fs could increase test coverage here with
34// simulated failed file opens and writes.
35
36func TestInitialize(t *testing.T) {
37cases := map[string]struct {
38reason string
39modifiers []FSSourceModifier
40want error
41}{
42"SuccessNotSuppliedNotExists": {
43reason: "If path is not supplied use default and create if not exist.",
44modifiers: []FSSourceModifier{
45func(f *FSSource) {
46f.fs = afero.NewMemMapFs()
47},
48},
49},
50"SuccessSuppliedNotExists": {
51reason: "If path is supplied but doesn't already exist create it.",
52modifiers: []FSSourceModifier{
53func(f *FSSource) {
54f.path = "/.up/config.json"
55f.fs = afero.NewMemMapFs()
56},
57},
58},
59"SuccessSuppliedExists": {
60reason: "If path is supplied and already exists do not return error.",
61modifiers: []FSSourceModifier{
62func(f *FSSource) {
63f.path = "/.up/config.json"
64fs := afero.NewMemMapFs()
65file, _ := fs.Create("/.up/config.json")
66defer file.Close()
67f.fs = fs
68},
69},
70},
71}
72for name, tc := range cases {
73t.Run(name, func(t *testing.T) {
74src := NewFSSource(tc.modifiers...)
75err := src.Initialize()
76if diff := cmp.Diff(tc.want, err, test.EquateErrors()); diff != "" {
77t.Errorf("\n%s\nInitialize(...): -want error, +got error:\n%s", tc.reason, diff)
78}
79})
80}
81}
82
83func TestGetConfig(t *testing.T) {
84testConf := &Config{
85Upbound: Upbound{
86Default: "test",
87},
88}
89cases := map[string]struct {
90reason string
91modifiers []FSSourceModifier
92want *Config
93err error
94}{
95"SuccessfulEmptyConfig": {
96reason: "An empty file should return an empty config.",
97modifiers: []FSSourceModifier{
98func(f *FSSource) {
99f.fs = afero.NewMemMapFs()
100},
101},
102want: &Config{},
103},
104"Successful": {
105reason: "If we are able to get config we should it.",
106modifiers: []FSSourceModifier{
107func(f *FSSource) {
108f.path = "/.up/config.json"
109fs := afero.NewMemMapFs()
110file, _ := fs.OpenFile("/.up/config.json", os.O_CREATE, 0600)
111defer file.Close()
112b, _ := json.Marshal(testConf) //nolint:errchkjson // marshalling should not fail
113_, _ = file.Write(b)
114f.fs = fs
115},
116},
117want: testConf,
118},
119}
120for name, tc := range cases {
121t.Run(name, func(t *testing.T) {
122src := NewFSSource(tc.modifiers...)
123conf, err := src.GetConfig()
124if diff := cmp.Diff(tc.err, err, test.EquateErrors()); diff != "" {
125t.Errorf("\n%s\nGetConfig(...): -want error, +got error:\n%s", tc.reason, diff)
126}
127if diff := cmp.Diff(tc.want, conf); diff != "" {
128t.Errorf("\n%s\nGetConfig(...): -want, +got:\n%s", tc.reason, diff)
129}
130})
131}
132}
133
134func TestUpdateConfig(t *testing.T) {
135testConf := &Config{
136Upbound: Upbound{
137Default: "test",
138},
139}
140cases := map[string]struct {
141reason string
142modifiers []FSSourceModifier
143conf *Config
144err error
145}{
146"EmptyConfig": {
147reason: "Updating with empty config should not cause an error.",
148modifiers: []FSSourceModifier{
149func(f *FSSource) {
150f.fs = afero.NewMemMapFs()
151},
152},
153},
154"PopulatedConfig": {
155reason: "Updating with populated config should not cause an error.",
156modifiers: []FSSourceModifier{
157func(f *FSSource) {
158f.fs = afero.NewMemMapFs()
159},
160},
161conf: testConf,
162},
163}
164for name, tc := range cases {
165t.Run(name, func(t *testing.T) {
166src := NewFSSource(tc.modifiers...)
167err := src.UpdateConfig(tc.conf)
168if diff := cmp.Diff(tc.err, err, test.EquateErrors()); diff != "" {
169t.Errorf("\n%s\nUpdateConfig(...): -want error, +got error:\n%s", tc.reason, diff)
170}
171})
172}
173}
174