Skip to content

Commit a2d1ae8

Browse files
andyzhangxk8s-infra-cherrypick-robot
authored andcommitted
test: add more ut
test: ignore govet golint fix
1 parent 3f274ed commit a2d1ae8

File tree

2 files changed

+117
-3
lines changed

2 files changed

+117
-3
lines changed

pkg/azurefile/azurefile.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,15 +1338,12 @@ func isKataNode(ctx context.Context, nodeID string, kubeClient clientset.Interfa
13381338
if nodeID == "" {
13391339
return false
13401340
}
1341-
13421341
kataCCIsolationLabel, kataVMIsolationLabel, kataRuntimeLabel, err := getNodeInfoFromLabels(ctx, nodeID, kubeClient)
13431342

13441343
if err != nil {
13451344
klog.Warningf("failed to get node info from labels: %v", err)
13461345
return false
13471346
}
1348-
13491347
klog.V(4).Infof("node(%s) labels: kataVMIsolationLabel(%s), kataRuntimeLabel(%s)", nodeID, kataVMIsolationLabel, kataRuntimeLabel)
1350-
13511348
return strings.EqualFold(kataCCIsolationLabel, "true") || strings.EqualFold(kataVMIsolationLabel, "true") || strings.EqualFold(kataRuntimeLabel, "true")
13521349
}

pkg/azurefile/azurefile_test.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
"sigs.k8s.io/cloud-provider-azure/pkg/azclient/accountclient/mock_accountclient"
4747
"sigs.k8s.io/cloud-provider-azure/pkg/azclient/fileshareclient/mock_fileshareclient"
4848
"sigs.k8s.io/cloud-provider-azure/pkg/azclient/mock_azclient"
49+
"sigs.k8s.io/cloud-provider-azure/pkg/cache"
4950
"sigs.k8s.io/cloud-provider-azure/pkg/provider/storage"
5051
)
5152

@@ -1768,3 +1769,119 @@ func TestIsKataNode(t *testing.T) {
17681769
})
17691770
}
17701771
}
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

Comments
 (0)