@@ -176,9 +176,10 @@ func TestMain(m *testing.M) {
176
176
logger .InitLogger ("testlogs" , 0 , 0 , "./" )
177
177
178
178
// Create the service. If CRD channel mode is needed, then at the start of the test,
179
- // it can stop the service (service.Stop), invoke startService again with new ServiceConfig (with CRD mode)
180
- // perform the test and then restore the service again.
181
- if err = startService (common.ServiceConfig {ChannelMode : cns .Direct }, configuration.CNSConfig {}); err != nil {
179
+ // it can stop the service (via the returned stop func), invoke startService again with
180
+ // new ServiceConfig (with CRD mode), perform the test and then restore the service again.
181
+ stop , _ , err := startService (common.ServiceConfig {ChannelMode : cns .Direct }, configuration.CNSConfig {})
182
+ if err != nil {
182
183
fmt .Printf ("Failed to start CNS Service. Error: %v" , err )
183
184
os .Exit (1 )
184
185
}
@@ -207,7 +208,8 @@ func TestMain(m *testing.M) {
207
208
exitCode := m .Run ()
208
209
209
210
// Cleanup.
210
- service .Stop ()
211
+ // Use the stop func returned by startService to ensure proper teardown.
212
+ stop ()
211
213
nmAgentServer .Stop ()
212
214
213
215
os .Exit (exitCode )
@@ -1031,7 +1033,7 @@ func publishNCViaCNS(
1031
1033
1032
1034
if bodyStatus != expStatus {
1033
1035
// nolint:goerr113 // this is okay in a test:
1034
- return fmt .Errorf ("unexpected status in body. exp: %d, got %d" , expStatus , bodyStatus )
1036
+ return fmt .Errorf ("unexpected status in body. exp: %d, got: %d" , expStatus , bodyStatus )
1035
1037
}
1036
1038
}
1037
1039
@@ -1787,23 +1789,23 @@ func setEnv(t *testing.T) *httptest.ResponseRecorder {
1787
1789
return w
1788
1790
}
1789
1791
1790
- func startService (serviceConfig common.ServiceConfig , _ configuration.CNSConfig ) error {
1792
+ func startService (serviceConfig common.ServiceConfig , _ configuration.CNSConfig ) ( func (), * HTTPRestService , error ) {
1791
1793
// Create the service.
1792
1794
config := serviceConfig
1793
1795
1794
1796
// Create the key value fileStore.
1795
1797
fileStore , err := store .NewJsonFileStore (cnsJsonFileName , processlock .NewMockFileLock (false ), nil )
1796
1798
if err != nil {
1797
1799
logger .Errorf ("Failed to create store file: %s, due to error %v\n " , cnsJsonFileName , err )
1798
- return err
1800
+ return nil , nil , err
1799
1801
}
1800
1802
config .Store = fileStore
1801
1803
1802
1804
nmagentClient := & fakes.NMAgentClientFake {}
1803
1805
service , err = NewHTTPRestService (& config , & fakes.WireserverClientFake {}, & fakes.WireserverProxyFake {}, & IPtablesProvider {},
1804
1806
nmagentClient , nil , nil , nil , fakes .NewMockIMDSClient ())
1805
1807
if err != nil {
1806
- return err
1808
+ return nil , nil , err
1807
1809
}
1808
1810
svc = service .(* HTTPRestService )
1809
1811
svc .Service .Options [acncommon .OptCnsURL ] = ""
@@ -1839,6 +1841,7 @@ func startService(serviceConfig common.ServiceConfig, _ configuration.CNSConfig)
1839
1841
return resp , nil
1840
1842
}
1841
1843
1844
+ var perTestSvc * HTTPRestService
1842
1845
if service != nil {
1843
1846
// Create empty azure-cns.json. CNS should start successfully by deleting this file
1844
1847
file , _ := os .Create (cnsJsonFileName )
@@ -1850,25 +1853,43 @@ func startService(serviceConfig common.ServiceConfig, _ configuration.CNSConfig)
1850
1853
err = service .Init (& config )
1851
1854
if err != nil {
1852
1855
logger .Errorf ("Failed to Init CNS, err:%v.\n " , err )
1853
- return err
1856
+ return nil , nil , err
1854
1857
}
1855
1858
1856
1859
err = service .Start (& config )
1857
1860
if err != nil {
1858
1861
logger .Errorf ("Failed to start CNS, err:%v.\n " , err )
1859
- return err
1862
+ return nil , nil , err
1860
1863
}
1861
1864
1862
1865
if _ , err := os .Stat (cnsJsonFileName ); err == nil || ! os .IsNotExist (err ) {
1863
1866
logger .Errorf ("Failed to remove empty CNS state file: %s, err:%v" , cnsJsonFileName , err )
1864
- return err
1867
+ return nil , nil , err
1865
1868
}
1869
+ perTestSvc = service .(* HTTPRestService )
1866
1870
}
1867
1871
1868
1872
// Get the internal http mux as test hook.
1869
- mux = service .(* HTTPRestService ).Listener .GetMux ()
1873
+ if perTestSvc == nil {
1874
+ return nil , nil , fmt .Errorf ("service was not created" )
1875
+ }
1876
+ mux = perTestSvc .Listener .GetMux ()
1870
1877
1871
- return nil
1878
+ // stop func cleans up the running service and any test artifacts.
1879
+ stop := func () {
1880
+ if service != nil {
1881
+ // ignore stop errors
1882
+ service .Stop ()
1883
+ }
1884
+ // best-effort remove temp file
1885
+ _ = os .Remove (cnsJsonFileName )
1886
+ // clear globals to avoid cross-test leakage
1887
+ svc = nil
1888
+ service = nil
1889
+ mux = nil
1890
+ }
1891
+
1892
+ return stop , perTestSvc , nil
1872
1893
}
1873
1894
1874
1895
func contains (networkContainers []cns.GetNetworkContainerResponse , str string ) bool {
@@ -1918,8 +1939,18 @@ func TestGetVMUniqueIDFailed(t *testing.T) {
1918
1939
}
1919
1940
1920
1941
func TestIBDevices (t * testing.T ) {
1942
+ // If a particular test needs to run with an isolated ServiceConfig (different ChannelMode
1943
+ // for example), startService now returns a stop func and the instantiated *HTTPRestService.
1944
+ // Example per-test usage (uncomment to use):
1945
+ stop , perTestSvc , err := startService (common.ServiceConfig {ChannelMode : cns .CRD }, configuration.CNSConfig {})
1946
+ if err != nil {
1947
+ t .Fatalf ("failed to start per-test service: %v" , err )
1948
+ }
1949
+ defer stop ()
1950
+ // You can then use 'perTestSvc' for test-local assertions and the returned stop func will
1951
+ // cleanly tear down the service when the test finishes.
1952
+
1921
1953
var (
1922
- err error
1923
1954
req * http.Request
1924
1955
ibDevicesReq cns.AssignIBDevicesToPodRequest
1925
1956
body bytes.Buffer
0 commit comments