> For the complete documentation index, see [llms.txt](https://docs.panther.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.panther.com/ko/detections/testing.md).

# 테스트

## 개요

디택션을 테스트하면 배포 후 디택션이 예상대로 동작하고 알러트를 [신호](/ko/detections/signals.md) 적절하게 생성함을 보장합니다. 테스트는 또한 신뢰성을 높이고 코드가 시간이 지나며 진화함에 따라 회귀를 방지합니다.

테스트는 특정 디택션에 대한 테스트 로그 이벤트를 정의하고, 해당 테스트 이벤트가 그 디택션에 의해 처리될 때 알러트가 생성될지 예상하는지 여부를 지정하는 방식으로 작동합니다. Panther는 각 디택션에 대해 필수 최소 테스트 수를 강제하지 않지만, 오탐 하나와 정탐 하나, 최소 두 개를 구성하는 것이 권장됩니다.

사용자 정의 디택션(즉, 다음이 아닌 것)에 대해 테스트를 생성, 편집 및 삭제할 수 있습니다 [Panther가 관리하는](/ko/detections/panther-managed.md)). 다음 디택션에 대한 테스트는 [Panther가 관리하는](/ko/detections/panther-managed.md) Panther가 관리하며 읽기 전용입니다.

Panther의 [데이터 리플레이](/ko/detections/testing/data-replay.md)결과를 미리 보기 위해 과거 로그 데이터를 룰에 통과시켜 실행할 수 있게 해주는 기능도 테스트할 때 유용합니다.

## 테스트 사용하기

### 테스트를 만드는 방법

다음이 아닌 디택션에 대한 테스트를 만들 수 있습니다 [Panther가 관리하는](/ko/detections/panther-managed.md) Panther Console에서, 또는 다음을 사용한 CLI 워크플로에서 [Panther Analysis Tool](/ko/panther/detections-repo/pat.md) (PAT).

{% tabs %}
{% tab title="Console: 디택션 페이지" %}

