summaryrefslogtreecommitdiff
path: root/validation/validlib/host_token.py
blob: d07a33be69967eb8dac0e67441ffb14913b9d9f0 (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
# -*- coding: utf-8 -*-

"""Module allowing to set, check and clear a token on a host"""

import os
import socket

import ssh

#Path of the token file
token_file_path = "/tmp/token.txt"

def get():
    user = os.getenv("USER")
    hostname = socket.gethostname()
    return user + "@" + hostname

def set(user, host):
    print "Setting token on", host, "...{"
    command = "echo " + get() + " > " + token_file_path
    ssh.call(user, host, command)
    print "}"
    check(user, host)

def clear(user, host):
    print "Clearing token on", host, "...{"
    command = "rm " + token_file_path
    ssh.call(user, host, command)
    print "}"

def check(user, host):
    print "Checking token on", host, "...{"
    command = "cat " + token_file_path
    expected = get()
    actual = ssh.check_output(user, host, command).rstrip("\r\n")
    info = "The expected token (" + expected + ") is not found on " + \
        user + "@" + host + ":" + token_file_path + " (" + actual + ")."
    assert expected == actual, info
    print "}"