@@ -20,6 +20,7 @@ import (
2020
2121 "github.com/Azure/azure-container-networking/cns"
2222 "github.com/Azure/azure-container-networking/cns/common"
23+ "github.com/Azure/azure-container-networking/cns/configuration"
2324 "github.com/Azure/azure-container-networking/cns/fakes"
2425 "github.com/Azure/azure-container-networking/cns/logger"
2526 "github.com/Azure/azure-container-networking/cns/types"
@@ -29,6 +30,7 @@ import (
2930 "github.com/Azure/azure-container-networking/store"
3031 "github.com/pkg/errors"
3132 "github.com/stretchr/testify/assert"
33+ "github.com/stretchr/testify/require"
3234)
3335
3436const (
@@ -172,8 +174,10 @@ func TestMain(m *testing.M) {
172174 var err error
173175 logger .InitLogger ("testlogs" , 0 , 0 , "./" )
174176
175- // Create the service.
176- if err = startService (); err != nil {
177+ // Create the service. If CRD channel mode is needed, then at the start of the test,
178+ // it can stop the service (service.Stop), invoke startService again with new ServiceConfig (with CRD mode)
179+ // perform the test and then restore the service again.
180+ if err = startService (common.ServiceConfig {ChannelMode : cns .Direct }, configuration.CNSConfig {}); err != nil {
177181 fmt .Printf ("Failed to start CNS Service. Error: %v" , err )
178182 os .Exit (1 )
179183 }
@@ -1666,9 +1670,9 @@ func setEnv(t *testing.T) *httptest.ResponseRecorder {
16661670 return w
16671671}
16681672
1669- func startService () error {
1673+ func startService (serviceConfig common. ServiceConfig , _ configuration. CNSConfig ) error {
16701674 // Create the service.
1671- config := common. ServiceConfig {}
1675+ config := serviceConfig
16721676
16731677 // Create the key value fileStore.
16741678 fileStore , err := store .NewJsonFileStore (cnsJsonFileName , processlock .NewMockFileLock (false ), nil )
@@ -1679,7 +1683,8 @@ func startService() error {
16791683 config .Store = fileStore
16801684
16811685 nmagentClient := & fakes.NMAgentClientFake {}
1682- service , err = NewHTTPRestService (& config , & fakes.WireserverClientFake {}, & fakes.WireserverProxyFake {}, nmagentClient , nil , nil , nil )
1686+ service , err = NewHTTPRestService (& config , & fakes.WireserverClientFake {}, & fakes.WireserverProxyFake {},
1687+ nmagentClient , nil , nil , nil , fakes .NewMockIMDSClient ())
16831688 if err != nil {
16841689 return err
16851690 }
@@ -1758,6 +1763,43 @@ func contains(networkContainers []cns.GetNetworkContainerResponse, str string) b
17581763 return false
17591764}
17601765
1766+ // Testing GetVMUniqueID API handler with success
1767+ func TestGetVMUniqueIDSuccess (t * testing.T ) {
1768+ req , err := http .NewRequestWithContext (context .TODO (), http .MethodGet , cns .GetVMUniqueID , http .NoBody )
1769+ if err != nil {
1770+ t .Fatal (err )
1771+ }
1772+
1773+ w := httptest .NewRecorder ()
1774+ mux .ServeHTTP (w , req )
1775+
1776+ var vmIDResp cns.GetVMUniqueIDResponse
1777+ err = decodeResponse (w , & vmIDResp )
1778+ require .NoError (t , err )
1779+ assert .Equal (t , types .Success , vmIDResp .Response .ReturnCode )
1780+ assert .Equal (t , "55b8499d-9b42-4f85-843f-24ff69f4a643" , vmIDResp .VMUniqueID )
1781+ }
1782+
1783+ // Testing GetVMUniqueID API handler with failure
1784+ func TestGetVMUniqueIDFailed (t * testing.T ) {
1785+ ctx := context .TODO ()
1786+ ctx = context .WithValue (ctx , fakes .SimulateError , Interface {})
1787+ req , err := http .NewRequestWithContext (ctx , http .MethodGet , cns .GetVMUniqueID , http .NoBody )
1788+ if err != nil {
1789+ t .Fatal (err )
1790+ }
1791+
1792+ w := httptest .NewRecorder ()
1793+ mux .ServeHTTP (w , req )
1794+
1795+ assert .Equal (t , http .StatusInternalServerError , w .Code )
1796+
1797+ var vmIDResp cns.GetVMUniqueIDResponse
1798+ err = json .NewDecoder (w .Body ).Decode (& vmIDResp )
1799+ require .NoError (t , err )
1800+ assert .Equal (t , types .UnexpectedError , vmIDResp .Response .ReturnCode )
1801+ }
1802+
17611803// IGNORE TEST AS IT IS FAILING. TODO:- Fix it https://msazure.visualstudio.com/One/_workitems/edit/7720083
17621804// // Tests CreateNetwork functionality.
17631805
0 commit comments