github.com/crowdsecurity/crowdsec@v1.6.1/test/bats/30_machines.bats (about) 1 #!/usr/bin/env bats 2 # vim: ft=bats:list:ts=8:sts=4:sw=4:et:ai:si: 3 4 set -u 5 6 setup_file() { 7 load "../lib/setup_file.sh" 8 } 9 10 teardown_file() { 11 load "../lib/teardown_file.sh" 12 } 13 14 setup() { 15 load "../lib/setup.sh" 16 ./instance-data load 17 ./instance-crowdsec start 18 } 19 20 teardown() { 21 ./instance-crowdsec stop 22 } 23 24 #---------- 25 26 @test "we have exactly one machine" { 27 rune -0 cscli machines list -o json 28 rune -0 jq -c '[. | length, .[0].machineId[0:32], .[0].isValidated]' <(output) 29 assert_output '[1,"githubciXXXXXXXXXXXXXXXXXXXXXXXX",true]' 30 } 31 32 @test "don't overwrite local credentials by default" { 33 rune -1 cscli machines add local -a -o json 34 rune -0 jq -r '.msg' <(stderr) 35 assert_output --partial 'already exists: please remove it, use "--force" or specify a different file with "-f"' 36 rune -0 cscli machines add local -a --force 37 assert_stderr --partial "Machine 'local' successfully added to the local API." 38 } 39 40 @test "passwords have a size limit" { 41 rune -1 cscli machines add local --password "$(printf '%73s' '' | tr ' ' x)" 42 assert_stderr --partial "password too long (max 72 characters)" 43 } 44 45 @test "add a new machine and delete it" { 46 rune -0 cscli machines add -a -f /dev/null CiTestMachine -o human 47 assert_stderr --partial "Machine 'CiTestMachine' successfully added to the local API" 48 assert_stderr --partial "API credentials written to '/dev/null'" 49 50 # we now have two machines 51 rune -0 cscli machines list -o json 52 rune -0 jq -c '[. | length, .[-1].machineId, .[0].isValidated]' <(output) 53 assert_output '[2,"CiTestMachine",true]' 54 55 # delete the test machine 56 rune -0 cscli machines delete CiTestMachine -o human 57 assert_stderr --partial "machine 'CiTestMachine' deleted successfully" 58 59 # we now have one machine again 60 rune -0 cscli machines list -o json 61 rune -0 jq '. | length' <(output) 62 assert_output 1 63 } 64 65 @test "register, validate and then remove a machine" { 66 rune -0 cscli lapi register --machine CiTestMachineRegister -f /dev/null -o human 67 assert_stderr --partial "Successfully registered to Local API (LAPI)" 68 assert_stderr --partial "Local API credentials written to '/dev/null'" 69 70 # the machine is not validated yet 71 rune -0 cscli machines list -o json 72 rune -0 jq '.[-1].isValidated' <(output) 73 assert_output 'null' 74 75 # validate the machine 76 rune -0 cscli machines validate CiTestMachineRegister -o human 77 assert_stderr --partial "machine 'CiTestMachineRegister' validated successfully" 78 79 # the machine is now validated 80 rune -0 cscli machines list -o json 81 rune -0 jq '.[-1].isValidated' <(output) 82 assert_output 'true' 83 84 # delete the test machine again 85 rune -0 cscli machines delete CiTestMachineRegister -o human 86 assert_stderr --partial "machine 'CiTestMachineRegister' deleted successfully" 87 88 # we now have one machine, again 89 rune -0 cscli machines list -o json 90 rune -0 jq '. | length' <(output) 91 assert_output 1 92 } 93 94 @test "cscli machines prune" { 95 rune -0 cscli metrics 96 97 rune -0 cscli machines prune 98 assert_output 'No machines to prune.' 99 100 rune -0 cscli machines list -o json 101 rune -0 jq -r '.[-1].machineId' <(output) 102 rune -0 cscli machines delete "$output" 103 104 rune -0 cscli machines prune 105 assert_output 'No machines to prune.' 106 }