Skip to content

Commit 6249fcc

Browse files
committed
Fix the ClusterManagerEx codes to enable getClusters UT.
Signed-off-by: Wei Zhang <[email protected]>
1 parent 22b88b1 commit 6249fcc

File tree

4 files changed

+334
-63
lines changed

4 files changed

+334
-63
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation
3+
* <p/>
4+
* All rights reserved.
5+
* <p/>
6+
* MIT License
7+
* <p/>
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
9+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
10+
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
11+
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12+
* <p/>
13+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
14+
* the Software.
15+
* <p/>
16+
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
17+
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package com.microsoft.azure.hdinsight.common
24+
25+
import com.microsoft.azure.hdinsight.sdk.cluster.ClusterDetail
26+
import com.microsoft.azure.hdinsight.sdk.cluster.EmulatorClusterDetail
27+
import com.microsoft.azure.hdinsight.sdk.cluster.HDInsightAdditionalClusterDetail
28+
import com.microsoft.azuretools.authmanage.SubscriptionManager
29+
import com.microsoft.azuretools.authmanage.models.SubscriptionDetail
30+
import com.microsoft.azuretools.sdkmanage.AzureManager
31+
import cucumber.api.DataTable
32+
import cucumber.api.java.Before
33+
import cucumber.api.java.en.Given
34+
import org.assertj.core.api.Assertions.assertThat
35+
import org.mockito.Mockito
36+
import org.mockito.Mockito.*
37+
import java.util.*
38+
39+
class ClusterManagerExScenario {
40+
data class SimpleCluster(val name: String,
41+
val storageAccount: String,
42+
val storageKey: String,
43+
val username: String,
44+
val password: String,
45+
val subscription: String)
46+
47+
data class SimpleSubscription(val name: String,
48+
val isSelected: Boolean)
49+
50+
private var clusterMagr: ClusterManagerEx? = null
51+
private var additionalClusters: List<HDInsightAdditionalClusterDetail> = ArrayList()
52+
private var emulatedClusters: List<EmulatorClusterDetail> = ArrayList()
53+
private var subscriptionClusters: List<ClusterDetail> = ArrayList()
54+
private var subscriptions = mapOf<String, SubscriptionDetail>()
55+
56+
@Before
57+
fun setUp() {
58+
clusterMagr = mock(ClusterManagerEx::class.java, CALLS_REAL_METHODS)
59+
}
60+
61+
@Given("^Linked HDInsight clusters are:$")
62+
fun initLinkedClusters(clusterDetails: DataTable) {
63+
additionalClusters = clusterDetails.asList(SimpleCluster::class.java)
64+
.map {
65+
val clusterMock = mock(HDInsightAdditionalClusterDetail::class.java, CALLS_REAL_METHODS)
66+
doReturn(it.name).`when`(clusterMock).name
67+
doReturn(it.username).`when`(clusterMock).httpUserName
68+
doReturn(it.password).`when`(clusterMock).httpPassword
69+
70+
clusterMock
71+
}
72+
73+
doReturn(additionalClusters).`when`(clusterMagr!!).additionalClusters
74+
}
75+
76+
@Given("^emulated HDInsight clusters are:$")
77+
fun initEmulatedLinkedClusters(clusterDetails: DataTable) {
78+
emulatedClusters = clusterDetails.asList(SimpleCluster::class.java)
79+
.map {
80+
val clusterMock = mock(EmulatorClusterDetail::class.java, CALLS_REAL_METHODS)
81+
doReturn(it.name).`when`(clusterMock).name
82+
doReturn(it.username).`when`(clusterMock).httpUserName
83+
doReturn(it.password).`when`(clusterMock).httpPassword
84+
85+
clusterMock
86+
}
87+
88+
doReturn(emulatedClusters).`when`(clusterMagr!!).emulatorClusters
89+
}
90+
91+
92+
@Given("^in subscription HDInsight clusters are:$")
93+
fun initSubscriptionClusters(clusterDetails: DataTable) {
94+
// create AzureManager mock instance
95+
val azureMgrMock = mock(AzureManager::class.java)
96+
doReturn(azureMgrMock).`when`(clusterMagr!!).azureManager
97+
98+
// create SubscriptionManager mock instance
99+
val subscriptionManagerMock = mock(SubscriptionManager::class.java)
100+
Mockito.`when`(azureMgrMock.subscriptionManager).thenReturn(subscriptionManagerMock)
101+
102+
Mockito.`when`(subscriptionManagerMock.subscriptionDetails).thenReturn(subscriptions.values.toList())
103+
104+
subscriptionClusters = clusterDetails.asList(SimpleCluster::class.java)
105+
.map {
106+
val clusterMock = mock(ClusterDetail::class.java, CALLS_REAL_METHODS)
107+
doReturn(it.name).`when`(clusterMock).name
108+
doReturn(it.username).`when`(clusterMock).httpUserName
109+
doReturn(it.password).`when`(clusterMock).httpPassword
110+
// FIXME: Hardcoded for spark version
111+
doReturn("2.2").`when`(clusterMock).sparkVersion
112+
doReturn(subscriptions[it.subscription]).`when`(clusterMock).subscription
113+
114+
clusterMock
115+
}
116+
117+
doReturn(Optional.of(subscriptionClusters)).`when`(clusterMagr!!)
118+
.getSubscriptionHDInsightClustersOfType(subscriptions.values.toList())
119+
}
120+
121+
@Given("^subscriptions mocked are:$")
122+
fun mockSubscriptions(subscriptionsMock: DataTable) {
123+
subscriptions = subscriptionsMock.asList(SimpleSubscription::class.java)
124+
.map {
125+
val subMock = mock(SubscriptionDetail::class.java)
126+
Mockito.`when`(subMock.subscriptionName).thenReturn(it.name)
127+
Mockito.`when`(subMock.isSelected).thenReturn(it.isSelected)
128+
129+
it.name to subMock
130+
}
131+
.toMap()
132+
133+
}
134+
135+
@Given("^check get all Cluster details should be:$")
136+
fun checkGetClusterDetails(clusterDetailsExpect: List<String>) {
137+
assertThat(clusterMagr!!.clusterDetails).extracting("title")
138+
.containsAll(clusterDetailsExpect)
139+
}
140+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation
3+
* <p/>
4+
* All rights reserved.
5+
* <p/>
6+
* MIT License
7+
* <p/>
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
9+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
10+
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
11+
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12+
* <p/>
13+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
14+
* the Software.
15+
* <p/>
16+
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
17+
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package com.microsoft.azure.hdinsight.common
24+
25+
import cucumber.api.CucumberOptions
26+
import cucumber.api.junit.Cucumber
27+
import org.junit.runner.RunWith
28+
29+
@RunWith(Cucumber::class)
30+
@CucumberOptions(
31+
plugin = arrayOf("html:target/cucumber"),
32+
name = arrayOf("ClusterManagerEx tests")
33+
)
34+
class ClusterManagerExTest
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Feature: ClusterManagerEx tests
2+
Scenario: getClusterDetails unit tests without Azure subscription, with linked clusters
3+
Given Linked HDInsight clusters are:
4+
| name | storageAccount | storageKey | username | password | subscription |
5+
| link0Mock | link0sa0 | link0saKey | my@foo | myPass | |
6+
| link1Mock | link1sa0 | link1saKey | my@foo | myPass | |
7+
Given emulated HDInsight clusters are:
8+
| name | storageAccount | storageKey | username | password | subscription |
9+
Then check get all Cluster details should be:
10+
| link0Mock [Linked] |
11+
| link1Mock [Linked] |
12+
13+
Scenario: getClusterDetails unit tests without Azure subscription, with linked cluster and emulated clusters
14+
Given Linked HDInsight clusters are:
15+
| name | storageAccount | storageKey | username | password | subscription |
16+
| link0Mock | link0sa0 | link0saKey | my@foo | myPass | |
17+
Given emulated HDInsight clusters are:
18+
| name | storageAccount | storageKey | username | password | subscription |
19+
| emu0Mock | emu0sa0 | | | | |
20+
Then check get all Cluster details should be:
21+
| link0Mock [Linked] |
22+
| emu0Mock (Spark: 1.6.0 Emulator) |
23+
24+
Scenario: getClusterDetails unit tests with Azure subscription, without linked cluster
25+
Given subscriptions mocked are:
26+
| name | isSelected |
27+
| subscrip0 | true |
28+
| subscripA | false |
29+
Given Linked HDInsight clusters are:
30+
| name | storageAccount | storageKey | username | password | subscription |
31+
Given in subscription HDInsight clusters are:
32+
| name | storageAccount | storageKey | username | password | subscription |
33+
| sub0 | | | admin | myPass | subscrip0 |
34+
| sub1 | | | admin | myPass | subscrip0 |
35+
| subA | | | admin | myPass | subscripA |
36+
Given emulated HDInsight clusters are:
37+
| name | storageAccount | storageKey | username | password | subscription |
38+
Then check get all Cluster details should be:
39+
| sub0 (Spark: 2.2) |
40+
| sub1 (Spark: 2.2) |
41+
42+
Scenario: getClusterDetails unit tests with Azure subscription, with linked cluster, the duplicated should be replaced by linked cluster
43+
Given subscriptions mocked are:
44+
| name | isSelected |
45+
| subscrip0 | true |
46+
| subscripA | true |
47+
Given Linked HDInsight clusters are:
48+
| name | storageAccount | storageKey | username | password | subscription |
49+
| link0Mock | link0sa0 | link0saKey | my@foo | myPass | |
50+
| sub1 | | | admin | myPass | |
51+
Given in subscription HDInsight clusters are:
52+
| name | storageAccount | storageKey | username | password | subscription |
53+
| sub0 | | | admin | myPass | subscrip0 |
54+
| sub1 | | | admin | myPass | subscrip0 |
55+
| subA | | | admin | myPass | subscripA |
56+
Given emulated HDInsight clusters are:
57+
| name | storageAccount | storageKey | username | password | subscription |
58+
Then check get all Cluster details should be:
59+
| sub0 (Spark: 2.2) |
60+
| sub1 [Linked] |
61+
| link0Mock [Linked] |
62+
| subA (Spark: 2.2) |

0 commit comments

Comments
 (0)