Skip to content

Commit fbd60a5

Browse files
chore: add missing files
1 parent c59f2dc commit fbd60a5

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package restserver
2+
3+
import (
4+
"context"
5+
"net/netip"
6+
7+
"github.com/Azure/azure-container-networking/cns"
8+
"github.com/Azure/azure-container-networking/cns/common"
9+
"github.com/Azure/azure-container-networking/cns/fakes"
10+
"github.com/Azure/azure-container-networking/cns/nodesubnet"
11+
acn "github.com/Azure/azure-container-networking/common"
12+
"github.com/Azure/azure-container-networking/nmagent"
13+
"github.com/Azure/azure-container-networking/store"
14+
)
15+
16+
func GetRestServiceObjectForNodeSubnetTest(generator CNIConflistGenerator) *HTTPRestService {
17+
config := &common.ServiceConfig{
18+
Name: "test",
19+
Version: "1.0",
20+
ChannelMode: "AzureHost",
21+
Store: store.NewMockStore("test"),
22+
}
23+
interfaces := nmagent.Interfaces{
24+
Entries: []nmagent.Interface{
25+
{
26+
MacAddress: nmagent.MACAddress{0x00, 0x0D, 0x3A, 0xF9, 0xDC, 0xA6},
27+
IsPrimary: true,
28+
InterfaceSubnets: []nmagent.InterfaceSubnet{
29+
{
30+
Prefix: "10.240.0.0/16",
31+
IPAddress: []nmagent.NodeIP{
32+
{
33+
Address: nmagent.IPAddress(netip.AddrFrom4([4]byte{10, 240, 0, 5})),
34+
IsPrimary: true,
35+
},
36+
{
37+
Address: nmagent.IPAddress(netip.AddrFrom4([4]byte{10, 240, 0, 6})),
38+
IsPrimary: false,
39+
},
40+
},
41+
},
42+
},
43+
},
44+
},
45+
}
46+
47+
svc, err := cns.NewService(config.Name, config.Version, config.ChannelMode, config.Store)
48+
if err != nil {
49+
return nil
50+
}
51+
52+
svc.SetOption(acn.OptCnsURL, "")
53+
svc.SetOption(acn.OptCnsPort, "")
54+
err = svc.Initialize(config)
55+
if err != nil {
56+
return nil
57+
}
58+
59+
return &HTTPRestService{
60+
Service: svc,
61+
cniConflistGenerator: generator,
62+
state: &httpRestServiceState{},
63+
PodIPConfigState: make(map[string]cns.IPConfigurationStatus),
64+
nma: &fakes.NMAgentClientFake{
65+
GetInterfaceIPInfoF: func(_ context.Context) (nmagent.Interfaces, error) {
66+
return interfaces, nil
67+
},
68+
},
69+
}
70+
}
71+
72+
// SetCNIConflistGenerator sets the CNIConflistGenerator for the HTTPRestService.
73+
func (service *HTTPRestService) SetCNIConflistGenerator(generator CNIConflistGenerator) {
74+
service.cniConflistGenerator = generator
75+
}
76+
77+
// GetNodesubnetIPFetcher gets the nodesubnet.IPFetcher from the HTTPRestService.
78+
func (service *HTTPRestService) GetNodesubnetIPFetcher() *nodesubnet.IPFetcher {
79+
return service.nodesubnetIPFetcher
80+
}

cns/restserver/nodesubnet_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package restserver_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/Azure/azure-container-networking/cns"
8+
"github.com/Azure/azure-container-networking/cns/logger"
9+
"github.com/Azure/azure-container-networking/cns/restserver"
10+
)
11+
12+
// Mock implementation of PodInfoByIPProvider
13+
type MockPodInfoByIPProvider struct{}
14+
15+
func (m *MockPodInfoByIPProvider) PodInfoByIP() (res map[string]cns.PodInfo, err error) {
16+
return res, nil
17+
}
18+
19+
// Mock implementation of CNIConflistGenerator
20+
type MockCNIConflistGenerator struct {
21+
GenerateCalled chan bool
22+
}
23+
24+
func (m *MockCNIConflistGenerator) Generate() error {
25+
m.GenerateCalled <- true
26+
return nil
27+
}
28+
29+
func (m *MockCNIConflistGenerator) Close() error {
30+
// Implement the Close method logic here if needed
31+
return nil
32+
}
33+
34+
func TestNodeSubnet(t *testing.T) {
35+
mockPodInfoProvider := new(MockPodInfoByIPProvider)
36+
37+
// Create a real HTTPRestService object
38+
mockCNIConflistGenerator := &MockCNIConflistGenerator{
39+
GenerateCalled: make(chan bool),
40+
}
41+
service := restserver.GetRestServiceObjectForNodeSubnetTest(mockCNIConflistGenerator)
42+
defer service.Service.Uninitialize()
43+
44+
ctx, cancel := testContext(t)
45+
defer cancel()
46+
47+
err := service.InitializeNodeSubnet(ctx, mockPodInfoProvider)
48+
service.StartNodeSubnet(ctx)
49+
50+
if err != nil {
51+
t.Errorf("InitializeNodeSubnet returned an error: %v", err)
52+
}
53+
54+
if service.GetNodesubnetIPFetcher() == nil {
55+
t.Error("NodeSubnetIPFetcher is not initialized")
56+
}
57+
58+
select {
59+
case <-ctx.Done():
60+
t.Error("Test context was canceled before conflist generation")
61+
return
62+
case <-mockCNIConflistGenerator.GenerateCalled:
63+
break
64+
}
65+
}
66+
67+
// testContext creates a context from the provided testing.T that will be
68+
// canceled if the test suite is terminated.
69+
func testContext(t *testing.T) (context.Context, context.CancelFunc) {
70+
if deadline, ok := t.Deadline(); ok {
71+
return context.WithDeadline(context.Background(), deadline)
72+
}
73+
return context.WithCancel(context.Background())
74+
}
75+
76+
func init() {
77+
logger.InitLogger("testlogs", 0, 0, "./")
78+
}

0 commit comments

Comments
 (0)