wu-sheng commented on code in PR #9822:
URL: https://github.com/apache/skywalking/pull/9822#discussion_r1002651726


##########
oap-server/analyzer/meter-analyzer/src/test/java/org/apache/skywalking/oap/meter/analyzer/dsl/RuleLoaderTest.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oap.meter.analyzer.dsl;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.skywalking.oap.meter.analyzer.prometheus.rule.Rule;
+import org.apache.skywalking.oap.meter.analyzer.prometheus.rule.Rules;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.UUID;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.fail;
+
+@Slf4j
+@RunWith(Parameterized.class)
+public class RuleLoaderTest {
+    @Parameterized.Parameter
+    public List<String> enabledRule;
+
+    @Parameterized.Parameter(1)
+    public int rulesNumber;
+
+    @Parameterized.Parameter(2)
+    public boolean isThrow;
+
+    @Parameterized.Parameters(name = "{index}: {0}")
+    public static Collection<Object[]> data() {
+        return Arrays.asList(new Object[][] {
+            // abc/abc.yml is a not well-formed yml file.
+            {Arrays.asList("abc/*"), 0, true},

Review Comment:
   The good test mock samples should be 
   - /illegal-yaml/test.yml
   - /test-folder/case1.yml
   - single-file-case.yml



##########
oap-server/analyzer/meter-analyzer/src/test/java/org/apache/skywalking/oap/meter/analyzer/dsl/RuleLoaderTest.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oap.meter.analyzer.dsl;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.skywalking.oap.meter.analyzer.prometheus.rule.Rule;
+import org.apache.skywalking.oap.meter.analyzer.prometheus.rule.Rules;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.UUID;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.fail;
+
+@Slf4j
+@RunWith(Parameterized.class)
+public class RuleLoaderTest {
+    @Parameterized.Parameter
+    public List<String> enabledRule;
+
+    @Parameterized.Parameter(1)
+    public int rulesNumber;
+
+    @Parameterized.Parameter(2)
+    public boolean isThrow;
+
+    @Parameterized.Parameters(name = "{index}: {0}")
+    public static Collection<Object[]> data() {
+        return Arrays.asList(new Object[][] {
+            // abc/abc.yml is a not well-formed yml file.
+            {Arrays.asList("abc/*"), 0, true},
+            {Arrays.asList("k8s/*"), 4, false},
+            {Arrays.asList("/k8s/*.yaml"), 4, false},
+            {Arrays.asList("/k8s/*.yml"), 4, false},
+            {Arrays.asList("k8s/*.yaml"), 4, false},
+            {Arrays.asList("k8s/*.yml"), 4, false},
+            {Arrays.asList("k8s/k8s-cluster.yml"), 1, false},
+            {Arrays.asList("/k8s/k8s-cluster.yml"), 1, false},
+            {Arrays.asList("/k8s/k8s-cluster.yaml"), 1, false},
+            {Arrays.asList("k8s/k8s-cluster.yaml"), 1, false},
+            {Arrays.asList("k8s/k8s-cluster"), 1, false},
+            {Arrays.asList("oap.yaml"), 1, false},
+            {Arrays.asList("oap.yml"), 1, false},
+            {Arrays.asList("oap"), 1, false},
+            {Arrays.asList("/oap.yaml"), 1, false},
+            {Arrays.asList("/oap.yml"), 1, false},
+            {Arrays.asList("/oap.yml", "/k8s/*"), 5, false},
+            //if enabledRule not found, will not fail but will not load any 
rules
+            {Arrays.asList(UUID.randomUUID().toString()), 0, false},

Review Comment:
   I think this should through an exception rather than a `false` return.



##########
oap-server/analyzer/meter-analyzer/src/test/java/org/apache/skywalking/oap/meter/analyzer/dsl/RuleLoaderTest.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oap.meter.analyzer.dsl;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.skywalking.oap.meter.analyzer.prometheus.rule.Rule;
+import org.apache.skywalking.oap.meter.analyzer.prometheus.rule.Rules;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.UUID;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.fail;
+
+@Slf4j
+@RunWith(Parameterized.class)
+public class RuleLoaderTest {
+    @Parameterized.Parameter
+    public List<String> enabledRule;
+
+    @Parameterized.Parameter(1)
+    public int rulesNumber;
+
+    @Parameterized.Parameter(2)
+    public boolean isThrow;
+
+    @Parameterized.Parameters(name = "{index}: {0}")
+    public static Collection<Object[]> data() {
+        return Arrays.asList(new Object[][] {
+            // abc/abc.yml is a not well-formed yml file.
+            {Arrays.asList("abc/*"), 0, true},
+            {Arrays.asList("k8s/*"), 4, false},
+            {Arrays.asList("/k8s/*.yaml"), 4, false},
+            {Arrays.asList("/k8s/*.yml"), 4, false},
+            {Arrays.asList("k8s/*.yaml"), 4, false},
+            {Arrays.asList("k8s/*.yml"), 4, false},
+            {Arrays.asList("k8s/k8s-cluster.yml"), 1, false},
+            {Arrays.asList("/k8s/k8s-cluster.yml"), 1, false},
+            {Arrays.asList("/k8s/k8s-cluster.yaml"), 1, false},
+            {Arrays.asList("k8s/k8s-cluster.yaml"), 1, false},
+            {Arrays.asList("k8s/k8s-cluster"), 1, false},
+            {Arrays.asList("oap.yaml"), 1, false},
+            {Arrays.asList("oap.yml"), 1, false},
+            {Arrays.asList("oap"), 1, false},
+            {Arrays.asList("/oap.yaml"), 1, false},
+            {Arrays.asList("/oap.yml"), 1, false},
+            {Arrays.asList("/oap.yml", "/k8s/*"), 5, false},
+            //if enabledRule not found, will not fail but will not load any 
rules
+            {Arrays.asList(UUID.randomUUID().toString()), 0, false},
+        });
+    }
+
+    @Test
+    public void test() {
+        List<Rule> rules = null;
+        try {
+            rules = Rules.loadRules("otel-rules", enabledRule);
+        } catch (Exception e) {
+            if (isThrow) {
+                return;
+            }
+            fail("load rules failed");
+        }

Review Comment:
   Learn how to wrong predicable exceptions in UT
   
   ```java
   @Test(expected = IllegalArgumentException.class)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to