# RUN: rm -rf %t.instance
# RUN: rm -rf %t.tmp && mkdir -p %t.tmp
# RUN: python %{shared_inputs}/create_temp_instance.py \
# RUN:   %s %{shared_inputs}/SmallInstance %t.instance
# RUN: %{shared_inputs}/server_wrapper.sh %t.instance 9092 /bin/sh %s %t.tmp %{shared_inputs}

set -eux
DIR="$1"
SHARED_INPUTS="$2"
cd "$DIR"

rm -rf lntadmin.yaml
lnt admin create-config > create_config.stdout
mv lntadmin.yaml create_config.txt
# RUN: filecheck %s --check-prefix=CREATE_CONFIG < %t.tmp/create_config.txt
# CREATE_CONFIG: lnt_url: "http://localhost:8000"
# CREATE_CONFIG-NEXT: database: default
# CREATE_CONFIG-NEXT: testsuite: nts
# CREATE_CONFIG-NEXT: # user: 'http_user'
# CREATE_CONFIG-NEXT: # password: 'http_password'
# CREATE_CONFIG-NEXT: # auth_token: 'secret'

cat > lntadmin.yaml << '__EOF__'
lnt_url: "http://localhost:9092"
database: default
testsuite: nts
auth_token: test_token
__EOF__

lnt admin post-run "${SHARED_INPUTS}/sample-a-small.plist" > post_run.stdout
# RUN: filecheck %s --check-prefix=POST_RN < %t.tmp/post_run.stdout
# POST_RN: http://localhost:9092/api/db_default/v4/nts/runs/3

lnt admin machine-info 1 > machine_info.stdout
# RUN: filecheck %s --check-prefix=MACHINE_INFO < %t.tmp/machine_info.stdout
# MACHINE_INFO: name: localhost__clang_DEV__x86_64
# MACHINE_INFO: id: 1
# MACHINE_INFO: hardware: x86_64
# MACHINE_INFO: hostname: localhost
# MACHINE_INFO: os: Darwin 11.3.0
# MACHINE_INFO: uname: Darwin localhost 11.3.0 Darwin Kernel Version 11.3.0: Thu Jan 12 18:47:41 PST 2012; root:xnu-1699.24.23~1/RELEASE_X86_64 x86_64

rm -rf run_3.json
lnt admin get-run 3 > get_run.stdout
# RUN: filecheck %s --check-prefix=GET_RN < %t.tmp/run_3.json
# GET_RN: {
# GET_RN:   "generated_by":
# GET_RN:   "machine": {
# ...
# GET_RN:   },
# GET_RN:   "run": {
# GET_RN:     "end_time": "2009-11-17T03:44:48",
# GET_RN:     "id": 3,
#...
# GET_RN:     "start_time": "2009-11-17T02:12:25"
# GET_RN:   },
# GET_RN:   "tests": [
#...
# GET_RN:   ]
# GET_RN: }

lnt admin list-machines > list_machines.stdout
# RUN: filecheck %s --check-prefix=LIST_MACHINES < %t.tmp/list_machines.stdout
# LIST_MACHINES: localhost__clang_DEV__x86_64:1
# LIST_MACHINES-NEXT: LNT SAMPLE MACHINE:2

lnt admin list-runs 1 > list_runs.stdout
# RUN: filecheck %s --check-prefix=LIST_RUNS < %t.tmp/list_runs.stdout
# LIST_RUNS: llvm_project_revision=154331 1
# LIST_RUNS: llvm_project_revision=152289 2

rm -rf machine_1.json
lnt admin get-machine 1 > get_machine.stdout
# RUN: filecheck %s --check-prefix=GET_MACHINE_JSON < %t.tmp/machine_1.json
# GET_MACHINE_JSON: {
# GET_MACHINE_JSON:   "generated_by":
# GET_MACHINE_JSON:   "machine": {
# GET_MACHIEN_JSON:     "hardware": "x86_64",
# GET_MACHIEN_JSON:     "hostname": "localhost",
# GET_MACHIEN_JSON:     "id": 1,
# GET_MACHIEN_JSON:     "name": "localhost__clang_DEV__x86_64",
# GET_MACHIEN_JSON:     "os": "Darwin 11.3.0",
# GET_MACHIEN_JSON:     "uname": "Darwin localhost 11.3.0 Darwin Kernel Version 11.3.0: Thu Jan 12 18:47:41 PST 2012; root:xnu-1699.24.23~1/RELEASE_X86_64 x86_64"
# GET_MACHINE_JSON:   },
# GET_MACHINE_JSON:   "runs": [
# ...
# GET_MACHINE_JSON:   ]
# GET_MACHINE_JSON: }

lnt admin rm-machine 1 > rm_machine.stdout
# RUN: filecheck %s --check-prefix=RM_MACHINE < %t.tmp/rm_machine.stdout
# RM_MACHINE: Deleting runs 1 2 (2/2)
# RM_MACHINE: Deleted machine localhost__clang_DEV__x86_64:1

lnt admin list-machines > list_machines2.stdout
# RUN: filecheck %s --check-prefix=LIST_MACHINES2 < %t.tmp/list_machines2.stdout
# LIST_MACHINES2-NOT: localhost__clang_DEV__x86_64:1
# LIST_MACHINES2: LNT SAMPLE MACHINE:2

lnt admin rename-machine 2 hal9000
# No output

lnt admin list-machines > list_machines3.stdout
# RUN: filecheck %s --check-prefix=LIST_MACHINES3 < %t.tmp/list_machines3.stdout
# LIST_MACHINES3: hal9000:2

# Just post again so we have a machine to merge
lnt admin post-run run_3.json > /dev/null

lnt admin merge-machine-into 2 3 > merge_machine_into.stdout
# No output

lnt admin rm-run 4
# No output
