Skip to content

Commit 3177719

Browse files
committed
list network interfaces test
1 parent 1b005d5 commit 3177719

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

api/console.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,11 @@ func (a *API) getListAvailableNetworkInterfaces(w http.ResponseWriter, r *http.R
2626
NetworkInterfaces: interfaces,
2727
}
2828

29-
json.NewEncoder(w).Encode(response)
29+
w.Header().Set("Content-Type", "application/json")
30+
w.WriteHeader(http.StatusOK)
31+
err = json.NewEncoder(w).Encode(response)
32+
if err != nil {
33+
a.logger.WithFields(logrusFieldsFromRequest(r)).WithError(err).
34+
Error("failed to encode available network interfaces")
35+
}
3036
}

api/integration/console_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,70 @@
11
package integration
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/gorilla/mux"
10+
"github.com/replicatedhq/embedded-cluster/api"
11+
"github.com/replicatedhq/embedded-cluster/api/controllers/console"
12+
"github.com/replicatedhq/embedded-cluster/api/controllers/install"
13+
"github.com/replicatedhq/embedded-cluster/api/pkg/installation"
14+
"github.com/stretchr/testify/assert"
15+
"github.com/stretchr/testify/require"
16+
)
17+
18+
func TestConsoleListAvailableNetworkInterfaces(t *testing.T) {
19+
netutils := &mockNetUtils{ifaces: []string{"eth0", "eth1"}}
20+
21+
// Create a console controller
22+
consoleController, err := console.NewConsoleController(
23+
console.WithNetUtils(netutils),
24+
)
25+
require.NoError(t, err)
26+
27+
// Create an install controller
28+
installController, err := install.NewInstallController(
29+
install.WithInstallationManager(installation.NewInstallationManager(
30+
installation.WithNetUtils(netutils),
31+
)),
32+
)
33+
require.NoError(t, err)
34+
35+
// Create the API with the install controller
36+
apiInstance, err := api.New(
37+
"password",
38+
api.WithInstallController(installController),
39+
api.WithConsoleController(consoleController),
40+
api.WithAuthController(&staticAuthController{"TOKEN"}),
41+
api.WithLogger(api.NewDiscardLogger()),
42+
)
43+
require.NoError(t, err)
44+
45+
// Create a router and register the API routes
46+
router := mux.NewRouter()
47+
apiInstance.RegisterRoutes(router)
48+
49+
// Create a request to the network interfaces endpoint
50+
req := httptest.NewRequest(http.MethodGet, "/console/available-network-interfaces", nil)
51+
req.Header.Set("Authorization", "Bearer TOKEN")
52+
rec := httptest.NewRecorder()
53+
54+
// Serve the request
55+
router.ServeHTTP(rec, req)
56+
57+
// Check the response
58+
assert.Equal(t, http.StatusOK, rec.Code)
59+
assert.Equal(t, "application/json", rec.Header().Get("Content-Type"))
60+
61+
// Parse the response body
62+
var response struct {
63+
NetworkInterfaces []string `json:"networkInterfaces"`
64+
}
65+
err = json.NewDecoder(rec.Body).Decode(&response)
66+
require.NoError(t, err)
67+
68+
// Verify the response contains the expected network interfaces
69+
assert.Equal(t, []string{"eth0", "eth1"}, response.NetworkInterfaces)
70+
}

0 commit comments

Comments
 (0)