@@ -46,6 +46,7 @@ import (
46
46
"sigs.k8s.io/cloud-provider-azure/pkg/azclient/accountclient/mock_accountclient"
47
47
"sigs.k8s.io/cloud-provider-azure/pkg/azclient/fileshareclient/mock_fileshareclient"
48
48
"sigs.k8s.io/cloud-provider-azure/pkg/azclient/mock_azclient"
49
+ "sigs.k8s.io/cloud-provider-azure/pkg/cache"
49
50
"sigs.k8s.io/cloud-provider-azure/pkg/provider/storage"
50
51
)
51
52
@@ -1768,3 +1769,119 @@ func TestIsKataNode(t *testing.T) {
1768
1769
})
1769
1770
}
1770
1771
}
1772
+
1773
+ func TestUseDataPlaneAPI (t * testing.T ) {
1774
+ d := NewFakeDriver ()
1775
+ d .cloud = & storage.AccountRepo {}
1776
+ ctrl := gomock .NewController (t )
1777
+ defer ctrl .Finish ()
1778
+
1779
+ tests := []struct {
1780
+ name string
1781
+ volumeID string
1782
+ accountName string
1783
+ dataPlaneAPIVolMap map [string ]string
1784
+ dataPlaneAPIAccountCache map [string ]string
1785
+ expectedResult string
1786
+ }{
1787
+ {
1788
+ name : "dataPlaneAPIVolMap & dataPlaneAPIAccountCache is empty" ,
1789
+ dataPlaneAPIVolMap : make (map [string ]string ),
1790
+ dataPlaneAPIAccountCache : make (map [string ]string ),
1791
+ expectedResult : "" ,
1792
+ },
1793
+ {
1794
+ name : "dataPlaneAPIVolMap is not empty" ,
1795
+ volumeID : "test-volume" ,
1796
+ dataPlaneAPIVolMap : map [string ]string {"test-volume" : "true" },
1797
+ dataPlaneAPIAccountCache : make (map [string ]string ),
1798
+ expectedResult : "true" ,
1799
+ },
1800
+ {
1801
+ name : "dataPlaneAPIAccountCache is not empty" ,
1802
+ accountName : "test-account" ,
1803
+ dataPlaneAPIVolMap : make (map [string ]string ),
1804
+ dataPlaneAPIAccountCache : map [string ]string {"test-account" : "oatuh" },
1805
+ expectedResult : "oatuh" ,
1806
+ },
1807
+ {
1808
+ name : "dataPlaneAPIVolMap & dataPlaneAPIAccountCache is not empty" ,
1809
+ volumeID : "test-volume" ,
1810
+ accountName : "test-account" ,
1811
+ dataPlaneAPIVolMap : map [string ]string {"test-volume" : "true" },
1812
+ dataPlaneAPIAccountCache : map [string ]string {"test-account" : "oatuh" },
1813
+ expectedResult : "true" ,
1814
+ },
1815
+ }
1816
+
1817
+ for _ , test := range tests {
1818
+ t .Run (test .name , func (t * testing.T ) {
1819
+ dataPlaneAPIAccountCache , _ := cache .NewTimedCache (10 * time .Minute , func (_ context.Context , _ string ) (interface {}, error ) { return nil , nil }, false )
1820
+ for k , v := range test .dataPlaneAPIVolMap {
1821
+ d .dataPlaneAPIVolMap .Store (k , v )
1822
+ }
1823
+ for k , v := range test .dataPlaneAPIAccountCache {
1824
+ dataPlaneAPIAccountCache .Set (k , v )
1825
+ }
1826
+ d .dataPlaneAPIAccountCache = dataPlaneAPIAccountCache
1827
+ result := d .useDataPlaneAPI (context .TODO (), test .volumeID , test .accountName )
1828
+ assert .Equal (t , test .expectedResult , result )
1829
+ })
1830
+ }
1831
+ }
1832
+
1833
+ func TestSetAzureCredentials (t * testing.T ) {
1834
+ testCases := []struct {
1835
+ name string
1836
+ secretName string
1837
+ seceretNamespace string
1838
+ accountName string
1839
+ accountKey string
1840
+ expectedError error
1841
+ expectedSecretName string
1842
+ }{
1843
+ {
1844
+ name : "kubeClient is nil" ,
1845
+ accountName : "test-account" ,
1846
+ accountKey : "test-key" ,
1847
+ expectedError : nil ,
1848
+ },
1849
+ {
1850
+ name : "accountName is empty" ,
1851
+ expectedError : fmt .Errorf ("the account info is not enough, accountName(%v), accountKey(%v)" , "" , "" ),
1852
+ },
1853
+ {
1854
+ name : "accountKey is empty" ,
1855
+ expectedError : fmt .Errorf ("the account info is not enough, accountName(%v), accountKey(%v)" , "" , "" ),
1856
+ },
1857
+ {
1858
+ name : "success when secretName is empty" ,
1859
+ accountName : "test-account" ,
1860
+ accountKey : "test-key" ,
1861
+ expectedError : nil ,
1862
+ expectedSecretName : fmt .Sprintf (secretNameTemplate , "test-account" ),
1863
+ },
1864
+ {
1865
+ name : "success when secretName is not empty" ,
1866
+ secretName : "test-secret" ,
1867
+ accountName : "test-account" ,
1868
+ accountKey : "test-key" ,
1869
+ expectedError : nil ,
1870
+ expectedSecretName : "test-secret" ,
1871
+ },
1872
+ }
1873
+
1874
+ for _ , tc := range testCases {
1875
+ t .Run (tc .name , func (t * testing.T ) {
1876
+ d := NewFakeDriver ()
1877
+ if tc .name == "kubeClient is nil" {
1878
+ d .kubeClient = nil
1879
+ } else {
1880
+ d .kubeClient = fake .NewSimpleClientset ()
1881
+ }
1882
+ secretName , err := d .SetAzureCredentials (context .TODO (), tc .accountName , tc .accountKey , tc .secretName , tc .seceretNamespace )
1883
+ assert .Equal (t , tc .expectedError , err )
1884
+ assert .Equal (t , tc .expectedSecretName , secretName )
1885
+ })
1886
+ }
1887
+ }
0 commit comments