|
1 | 1 | 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