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,86 @@ 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"
107
-
108
- attributes.Put(constants.ExternalDevWorkspaceConfiguration, externalDWOCMeta , nil)
110
+ namespacedName := types. NamespacedName {
111
+ Name : "external-config-name" ,
112
+ Namespace : "external-config-namespace" ,
113
+ }
114
+ attributes .Put (constants .ExternalDevWorkspaceConfiguration , namespacedName , nil )
109
115
workspace .Spec .Template .DevWorkspaceTemplateSpecContent = dw.DevWorkspaceTemplateSpecContent {
110
116
Attributes : attributes ,
111
117
}
112
-
113
118
client := fake .NewClientBuilder ().WithScheme (scheme ).Build ()
114
119
115
- err := ApplyExternalDWOCConfig (workspace, client)
120
+ resolvedConfig , err := ResolveConfigForWorkspace (workspace , client )
116
121
if ! assert .Error (t , err , "Error should be given if external DWOC specified in workspace spec does not exist" ) {
117
122
return
118
123
}
124
+ assert .Equal (t , resolvedConfig , internalConfig , "Internal/Global DWOC should be used as fallback if external DWOC could not be resolved" )
119
125
}
120
126
121
- func TestConfigUpdatedAfterMerge (t *testing.T) {
127
+ func TestMergeExternalConfig (t * testing.T ) {
122
128
setupForTest (t )
123
129
124
130
workspace := & dw.DevWorkspace {}
125
131
attributes := attributes.Attributes {}
126
- externalDWOCMeta := v1alpha1.ExternalConfig{}
127
- externalDWOCMeta.Name = "external-config-name"
128
- externalDWOCMeta.Namespace = "external-config-namespace"
132
+ namespacedName := types.NamespacedName {
133
+ Name : ExternalConfigName ,
134
+ Namespace : ExternalConfigNamespace ,
135
+ }
129
136
130
- attributes.Put(constants.ExternalDevWorkspaceConfiguration, externalDWOCMeta , nil)
137
+ attributes .Put (constants .ExternalDevWorkspaceConfiguration , namespacedName , nil )
131
138
workspace .Spec .Template .DevWorkspaceTemplateSpecContent = dw.DevWorkspaceTemplateSpecContent {
132
139
Attributes : attributes ,
133
140
}
134
141
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
- })
142
+ clusterConfig := buildConfig (defaultConfig .DeepCopy ())
143
+ originalInternalConfig := clusterConfig .Config .DeepCopy ()
144
+
145
+ // External config is based off of the default/internal config, with just a few changes made
146
+ // So when the internal config is merged with the external one, they will become identical
147
+ externalConfig := buildExternalConfig (defaultConfig .DeepCopy ())
148
+ externalConfig .Config .Workspace .ImagePullPolicy = "Always"
149
+ externalConfig .Config .Workspace .PVCName = "test-PVC-name"
150
+ storageSize := resource .MustParse ("15Gi" )
151
+ externalConfig .Config .Workspace .DefaultStorageSize = & v1alpha1.StorageSizes {
152
+ Common : & storageSize ,
153
+ PerWorkspace : & storageSize ,
154
+ }
158
155
159
156
client := fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (clusterConfig ).WithObjects (externalConfig ).Build ()
157
+ err := SetupControllerConfig (client )
158
+ if ! assert .NoError (t , err , "Should not return error" ) {
159
+ return
160
+ }
161
+
162
+ // Sanity check
163
+ if ! cmp .Equal (clusterConfig .Config , internalConfig ) {
164
+ t .Error ("Internal config should be same as cluster config before starting:" , cmp .Diff (clusterConfig .Config , internalConfig ))
165
+ }
160
166
161
- err := ApplyExternalDWOCConfig (workspace, client)
167
+ resolvedConfig , err := ResolveConfigForWorkspace (workspace , client )
162
168
if ! assert .NoError (t , err , "Should not return error" ) {
163
169
return
164
170
}
165
171
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")
172
+ // Compare the resolved config and external config
173
+ if ! cmp .Equal (resolvedConfig , externalConfig .Config ) {
174
+ t .Error ("Resolved config and external config should match after merge:" , cmp .Diff (resolvedConfig , externalConfig .Config ))
175
+ }
176
+
177
+ // Ensure the internal config was not affected by merge
178
+ if ! cmp .Equal (originalInternalConfig , internalConfig ) {
179
+ t .Error ("Internal config should remain the same after merge" )
169
180
}
170
181
171
182
// Get the global config off cluster and ensure it hasn't changed
172
183
retrievedClusterConfig := & v1alpha1.DevWorkspaceOperatorConfig {}
173
- namespacedName : = types.NamespacedName{
184
+ namespacedName = types.NamespacedName {
174
185
Name : OperatorConfigName ,
175
186
Namespace : testNamespace ,
176
187
}
@@ -180,10 +191,10 @@ func TestConfigUpdatedAfterMerge(t *testing.T) {
180
191
}
181
192
182
193
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" )
194
+ 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
195
}
185
196
}
186
- */
197
+
187
198
func TestSetupControllerAlwaysSetsDefaultClusterRoutingSuffix (t * testing.T ) {
188
199
setupForTest (t )
189
200
infrastructure .InitializeForTesting (infrastructure .OpenShiftv4 )
0 commit comments