Skip to content

chore: add tests to the console API #2204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2025
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
94 changes: 83 additions & 11 deletions api/integration/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package integration

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

"github.com/gorilla/mux"
"github.com/replicatedhq/embedded-cluster/api"
"github.com/replicatedhq/embedded-cluster/api/controllers/console"
"github.com/replicatedhq/embedded-cluster/api/controllers/install"
"github.com/replicatedhq/embedded-cluster/api/pkg/installation"
"github.com/replicatedhq/embedded-cluster/api/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -24,18 +24,9 @@ func TestConsoleListAvailableNetworkInterfaces(t *testing.T) {
)
require.NoError(t, err)

// Create an install controller
installController, err := install.NewInstallController(
install.WithInstallationManager(installation.NewInstallationManager(
installation.WithNetUtils(netutils),
)),
)
require.NoError(t, err)

// Create the API with the install controller
apiInstance, err := api.New(
"password",
api.WithInstallController(installController),
api.WithConsoleController(consoleController),
api.WithAuthController(&staticAuthController{"TOKEN"}),
api.WithLogger(api.NewDiscardLogger()),
Expand Down Expand Up @@ -68,3 +59,84 @@ func TestConsoleListAvailableNetworkInterfaces(t *testing.T) {
// Verify the response contains the expected network interfaces
assert.Equal(t, []string{"eth0", "eth1"}, response.NetworkInterfaces)
}

func TestConsoleListAvailableNetworkInterfacesUnauthorized(t *testing.T) {
netutils := &mockNetUtils{ifaces: []string{"eth0", "eth1"}}

// Create a console controller
consoleController, err := console.NewConsoleController(
console.WithNetUtils(netutils),
)
require.NoError(t, err)

// Create the API with the install controller
apiInstance, err := api.New(
"password",
api.WithConsoleController(consoleController),
api.WithAuthController(&staticAuthController{"VALID_TOKEN"}),
api.WithLogger(api.NewDiscardLogger()),
)
require.NoError(t, err)

// Create a router and register the API routes
router := mux.NewRouter()
apiInstance.RegisterRoutes(router)

// Create a request with an invalid token
req := httptest.NewRequest(http.MethodGet, "/console/available-network-interfaces", nil)
req.Header.Set("Authorization", "Bearer INVALID_TOKEN")
rec := httptest.NewRecorder()

// Serve the request
router.ServeHTTP(rec, req)

// Check the response is unauthorized
assert.Equal(t, http.StatusUnauthorized, rec.Code)
assert.Equal(t, "application/json", rec.Header().Get("Content-Type"))
// Check that the API response is of type APIError
var apiErr *types.APIError
err = json.NewDecoder(rec.Body).Decode(&apiErr)
require.NoError(t, err)
assert.Equal(t, http.StatusUnauthorized, apiErr.StatusCode)
}

func TestConsoleListAvailableNetworkInterfacesError(t *testing.T) {
// Create a mock that returns an error
netutils := &mockNetUtils{err: errors.New("failed to list network interfaces")}

// Create a console controller
consoleController, err := console.NewConsoleController(
console.WithNetUtils(netutils),
)
require.NoError(t, err)

// Create the API with the install controller
apiInstance, err := api.New(
"password",
api.WithConsoleController(consoleController),
api.WithAuthController(&staticAuthController{"TOKEN"}),
api.WithLogger(api.NewDiscardLogger()),
)
require.NoError(t, err)

// Create a router and register the API routes
router := mux.NewRouter()
apiInstance.RegisterRoutes(router)

// Create a request to the network interfaces endpoint
req := httptest.NewRequest(http.MethodGet, "/console/available-network-interfaces", nil)
req.Header.Set("Authorization", "Bearer TOKEN")
rec := httptest.NewRecorder()

// Serve the request
router.ServeHTTP(rec, req)

// Check the response
assert.Equal(t, http.StatusInternalServerError, rec.Code)
assert.Equal(t, "application/json", rec.Header().Get("Content-Type"))
// Check that the API response is of type APIError
var apiErr *types.APIError
err = json.NewDecoder(rec.Body).Decode(&apiErr)
require.NoError(t, err)
assert.Equal(t, http.StatusInternalServerError, apiErr.StatusCode)
}
7 changes: 7 additions & 0 deletions api/integration/netutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ import "github.com/replicatedhq/embedded-cluster/api/pkg/utils"
var _ utils.NetUtils = &mockNetUtils{}

type mockNetUtils struct {
err error
ifaces []string
}

func (m *mockNetUtils) ListValidNetworkInterfaces() ([]string, error) {
if m.err != nil {
return nil, m.err
}
return m.ifaces, nil
}

func (m *mockNetUtils) DetermineBestNetworkInterface() (string, error) {
if m.err != nil {
return "", m.err
}
return m.ifaces[0], nil
}
Loading