Skip to content

Commit 71c7045

Browse files
committed
Mock out HTTP calls in DevWorkspace controller tests
Introduce a fake http.Transport to allow simulating responses to HTTP calls in DevWorkspace Operator tests. This is required for testing that DevWorkspaces with mainUrls can enter the Running state. Signed-off-by: Angel Misevski <[email protected]>
1 parent f5b9d40 commit 71c7045

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

controllers/workspace/http_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2019-2022 Red Hat, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package controllers
15+
16+
import "net/http"
17+
18+
func SetupHttpClientsForTesting(client *http.Client) {
19+
httpClient = client
20+
healthCheckHttpClient = client
21+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) 2019-2022 Red Hat, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package testutil
15+
16+
import (
17+
"bytes"
18+
"fmt"
19+
"io"
20+
"net/http"
21+
)
22+
23+
type TestRoundTripper struct {
24+
Data map[string]TestResponse
25+
}
26+
27+
type TestResponse struct {
28+
StatusCode int
29+
Bytes []byte
30+
Err error
31+
}
32+
33+
func (rt *TestRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
34+
if req.Method != http.MethodGet {
35+
return nil, fmt.Errorf("test HTTP client only supports GET requests")
36+
}
37+
resp, ok := rt.Data[req.URL.String()]
38+
if !ok {
39+
return nil, fmt.Errorf("unexpected request URL in test HTTP client: %s", req.URL.String())
40+
}
41+
42+
if resp.Err != nil {
43+
return nil, resp.Err
44+
}
45+
46+
return &http.Response{
47+
StatusCode: resp.StatusCode,
48+
Body: io.NopCloser(bytes.NewBuffer(resp.Bytes)),
49+
ContentLength: int64(len(resp.Bytes)),
50+
}, nil
51+
}
52+
53+
var _ http.RoundTripper = (*TestRoundTripper)(nil)

controllers/workspace/suite_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ package controllers_test
1616
import (
1717
"context"
1818
"fmt"
19+
"net/http"
1920
"os"
2021
"path/filepath"
2122
"testing"
2223

2324
dwv1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha1"
2425
dwv2 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
2526
controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
27+
"github.com/devfile/devworkspace-operator/controllers/workspace/internal/testutil"
2628
appsv1 "k8s.io/api/apps/v1"
2729
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2830
"sigs.k8s.io/yaml"
@@ -134,6 +136,9 @@ var _ = BeforeSuite(func() {
134136
}).SetupWithManager(mgr)
135137
Expect(err).NotTo(HaveOccurred())
136138

139+
// Set HTTP client to fail all requests by default; tests that require HTTP must set this up directly
140+
workspacecontroller.SetupHttpClientsForTesting(getBasicTestHttpClient())
141+
137142
// Skip trying to set up / test webhooks for now
138143

139144
By("Creating Namespace for the DevWorkspace")
@@ -187,3 +192,9 @@ func setupEnvVars() error {
187192

188193
return nil
189194
}
195+
196+
func getBasicTestHttpClient() *http.Client {
197+
return &http.Client{
198+
Transport: &testutil.TestRoundTripper{},
199+
}
200+
}

0 commit comments

Comments
 (0)