Skip to content

Commit 0d60061

Browse files
committed
fix: remove unrelated file and fix broken kube tests
- Remove generated connectruntimeimagespec.go not related to this PR - Extract isCRDNotFoundError() helper for testability - Rewrite kube_test.go with working tests for CRD error detection
1 parent 7695910 commit 0d60061

File tree

3 files changed

+31
-154
lines changed

3 files changed

+31
-154
lines changed

client-go/applyconfiguration/core/v1beta1/connectruntimeimagespec.go

Lines changed: 0 additions & 62 deletions
This file was deleted.

flightdeck/internal/kube.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,7 @@ func (c *siteClient) Get(name string, namespace string, opts metav1.GetOptions,
111111
Into(&result)
112112

113113
if err != nil {
114-
// Check if the error is because Sites CRD doesn't exist
115-
// This can happen on clusters without sites
116-
// The error will typically be "the server could not find the requested resource"
117-
// when the CRD is not installed
118-
errStr := err.Error()
119-
if strings.Contains(errStr, "the server could not find the requested resource") ||
120-
strings.Contains(errStr, "no matches for kind") {
114+
if isCRDNotFoundError(err.Error()) {
121115
slog.Info("Sites CRD not found on cluster, returning empty site", "name", name, "namespace", namespace)
122116
// Return an empty Site with minimal info for display
123117
result.Name = name
@@ -131,3 +125,9 @@ func (c *siteClient) Get(name string, namespace string, opts metav1.GetOptions,
131125
slog.Debug("site fetched successfully", "name", name, "namespace", namespace)
132126
return &result, err
133127
}
128+
129+
// isCRDNotFoundError checks if an error message indicates the Site CRD is not installed.
130+
func isCRDNotFoundError(errMsg string) bool {
131+
return strings.Contains(errMsg, "the server could not find the requested resource") ||
132+
strings.Contains(errMsg, "no matches for kind")
133+
}

flightdeck/internal/kube_test.go

Lines changed: 24 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,48 @@
11
package internal
22

33
import (
4-
"context"
5-
"errors"
64
"testing"
75

8-
positcov1beta1 "github.com/posit-dev/team-operator/api/core/v1beta1"
96
"github.com/stretchr/testify/assert"
10-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11-
"k8s.io/apimachinery/pkg/runtime"
12-
"k8s.io/client-go/rest"
13-
"k8s.io/client-go/rest/fake"
147
)
158

16-
func TestSiteClient_Get_HandlesNoCRD(t *testing.T) {
9+
func TestIsCRDNotFoundError(t *testing.T) {
1710
tests := []struct {
18-
name string
19-
setupREST func() *fake.RESTClient
20-
wantError bool
21-
wantEmpty bool
22-
errMsg string
11+
name string
12+
errMsg string
13+
isCRDAbsent bool
2314
}{
2415
{
25-
name: "CRD not found - returns empty site",
26-
setupREST: func() *fake.RESTClient {
27-
client := &fake.RESTClient{
28-
NegotiatedSerializer: runtime.NewSimpleNegotiatedSerializer(runtime.SerializerInfo{}),
29-
}
30-
client.Err = errors.New("the server could not find the requested resource")
31-
return client
32-
},
33-
wantError: false,
34-
wantEmpty: true,
16+
name: "resource not found indicates missing CRD",
17+
errMsg: "the server could not find the requested resource",
18+
isCRDAbsent: true,
3519
},
3620
{
37-
name: "No matches for kind - returns empty site",
38-
setupREST: func() *fake.RESTClient {
39-
client := &fake.RESTClient{
40-
NegotiatedSerializer: runtime.NewSimpleNegotiatedSerializer(runtime.SerializerInfo{}),
41-
}
42-
client.Err = errors.New("no matches for kind \"Site\" in version \"core.posit.team/v1beta1\"")
43-
return client
44-
},
45-
wantError: false,
46-
wantEmpty: true,
21+
name: "no matches for kind indicates missing CRD",
22+
errMsg: "no matches for kind \"Site\" in version \"core.posit.team/v1beta1\"",
23+
isCRDAbsent: true,
4724
},
4825
{
49-
name: "Other error - returns error",
50-
setupREST: func() *fake.RESTClient {
51-
client := &fake.RESTClient{
52-
NegotiatedSerializer: runtime.NewSimpleNegotiatedSerializer(runtime.SerializerInfo{}),
53-
}
54-
client.Err = errors.New("connection refused")
55-
return client
56-
},
57-
wantError: true,
58-
wantEmpty: false,
59-
errMsg: "connection refused",
26+
name: "connection refused is not a missing CRD",
27+
errMsg: "connection refused",
28+
isCRDAbsent: false,
6029
},
6130
{
62-
name: "Site found - returns site",
63-
setupREST: func() *fake.RESTClient {
64-
site := &positcov1beta1.Site{
65-
ObjectMeta: metav1.ObjectMeta{
66-
Name: "test-site",
67-
Namespace: "posit-team",
68-
},
69-
}
70-
client := &fake.RESTClient{
71-
NegotiatedSerializer: runtime.NewSimpleNegotiatedSerializer(runtime.SerializerInfo{}),
72-
Resp: &rest.Response{
73-
Response: nil,
74-
},
75-
}
76-
// In a real test, we'd properly mock the response
77-
// For now, we're testing the error handling logic
78-
return client
79-
},
80-
wantError: false,
81-
wantEmpty: false,
31+
name: "timeout is not a missing CRD",
32+
errMsg: "context deadline exceeded",
33+
isCRDAbsent: false,
34+
},
35+
{
36+
name: "empty string is not a missing CRD",
37+
errMsg: "",
38+
isCRDAbsent: false,
8239
},
8340
}
8441

8542
for _, tt := range tests {
8643
t.Run(tt.name, func(t *testing.T) {
87-
client := &siteClient{
88-
restClient: tt.setupREST(),
89-
}
90-
91-
ctx := context.Background()
92-
result, err := client.Get("test-site", "posit-team", metav1.GetOptions{}, ctx)
93-
94-
if tt.wantError {
95-
assert.Error(t, err)
96-
if tt.errMsg != "" {
97-
assert.Contains(t, err.Error(), tt.errMsg)
98-
}
99-
} else {
100-
assert.NoError(t, err)
101-
if tt.wantEmpty {
102-
// When CRD doesn't exist, we return an empty site with just name/namespace
103-
assert.Equal(t, "test-site", result.Name)
104-
assert.Equal(t, "posit-team", result.Namespace)
105-
}
106-
}
44+
result := isCRDNotFoundError(tt.errMsg)
45+
assert.Equal(t, tt.isCRDAbsent, result)
10746
})
10847
}
109-
}
48+
}

0 commit comments

Comments
 (0)