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