NLW_TOKEN=
NLW_ZONE_DYNAMIC=
NLW_URL=https://neoload-rest.saas.neotys.com

pip install neoload==1.0.0

neoload --version
neoload --help

# NOTE: the CLI must sense if it is in non-interactive mode, so that:
#  1. if there is a blocking stdin operation, it defaults to exit with failure of why
#  2. if there is a operation (delete) which usually prompts a user before, it assumes yes if in non-interactive
# NOTE: quiet mode referred to overriding other verbose/debug outputs to produce a single, simple outcome no matter what
# GOAL: simplify test execution and platform management tasks to as-close-to one-liners as possible : click framework allows commands chaining
# NOT-GOAL: write commands that simply reflect our API structure, as nice and normalized as it is

# TL;DR: login to the platform and run a test
neoload login --url $NLW_URL $NLW_TOKEN \
        test-settings --zone $NLW_ZONE --scenario sanityScenario create NewTest1 \
        project --path tests/neoload_projects/example_1/ upload NewTest1 \
  # stores the configuration of the test in Neoload Web (store the current test-settings id in the CLI for one-liner simplicity)
neoload run
  # produces blocking, unbuffered output about test execution process, including readout of current data points

#login
neoload login
  # if no --token provided, asks for it by stdin prompt if in interactive mode
neoload login $NLW_TOKEN   # defaults to use SaaS, store in 'defaults'
neoload login --url $NLW_UR $NLW_TOKEN # uses a specific NLW instance for login, store in 'defaults'
  # Display the api url and the currently stored data (test-settings-id, test-result-id)
neoload status

#config
  # we need config sets beyond simple login, such as default zone etc, see https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/
neoload test-settings ls NewTest1 # shows stored YAML for stored 'NewTest1'
neoload test-settings --zone $NLW_ZONE_DYNAMIC --lgs 5 patch NewTest1  # sets zone and number of lgs for stored 'NewTest1'
neoload test-settings --zone $NLW_CONTROLLER_ZONE --lgs $NLW_LG_ZONE_DYNAMIC:1,zone2:5 patch NewTest1 # sets advanced controls on controller zones and lg zones
  #(any assets provided should be validated internally as above before being uploaded if at all possible)
neoload project --path tests/neoload_projects/example_1/ upload

#validate
neoload validate tests/neoload_projects/example_1/default.yaml
  # is this valid YAML, and is it valid to our DSL based on JSON-schema? If not, provide detailed reasons why
# TODO neoload validate --zone $NLW_ZONE_DYNAMIC --available-lgs 5
  # is this a valid zone, and does it have at least 5 available load generators? if not, throw exit code > 1 with detailed printout of why not

#infra
neoload infra attach docker --lg-count 5
  # create and attach 1 controller and 5 load generators via the currently configured Docker socket; use current config set zone; but assume to use public docker images "neotys/neoload-..."
neoload infra attach docker --lg-count 5 --version "7.0.2"
  # same as above, but specify exact version (Docker tag)
neoload infra attach docker --lg-count 5 --version "7.0.2" --ctrl-image "paulsbruce/neoload-controller" --lg-image "paulsbruce/neoload-loadgenerator"
  # same as above and use base image URIs for CTRL and LGs
neoload infra detatch
  # destroy the last local docker containers attached (see above)
neoload infra detatchall
  # destroy all containers on the connected Docker host that were initialized using this CLI at any point in time

#run
  # produces blocking, unbuffered output about test execution process, including readout of current data points
