~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/lxc/lxd/lxc/main_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package main
 
2
 
 
3
import (
 
4
        "testing"
 
5
 
 
6
        "github.com/lxc/lxd"
 
7
)
 
8
 
 
9
type aliasTestcase struct {
 
10
        input    []string
 
11
        expected []string
 
12
}
 
13
 
 
14
func slicesEqual(a, b []string) bool {
 
15
        if a == nil && b == nil {
 
16
                return true
 
17
        }
 
18
 
 
19
        if a == nil || b == nil {
 
20
                return false
 
21
        }
 
22
 
 
23
        if len(a) != len(b) {
 
24
                return false
 
25
        }
 
26
 
 
27
        for i := range a {
 
28
                if a[i] != b[i] {
 
29
                        return false
 
30
                }
 
31
        }
 
32
 
 
33
        return true
 
34
}
 
35
 
 
36
func TestExpandAliases(t *testing.T) {
 
37
        aliases := map[string]string{
 
38
                "tester 12": "list",
 
39
                "foo":       "list @ARGS@ -c n",
 
40
        }
 
41
 
 
42
        testcases := []aliasTestcase{
 
43
                aliasTestcase{
 
44
                        input:    []string{"lxc", "list"},
 
45
                        expected: []string{"lxc", "list"},
 
46
                },
 
47
                aliasTestcase{
 
48
                        input:    []string{"lxc", "tester", "12"},
 
49
                        expected: []string{"lxc", "list", "--no-alias"},
 
50
                },
 
51
                aliasTestcase{
 
52
                        input:    []string{"lxc", "foo", "asdf"},
 
53
                        expected: []string{"lxc", "list", "--no-alias", "asdf", "-c", "n"},
 
54
                },
 
55
        }
 
56
 
 
57
        conf := &lxd.Config{Aliases: aliases}
 
58
 
 
59
        for _, tc := range testcases {
 
60
                result, expanded := expandAlias(conf, tc.input)
 
61
                if !expanded {
 
62
                        if !slicesEqual(tc.input, tc.expected) {
 
63
                                t.Errorf("didn't expand when expected to: %s", tc.input)
 
64
                        }
 
65
                        continue
 
66
                }
 
67
 
 
68
                if !slicesEqual(result, tc.expected) {
 
69
                        t.Errorf("%s didn't match %s", result, tc.expected)
 
70
                }
 
71
        }
 
72
}