Skip to content
This repository was archived by the owner on Nov 14, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions internal/checks/kube/rbac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package kube

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

authorizationv1 "k8s.io/api/authorization/v1"
Expand All @@ -28,6 +26,7 @@ func Test_CheckRBAC(t *testing.T) {
Name: "all allowed",
Response: &selfSubjectAccessReviewAllowed,
F: func(t *testing.T, results []*api.CheckResult) {
assert.False(t, "results should not be empty", len(results) == 0)
for _, result := range results {
assert.True(t, result.Name+" should not error", result.Details["error"] == nil)
assert.True(t, result.Name+" should pass", result.State == api.StatePassed)
Expand All @@ -38,6 +37,7 @@ func Test_CheckRBAC(t *testing.T) {
Name: "all denied",
Response: &selfSubjectAccessReviewDenied,
F: func(t *testing.T, results []*api.CheckResult) {
assert.False(t, "results should not be empty", len(results) == 0)
for _, result := range results {
assert.True(t, result.Name+" should have an error", result.Details["error"] != nil)
assert.True(t, result.Name+" should fail", result.State == api.StateFailed)
Expand All @@ -51,12 +51,7 @@ func Test_CheckRBAC(t *testing.T) {
t.Run(test.Name, func(t *testing.T) {
t.Parallel()

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(test.Response)
assert.Success(t, "failed to encode response", err)
}))
server := newTestHTTPServer(t, http.StatusOK, test.Response)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering whether we should override the client's RoundTripper instead of creating a test server, but 🤷‍♂️

seems good to me!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you can do some fun stuff with that. I especially like the look of NewFileTransport (https://pkg.go.dev/net/http#NewFileTransport).

If (when?) we end up needing to do lots of complex request/response interactions in unit tests, it might be worthwhile looking into this.

defer server.Close()

client, err := kubernetes.NewForConfig(&rest.Config{Host: server.URL})
Expand All @@ -72,11 +67,7 @@ func Test_CheckRBAC(t *testing.T) {
func Test_CheckRBAC_ClientError(t *testing.T) {
t.Parallel()

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
}))
defer server.Close()
server := newTestHTTPServer(t, http.StatusInternalServerError, nil)

client, err := kubernetes.NewForConfig(&rest.Config{Host: server.URL})
assert.Success(t, "failed to create client", err)
Expand Down
11 changes: 11 additions & 0 deletions internal/checks/kube/resources.go
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
package kube

import (
"context"

"github.com/cdr/coder-doctor/internal/api"
)

func (k *KubernetesChecker) CheckResources(ctx context.Context) []*api.CheckResult {
results := make([]*api.CheckResult, 0)
return results
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually shouldn't be in here yet 😅

22 changes: 22 additions & 0 deletions internal/checks/kube/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package kube

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"cdr.dev/slog/sloggers/slogtest/assert"
)

// newTestHTTPServer creates a HTTP server that just returns the given status code and response.
func newTestHTTPServer(t *testing.T, statusCode int, resp interface{}) *httptest.Server {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
err := json.NewEncoder(w).Encode(resp)
assert.Success(t, "failed to encode response", err)
}))
return srv
}
24 changes: 4 additions & 20 deletions internal/checks/kube/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package kube

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/Masterminds/semver/v3"
Expand Down Expand Up @@ -102,12 +100,7 @@ func TestVersions(t *testing.T) {
t.Run(test.Name, func(t *testing.T) {
t.Parallel()

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(test.KubernetesVersion)
assert.Success(t, "failed to encode response", err)
}))
server := newTestHTTPServer(t, http.StatusOK, test.KubernetesVersion)
defer server.Close()

client, err := kubernetes.NewForConfig(&rest.Config{
Expand All @@ -125,10 +118,7 @@ func TestVersions(t *testing.T) {
func TestUnknownRoute(t *testing.T) {
t.Parallel()

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
}))
server := newTestHTTPServer(t, http.StatusNotFound, nil)
defer server.Close()

client, err := kubernetes.NewForConfig(&rest.Config{
Expand All @@ -145,14 +135,8 @@ func TestUnknownRoute(t *testing.T) {
func TestCorruptResponse(t *testing.T) {
t.Parallel()

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(map[string]interface{}{
"gitVersion": 10,
})
assert.Success(t, "failed to encode response", err)
}))
garbage := map[string]interface{}{"gitVersion": 10}
server := newTestHTTPServer(t, http.StatusOK, garbage)
defer server.Close()

client, err := kubernetes.NewForConfig(&rest.Config{
Expand Down