b7a06ae12d1b5a4fd05979798aed7730919871f8
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2021 Samsung Electronics. All rights reserved.
6  * ================================================================================
7  *
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.ccsdk.sli.adaptors.saltstack.impl;
26
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResult;
30 import org.onap.ccsdk.sli.adaptors.saltstack.impl.SshConnection;
31
32
33 import java.io.FileOutputStream;
34 import java.io.OutputStream;
35 import java.util.Properties;
36
37 import static org.junit.Assert.assertEquals;
38
39 public class TestSshConnection {
40
41 private SshConnection sshConnection;
42 private Properties params;
43
44
45     @Before
46     public void setup() throws IllegalArgumentException {
47         String HostName = "localhost";
48         int Port = 22;
49         String User = "test";
50         String Password = "test";
51         sshConnection = new SshConnection(HostName, Port, User, Password);
52         params = new Properties();
53     }
54
55     @Test(expected=NullPointerException.class)
56     public void reqConnect_exitStatusFailed() {
57             sshConnection.setExecTimeout(10);
58             sshConnection.connect();
59     }
60
61     @Test
62     public void reqexecCommandWithPty_exitStatusFailed() {
63         sshConnection.setExecTimeout(10);
64         int outcome = 999;
65         try {
66             params.put("org.onap.appc.adaptor.saltstack.clientType", "SSH_CERT");
67             params.put("org.onap.appc.adaptor.saltstack.host", "test");
68             params.put("org.onap.appc.adaptor.saltstack.port", "10");
69             params.put("org.onap.appc.adaptor.saltstack.userName", "test");
70             params.put("org.onap.appc.adaptor.saltstack.userPasswd", "test");
71             params.put("org.onap.appc.adaptor.saltstack.sshKey", "test");
72             OutputStream res = new FileOutputStream("test.out");
73             outcome = sshConnection.execCommandWithPty("ls",res);
74             assertEquals(1,outcome);
75         } catch (Exception e) {
76             e.printStackTrace();
77         }
78     }
79
80     @Test(expected=NullPointerException.class)
81     public void reqDisconnect_exitStatusFailed() {
82         params.put("org.onap.appc.adaptor.saltstack.clientType", "SSH_CERT");
83         params.put("org.onap.appc.adaptor.saltstack.host", "test");
84         params.put("org.onap.appc.adaptor.saltstack.port", "10");
85         params.put("org.onap.appc.adaptor.saltstack.userName", "test");
86         params.put("org.onap.appc.adaptor.saltstack.userPasswd", "test");
87         params.put("org.onap.appc.adaptor.saltstack.sshKey", "test");
88         sshConnection.setExecTimeout(10);
89         sshConnection.disconnect();
90     }
91
92     @Test
93     public void reqexecCommand_exitStatusFailed() {
94         sshConnection.setExecTimeout(10);
95         int outcome=999;
96         try {
97             params.put("org.onap.appc.adaptor.saltstack.clientType", "SSH_CERT");
98             params.put("org.onap.appc.adaptor.saltstack.host", "test");
99             params.put("org.onap.appc.adaptor.saltstack.port", "10");
100             params.put("org.onap.appc.adaptor.saltstack.userName", "test");
101             params.put("org.onap.appc.adaptor.saltstack.userPasswd", "test");
102             params.put("org.onap.appc.adaptor.saltstack.sshKey", "test");
103             OutputStream res = new FileOutputStream("test.out");
104             OutputStream resErr = new FileOutputStream("test.out");
105             outcome = sshConnection.execCommand("ls",res, resErr);
106             assertEquals(1,outcome);
107         } catch (Exception e) {
108             e.printStackTrace();
109         }
110     }
111 }
112