#!/bin/sh

test_nonexisting() {
	echo "Getting a configuration value that shouldn't be there"
	if snapctl get non-existing; then
		echo "Expected getting a non-existing value to be an error"
		exit 1
	fi
}

test_snapctl_set_foo() {
	echo "Setting foo"
	if ! snapctl set foo=bar; then
		echo "snapctl set unexpectedly failed"
		exit 1
	fi
}

test_snapctl_get_foo() {
	echo "Getting foo"
	if ! output=$(snapctl get foo); then
		echo "Expected snapctl get to be able to retrieve value just set"
		exit 1
	fi

	expected_output="bar"
	if "$output" -ne "$expected_output"; then
		echo "Expected output to be '$expected_output', but it was '$output'"
		exit 1
	fi

	echo "Getting foo with full document"
	if ! output=$(snapctl get -d foo); then
		echo "Expected snapctl get to be able to retrieve value just set"
		exit 1
	fi

	expected_output='{\n"foo": "bar"\n}'
	if "$output" -ne "$expected_output"; then
		echo "Expected output to be '$expected_output', but it was '$output'"
		exit 1
	fi
}

command=$(snapctl get command)
case $command in
	'"test-nonexisting"')
		test_nonexisting
		;;
	'"test-snapctl-set-foo"')
		test_snapctl_set_foo
		;;
	'"test-snapctl-get-foo"')
		test_snapctl_get_foo
		;;
	*)
		echo "Invalid command: '$command'"
		exit 1
		;;
esac
