|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * 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, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.dolphinscheduler.common.log.remote; |
| 19 | + |
| 20 | +import static org.mockito.ArgumentMatchers.any; |
| 21 | +import static org.mockito.Mockito.times; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | + |
| 24 | +import org.apache.dolphinscheduler.common.constants.Constants; |
| 25 | +import org.apache.dolphinscheduler.common.utils.LogUtils; |
| 26 | +import org.apache.dolphinscheduler.common.utils.PropertyUtils; |
| 27 | + |
| 28 | +import lombok.extern.slf4j.Slf4j; |
| 29 | + |
| 30 | +import org.junit.jupiter.api.Assertions; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 33 | +import org.mockito.Mock; |
| 34 | +import org.mockito.MockedConstruction; |
| 35 | +import org.mockito.MockedStatic; |
| 36 | +import org.mockito.Mockito; |
| 37 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 38 | + |
| 39 | +import com.azure.storage.blob.BlobClient; |
| 40 | +import com.azure.storage.blob.BlobContainerClient; |
| 41 | +import com.azure.storage.blob.BlobServiceClient; |
| 42 | +import com.azure.storage.blob.BlobServiceClientBuilder; |
| 43 | +import com.azure.storage.common.StorageSharedKeyCredential; |
| 44 | + |
| 45 | +@Slf4j |
| 46 | +@ExtendWith(MockitoExtension.class) |
| 47 | +public class AbsRemoteLogHandlerTest { |
| 48 | + |
| 49 | + @Mock |
| 50 | + BlobServiceClient blobServiceClient; |
| 51 | + |
| 52 | + @Mock |
| 53 | + BlobContainerClient blobContainerClient; |
| 54 | + |
| 55 | + @Mock |
| 56 | + BlobClient blobClient; |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testAbsRemoteLogHandlerContainerNameBlack() { |
| 60 | + try ( |
| 61 | + MockedStatic<PropertyUtils> propertyUtilsMockedStatic = Mockito.mockStatic(PropertyUtils.class); |
| 62 | + MockedStatic<LogUtils> remoteLogUtilsMockedStatic = Mockito.mockStatic(LogUtils.class)) { |
| 63 | + propertyUtilsMockedStatic.when(() -> PropertyUtils.getString(Constants.REMOTE_LOGGING_ABS_ACCOUNT_NAME)) |
| 64 | + .thenReturn("account_name"); |
| 65 | + propertyUtilsMockedStatic.when(() -> PropertyUtils.getString(Constants.REMOTE_LOGGING_ABS_ACCOUNT_KEY)) |
| 66 | + .thenReturn("account_key"); |
| 67 | + propertyUtilsMockedStatic.when(() -> PropertyUtils.getString(Constants.REMOTE_LOGGING_ABS_CONTAINER_NAME)) |
| 68 | + .thenReturn(""); |
| 69 | + remoteLogUtilsMockedStatic.when(LogUtils::getLocalLogBaseDir).thenReturn("logs"); |
| 70 | + |
| 71 | + IllegalArgumentException thrown = Assertions.assertThrows(IllegalArgumentException.class, () -> { |
| 72 | + AbsRemoteLogHandler.getInstance(); |
| 73 | + }); |
| 74 | + Assertions.assertEquals("remote.logging.abs.container.name is blank", thrown.getMessage()); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testAbsRemoteLogHandlerContainerNotExists() { |
| 80 | + try ( |
| 81 | + MockedStatic<PropertyUtils> propertyUtilsMockedStatic = Mockito.mockStatic(PropertyUtils.class); |
| 82 | + MockedStatic<LogUtils> remoteLogUtilsMockedStatic = Mockito.mockStatic(LogUtils.class); |
| 83 | + MockedConstruction<BlobServiceClientBuilder> k8sClientWrapperMockedConstruction = |
| 84 | + Mockito.mockConstruction(BlobServiceClientBuilder.class, (mock, context) -> { |
| 85 | + when(mock.endpoint(any(String.class))).thenReturn(mock); |
| 86 | + when(mock.credential(any(StorageSharedKeyCredential.class))).thenReturn(mock); |
| 87 | + when(mock.buildClient()) |
| 88 | + .thenReturn(blobServiceClient); |
| 89 | + })) { |
| 90 | + propertyUtilsMockedStatic.when(() -> PropertyUtils.getString(Constants.REMOTE_LOGGING_ABS_ACCOUNT_NAME)) |
| 91 | + .thenReturn("account_name"); |
| 92 | + propertyUtilsMockedStatic.when(() -> PropertyUtils.getString(Constants.REMOTE_LOGGING_ABS_ACCOUNT_KEY)) |
| 93 | + .thenReturn("account_key"); |
| 94 | + propertyUtilsMockedStatic.when(() -> PropertyUtils.getString(Constants.REMOTE_LOGGING_ABS_CONTAINER_NAME)) |
| 95 | + .thenReturn("container_name"); |
| 96 | + remoteLogUtilsMockedStatic.when(LogUtils::getLocalLogBaseDir).thenReturn("logs"); |
| 97 | + |
| 98 | + when(blobServiceClient.getBlobContainerClient(any(String.class))).thenThrow( |
| 99 | + new NullPointerException("container not exists")); |
| 100 | + IllegalArgumentException thrown = Assertions.assertThrows(IllegalArgumentException.class, () -> { |
| 101 | + AbsRemoteLogHandler.getInstance(); |
| 102 | + }); |
| 103 | + Assertions.assertEquals("containerName: container_name is not exists, you need to create them by yourself", |
| 104 | + thrown.getMessage()); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testAbsRemoteLogHandler() { |
| 110 | + |
| 111 | + try ( |
| 112 | + MockedStatic<PropertyUtils> propertyUtilsMockedStatic = Mockito.mockStatic(PropertyUtils.class); |
| 113 | + MockedStatic<LogUtils> remoteLogUtilsMockedStatic = Mockito.mockStatic(LogUtils.class); |
| 114 | + MockedConstruction<BlobServiceClientBuilder> blobServiceClientBuilderMockedConstruction = |
| 115 | + Mockito.mockConstruction(BlobServiceClientBuilder.class, (mock, context) -> { |
| 116 | + when(mock.endpoint(any(String.class))).thenReturn(mock); |
| 117 | + when(mock.credential(any(StorageSharedKeyCredential.class))).thenReturn(mock); |
| 118 | + when(mock.buildClient()) |
| 119 | + .thenReturn(blobServiceClient); |
| 120 | + }); |
| 121 | + MockedStatic<RemoteLogUtils> remoteLogUtilsMockedStatic1 = Mockito.mockStatic(RemoteLogUtils.class)) { |
| 122 | + propertyUtilsMockedStatic.when(() -> PropertyUtils.getString(Constants.REMOTE_LOGGING_ABS_ACCOUNT_NAME)) |
| 123 | + .thenReturn("account_name"); |
| 124 | + propertyUtilsMockedStatic.when(() -> PropertyUtils.getString(Constants.REMOTE_LOGGING_ABS_ACCOUNT_KEY)) |
| 125 | + .thenReturn("account_key"); |
| 126 | + propertyUtilsMockedStatic.when(() -> PropertyUtils.getString(Constants.REMOTE_LOGGING_ABS_CONTAINER_NAME)) |
| 127 | + .thenReturn("container_name"); |
| 128 | + remoteLogUtilsMockedStatic.when(LogUtils::getLocalLogBaseDir).thenReturn("logs"); |
| 129 | + String logPath = "logpath"; |
| 130 | + String objectName = "objectname"; |
| 131 | + remoteLogUtilsMockedStatic1.when(() -> RemoteLogUtils.getObjectNameFromLogPath(logPath)) |
| 132 | + .thenReturn(objectName); |
| 133 | + |
| 134 | + when(blobServiceClient.getBlobContainerClient(any(String.class))).thenReturn(blobContainerClient); |
| 135 | + when(blobContainerClient.getBlobClient(objectName)).thenReturn(blobClient); |
| 136 | + |
| 137 | + AbsRemoteLogHandler absRemoteLogHandler = AbsRemoteLogHandler.getInstance(); |
| 138 | + Assertions.assertNotNull(absRemoteLogHandler); |
| 139 | + |
| 140 | + absRemoteLogHandler.sendRemoteLog(logPath); |
| 141 | + Mockito.verify(blobClient, times(1)).uploadFromFile(logPath); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments