Renaming Files having BluePrint to have Blueprint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / ssh-lib / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / ssh / service / BlueprintSshClientServiceTest.kt
1 /*
2  *  Copyright © 2019 IBM.
3  *
4  *  Modifications Copyright © 2018-2019 IBM, Bell Canada
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18
19 package org.onap.ccsdk.cds.blueprintsprocessor.ssh.service
20
21 import kotlinx.coroutines.runBlocking
22 import org.apache.sshd.common.config.keys.KeyUtils.RSA_ALGORITHM
23 import org.apache.sshd.common.keyprovider.KeyPairProvider
24 import org.apache.sshd.server.SshServer
25 import org.apache.sshd.server.auth.password.PasswordAuthenticator
26 import org.apache.sshd.server.auth.pubkey.AcceptAllPublickeyAuthenticator
27 import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider
28 import org.apache.sshd.server.session.ServerSession
29 import org.apache.sshd.server.shell.ProcessShellCommandFactory
30 import org.junit.runner.RunWith
31 import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertiesService
32 import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertyConfiguration
33 import org.onap.ccsdk.cds.blueprintsprocessor.ssh.BlueprintSshLibConfiguration
34 import org.onap.ccsdk.cds.blueprintsprocessor.ssh.service.echoShell.EchoShellFactory
35 import org.springframework.beans.factory.annotation.Autowired
36 import org.springframework.test.context.ContextConfiguration
37 import org.springframework.test.context.TestPropertySource
38 import org.springframework.test.context.junit4.SpringRunner
39 import java.nio.file.Paths
40 import kotlin.test.AfterTest
41 import kotlin.test.BeforeTest
42 import kotlin.test.Test
43 import kotlin.test.assertEquals
44 import kotlin.test.assertNotNull
45 import kotlin.test.assertTrue
46
47 @RunWith(SpringRunner::class)
48 @ContextConfiguration(
49     classes = [
50         BlueprintSshLibConfiguration::class,
51         BlueprintPropertyConfiguration::class, BlueprintPropertiesService::class
52     ]
53 )
54 @TestPropertySource(
55     properties =
56         [
57             "blueprintsprocessor.sshclient.sample.type=basic-auth",
58             "blueprintsprocessor.sshclient.sample.host=localhost",
59             "blueprintsprocessor.sshclient.sample.port=52815",
60             "blueprintsprocessor.sshclient.sample.username=root",
61             "blueprintsprocessor.sshclient.sample.password=dummyps"
62         ]
63 )
64 class BlueprintSshClientServiceTest {
65
66     @Autowired
67     lateinit var bluePrintSshLibPropertyService: BlueprintSshLibPropertyService
68
69     lateinit var bluePrintSshLibPropertyServiceMock: BlueprintSshLibPropertyService
70
71     private lateinit var sshServer: SshServer
72
73     @BeforeTest
74     fun startShellServer() {
75         runBlocking {
76             println("Start local Shell server")
77             sshServer = setupTestServer("localhost", 52815, "root", "dummyps")
78             sshServer.start()
79             println(sshServer)
80         }
81     }
82
83     @AfterTest
84     fun stopShellServer() {
85         println("End the Shell server")
86         sshServer.stop(true)
87     }
88
89     @Test
90     fun testStartSessionNB() {
91         val clientSession = getSshClientService().startSession()
92         assertNotNull(clientSession, "Failed to start ssh session with server")
93     }
94
95     @Test
96     fun testBasicAuthSshClientService() {
97         runBlocking {
98             val blueprintSshClientService = getSshClientService()
99             blueprintSshClientService.startSession()
100             // Preparing response
101             val commandResults = arrayListOf<CommandResult>()
102             commandResults.add(CommandResult("echo 1", "echo 1\n#", true))
103             commandResults.add(CommandResult("echo 2", "echo 1\n#echo 2\n#", true))
104             val response = blueprintSshClientService.executeCommands(arrayListOf("echo 1", "echo 2"), 2000)
105             blueprintSshClientService.closeSession()
106
107             assertEquals(response, commandResults, "failed to get command responses")
108         }
109     }
110
111     @Test
112     fun `testBasicAuthSshClientService single execution command`() {
113         runBlocking {
114             val blueprintSshClientService = getSshClientService()
115             blueprintSshClientService.startSession()
116             val response = blueprintSshClientService.executeCommand("echo 1", 2000)
117             blueprintSshClientService.closeSession()
118
119             assertEquals(response, CommandResult("echo 1", "echo 1\n#", true), "failed to get command response")
120         }
121     }
122
123     @Test
124     fun testCloseSessionNB() {
125         val bluePrintSshLibPropertyService = bluePrintSshLibPropertyService.blueprintSshClientService("sample")
126         val clientSession = bluePrintSshLibPropertyService.startSession()
127         bluePrintSshLibPropertyService.closeSession()
128         assertTrue(clientSession.isClosed, "Failed to close ssh session with server")
129     }
130
131     private fun setupTestServer(host: String, port: Int, username: String, password: String): SshServer {
132         val sshd = SshServer.setUpDefaultServer()
133         sshd.port = port
134         sshd.host = host
135         sshd.keyPairProvider = createTestHostKeyProvider()
136         sshd.passwordAuthenticator = BogusPasswordAuthenticator(username, password)
137         sshd.publickeyAuthenticator = AcceptAllPublickeyAuthenticator.INSTANCE
138         sshd.shellFactory = EchoShellFactory.INSTANCE
139         sshd.commandFactory = ProcessShellCommandFactory.INSTANCE
140         return sshd
141     }
142
143     private fun createTestHostKeyProvider(): KeyPairProvider {
144         val keyProvider = SimpleGeneratorHostKeyProvider()
145         keyProvider.path = Paths.get("target").resolve("hostkey." + RSA_ALGORITHM.toLowerCase())
146         keyProvider.algorithm = RSA_ALGORITHM
147         return keyProvider
148     }
149
150     private fun getSshClientService(): BlueprintSshClientService {
151         return bluePrintSshLibPropertyService.blueprintSshClientService("sample")
152     }
153 }
154
155 class BogusPasswordAuthenticator(private val usr: String, private val pwd: String) : PasswordAuthenticator {
156
157     override fun authenticate(username: String, password: String, serverSession: ServerSession): Boolean {
158         assertEquals(username, usr, "failed to match username")
159         assertEquals(password, pwd, "failed to match password")
160         return true
161     }
162 }