Skip to content

Commit 81cac82

Browse files
.
1 parent 2d45308 commit 81cac82

File tree

5 files changed

+55
-41
lines changed

5 files changed

+55
-41
lines changed

cns/fakes/imdsclientfake.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ func (m *MockIMDSClient) GetNCVersions(ctx context.Context) ([]imds.NetworkInter
6666
// Return some mock NC versions for testing
6767
return []imds.NetworkInterface{
6868
{
69-
InterfaceCompartmentId: "nc1",
69+
InterfaceCompartmentID: "nc1",
7070
InterfaceCompartmentVersion: "1",
7171
},
7272
{
73-
InterfaceCompartmentId: "nc2",
73+
InterfaceCompartmentID: "nc2",
7474
InterfaceCompartmentVersion: "2",
7575
},
7676
}, nil

cns/imds/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func (c *Client) GetNCVersions(ctx context.Context) ([]NetworkInterface, error)
124124
return nil
125125
}, retry.Context(ctx), retry.Attempts(c.config.retryAttempts), retry.DelayType(retry.BackOffDelay))
126126
if err != nil {
127-
return nil, err
127+
return nil, errors.Wrap(err, "external call failed")
128128
}
129129

130130
return networkData.Interface, nil
@@ -165,7 +165,7 @@ func (c *Client) getInstanceMetadata(ctx context.Context, imdsComputePath string
165165
// NetworkInterface represents a network interface from IMDS
166166
type NetworkInterface struct {
167167
// IMDS only returns compartment fields - these are mapped to NC ID and NC version concepts
168-
InterfaceCompartmentId string `json:"interfaceCompartmentId,omitempty"`
168+
InterfaceCompartmentID string `json:"interfaceCompartmentID,omitempty"`
169169
InterfaceCompartmentVersion string `json:"interfaceCompartmentVersion,omitempty"`
170170
}
171171

cns/imds/client_test.go

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,17 @@ func TestInvalidVMUniqueID(t *testing.T) {
103103

104104
func TestGetNCVersions(t *testing.T) {
105105
networkMetadata := []byte(`{
106-
"interface": [
107-
{
108-
"interfaceCompartmentVersion": "1",
109-
"interfaceCompartmentId": "nc-12345-67890"
110-
},
111-
{
112-
"interfaceCompartmentVersion": "1",
113-
"interfaceCompartmentId": ""
114-
}
115-
]
116-
}`)
106+
"interface": [
107+
{
108+
"interfaceCompartmentVersion": "1",
109+
"interfaceCompartmentID": "nc-12345-67890"
110+
},
111+
{
112+
"interfaceCompartmentVersion": "1",
113+
"interfaceCompartmentID": ""
114+
}
115+
]
116+
}`)
117117

118118
mockIMDSServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
119119
// request header "Metadata: true" must be present
@@ -131,7 +131,10 @@ func TestGetNCVersions(t *testing.T) {
131131

132132
w.WriteHeader(http.StatusOK)
133133
_, writeErr := w.Write(networkMetadata)
134-
require.NoError(t, writeErr, "error writing response")
134+
if writeErr != nil {
135+
t.Errorf("error writing response: %v", writeErr)
136+
return
137+
}
135138
}))
136139
defer mockIMDSServer.Close()
137140

@@ -143,11 +146,11 @@ func TestGetNCVersions(t *testing.T) {
143146
require.Len(t, interfaces, 2, "expected 2 interfaces")
144147

145148
// Check first interface
146-
assert.Equal(t, "nc-12345-67890", interfaces[0].InterfaceCompartmentId)
149+
assert.Equal(t, "nc-12345-67890", interfaces[0].InterfaceCompartmentID)
147150
assert.Equal(t, "1", interfaces[0].InterfaceCompartmentVersion)
148151

149152
// Check second interface
150-
assert.Equal(t, "", interfaces[1].InterfaceCompartmentId)
153+
assert.Equal(t, "", interfaces[1].InterfaceCompartmentID)
151154
assert.Equal(t, "1", interfaces[1].InterfaceCompartmentVersion)
152155
}
153156

@@ -158,10 +161,13 @@ func TestGetNCVersionsInvalidEndpoint(t *testing.T) {
158161
}
159162

160163
func TestGetNCVersionsInvalidJSON(t *testing.T) {
161-
mockIMDSServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
164+
mockIMDSServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
162165
w.WriteHeader(http.StatusOK)
163166
_, err := w.Write([]byte("not json"))
164-
require.NoError(t, err)
167+
if err != nil {
168+
t.Errorf("error writing response: %v", err)
169+
return
170+
}
165171
}))
166172
defer mockIMDSServer.Close()
167173

@@ -172,27 +178,30 @@ func TestGetNCVersionsInvalidJSON(t *testing.T) {
172178

173179
func TestGetNCVersionsNoNCIDs(t *testing.T) {
174180
networkMetadataNoNC := []byte(`{
175-
"interface": [
176-
{
177-
"ipv4": {
178-
"ipAddress": [
179-
{
180-
"privateIpAddress": "10.0.0.4",
181-
"publicIpAddress": ""
182-
}
183-
]
184-
}
185-
}
186-
]
187-
}`)
181+
"interface": [
182+
{
183+
"ipv4": {
184+
"ipAddress": [
185+
{
186+
"privateIpAddress": "10.0.0.4",
187+
"publicIpAddress": ""
188+
}
189+
]
190+
}
191+
}
192+
]
193+
}`)
188194

189195
mockIMDSServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
190196
metadataHeader := r.Header.Get("Metadata")
191197
assert.Equal(t, "true", metadataHeader)
192198

193199
w.WriteHeader(http.StatusOK)
194200
_, writeErr := w.Write(networkMetadataNoNC)
195-
require.NoError(t, writeErr, "error writing response")
201+
if writeErr != nil {
202+
t.Errorf("error writing response: %v", writeErr)
203+
return
204+
}
196205
}))
197206
defer mockIMDSServer.Close()
198207

