|
| 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 | + |
| 19 | +package org.apache.skywalking.oap.query.graphql.mqe.rt; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.List; |
| 25 | +import org.apache.skywalking.mqe.rt.exception.IllegalExpressionException; |
| 26 | +import org.apache.skywalking.oap.server.core.CoreModule; |
| 27 | +import org.apache.skywalking.oap.server.core.query.enumeration.Step; |
| 28 | +import org.apache.skywalking.oap.server.core.query.input.AttrCondition; |
| 29 | +import org.apache.skywalking.oap.server.core.query.input.Duration; |
| 30 | +import org.apache.skywalking.oap.server.core.query.input.Entity; |
| 31 | +import org.apache.skywalking.oap.server.core.storage.model.ColumnName; |
| 32 | +import org.apache.skywalking.oap.server.core.storage.model.IModelManager; |
| 33 | +import org.apache.skywalking.oap.server.core.storage.model.Model; |
| 34 | +import org.apache.skywalking.oap.server.core.storage.model.ModelColumn; |
| 35 | +import org.apache.skywalking.oap.server.library.module.ModuleManager; |
| 36 | +import org.apache.skywalking.oap.server.library.module.ModuleProviderHolder; |
| 37 | +import org.apache.skywalking.oap.server.library.module.ModuleServiceHolder; |
| 38 | +import org.junit.jupiter.api.BeforeEach; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 41 | +import org.mockito.Mock; |
| 42 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 43 | +import org.mockito.junit.jupiter.MockitoSettings; |
| 44 | +import org.mockito.quality.Strictness; |
| 45 | + |
| 46 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 47 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 48 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 49 | +import static org.mockito.Mockito.mock; |
| 50 | +import static org.mockito.Mockito.when; |
| 51 | + |
| 52 | +/** |
| 53 | + * Guards the top_n attribute pre-check in {@link MQEVisitor#checkAttributes}: an attribute condition whose |
| 54 | + * key is not a queryable column of the metric must be rejected with a descriptive {@link |
| 55 | + * IllegalExpressionException} before the request reaches storage, instead of letting the backend surface a |
| 56 | + * raw IO exception. Metrics with no attribute columns (relations, database/cache/mq access) are the case |
| 57 | + * that used to fail. |
| 58 | + */ |
| 59 | +@ExtendWith(MockitoExtension.class) |
| 60 | +@MockitoSettings(strictness = Strictness.LENIENT) |
| 61 | +public class MQEVisitorAttributeTest { |
| 62 | + @Mock |
| 63 | + private ModuleManager moduleManager; |
| 64 | + @Mock |
| 65 | + private IModelManager modelManager; |
| 66 | + |
| 67 | + private MQEVisitor visitor; |
| 68 | + |
| 69 | + @BeforeEach |
| 70 | + public void setup() { |
| 71 | + final ModuleProviderHolder providerHolder = mock(ModuleProviderHolder.class); |
| 72 | + final ModuleServiceHolder serviceHolder = mock(ModuleServiceHolder.class); |
| 73 | + when(moduleManager.find(CoreModule.NAME)).thenReturn(providerHolder); |
| 74 | + when(providerHolder.provider()).thenReturn(serviceHolder); |
| 75 | + when(serviceHolder.getService(IModelManager.class)).thenReturn(modelManager); |
| 76 | + // Build the model mocks before stubbing allModels(); nesting when() calls confuses Mockito. |
| 77 | + final List<Model> models = Arrays.asList( |
| 78 | + // endpoint_cpm is decorated, so its measure carries the attr0 tag. |
| 79 | + modelWithColumns("endpoint_cpm", "entity_id", "service_id", "attr0"), |
| 80 | + // a relation metric carries no attribute columns at all. |
| 81 | + modelWithColumns("service_relation_client_cpm", "entity_id", "source_service_id", "dest_service_id")); |
| 82 | + when(modelManager.allModels()).thenReturn(models); |
| 83 | + |
| 84 | + final Duration duration = mock(Duration.class); |
| 85 | + when(duration.getStep()).thenReturn(Step.MINUTE); |
| 86 | + visitor = new MQEVisitor(moduleManager, mock(Entity.class), duration); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void rejectsAttributeMissingFromMetricSchema() { |
| 91 | + final IllegalExpressionException ex = assertThrows(IllegalExpressionException.class, () -> |
| 92 | + visitor.checkAttributes( |
| 93 | + "service_relation_client_cpm", |
| 94 | + Collections.singletonList(new AttrCondition("attr0", "VIRTUAL_DATABASE", true)))); |
| 95 | + assertTrue(ex.getMessage().contains("attr0")); |
| 96 | + assertTrue(ex.getMessage().contains("service_relation_client_cpm")); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void rejectsNotEqualsAttributeMissingFromMetricSchema() { |
| 101 | + // the same holds for `attr0 != x`, since the check is on the attribute key. |
| 102 | + assertThrows(IllegalExpressionException.class, () -> |
| 103 | + visitor.checkAttributes( |
| 104 | + "service_relation_client_cpm", |
| 105 | + Collections.singletonList(new AttrCondition("attr0", "MESH", false)))); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void allowsAttributePresentInMetricSchema() { |
| 110 | + assertDoesNotThrow(() -> |
| 111 | + visitor.checkAttributes( |
| 112 | + "endpoint_cpm", |
| 113 | + Collections.singletonList(new AttrCondition("attr0", "GENERAL", true)))); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void allowsEmptyAttributes() { |
| 118 | + assertDoesNotThrow(() -> visitor.checkAttributes("service_relation_client_cpm", Collections.emptyList())); |
| 119 | + } |
| 120 | + |
| 121 | + private static Model modelWithColumns(String name, String... columnNames) { |
| 122 | + final Model model = mock(Model.class); |
| 123 | + when(model.getName()).thenReturn(name); |
| 124 | + final List<ModelColumn> columns = new ArrayList<>(columnNames.length); |
| 125 | + for (final String columnName : columnNames) { |
| 126 | + final ColumnName wrappedName = mock(ColumnName.class); |
| 127 | + when(wrappedName.getName()).thenReturn(columnName); |
| 128 | + final ModelColumn column = mock(ModelColumn.class); |
| 129 | + when(column.getColumnName()).thenReturn(wrappedName); |
| 130 | + when(column.shouldIndex()).thenReturn(true); |
| 131 | + columns.add(column); |
| 132 | + } |
| 133 | + when(model.getColumns()).thenReturn(columns); |
| 134 | + return model; |
| 135 | + } |
| 136 | +} |
0 commit comments