github.com/argoproj-labs/argocd-operator@v0.10.0/docs/e2e-test-guide.md (about)

     1  # Argo CD Operator E2E Test Guide
     2  
     3  E2E tests are written using [KUTTL](https://kuttl.dev/docs/#install-kuttl-cli).
     4  
     5  ## Requirements
     6  
     7  This test suite assumes that an Argo CD Operator is installed on the cluster or running locally using `ARGOCD_CLUSTER_CONFIG_NAMESPACES=argocd-e2e-cluster-config make install run`.
     8  
     9  The system executing the tests must have following tools installed:
    10  
    11  * `kuttl` kubectl plugin (>= v0.11.1)
    12  * `oc` and `kubectl` client
    13  * `jq` for parsing JSON data
    14  * `curl`
    15  
    16  There should be a `kubeconfig` pointing to your cluster, user should have full admin privileges (i.e. `kubeadm`).
    17  
    18  !!! note 
    19      E2E tests utilize GNU Grep under the hood. Please make sure that you have the GNU compatible `grep` installed.
    20  
    21  If you are on OSX you can install GNU compatible grep using the below command. The package is installed as `ggrep` by default. Please set this(ggrep) as an alias to `grep`.  
    22  
    23  ```sh
    24  brew install grep
    25  ```
    26  
    27  Use the below commands to install GNU compatible `grep` on OSX.
    28  
    29  Also, note that the e2e tests for Redis HA mode require a cluster with at least three worker nodes.  A local three-worker node
    30  cluster can be created using [k3d](https://k3d.io/)
    31  
    32  ## Running the tests
    33  
    34  In any case, you should have set up your `kubeconfig` in such a way that your
    35  default context points to the cluster you want to test. You can use the
    36  `kubectl login ...` command to set this up for you.
    37  
    38  ## Run e2e tests
    39  
    40  ```sh
    41  make e2e
    42  ```
    43  
    44  ## Run Operator locally and execute e2e tests
    45  
    46  ```sh
    47  make all
    48  ```
    49  
    50  ### Running manual with kuttl
    51  
    52  ```sh
    53  kubectl kuttl test ./tests/k8s --config ./tests/kuttl-tests.yaml
    54  ```
    55  
    56  ### Running single tests
    57  
    58  Sometimes (e.g. when initially writing a test or troubleshooting an existing
    59  one), you may want to run single test cases isolated. To do so, you can pass
    60  the name of the test using `--test` to `kuttl`, i.e.
    61  
    62  ```sh
    63  kubectl kuttl test ./tests/k8s --config ./tests/kuttl-tests.yaml --test 1-004_validate_namespace_scoped_install
    64  ```
    65  
    66  The name of the test is the name of the directory containing its steps and
    67  assertions.
    68  
    69  If you are troubleshooting, you may want to prevent `kuttl` from deleting the
    70  test's namespace afterwards. In order to do so, just pass the additional flag
    71  `--skip-delete` to above command.
    72  
    73  ## Writing new tests
    74  
    75  ### Name of the test
    76  
    77  Each test comes in its own directory, containing all its test steps. The name
    78  of the test is defined by the name of this directory.
    79  
    80  The name of the test should be short, but expressive. The format for naming a
    81  test is currently `<test ID>_<short description>`.
    82  
    83  The `<test ID>` is the serial number of the test as defined in the Test Plan
    84  document. The `<short description>` is exactly that, a short description of
    85  what happens in the test.
    86  
    87  ### Name of the test steps
    88  
    89  Each test step is a unique YAML file within the test's directory. The name of
    90  the step is defined by its file name.
    91  
    92  The test steps must be named `XX-<name>.yaml`. This is a `kuttl` convention
    93  and cannot be overriden. `XX` is a number (prefixed with `0`, so step `1` must
    94  be `01`), and `<name>` is a free form value for the test step.
    95  
    96  There are two reserved words you cannot use for `<name>`:
    97  
    98  * `assert` contains positive assertions (i.e. resources that must exist) and
    99  * `errors` contains negative assertions (i.e. resources that must not exist)
   100  
   101  Refer to the
   102  [kuttl documentation](https://kuttl.dev/docs)
   103  for more information.
   104  
   105  ### Documentation
   106  
   107  Documentation is important, even for tests. You can should provide inline
   108  documentation in your YAML files (using comments) and a `README.md` in your
   109  test case's directory. The `README.md` should provide some context for the
   110  test case, e.g. what it tries to assert for under which circumstances. This
   111  will help others in troubleshooting failing tests.
   112  
   113  ### Recipes
   114  
   115  `kuttl` unfortunately neither encourages or supports re-use of your test steps
   116  and assertions yet.
   117  
   118  Generally, you should try to use `assert` and `errors` declaration whenever
   119  possible and viable. For some cases, you may need to use custom scripts to
   120  get the results you are looking for.
   121  
   122  #### Scripts general
   123  
   124  Scripts can be executed in a `kuttl.dev/TestStep` resources from a usual test
   125  step declaration.
   126  
   127  Your script probably will retrieve some information, and asserts it state. If
   128  the assertion fails, the script should exit with a code > 0, and also print
   129  some information why it failed, e.g.
   130  
   131  ```yaml
   132  apiVersion: kuttl.dev/v1beta1
   133  kind: TestStep
   134  commands:
   135  - script: |
   136      # Get some piece of information...
   137      if test "$result" != "expected"; then
   138        echo "Expectation failed, should 'expected', is '$result'"
   139        exit 1
   140      fi
   141  ```
   142  
   143  Also, you may want to use `set -e` and `set -o pipefail` at the top of your
   144  script to catch unexpected errors as test case failures, e.g.
   145  
   146  ```yaml
   147  apiVersion: kuttl.dev/v1beta1
   148  kind: TestStep
   149  commands:
   150  - script: |
   151      set -e
   152      set -o pipefail
   153      # rest of your script
   154  ```
   155  
   156  #### Getting values of a resource's environment variables
   157  
   158  YAML declarations used in `assert` or `errors` files unfortunately don't handle
   159  arrays very well yet. You will always have to specify the complete expectation,
   160  i.e. the complete array.
   161  
   162  If you are just interested in a certain variable, and don't care about the rest,
   163  you can use a script similar to the following using `jq`. E.g. to get the value
   164  of a variable named `FOO` for the `argocd-server` deployment in the test's
   165  namespace:
   166  
   167  ```yaml
   168  apiVersion: kuttl.dev/v1beta1
   169  kind: TestStep
   170  commands:
   171  - script: |
   172      val=$(kubectl get -n $NAMESPACE deployments argocd-server -o json \
   173        | jq -r '.spec.templates.spec.containers[0].env[]|select(.name=="FOO").value')
   174      if test "$val" != "bar"; then
   175        echo "Expectation failed for for env FOO in argocd-server: should 'bar', is '$val'"
   176        exit 1
   177      fi
   178  ```