<p>I wish to use <code>promtool</code> to run unit tests against alerts that I have setup e.g.</p>
<pre><code>promtool test rules alert-test.yaml
</code></pre>
<p>Here's an example test file:</p>
<pre><code># alert-test.yaml
rule_files: 
- 'my-alert.yaml'
tests:
- name: 'Fire ManyRequests Alert'
  interval: 1s # every second
  input_series:
    - series: rate(http_requests_total[5m])
      values: '0+1x30' # starting at 0 requests per second increase to 30 requests per second
  alert_rule_test:
    - alertname: ManyRequests
      eval_time: 10s
      exp_alerts: # no alert
    - alertname: ManyRequests
      eval_time: 25s
      exp_alerts:
        - exp_labels:
            severity: p2
          exp_annotations:
            description: Request increase too high
</code></pre>
<p>Nothing new here. And this would be no problem if <code>my-alert.yaml</code> looked like this:</p>
<pre><code>groups:
  - name: alert_group_name
    rules:
      - alert: ManyRequests
        expr: rate(http_requests_total[5m]) &gt; 20
        for: 5s
        annotations:
          description: Request increase too high
        labels:
          severity: P2
</code></pre>
<p>but instead i have a Kubernetes manifest that is being deployed i.e.</p>
<pre><code>my-alert.yaml
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  labels:
    prometheus: prometheus-core-rules
    role: alert-rules
  name: tesla-maps-cert-alert-rules
spec:
  groups:
    - name: alert_group_name
      rules:
        - alert: ManyRequests
          expr: rate(http_requests_total[5m]) &gt; 20
          for: 5s
          annotations:
            description: Request increase too high
          labels:
            severity: P2
</code></pre>
<p>so running <code>promtool test rules alert-test.yaml</code> throws this error:</p>
<pre><code>Unit Testing:  alerts-test.yaml
  FAILED:
my-alert.yaml: yaml: unmarshal errors:
  line 1: field apiVersion not found in type rulefmt.RuleGroups
  line 2: field kind not found in type rulefmt.RuleGroups
  line 3: field metadata not found in type rulefmt.RuleGroups
  line 8: field spec not found in type rulefmt.RuleGroups
</code></pre>
<p>what can i do to get this working?</p>

Erich Shan
<blockquote>
<p>what can i do to get this working?</p>
</blockquote>
<p>Just extract the relevant portion of the YAML manifest:</p>
<pre><code>yq .spec my-alert.yaml &gt; my-alert-for-testing.yaml
</code></pre>
<p>And modify your test file to reference the extracted file:</p>
<pre><code>rules_files:
  - my-alert-for-testing.yaml
</code></pre>
<p>I'm using <a href="https://kislyuk.github.io/yq/" rel="nofollow noreferrer">this yq</a>, but <a href="https://github.com/mikefarah/yq" rel="nofollow noreferrer">the other one</a> should work as well.</p>