@@ -202,5 +211,7 @@ func TestGetNCVersionsNoNCIDs(t *testing.T) {
202211

203212
// Verify we got interfaces but they don't have compartment IDs
204213
require.Len(t, interfaces, 1, "expected 1 interface")
205-
assert.Equal(t, "", interfaces[0].InterfaceCompartmentId)
214+
215+
// Check that interfaces don't have compartment IDs
216+
assert.Equal(t, "", interfaces[0].InterfaceCompartmentID)
206217
}

cns/restserver/internalapi.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,7 @@ func (service *HTTPRestService) GetIMDSNCVersions(ctx context.Context) (map[stri
686686
// Check NMAgent API support for SwiftV2, if it fails return empty map assuming support might not be available in that nma build
687687
swiftV2Support, err := service.checkNMAgentAPISupport(ctx)
688688
if err != nil || !swiftV2Support {
689+
logger.Errorf("NMAgent does not support SwiftV2 API or encountered an error: %v", err)
689690
return make(map[string]string), nil
690691
}
691692

@@ -694,14 +695,15 @@ func (service *HTTPRestService) GetIMDSNCVersions(ctx context.Context) (map[stri
694695
// Get all NC versions from IMDS
695696
networkInterfaces, err := imdsClient.GetNCVersions(ctx)
696697
if err != nil {
698+
logger.Errorf("Failed to get NC versions from IMDS: %v", err)
697699
return make(map[string]string), nil
698700
}
699701

700702
// Build ncVersions map from the network interfaces
701703
ncVersions := make(map[string]string)
702704
for _, iface := range networkInterfaces {
703-
// IMDS returns interfaceCompartmentId, interfaceCompartmentVersion fields, as nc id guid has different context on nma. We map these to NC ID and NC version
704-
ncID := iface.InterfaceCompartmentId
705+
// IMDS returns interfaceCompartmentID, interfaceCompartmentVersion fields, as nc id guid has different context on nma. We map these to NC ID and NC version
706+
ncID := iface.InterfaceCompartmentID
705707
ncVersion := iface.InterfaceCompartmentVersion
706708

707709
if ncID != "" {

cns/restserver/internalapi_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ func TestSyncHostNCVersion(t *testing.T) {
277277
ncVersions: func(_ context.Context) ([]imds.NetworkInterface, error) {
278278
return []imds.NetworkInterface{
279279
{
280-
InterfaceCompartmentId: imdsNCID,
280+
InterfaceCompartmentID: imdsNCID,
281281
InterfaceCompartmentVersion: "2",
282282
},
283283
}, nil
@@ -377,7 +377,7 @@ func TestSyncHostNCVersionErrorMissingNC(t *testing.T) {
377377
ncVersions: func(_ context.Context) ([]imds.NetworkInterface, error) {
378378
return []imds.NetworkInterface{
379379
{
380-
InterfaceCompartmentId: "different-nc-id",
380+
InterfaceCompartmentID: "different-nc-id",
381381
InterfaceCompartmentVersion: "1", // IMDS has a different NC, not the outdated one
382382
},
383383
}, nil
@@ -439,7 +439,7 @@ func TestSyncHostNCVersionLocalVersionHigher(t *testing.T) {
439439
ncVersions: func(_ context.Context) ([]imds.NetworkInterface, error) {
440440
return []imds.NetworkInterface{
441441
{
442-
InterfaceCompartmentId: req.NetworkContainerid,
442+
InterfaceCompartmentID: req.NetworkContainerid,
443443
InterfaceCompartmentVersion: "1", // IMDS has version 1, which is lower than local version 3
444444
},
445445
}, nil
@@ -574,7 +574,8 @@ func TestSyncHostNCVersionNMAgentAPICallFailed(t *testing.T) {
574574
},
575575
SupportedAPIsF: func(_ context.Context) ([]string, error) {
576576
// NMAgent SupportedAPIs call fails
577-
return nil, fmt.Errorf("failed to connect to NMAgent API endpoint")
577+
var errNMAgentAPI = errors.New("failed to connect to NMAgent API endpoint")
578+
return nil, fmt.Errorf("%w", errNMAgentAPI)
578579
},
579580
}
580581
cleanup := setMockNMAgent(svc, mnma)

0 commit comments

Comments
 (0)