summaryrefslogtreecommitdiff
path: root/yarns/900-implements.yarn
blob: 26a05a7b393ade4c5cdd1d95d91ad4a49af04321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Appendix: Scenario step implementations

## Configuration

Save the name of the server to be tested, so it doesn't need to be
repeated for every step.

    IMPLEMENTS GIVEN server name is (.+)
    echo "SERVER=$MATCH_1" >> "$DATADIR/config.sh"

Set account name to use on server.

    IMPLEMENTS GIVEN server has account (.+)
    echo "ACCOUNT=$MATCH_1" >> "$DATADIR/config.sh"

## Ping

Does the server respond to a ping?

    IMPLEMENTS THEN server responds to ping
    . "$DATADIR/config.sh"
    ping -c1 "$SERVER"

## Check server hostname

Does the server's hostname match its domain name?

    IMPLEMENTS THEN server hostname is as expected
    . "$DATADIR/config.sh"
    ssh "$ACCOUNT@$SERVER" hostname | grep -Fx "$SERVER"

## Ansible account

Does the server have an account `ansible` that has passwordless sudo
access to root?

    IMPLEMENTS THEN server account has sudo
    . "$DATADIR/config.sh"
    ssh "$ACCOUNT@$SERVER" sudo -n id -u > "$DATADIR/id.out"
    grep -F 0 "$DATADIR/id.out"

## Python 2 installed?

Does the server have Python 2 installed, by invoking `python`?

    IMPLEMENTS THEN server has python version 2 installed
    . "$DATADIR/config.sh"
    ssh "$ACCOUNT@$SERVER" python --version 2> "$DATADIR/python.out"
    grep "^Python 2\." "$DATADIR/python.out"

## Running command over ssh

Run a command on the server over ssh, capturing stdout and stderr and
exit code for later inspection.

    IMPLEMENTS WHEN running ssh (.*)
    if ssh $MATCH_1 > "$DATADIR/stdout" 2> "$DATADIR/stderr"
    then
        echo 0 > "$DATADIR/exit"
    else
        echo $? > "$DATADIR/exit"
    fi

## Access server over SFTP

Put and get a file over SFTP.

    IMPLEMENTS THEN server allows SFTP
    . "$DATADIR/config.sh"
    echo test.file > "$DATADIR/sftp.file"
    cat > "$DATADIR/sftp.commands" << EOF
    put $DATADIR/sftp.file sftp.file
    get sftp.file $DATADIR/sftp.file.copy
    rm sftp.file
    EOF
    sftp -b "$DATADIR/sftp.commands" "$ACCOUNT@$SERVER"
    cmp "$DATADIR/sftp.file"  "$DATADIR/sftp.file.copy"

## Inspect results of running a command

The stdout, stderr, and exit code have been saved. Inspect them.

    IMPLEMENTS THEN output matches (.+)
    cat "$DATADIR/stdout"
    grep "$MATCH_1" "$DATADIR/stdout"

## Manage a remote temporary directory

Whenever we need to do anything that takes more than one step, we'll
need a temporary directory in which to work. This is similar to the
`DATADIR` that yarn provides us on the local machine.

    IMPLEMENTS GIVEN a remote temporary directory to use
    . "$DATADIR/config.sh"
    name="$(ssh "$ACCOUNT@$SERVER" mktemp -d)"
    echo "REMOTETMP=$name" >> "$DATADIR/config.sh"

    IMPLEMENTS FINALLY remove remote temporary directory
    # FIXME: The following will fail if REMOTETMP contains shell's
    # magic characters or whitespace. Ugh.
    . "$DATADIR/config.sh"
    ssh "$ACCOUNT@$SERVER" rm -rf "$REMOTETMP"

## Create remote directory

    IMPLEMENTS GIVEN a directory (\S+) on the server
    . "$DATADIR/config.sh"
    run_remotely "$ACCOUNT@$SERVER" "$REMOTETMP" "mkdir $MATCH_1"

## Check for files on the remote end

    IMPLEMENTS THEN remote files (\S+) exist
    . "$DATADIR/config.sh"
    run_remotely "$ACCOUNT@$SERVER" "$REMOTETMP" "ls -d $MATCH_1" |
        grep .

## Run a command on the server

    IMPLEMENTS WHEN server runs (.+)
    . "$DATADIR/config.sh"
    run_remotely "$ACCOUNT@$SERVER" "$REMOTETMP" "$MATCH_1"