neoload run
  Example output :
    Result created: #1 ea0b655f-0bd9-4d89-85c1-ed9c8a35feea
    Logs available at https://neoload.saas.neotys.com/#!result/ea0b655f-0bd9-4d89-85c1-ed9c8a35feea/overview
    INITIALIZING... Done
    STARTING... Done
    RUNNING...
	0: 0:00:00.253307       Err[0],LGs[1]  VUs:1   BPS[0.0]        RPS:0.0         avg(rql):0.0
	3: 0:00:15.799357       Err[0],LGs[1]  VUs:4   BPS[1268.1061]  RPS:0.17650174  avg(rql):5558.0
	6: 0:00:31.338746       Err[0],LGs[1]  VUs:5   BPS[1956.0303]  RPS:0.24242425  avg(rql):6613.0
	9: 0:00:46.884859       Err[2],LGs[1]  VUs:2   BPS[1505.337]   RPS:0.22450355  avg(rql):9242.0
	12: 0:01:02.436620      Err[2],LGs[1]  VUs:2   BPS[1134.7405]  RPS:0.16923337  avg(rql):9242.0
    TERMINATED with [success|failure]   # This color should be green for success and red for failure
    # After TERMINATED, display the summary that includes SLAs. Failed SLAs lines are red, success are green (@see existing code)

  # run OPTIONS
    -d, --detach            (optional) Launch the test and return immediately without waiting until it terminates. Only print the Two first lines of the output
    --name              (optional) Result name. By default, use the test result naming pattern defined in the test-settings (#1, #2...)
    --description           (optional) Result description
    --as-code           Comma-separated as-code files to use. Those files must be part of the uploaded project
    --web-vu
    --sap-vu
    --citrix-vu
  # run Arguments
    name_or_id                    (optional) Use uuid instead or the name of the test settings to run

  # Options not yet implemented
    --reservation-id        The reservation identifier to use for the test
    --reservation-duration
    --publish-test-result <flag>   Only for collaborative projects: publish the test results onto the server

  # Run in interactive mode (a user from a shell)
    # When hitting Ctrl+C : send the stop test signal (graceful stop). Blocking status until stop completes.
    # When hitting the second time Ctrl+C : send the stop test --force (terminate)
  # Run in non interactive mode (from a CI script), when aborting the job, do as if Ctrl+C was hit in interactive mode

  # if non-blocking (detach)
neoload stop --force
  # blocking status until complete, with a timeout of 30s

  # and also one-liners, at the end of the test: the sla report output to named file and the summary
neoload run NewTest1 test-results --junit-file junit.xml junitsla


# wait (BONUS)
  # blocking, display status of a running test starting from now, or display TERMINATED with [success|error] if the test finished
neoload wait
  Example output :
    Result: #1 ea0b655f-0bd9-4d89-85c1-ed9c8a35feea
    Logs available at https://neoload.saas.neotys.com/#!result/ea0b655f-0bd9-4d89-85c1-ed9c8a35feea/overview
    STARTING... Done
    RUNNING...
neoload wait
  Example output :
    Result: #1 ea0b655f-0bd9-4d89-85c1-ed9c8a35feea
    Logs available at https://neoload.saas.neotys.com/#!result/ea0b655f-0bd9-4d89-85c1-ed9c8a35feea/overview
    TERMINATED with success


# results
  # Set a specific test result to work with (the result is automatically set when running a test)
neoload test-results use ee50e3a2-3cf4-4667-8186-9950895e0a04
  # the sla report output to named file
neoload test-results --junit-file junit.xml junitsla
  # The current test result summary
neoload test-results summary


#lists
  # I'd like to have listings that produce one or more outputs, be referenced in the plural, not singular command
  # For operations on a specific object (test, result), those should use the singular command name
  # (if --sort-by is a date, default to --sort-order desc, else --sort-order asc)

#list tests (defaults --limit 50 --sort-by lastUpdateDate)
neoload tests ls
  # should list out top 50 tests, sorted by lastUpdateDate descending
neoload tests ls --limit 200
  # should list out top 200 tests, ...
neoload tests ls --project "projectName"
  # why is the project name not part of the v2/tests response spec (but scenarioName is)?
neoload tests ls --scenario "scenarioName"
  # lists the top 50 tests matching this scenario name, ordered by lastUpdateDate desc

#list results (defaults --limit 50 --sort-by startDate)
#(if --sort-by is a date, default to --sort-order desc, else --sort-order asc)
neoload test-results ls
  # should list out top 50 results, sorted by startDate descending
neoload test-results ls --status RUNNING
neoload test-results ls --project "projectName"
neoload test-results ls --scenario "scenarioName"
neoload test-results ls --qualityStatus FAILED

neoload project meta

neoload zones ls
  # list out all zones
