16
16
package config
17
17
18
18
import (
19
+ "context"
19
20
"fmt"
20
21
"testing"
21
22
23
+ dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
24
+ attributes "github.com/devfile/api/v2/pkg/attributes"
22
25
"github.com/google/go-cmp/cmp"
23
26
fuzz "github.com/google/gofuzz"
24
27
routev1 "github.com/openshift/api/route/v1"
25
28
"github.com/stretchr/testify/assert"
29
+ "k8s.io/apimachinery/pkg/api/resource"
26
30
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
31
"k8s.io/apimachinery/pkg/runtime"
32
+ "k8s.io/apimachinery/pkg/types"
28
33
"sigs.k8s.io/controller-runtime/pkg/client/fake"
29
34
30
35
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
36
+ "github.com/devfile/devworkspace-operator/pkg/constants"
31
37
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
32
38
)
33
39
@@ -96,81 +102,90 @@ func TestSetupControllerMergesClusterConfig(t *testing.T) {
96
102
assert .Equal (t , internalConfig .Workspace , Workspace , fmt .Sprintf ("Changes to config should be propagated to exported fields" ))
97
103
}
98
104
99
- /* func TestCatchesNonExistentExternalDWOC(t *testing.T) {
105
+ func TestCatchesNonExistentExternalDWOC (t * testing.T ) {
100
106
setupForTest (t )
101
107
102
108
workspace := & dw.DevWorkspace {}
103
109
attributes := attributes.Attributes {}
104
- externalDWOCMeta := v1alpha1.ExternalConfig{}
105
- externalDWOCMeta.Name = "external-config-name"
106
- externalDWOCMeta.Namespace = "external-config-namespace"
110
+ namespacedName := types.NamespacedName {
111
+ Name : "external-config-name" ,
112
+ Namespace : "external-config-namespace" ,
113
+ }
107
114
108
- attributes.Put(constants.ExternalDevWorkspaceConfiguration, externalDWOCMeta , nil)
115
+ attributes .Put (constants .ExternalDevWorkspaceConfiguration , namespacedName , nil )
109
116
workspace .Spec .Template .DevWorkspaceTemplateSpecContent = dw.DevWorkspaceTemplateSpecContent {
110
117
Attributes : attributes ,
111
118
}
112
119
113
120
client := fake .NewClientBuilder ().WithScheme (scheme ).Build ()
114
121
115
- err := ApplyExternalDWOCConfig (workspace, client)
122
+ resolvedConfig , err := ResolveConfigForWorkspace (workspace , client )
116
123
if ! assert .Error (t , err , "Error should be given if external DWOC specified in workspace spec does not exist" ) {
117
124
return
118
125
}
126
+
127
+ assert .Equal (t , resolvedConfig , internalConfig , "Internal/Global DWOC should be used as fallback if external DWOC could not be resolved" )
128
+
119
129
}
120
130
121
- func TestConfigUpdatedAfterMerge (t *testing.T) {
131
+ func TestMergeExternalConfig (t * testing.T ) {
122
132
setupForTest (t )
123
133
124
134
workspace := & dw.DevWorkspace {}
125
135
attributes := attributes.Attributes {}
126
- externalDWOCMeta := v1alpha1.ExternalConfig{}
127
- externalDWOCMeta.Name = "external-config-name"
128
- externalDWOCMeta.Namespace = "external-config-namespace"
136
+ namespacedName := types.NamespacedName {
137
+ Name : ExternalConfigName ,
138
+ Namespace : ExternalConfigNamespace ,
139
+ }
129
140
130
- attributes.Put(constants.ExternalDevWorkspaceConfiguration, externalDWOCMeta , nil)
141
+ attributes .Put (constants .ExternalDevWorkspaceConfiguration , namespacedName , nil )
131
142
workspace .Spec .Template .DevWorkspaceTemplateSpecContent = dw.DevWorkspaceTemplateSpecContent {
132
143
Attributes : attributes ,
133
144
}
134
145
135
- clusterConfig := buildConfig(&v1alpha1.OperatorConfiguration{
136
- Routing: &v1alpha1.RoutingConfig{
137
- DefaultRoutingClass: "test-routingClass",
138
- ClusterHostSuffix: "192.168.0.1.nip.io",
139
- },
140
- Workspace: &v1alpha1.WorkspaceConfig{
141
- ImagePullPolicy: "IfNotPresent",
142
- },
143
- EnableExperimentalFeatures: &trueBool,
144
- })
145
-
146
- internalConfig = clusterConfig.Config.DeepCopy()
147
-
148
- externalConfig := buildExternalConfig(&v1alpha1.OperatorConfiguration{
149
- Routing: &v1alpha1.RoutingConfig{
150
- DefaultRoutingClass: "test-routingClass",
151
- ClusterHostSuffix: "192.168.0.1.nip.io",
152
- },
153
- Workspace: &v1alpha1.WorkspaceConfig{
154
- ImagePullPolicy: "Always",
155
- },
156
- EnableExperimentalFeatures: &trueBool,
157
- })
146
+ clusterConfig := buildConfig (defaultConfig .DeepCopy ())
147
+ originalInternalConfig := clusterConfig .Config .DeepCopy ()
148
+
149
+ // External config is based off of the default/internal config, with just a few changes made
150
+ // So when the internal config is merged with the external one, they will become identical
151
+ externalConfig := buildExternalConfig (defaultConfig .DeepCopy ())
152
+ externalConfig .Config .Workspace .ImagePullPolicy = "Always"
153
+ externalConfig .Config .Workspace .PVCName = "test-PVC-name"
154
+ storageSize := resource .MustParse ("15Gi" )
155
+ externalConfig .Config .Workspace .DefaultStorageSize = & v1alpha1.StorageSizes {
156
+ Common : & storageSize ,
157
+ PerWorkspace : & storageSize ,
158
+ }
158
159
159
160
client := fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (clusterConfig ).WithObjects (externalConfig ).Build ()
161
+ err := SetupControllerConfig (client )
162
+ if ! assert .NoError (t , err , "Should not return error" ) {
163
+ return
164
+ }
165
+
166
+ // Sanity check
167
+ if ! cmp .Equal (clusterConfig .Config , internalConfig ) {
168
+ t .Error ("Internal config should be same as cluster config before starting:" , cmp .Diff (originalInternalConfig , internalConfig ))
169
+ }
160
170
161
- err := ApplyExternalDWOCConfig (workspace, client)
171
+ resolvedConfig , err := ResolveConfigForWorkspace (workspace , client )
162
172
if ! assert .NoError (t , err , "Should not return error" ) {
163
173
return
164
174
}
165
175
166
- // Compare the internal config and external config
167
- if !cmp.Equal(internalConfig, externalConfig.Config) {
168
- t.Error("Internal config and external config should match after merge")
176
+ // Compare the resolved config and external config
177
+ if ! cmp .Equal (resolvedConfig , externalConfig .Config ) {
178
+ t .Error ("Resolved config and external config should match after merge:" , cmp .Diff (resolvedConfig , externalConfig .Config ))
179
+ }
180
+
181
+ // Ensure the internal config was not affected by merge
182
+ if ! cmp .Equal (originalInternalConfig , internalConfig ) {
183
+ t .Error ("Internal config should remain the same after merge" )
169
184
}
170
185
171
186
// Get the global config off cluster and ensure it hasn't changed
172
187
retrievedClusterConfig := & v1alpha1.DevWorkspaceOperatorConfig {}
173
- namespacedName : = types.NamespacedName{
188
+ namespacedName = types.NamespacedName {
174
189
Name : OperatorConfigName ,
175
190
Namespace : testNamespace ,
176
191
}
@@ -180,10 +195,10 @@ func TestConfigUpdatedAfterMerge(t *testing.T) {
180
195
}
181
196
182
197
if ! cmp .Equal (retrievedClusterConfig .Config , clusterConfig .Config ) {
183
- t.Error("Config on cluster and global config should match after merge; global config should not have been modified from merge" )
198
+ t .Error ("Config on cluster and global config should match after merge; global config should not have been modified from merge:" , cmp . Diff ( retrievedClusterConfig , clusterConfig . Config ) )
184
199
}
185
200
}
186
- */
201
+
187
202
func TestSetupControllerAlwaysSetsDefaultClusterRoutingSuffix (t * testing.T ) {
188
203
setupForTest (t )
189
204
infrastructure .InitializeForTesting (infrastructure .OpenShiftv4 )
0 commit comments