Skip to content

Commit 592b788

Browse files
committed
Rework sync tests for external DWOC
1 parent 86e7f68 commit 592b788

File tree

1 file changed

+56
-41
lines changed

1 file changed

+56
-41
lines changed

pkg/config/sync_test.go

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,24 @@
1616
package config
1717

1818
import (
19+
"context"
1920
"fmt"
2021
"testing"
2122

23+
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
24+
attributes "github.com/devfile/api/v2/pkg/attributes"
2225
"github.com/google/go-cmp/cmp"
2326
fuzz "github.com/google/gofuzz"
2427
routev1 "github.com/openshift/api/route/v1"
2528
"github.com/stretchr/testify/assert"
29+
"k8s.io/apimachinery/pkg/api/resource"
2630
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2731
"k8s.io/apimachinery/pkg/runtime"
32+
"k8s.io/apimachinery/pkg/types"
2833
"sigs.k8s.io/controller-runtime/pkg/client/fake"
2934

3035
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
36+
"github.com/devfile/devworkspace-operator/pkg/constants"
3137
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
3238
)
3339

@@ -96,81 +102,90 @@ func TestSetupControllerMergesClusterConfig(t *testing.T) {
96102
assert.Equal(t, internalConfig.Workspace, Workspace, fmt.Sprintf("Changes to config should be propagated to exported fields"))
97103
}
98104

99-
/* func TestCatchesNonExistentExternalDWOC(t *testing.T) {
105+
func TestCatchesNonExistentExternalDWOC(t *testing.T) {
100106
setupForTest(t)
101107

102108
workspace := &dw.DevWorkspace{}
103109
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+
}
107114

108-
attributes.Put(constants.ExternalDevWorkspaceConfiguration, externalDWOCMeta, nil)
115+
attributes.Put(constants.ExternalDevWorkspaceConfiguration, namespacedName, nil)
109116
workspace.Spec.Template.DevWorkspaceTemplateSpecContent = dw.DevWorkspaceTemplateSpecContent{
110117
Attributes: attributes,
111118
}
112119

113120
client := fake.NewClientBuilder().WithScheme(scheme).Build()
114121

115-
err := ApplyExternalDWOCConfig(workspace, client)
122+
resolvedConfig, err := ResolveConfigForWorkspace(workspace, client)
116123
if !assert.Error(t, err, "Error should be given if external DWOC specified in workspace spec does not exist") {
117124
return
118125
}
126+
127+
assert.Equal(t, resolvedConfig, internalConfig, "Internal/Global DWOC should be used as fallback if external DWOC could not be resolved")
128+
119129
}
120130

121-
func TestConfigUpdatedAfterMerge(t *testing.T) {
131+
func TestMergeExternalConfig(t *testing.T) {
122132
setupForTest(t)
123133

124134
workspace := &dw.DevWorkspace{}
125135
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+
}
129140

130-
attributes.Put(constants.ExternalDevWorkspaceConfiguration, externalDWOCMeta, nil)
141+
attributes.Put(constants.ExternalDevWorkspaceConfiguration, namespacedName, nil)
131142
workspace.Spec.Template.DevWorkspaceTemplateSpecContent = dw.DevWorkspaceTemplateSpecContent{
132143
Attributes: attributes,
133144
}
134145

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+
}
158159

159160
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+
}
160170

161-
err := ApplyExternalDWOCConfig(workspace, client)
171+
resolvedConfig, err := ResolveConfigForWorkspace(workspace, client)
162172
if !assert.NoError(t, err, "Should not return error") {
163173
return
164174
}
165175

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")
169184
}
170185

171186
// Get the global config off cluster and ensure it hasn't changed
172187
retrievedClusterConfig := &v1alpha1.DevWorkspaceOperatorConfig{}
173-
namespacedName := types.NamespacedName{
188+
namespacedName = types.NamespacedName{
174189
Name: OperatorConfigName,
175190
Namespace: testNamespace,
176191
}
@@ -180,10 +195,10 @@ func TestConfigUpdatedAfterMerge(t *testing.T) {
180195
}
181196

182197
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))
184199
}
185200
}
186-
*/
201+
187202
func TestSetupControllerAlwaysSetsDefaultClusterRoutingSuffix(t *testing.T) {
188203
setupForTest(t)
189204
infrastructure.InitializeForTesting(infrastructure.OpenShiftv4)

0 commit comments

Comments
 (0)