13
13
package pluginregistry
14
14
15
15
import (
16
+ "os"
17
+
16
18
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
19
+ chev2 "github.com/eclipse-che/che-operator/api/v2"
20
+ defaults "github.com/eclipse-che/che-operator/pkg/common/operator-defaults"
17
21
"github.com/eclipse-che/che-operator/pkg/common/test"
18
22
"github.com/stretchr/testify/assert"
19
23
appsv1 "k8s.io/api/apps/v1"
20
24
corev1 "k8s.io/api/core/v1"
25
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21
26
"k8s.io/apimachinery/pkg/runtime"
22
27
"k8s.io/apimachinery/pkg/types"
28
+ "k8s.io/utils/pointer"
23
29
24
30
"testing"
25
31
)
26
32
27
- func TestPluginRegistryReconcile (t * testing.T ) {
33
+ func TestShouldDeployPluginRegistryIfOpenVSXIsEmpty (t * testing.T ) {
28
34
infrastructure .InitializeForTesting (infrastructure .OpenShiftv4 )
29
35
30
- ctx := test .GetDeployContext (nil , []runtime.Object {})
36
+ ctx := test .GetDeployContext (& chev2.CheCluster {
37
+ ObjectMeta : metav1.ObjectMeta {
38
+ Name : "eclipse-che" ,
39
+ Namespace : "eclipse-che" ,
40
+ },
41
+ Spec : chev2.CheClusterSpec {
42
+ Components : chev2.CheClusterComponents {
43
+ PluginRegistry : chev2.PluginRegistry {
44
+ OpenVSXURL : pointer .String ("" ),
45
+ },
46
+ },
47
+ },
48
+ }, []runtime.Object {})
31
49
32
50
pluginregistry := NewPluginRegistryReconciler ()
33
51
_ , done , err := pluginregistry .Reconcile (ctx )
@@ -39,3 +57,96 @@ func TestPluginRegistryReconcile(t *testing.T) {
39
57
assert .True (t , test .IsObjectExists (ctx .ClusterAPI .Client , types.NamespacedName {Name : "plugin-registry" , Namespace : "eclipse-che" }, & appsv1.Deployment {}))
40
58
assert .NotEmpty (t , ctx .CheCluster .Status .PluginRegistryURL )
41
59
}
60
+
61
+ func TestShouldDeployPluginRegistryIfOpenVSXIsEmptyByDefault (t * testing.T ) {
62
+ defaultOpenVSXURL := os .Getenv ("CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL" )
63
+
64
+ err := os .Unsetenv ("CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL" )
65
+ assert .NoError (t , err )
66
+
67
+ defer func () {
68
+ _ = os .Setenv ("CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL" , defaultOpenVSXURL )
69
+ }()
70
+
71
+ // re initialize defaults with new env var
72
+ defaults .Initialize ()
73
+
74
+ infrastructure .InitializeForTesting (infrastructure .OpenShiftv4 )
75
+
76
+ ctx := test .GetDeployContext (& chev2.CheCluster {
77
+ ObjectMeta : metav1.ObjectMeta {
78
+ Name : "eclipse-che" ,
79
+ Namespace : "eclipse-che" ,
80
+ },
81
+ }, []runtime.Object {})
82
+
83
+ pluginregistry := NewPluginRegistryReconciler ()
84
+ _ , done , err := pluginregistry .Reconcile (ctx )
85
+ assert .True (t , done )
86
+ assert .Nil (t , err )
87
+
88
+ assert .True (t , test .IsObjectExists (ctx .ClusterAPI .Client , types.NamespacedName {Name : "plugin-registry" , Namespace : "eclipse-che" }, & corev1.Service {}))
89
+ assert .True (t , test .IsObjectExists (ctx .ClusterAPI .Client , types.NamespacedName {Name : "plugin-registry" , Namespace : "eclipse-che" }, & corev1.ConfigMap {}))
90
+ assert .True (t , test .IsObjectExists (ctx .ClusterAPI .Client , types.NamespacedName {Name : "plugin-registry" , Namespace : "eclipse-che" }, & appsv1.Deployment {}))
91
+ assert .NotEmpty (t , ctx .CheCluster .Status .PluginRegistryURL )
92
+ }
93
+
94
+ func TestShouldNotDeployPluginRegistryIfOpenVSXConfigured (t * testing.T ) {
95
+ infrastructure .InitializeForTesting (infrastructure .OpenShiftv4 )
96
+
97
+ ctx := test .GetDeployContext (& chev2.CheCluster {
98
+ ObjectMeta : metav1.ObjectMeta {
99
+ Name : "eclipse-che" ,
100
+ Namespace : "eclipse-che" ,
101
+ },
102
+ Spec : chev2.CheClusterSpec {
103
+ Components : chev2.CheClusterComponents {
104
+ PluginRegistry : chev2.PluginRegistry {
105
+ OpenVSXURL : pointer .String ("https://openvsx.org" ),
106
+ },
107
+ },
108
+ },
109
+ }, []runtime.Object {})
110
+
111
+ pluginregistry := NewPluginRegistryReconciler ()
112
+ _ , done , err := pluginregistry .Reconcile (ctx )
113
+ assert .True (t , done )
114
+ assert .Nil (t , err )
115
+
116
+ assert .False (t , test .IsObjectExists (ctx .ClusterAPI .Client , types.NamespacedName {Name : "plugin-registry" , Namespace : "eclipse-che" }, & corev1.Service {}))
117
+ assert .False (t , test .IsObjectExists (ctx .ClusterAPI .Client , types.NamespacedName {Name : "plugin-registry" , Namespace : "eclipse-che" }, & corev1.ConfigMap {}))
118
+ assert .False (t , test .IsObjectExists (ctx .ClusterAPI .Client , types.NamespacedName {Name : "plugin-registry" , Namespace : "eclipse-che" }, & appsv1.Deployment {}))
119
+ assert .Empty (t , ctx .CheCluster .Status .PluginRegistryURL )
120
+ }
121
+
122
+ func TestShouldNotDeployPluginRegistryIfOpenVSXConfiguredByDefault (t * testing.T ) {
123
+ defaultOpenVSXURL := os .Getenv ("CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL" )
124
+ err := os .Setenv ("CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL" , "https://openvsx.org" )
125
+ assert .NoError (t , err )
126
+
127
+ defer func () {
128
+ _ = os .Setenv ("CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL" , defaultOpenVSXURL )
129
+ }()
130
+
131
+ // re initialize defaults with new env var
132
+ defaults .Initialize ()
133
+
134
+ infrastructure .InitializeForTesting (infrastructure .OpenShiftv4 )
135
+
136
+ ctx := test .GetDeployContext (& chev2.CheCluster {
137
+ ObjectMeta : metav1.ObjectMeta {
138
+ Name : "eclipse-che" ,
139
+ Namespace : "eclipse-che" ,
140
+ },
141
+ }, []runtime.Object {})
142
+
143
+ pluginregistry := NewPluginRegistryReconciler ()
144
+ _ , done , err := pluginregistry .Reconcile (ctx )
145
+ assert .True (t , done )
146
+ assert .Nil (t , err )
147
+
148
+ assert .False (t , test .IsObjectExists (ctx .ClusterAPI .Client , types.NamespacedName {Name : "plugin-registry" , Namespace : "eclipse-che" }, & corev1.Service {}))
149
+ assert .False (t , test .IsObjectExists (ctx .ClusterAPI .Client , types.NamespacedName {Name : "plugin-registry" , Namespace : "eclipse-che" }, & corev1.ConfigMap {}))
150
+ assert .False (t , test .IsObjectExists (ctx .ClusterAPI .Client , types.NamespacedName {Name : "plugin-registry" , Namespace : "eclipse-che" }, & appsv1.Deployment {}))
151
+ assert .Empty (t , ctx .CheCluster .Status .PluginRegistryURL )
152
+ }
0 commit comments