|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.kubernetes.cluster.utils; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | + |
| 21 | +import org.junit.Assert; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.runner.RunWith; |
| 24 | +import org.mockito.Mockito; |
| 25 | +import org.powermock.api.mockito.PowerMockito; |
| 26 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 27 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 28 | + |
| 29 | +import com.cloud.utils.Pair; |
| 30 | +import com.cloud.utils.ssh.SshHelper; |
| 31 | + |
| 32 | +@RunWith(PowerMockRunner.class) |
| 33 | +@PrepareForTest(SshHelper.class) |
| 34 | +public class KubernetesClusterUtilTest { |
| 35 | + String ipAddress = "10.1.1.1"; |
| 36 | + int port = 2222; |
| 37 | + String user = "user"; |
| 38 | + File sshKeyFile = Mockito.mock(File.class); |
| 39 | + String hostName = "host"; |
| 40 | + |
| 41 | + private void mockSshHelperExecuteThrowAndTestVersionMatch() { |
| 42 | + try { |
| 43 | + Mockito.when(SshHelper.sshExecute(ipAddress, port, user, sshKeyFile, null, KubernetesClusterUtil.CLUSTER_NODE_VERSION_COMMAND, 10000, 10000, 20000)).thenThrow(Exception.class); |
| 44 | + } catch (Exception e) { |
| 45 | + Assert.fail(String.format("Exception: %s", e.getMessage())); |
| 46 | + } |
| 47 | + Assert.assertFalse(KubernetesClusterUtil.clusterNodeVersionMatches("1.24.0", false, ipAddress, port, user, sshKeyFile, hostName)); |
| 48 | + } |
| 49 | + |
| 50 | + private void mockSshHelperExecuteAndTestVersionMatch(boolean status, String response, boolean isControlNode, boolean expectedResult) { |
| 51 | + try { |
| 52 | + Mockito.when(SshHelper.sshExecute(ipAddress, port, user, sshKeyFile, null, KubernetesClusterUtil.CLUSTER_NODE_VERSION_COMMAND, 10000, 10000, 20000)).thenReturn(new Pair<>(status, response)); |
| 53 | + } catch (Exception e) { |
| 54 | + Assert.fail(String.format("Exception: %s", e.getMessage())); |
| 55 | + } |
| 56 | + boolean result = KubernetesClusterUtil.clusterNodeVersionMatches("1.24.0", isControlNode, ipAddress, port, user, sshKeyFile, hostName); |
| 57 | + Assert.assertEquals(expectedResult, result); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testClusterNodeVersionMatches() { |
| 62 | + PowerMockito.mockStatic(SshHelper.class); |
| 63 | + String v1233WorkerNodeOutput = "Client Version: v1.23.3\n" + |
| 64 | + "The connection to the server localhost:8080 was refused - did you specify the right host or port?"; |
| 65 | + String v1240WorkerNodeOutput = "Client Version: v1.24.0\n" + |
| 66 | + "Kustomize Version: v4.5.4\n" + |
| 67 | + "The connection to the server localhost:8080 was refused - did you specify the right host or port?"; |
| 68 | + String v1240ControlNodeOutput = "Client Version: v1.24.0\n" + |
| 69 | + "Kustomize Version: v4.5.4\n" + |
| 70 | + "Server Version: v1.24.0"; |
| 71 | + mockSshHelperExecuteAndTestVersionMatch(true, v1240WorkerNodeOutput, false, true); |
| 72 | + |
| 73 | + mockSshHelperExecuteAndTestVersionMatch(true, v1240ControlNodeOutput, true, true); |
| 74 | + |
| 75 | + mockSshHelperExecuteAndTestVersionMatch(true, v1240WorkerNodeOutput, true, false); |
| 76 | + |
| 77 | + mockSshHelperExecuteAndTestVersionMatch(false, v1240WorkerNodeOutput, false, false); |
| 78 | + |
| 79 | + mockSshHelperExecuteAndTestVersionMatch(true, v1233WorkerNodeOutput, false, false); |
| 80 | + |
| 81 | + mockSshHelperExecuteAndTestVersionMatch(true, "Client Version: v1.24.0\n" + |
| 82 | + "Kustomize Version: v4.5.4\n" + |
| 83 | + "Server Version: v1.23.0", true, false); |
| 84 | + |
| 85 | + mockSshHelperExecuteAndTestVersionMatch(true, null, false, false); |
| 86 | + |
| 87 | + mockSshHelperExecuteAndTestVersionMatch(false, "-\n-", false, false); |
| 88 | + |
| 89 | + mockSshHelperExecuteAndTestVersionMatch(false, "1.24.0", false, false); |
| 90 | + |
| 91 | + mockSshHelperExecuteThrowAndTestVersionMatch(); |
| 92 | + } |
| 93 | +} |
0 commit comments