1. Panther Console의 왼쪽 탐색 표시줄에서 **디택션**.
2. 기존 디택션의 이름을 클릭하거나 [새 디택션을 생성합니다](/ko/detections.md#how-to-write-detections).
3. 다음으로 스크롤합니다. **테스트** 섹션의 명령을 실행하세요.
4. 오른쪽의 **단위 테스트** 타일에서 **새로 추가**.\
   ![The Unit Test section of the detection page shows the names of one already-created test ("Failed login"). In the upper right corner there's a "Add New" button, which is circled.](/files/bc3698c17ea647ddb0b5a0bc5463b49563e1448f)
   * 디택션이 [Panther가 관리하는](/ko/detections/panther-managed.md), **새로 추가** 새 테스트를 만들 수 없으므로 비활성화됩니다.
5. 새로 생성된 테스트에는 자리표시자 이름이 지정됩니다. 오른쪽에서 세 개의 점 아이콘을 클릭하고 **이름 바꾸기**.\
   ![In the Unit Test section, a test with the placeholder name "Test-b789c4" is shown. The three dot icon to its right has been selected, which opened a menu with two options: Rename and Delete.](/files/75ca7659b4fd6b3d0208673e8a1fc9ed5e02c28e)
6. 테스트에 의미 있는 이름을 지정한 다음 Enter 또는 Return 키를 눌러 저장합니다.
7. 다음을 설정하세요 **디택션은 예시 이벤트를 기반으로 트리거되어야 합니다** 토글을`YES` 또는 `아니오`.
8. 텍스트 편집기에서 테스트 JSON 이벤트를 작성합니다.
9. 테스트가 통과하는지 확인하려면 **테스트 실행**.
10. 작업을 마치면 페이지 오른쪽 상단에서 **업데이트** 또는 **저장**.
    {% endtab %}

{% tab title="Console: 알러트 이벤트" %}
Panther Console에서 알러트와 연결된 실제 이벤트를 사용하여 알러트에서 바로 새 단위 테스트를 만들 수 있습니다. 다음의 지침을 따르세요 [알러트 이벤트에서 필터 추가](/ko/detections/rules/inline-filters.md#add-filters-from-an-alert-event)에서 9단계와 10단계를 참고하세요.

이 기능은 룰에서만 사용할 수 있습니다.
{% endtab %}

{% tab title="CLI" %}
**CLI 워크플로에서 테스트를 만드는 방법**

**룰 및 예약된 룰**

YAML 파일에서(둘 다 [Python](/ko/detections/rules/python.md) 및 [간단한 디택션](/ko/detections/rules/writing-simple-detections.md)), 다음을 추가합니다 `테스트` 키에 다음 필드를 포함합니다:

```yaml
테스트:
  -
    이름: 첫 번째 테스트를 설명하는 이름
    LogType: LogType.GoesHere
    ExpectedResult: true 또는 false
    로그:
      {
        "hostName": "test-01.prod.acme.io",
        "user": "martin_smith",
        "eventTime": "June 22 5:50:52 PM"
      }
```

**정책**

YAML 파일에 다음을 추가합니다 `테스트` 키. 다음의 값은 `리소스` 에서 바로 복사한 JSON 객체일 수 있습니다. **정책** > **리소스** 탐색기.

```yaml
테스트:
  -
    이름: 첫 번째 테스트를 설명하는 이름.
    ResourceType: AWS.S3.Bucket
    예상 결과: true
    리소스:
      {
        "PublicAccessBlockConfiguration": null,
        "Region": "us-east-1",
        "Policy": null,
        "AccountId": "123456789012",
        "LoggingPolicy": {
          "TargetBucket": "access-logs-us-east-1-100",
          "TargetGrants": null,
          "TargetPrefix": "acmecorp-fiancial-data/"
        },
        "EncryptionRules": [
          {
            "ApplyServerSideEncryptionByDefault": {
              "SSEAlgorithm": "AES256",
              "KMSMasterKeyID": null
            }
          }
        ],
        "Arn": "arn:aws:s3:::acmecorp-fiancial-data",
        "Name": "acmecorp-fiancial-data",
        "LifecycleRules": null,
        "ResourceType": "AWS.S3.Bucket",
        "Grants": [
          {
            "Permission": "FULL_CONTROL",
            "Grantee": {
              "URI": null,
              "EmailAddress": null,
              "DisplayName": "admins",
              "Type": "CanonicalUser",
              "ID": "013ae1034i130431431"
            }
          }
        ],
        "Versioning": "Enabled",
        "ResourceId": "arn:aws:s3:::acmecorp-fiancial-data",
        "Tags": {
          "aws:cloudformation:logical-id": "FinancialDataBucket"
        },
        "Owner": {
          "ID": "013ae1034i130431431",
          "DisplayName": "admins"
        },
        "TimeCreated": "2020-06-13T17:16:36.000Z",
        "ObjectLockConfiguration": null,
        "MFADelete": null
      }
```

{% endtab %}
{% endtabs %}

### Panther Console에서 테스트 이름을 바꾸거나 삭제하는 방법

다음이 아닌 디택션에 대해 테스트의 이름을 바꾸거나 삭제할 수 있습니다 [Panther가 관리하는](/ko/detections/panther-managed.md).

1. Panther Console의 왼쪽 탐색 표시줄에서 **디택션**.
2. 디택션의 이름을 클릭합니다.
3. 다음으로 스크롤합니다. **테스트** 섹션의 명령을 실행하세요.
4. 해당 **단위 테스트** 타일에서 이름을 바꾸거나 삭제할 테스트를 찾습니다.
5. 테스트 이름 오른쪽에서 세 점 아이콘을 클릭합니다.

   <figure><img src="/files/4da35f4eb4a5f1053dd57124318f49a72070fe09" alt="In the Unit Test section, a test named &#x22;Reset by Company Admin&#x22; is shown. The three dot icon to its right has been selected, which has opened a menu with two options: Rename and Delete."><figcaption></figcaption></figure>

   * 디택션이 [Panther가 관리하는](/ko/detections/panther-managed.md)디택션은 테스트의 이름을 바꾸거나 삭제할 수 없습니다. 각 테스트 이름 옆에 세 점 아이콘 대신 Panther 아이콘이 표시됩니다.

     <figure><img src="/files/9f9602df0748e26e84bfaa3440d5eae75795acbd" alt="The Unit Test section shows multiple test names, each with a Panther icon to its left."><figcaption></figcaption></figure>
6. 다음을 클릭합니다 **이름 바꾸기** 또는 **삭제**.
   * 이름을 바꾸는 경우 새 이름을 입력하고 Enter 또는 Return을 눌러 저장합니다.
   * 삭제하는 경우, **테스트 삭제** 확인 모달이 나타납니다.
     * 선택하세요 **확인**.

### 테스트 예시

* 다음을 클릭합니다 **편집** 페이지 오른쪽 상단에서.
* 아래로 스크롤하여 **룰 함수** 텍스트 편집기 아래의 **단위 테스트** 텍스트 편집기.

이전 예시를 이어서(다음에서 [룰](/ko/detections/rules.md)), 이 디택션에 대한 두 개의 테스트를 작성해 봅시다:

{% tabs %}
{% tab title="Python" %}

```python
def 룰(event):
  return event.get('status') == 200 and 'admin-panel' in event.get('request')

    
def title(event):
  return f"{event.get('remoteAddr')}에서 성공적인 관리자 패널 로그인이 감지되었습니다"


def dedup(event):
  return event.get('remoteAddr')
```

{% endtab %}

{% tab title="Simple 디택션" %}

```yaml
디택션:
  - KeyPath: status
    조건: 같음
    값: 200
  - KeyPath: request
    조건: 포함
    값: 'admin-panel'

알러트 제목: "성공적인 관리자 패널 로그인이 감지되었습니다 from {remoteAddr}"

GroupBy:
  - KeyPath: remoteAddr
```

{% endtab %}
{% endtabs %}

`이름`: 성공적인 관리자 패널 로그인

`테스트 이벤트는 알러트를 트리거해야 합니다`: 예

```javascript
{
  "httpReferer": "https://domain1.com/?p=1",
  "httpUserAgent": "Chrome/80.0.3987.132 Safari/537.36",
  "remoteAddr": "180.76.15.143",
  "request": "GET /admin-panel/ HTTP/1.1",
  "status": 200,
  "time": "2019-02-06 00:00:38 +0000 UTC"
}
```

`이름`: 액세스 정책 페이지로의 오류 요청

`테스트 이벤트는 알러트를 트리거해야 합니다`: 아니오

```javascript
{
  "httpReferer": "https://domain1.com/?p=1",
  "httpUserAgent": "Chrome/80.0.3987.132 Safari/537.36",
  "remoteAddr": "180.76.15.143",
  "request": "GET /access-policy/ HTTP/1.1",
  "status": 500,
  "time": "2019-02-06 00:00:38 +0000 UTC"
}
```

{% hint style="info" %}
디택션의 신뢰성을 최대한 높이기 위해 원하는 만큼 다양한 조합을 사용하세요.
{% endhint %}

## 목

{% hint style="warning" %}
Panther의 디택션 내에서 외부 API 요청을 수행하는 것은 매우 권장되지 않습니다. 일반적으로 디택션은 매우 대규모로 처리되며, API 요청을 수행하면 수신 시스템에 과부하를 일으킬 수 있고 룰이 [15초 실행 시간 제한](/ko/detections/rules.md#rule-errors-and-scheduled-rule-errors).
{% endhint %}

Panther의 테스트 프레임워크는 Python 함수 호출에 대한 기본적인 모킹을 지원합니다.

외부 API 호출을 수행하는 디택션의 단위 테스트를 작성할 때, 실제로 API 호출을 실행하지 않고도 테스트에서 서버 응답을 흉내 내기 위해 목을 사용할 수 있습니다.

목은 다음으로 정의됩니다 **Mock 이름** 및 **반환 값** (또는 `objectName` 및 `returnValue`CLI 워크플로에서), 이는 각각 모킹할 함수의 이름과 반환할 목 값을 나타냅니다(해당 값은 다음이어야 합니다 `string`).

목은 단위 테스트 수준에서 정의되므로 각 단위 테스트마다 다른 목을 정의할 수 있습니다.

### 목 사용 방법

Console과 CLI 워크플로 모두에서 테스트에 대한 목을 구성할 수 있습니다.

목은 다음만 반환할 수 있습니다 `string` 값입니다. 모킹된 함수가 다른 데이터 형식을 반환하더라도 마찬가지입니다. 이는 목 기능의 알려진 제한 사항입니다.

{% hint style="info" %}
목 반환 값은 다음 유형이어야 한다는 제한 때문에 `string`string 이외의 데이터 형식을 반환하는 함수를 모킹하는 경우, 테스트가 목 값을 성공적으로 사용하도록 함수의 반환 값을 원래 데이터 형식으로 형 변환하는 것이 권장됩니다.

예를 들어, 다음에서 `룰()` 함수 `get_counter()` 정수를 반환한다면, 다음을 사용하세요 `int(get_counter())`.
{% endhint %}

{% tabs %}
{% tab title="콘솔" %}
Console에서 단위 테스트에 목을 추가하려면:

1. 해당 **단위 테스트** 타일에서 다음을 찾습니다 **Mock 테스트** 섹션. 테스트 이벤트 편집기 아래의
2. 다음의 값을 추가합니다 **Mock 이름** 및 **반환 값**.
3. 목이 예상대로 동작하는지 테스트하려면 **테스트 실행**.

<figure><img src="/files/31be8bde6936d9bc595888ea73c387fbb9bffdf3" alt="In the Panther Console, the Unit Test section of a detection page is shown. Below the test event, there is a Mock Testing section. It contains fields for Mock Name and Return Value."><figcaption></figcaption></figure>
{% endtab %}

{% tab title="CLI" %}
CLI 워크플로에서 단위 테스트에 목을 추가하려면:

테스트 사례에 다음을 추가합니다 `목` 키. 이 키는 모킹할 함수 목록과 각 목 반환 값을 정의합니다. 하나의 테스트에서 여러 함수를 모킹할 수 있습니다.

다음의 값은 `returnValue` 다음이어야 합니다 `string`실제 디택션 코드의 함수가 다른 데이터 형식을 반환하더라도 마찬가지입니다. 이는 다음에 의해 검증됩니다 [`pat test`](/ko/panther/detections-repo/pat/pat-commands.md#test-running-unit-tests).

예를 들어, 다음 함수를 모킹하려는 경우 `get_counter` 항상 다음을 반환하도록 `"1"` 그리고 함수 `geoinfo_from_ip` 가 항상 특정 Geo IP 정보 집합을 반환하도록 하려면, 단위 테스트를 다음과 같이 작성할 수 있습니다:

```yaml
테스트:
  -
    이름: 목이 포함된 테스트
    LogType: LogType.Custom
    예상 결과: true
    목:
      - objectName: get_counter
        returnValue: "1"
      - objectName: geoinfo_from_ip
        returnValue: |
          {
            "region": "UnitTestRegion",
            "city": "UnitTestCityNew",
            "country": "UnitTestCountry"
          }
    로그:
      {
        "hostName": "test-01.prod.acme.io",
        "user": "martin_smith",
        "eventTime": "June 22 5:50:52 PM"
      }
```

모킹을 사용하면 API 키나 네트워크 액세스 없이 CI/CD 파이프라인에서 네트워크 호출을 에뮬레이션할 수 있으며, panther KV store와 같은 외부 추적 시스템의 상태를 흐리지 않을 수 있습니다.

다음 사용에 대한 자세한 내용은 관련 문서를 참조하세요 [Panther Analysis Tool (PAT)](/ko/panther/detections-repo/pat.md) 및 [CI/CD 워크플로](/ko/panther/detections-repo/ci-cd/deployment-workflows/circle-ci.md).
{% endtab %}
{% endtabs %}

목은 다음에서 허용됩니다 **전역** 및 **내장** Python 네임스페이스. 여기에는 다음이 포함됩니다:

* 가져온 모듈과 함수
* [전역 도우미](/ko/detections/rules/python/globals.md)Panther가 관리하는 것과 사용자 정의된 것 모두 포함

Python은 두 가지 가져오기 형식을 제공하며, 다음과 같이 모킹할 수 있습니다:

* `import package`
  * 목 이름: `패키지`
* `from package import module`
  * 목 이름: `모듈에서 가져오기`

### 목 예시

이 예시는 다음을 기반으로 합니다 [AWS Config Global Resources](https://github.com/panther-labs/panther-analysis/blob/main/policies/aws_config_policies/aws_config_global_resources.py) 디택션.

디택션은 전역 도우미 함수 `resource_lookup` 를 `panther_aws_helpers` 사용하며 `resources-api` 를 조회하고 리소스 속성을 반환합니다. 그러나 단위 테스트는 외부 API 호출 없이 수행할 수 있어야 합니다.

<figure><img src="/files/10a26e2834ba8b5ca065d0e52efe21e96c0b5d2a" alt="A Unit Test within a Detection&#x27;s details page is shown. In the Mock Testing section, the fields for Mock Name and Return Value are blank."><figcaption></figcaption></figure>

이 테스트는 일반 예시 데이터에 대응하는 리소스 매핑이 없어서 실패합니다.

#### 디택션 자세히 살펴보기

```python
# --- Snipped ---
from panther_aws_helpers import resource_lookup
# --- Snipped ---
def policy(resource):
    # --- Snipped ---
    for recorder_name in resource.get("Recorders", []):
        recorder = resource_lookup(recorder_name)
        resource_records_global_resources = bool(
            deep_get(recorder, "RecordingGroup", "IncludeGlobalResourceTypes")
            and deep_get(recorder, "Status", "Recording")
        )
        if resource_records_global_resources:
            return True
    return False
    # --- Snipped ---
```

디택션은 `from panther_aws_helpers import resource_lookup` 관례를 사용하므로 목은 `resource_lookup` 함수에 대해 정의되어야 합니다.

목은 실제 데이터를 활용해 디택션 로직을 테스트할 수 있는 방법을 제공합니다.

**사용된 반환 값은**:

```
 { "AccountId": "012345678910", "Name": "Default", "RecordingGroup": { "AllSupported": true, "IncludeGlobalResourceTypes": true, "ResourceTypes": null }, "Region": "us-east-1", "ResourceId": "012345678910:us-east-1:AWS.Config.Recorder", "ResourceType": "AWS.Config.Recorder", "RoleARN": "arn:aws:iam::012345678910:role/PantherAWSConfig", "Status": { "LastErrorCode": null, "LastErrorMessage": null, "LastStartTime": "2018-10-05T22:45:01.838Z", "LastStatus": "SUCCESS", "LastStatusChangeTime": "2021-05-28T17:45:14.916Z", "LastStopTime": null, "Name": "Default", "Recording": true }, "Tags": null, "TimeCreated": null }
```

<figure><img src="/files/b968c9c098c7bf439209a3b725abc662e6db369b" alt="A Unit Test within a Detection&#x27;s details page is shown. The Mock Name field contains &#x22;resource_lookup&#x22; and the Return Value contains the value from the &#x22;Return Value Used&#x22; section in the documentation above this image."><figcaption></figcaption></figure>

이 리소스는 준수해야 하지만, 단위 테스트는 실패합니다.\
반환을 예상하지 않는 디택션은 `string` 반환되도록 하려면 목에 약간의 조정이 필요합니다.

이 단위 테스트가 예상대로 작동하도록 하려면 다음과 같이 수정해야 합니다:

```python
# --- Snipped ---
import json
# 또 다른 옵션은 다음을 사용하는 것입니다: from ast import literal_eval
# --- Snipped ---
def policy(resource):
    # --- Snipped ---
        recorder = resource_lookup(recorder_name)
        if isinstance(recorder, str):
            recorder = json.loads(recorder)
    # --- Snipped ---
```

이 수정 사항을 추가하면 이제 실제 데이터로 디택션 로직을 테스트할 수 있습니다!

<figure><img src="/files/3f3dde6fbbb4ac3e9f05da87727b34c5229bb215" alt="A full Policy Function and Unit Test with Mock Testing is shown. The test is named &#x22;Global Recorders Present - None Recording Global Resources&#x22;"><figcaption></figcaption></figure>

#### CLI의 목

디택션을 작성하는 CLI 기반 워크플로에서도 단위 테스트 모킹을 지원합니다. CLI 기반 디택션에 단위 테스트 목을 추가하는 방법에 대한 자세한 내용은 다음을 참조하세요 [단위 테스트 모킹](#how-to-use-mocks) 섹션을 참조하세요.

## 테스트 데이터 보강

테스트 이벤트를 다음으로 보강할 수 있습니다 `p_enrichment` Panther Console과 CLI 워크플로 모두에서.

{% hint style="info" %}
다음을 사용 중이라면 `lookup()` 함수를 디택션에서 사용하고 있다면, 대신 다음의 지침을 따르세요 [다음을 사용하는 디택션의 단위 테스트 `lookup()`](/ko/detections/rules/python.md#unit-testing-detections-that-use-lookup).
{% endhint %}

{% tabs %}
{% tab title="콘솔" %}
다음이 있다면 [조회 테이블](/ko/enrichment/custom.md) 을 구성하고 이를 활용하는 디택션을 생성했다면, 다음을 클릭합니다 **테스트 데이터 보강** JSON 이벤트 편집기 오른쪽 상단에서, 다음이 올바르게 채워졌는지 확인합니다 `p_enrichment` 가 올바르게 채워졌는지 확인합니다.

<figure><img src="/files/0245f8cdcff25ee11ecff0442c9114a30b5b3072" alt="Within the Unit Test section, the test event editor is shown. In the top right corner of the editor is a circled button that reads Enrich Test Data."><figcaption></figcaption></figure>
{% endtab %}

{% tab title="CLI" %}

* PAT의 다음을 사용하세요 `enrich-test-data` 명령을 사용합니다. [자세히 알아보기 `enrich-test-data` 여기](/ko/panther/detections-repo/pat/pat-commands.md#enrich-test-data-enriching-test-data-with-enrichment-content).
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.panther.com/ko/detections/testing.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
