~nskaggs/+junk/xenial-test

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
package main

import (
	"testing"

	"github.com/lxc/lxd"
)

type aliasTestcase struct {
	input    []string
	expected []string
}

func slicesEqual(a, b []string) bool {
	if a == nil && b == nil {
		return true
	}

	if a == nil || b == nil {
		return false
	}

	if len(a) != len(b) {
		return false
	}

	for i := range a {
		if a[i] != b[i] {
			return false
		}
	}

	return true
}

func TestExpandAliases(t *testing.T) {
	aliases := map[string]string{
		"tester 12": "list",
		"foo":       "list @ARGS@ -c n",
	}

	testcases := []aliasTestcase{
		aliasTestcase{
			input:    []string{"lxc", "list"},
			expected: []string{"lxc", "list"},
		},
		aliasTestcase{
			input:    []string{"lxc", "tester", "12"},
			expected: []string{"lxc", "list", "--no-alias"},
		},
		aliasTestcase{
			input:    []string{"lxc", "foo", "asdf"},
			expected: []string{"lxc", "list", "--no-alias", "asdf", "-c", "n"},
		},
	}

	conf := &lxd.Config{Aliases: aliases}

	for _, tc := range testcases {
		result, expanded := expandAlias(conf, tc.input)
		if !expanded {
			if !slicesEqual(tc.input, tc.expected) {
				t.Errorf("didn't expand when expected to: %s", tc.input)
			}
			continue
		}

		if !slicesEqual(result, tc.expected) {
			t.Errorf("%s didn't match %s", result, tc.expected)
		}
	}
}