Skip to content

Commit

Permalink
Merge branch 'master' into env-tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Dorthu authored Oct 12, 2018
2 parents 549ad4b + 8333dcd commit c03eb69
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 3 deletions.
4 changes: 4 additions & 0 deletions linodecli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def main():
parser.add_argument('--no-defaults', action='store_true',
help="Suppress default values for arguments. Default values "
"are configured on initial setup or with linode-cli configure")
parser.add_argument('--suppress-warnings', action='store_true',
help="Suppress warnings that are intended for human users. "
"This is useful for scripting the CLI's behavior.")
parser.add_argument('--version', '-v', action="store_true",
help="Prints version information and exits.")

Expand All @@ -80,6 +83,7 @@ def main():
cli.output_handler.columns = parsed.format

cli.defaults = not parsed.no_defaults
cli.suppress_warnings = parsed.suppress_warnings
cli.page = parsed.page

if parsed.version:
Expand Down
3 changes: 2 additions & 1 deletion linodecli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(self, version, base_url, skip_config=False):
self.version = version
self.base_url = base_url
self.spec_version = 'None'
self.suppress_warnings = False

self.output_handler = OutputHandler()
self.config = CLIConfig(self.base_url, skip_config=skip_config)
Expand Down Expand Up @@ -373,7 +374,7 @@ def do_request(self, operation, args):
if 'X-Spec-Version' in result.headers:
spec_version = result.headers.get('X-Spec-Version')
try:
if LooseVersion(spec_version) > LooseVersion(self.spec_version):
if LooseVersion(spec_version) > LooseVersion(self.spec_version) and not self.suppress_warnings:
print("The API responded with version {}, which is newer than "
"the CLI's version of {}. Please update the CLI to get "
"access to the newest features. You can update with a "
Expand Down
6 changes: 6 additions & 0 deletions linodecli/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def __init__(self, base_url, username=None, skip_config=False):
self.base_url = base_url
self.username = username
self.config = self._get_config()

self._configured = False

if not self.config.has_option('DEFAULT', 'token') and not skip_config and not os.environ[ENV_TOKEN_NAME]:
self.configure()

Expand Down Expand Up @@ -79,6 +82,8 @@ def configure(self, username=None):
for a series of defaults in order to make future CLI calls
easier. This also sets up the config file.
"""
# If configuration has already been done in this run, don't do it again.
if self._configured: return
config = {}
is_default = username == None

Expand Down Expand Up @@ -149,6 +154,7 @@ def configure(self, username=None):
with open(self._get_config_path(), 'w') as f:
self.config.write(f)
os.chmod(self._get_config_path(), 0o600)
self._configured = True

print("\nConfig written to {}".format(self._get_config_path()))

Expand Down
31 changes: 30 additions & 1 deletion test/common.bash
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
createLinode() {
local linode_type=$(linode-cli linodes types --text --no-headers --format="id" | xargs | awk '{ print $1 }')
run bash -c "linode-cli linodes create --type=$linode_type --region us-east --image=$test_image --root_pass=$random_pass"
assert_success
}

createVolume() {
timestamp=$(date +%s)
run bash -c "linode-cli volumes create --label=A$timestamp --size=10 --region=us-east"
assert_success
}

shutdownLinodes() {
local linode_ids="( $(linode-cli --text --no-headers linodes list | awk '{ print $1 }' | xargs) )"
local id

for id in $linode_ids ; do
run bash -c "linode-cli linodes shutdown $id"
done
}

removeLinodes() {
local linode_ids="( $(linode-cli --text --no-headers linodes list | awk '{ print $1 }' | xargs) )"
local id

for id in $linode_ids ; do
run bash -c "linode-cli linodes delete $id"
[ "$status" -eq 0 ]
done
}

Expand All @@ -18,6 +38,15 @@ removeDomains() {
done
}

removeVolumes() {
local volume_ids="( $(linode-cli volumes list --text --no-headers --format="id" | xargs) )"
local id

for id in $volume_ids ; do
run bash -c "linode-cli volumes delete $id"
done
}

# Get an available image and set it as an env variable
if [ -z "$test_image" ]; then
export test_image=$(linode-cli images list --format id --text --no-header | egrep "linode\/.*" | head -n 1)
Expand Down
2 changes: 1 addition & 1 deletion test/test-runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ then
cd $PWD/test
fi

bats linodes domains
bats linodes domains volumes
else
echo -e "\n ####WARNING!#### \n"
echo -e "Running the Linode CLI tests requires removing all resources on your account\n"
Expand Down
66 changes: 66 additions & 0 deletions test/volumes/attach-detach-volumes.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env bats

# THESE TESTS HAVE BEEN DISABLED UNTIL A BUG IS ADDRESSED

# load '../test_helper/bats-support/load'
# load '../test_helper/bats-assert/load'
# load '../common'

# ##################################################################
# # WARNING: THIS TEST WILL DELETE ALL OF YOUR LINODES #
# # WARNING: USE A SEPARATE TEST ACCOUNT WHEN RUNNING THESE TESTS #
# ##################################################################

# @test "it should attach volume to a linode during volume creation" {
# createLinode

# linode_id=$(linode-cli linodes list --format="id" --text --no-headers)

# until [ $(linode-cli linodes view $linode_id --format="status" --text --no-headers) = "running" ]; do
# echo 'still provisioning'
# done

# linode_id=$(linode-cli linodes list --format="id" --text --no-headers)
# run linode-cli volumes create --label="attachedVolume" --size=10 --linode_id=$linode_id --text --no-headers --delimiter="," --format="id,label,size,region,linode_id"
# assert_success
# assert_output --regexp "[0-9]+,attachedVolume,10,us-east,$linode_id"
# }

# @test "it should detach from linode" {
# attached_volume_id=$(linode-cli volumes list --label="attachedVolume" --format="id" --text --no-headers)
# run linode-cli volumes detach $attached_volume_id --text --no-headers
# assert_success
# run linode-cli volumes view $attached_volume_id --text --no-headers --delimiter="," --format="id,label,size,region,linode_id"
# assert_success
# assert_output --regexp "$attached_volume_id,attachedVolume,10,us-east,"
# }

# @test "it should attach to a linode" {
# volume_id=$(linode-cli volumes list --format="id" --text --no-headers)
# linode_id=$(linode-cli linodes list --format="id" --text --no-headers)
# run linode-cli volumes attach --linode_id=$linode_id $volume_id --text --no-headers --delimiter="," --format="id,label,size,region,linode_id"
# assert_success
# assert_output --regexp "$volume_id,attachedVolume,10,us-east,$linode_id"
# }

# @test "it should fail to remove while attached" {
# attached_volume=$(linode-cli volumes list --format="id" --text --no-headers)
# run linode-cli volumes delete $attached_volume --text --no-headers
# assert_failure
# assert_output --partial "Request failed: 400"
# assert_output --partial "This volume must be detached before it can be deleted."
# }

# @test "it should fail to attach to a linode that does not belong to the user" {
# unattached_volume=$(linode-cli volumes create --label="unattachedVolume" --region="us-east" --size=10 --text --no-headers --format="id")
# run linode-cli volumes attach --linode_id=1 $unattached_volume --text --no-headers
# assert_failure
# assert_output --partial "Request failed: 403"
# assert_output --partial "linode_id You do not have permission to access this Linode"
# }

# @test "it should remove all volumes" {
# run shutdownLinodes
# run removeLinodes
# run removeVolumes
# }
65 changes: 65 additions & 0 deletions test/volumes/create-volumes.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env bats

load '../test_helper/bats-support/load'
load '../test_helper/bats-assert/load'
load '../common'

##################################################################
# WARNING: THIS TEST WILL DELETE ALL OF YOUR LINODES #
# WARNING: USE A SEPARATE TEST ACCOUNT WHEN RUNNING THESE TESTS #
##################################################################

@test "it should fail to create a volume under 10gb" {
timestamp=$(date +%s)
run linode-cli volumes create --label "A$timestmap" --region us-east --size 5 --text --no-headers
assert_failure
assert_output --partial "size Must be 10-10240"
}

@test "it should fail to create a volume without a region" {
run linode-cli volumes create --label "A$timestamp" --size 10 --text --no-headers
assert_failure
assert_output --partial "Request failed: 400"
assert_output --partial "Must provide a region or a Linode ID"
}

@test "it should fail to create a volume without a label" {
run linode-cli volumes create --size 10 --region us-east --text --no-headers
assert_failure
assert_output --partial "Request failed: 400"
assert_output --partial "label label is required"
}

@test "it should fail to create a volume over 10240gb in size" {
run linode-cli volumes create --size 10241 --label "A$timestamp" --region us-east --text --no-headers
assert_failure
assert_output --partial "Request failed: 400"
assert_output --partial "size Must be 10-10240"
}

@test "it should fail to create a volume with an all numeric label" {
run linode-cli volumes create --label "9200900" --region us-east --size 10 --text --no-headers
assert_failure
assert_output --partial "Request failed: 400"
assert_output --partial "label Must begin with a letter"
}

@test "it should create an unattached volume" {
timestamp=$(date +%s)
run linode-cli volumes create --label "A$timestamp" --region us-east --size 10 --text --no-headers --delimiter ","
assert_success
assert_output --regexp "[0-9]+,A[0-9]+,creating,10,us-east"
}

# @test "it should create volume attached to a linode" {
# timestamp=$(date +%s)
# linode_id=$(linode-cli linodes list --text --no-headers --format "id")
# run linode-cli volumes create --label "A$timestamp" --region us-east --size 10 --linode_id=$linode_id --text --no-headers --delimiter ","
# assert_success
# assert_output --regexp "[0-9]+,A$timestamp,creating,10,us-east,$linode_id"
# }

@test "it should remove all volumes" {
run removeLinodes
run removeVolumes
}
43 changes: 43 additions & 0 deletions test/volumes/list-view-update-volumes.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bats

load '../test_helper/bats-support/load'
load '../test_helper/bats-assert/load'
load '../common'

##################################################################
# WARNING: THIS TEST WILL DELETE ALL OF YOUR LINODES #
# WARNING: USE A SEPARATE TEST ACCOUNT WHEN RUNNING THESE TESTS #
##################################################################

@test "it should list volumes" {
run createVolume
run linode-cli volumes list --text --no-headers --delimiter=","
assert_success
assert_output --regexp "[0-9]+,A[0-9]+,creating,10,us-east"
}

@test "it should view a single volume" {
volume_id=$(linode-cli volumes list --text --no-headers --delimiter="," --format="id")
run linode-cli volumes view $volume_id --text --no-headers --delimiter="," --format="id,label,size,region"
assert_success
assert_output --regexp "$volume_id,A[0-9]+,10,us-east"
}

@test "it should update a volume label" {
volume_id=$(linode-cli volumes list --text --no-headers --delimiter="," --format="id")
run linode-cli volumes update --label=A-NewLabel-2 $volume_id --text --no-headers --format="label"
assert_success
assert_output "A-NewLabel-2"
}

@test "it should fail to update volume size" {
volume_id=$(linode-cli volumes list --text --no-headers --delimiter="," --format="id")
run linode-cli volumes update --size=15 $volume_id --text --no-headers --format="size"
assert_failure
assert_output --partial "Request failed: 400"
assert_output --partial "size size is not an editable field"
}

@test "it should remove all volumes" {
run removeVolumes
}
40 changes: 40 additions & 0 deletions test/volumes/resize-volumes.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env bats

load '../test_helper/bats-support/load'
load '../test_helper/bats-assert/load'
load '../common'

##################################################################
# WARNING: THIS TEST WILL DELETE ALL OF YOUR LINODES #
# WARNING: USE A SEPARATE TEST ACCOUNT WHEN RUNNING THESE TESTS #
##################################################################

@test "it should fail to resize a volume smaller" {
createVolume
volume_id=$(linode-cli volumes list --text --no-headers --format="id")
run linode-cli volumes resize $volume_id --size=5 --text --no-headers
assert_failure
assert_output --partial "Request failed: 400"
assert_output --partial "Storage volumes can only be resized up"
}

@test "it should fail to resize a volume greater than 10240gb" {
volume_id=$(linode-cli volumes list --text --no-headers --format="id")
run linode-cli volumes resize $volume_id --size=1024893405 --text --no-headers
assert_failure
assert_output --partial "Request failed: 400"
assert_output --partial "Storage volumes cannot be resized larger than 10240 gigabytes"
}

@test "it should resize a volume" {
volume_id=$(linode-cli volumes list --text --no-headers --format="id")
run linode-cli volumes resize $volume_id --size=11 --text --no-headers
assert_success
run linode-cli volumes view $volume_id --format="size" --text --no-headers
assert_success
assert_output "11"
}

@test "it should remove all volumes" {
run removeVolumes
}

0 comments on commit c03eb69

Please sign in to comment.