|
1 | 1 | package config |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "encoding/base64" |
| 6 | + "fmt" |
5 | 7 | "io/ioutil" |
6 | 8 | "os" |
7 | 9 | "testing" |
8 | 10 | ) |
9 | 11 |
|
| 12 | +type KVPair struct { |
| 13 | + key string |
| 14 | + value string |
| 15 | +} |
| 16 | + |
| 17 | +type MockStore []KVPair |
| 18 | + |
| 19 | +type mockClient struct { |
| 20 | + expected MockStore |
| 21 | +} |
| 22 | + |
| 23 | +func (m mockClient) Get(key string) (value string, err error) { |
| 24 | + for _, expect := range m.expected { |
| 25 | + if expect.key == key { |
| 26 | + return expect.value, nil |
| 27 | + } |
| 28 | + } |
| 29 | + return "", fmt.Errorf("%s does not exist", m.expected) |
| 30 | +} |
| 31 | + |
| 32 | +func (m mockClient) Set(key, value string) (returnedValue string, err error) { |
| 33 | + for _, expect := range m.expected { |
| 34 | + if expect.key == key { |
| 35 | + return value, nil |
| 36 | + } |
| 37 | + } |
| 38 | + return "", fmt.Errorf("%s does not exist", m.expected) |
| 39 | +} |
| 40 | + |
| 41 | +func (m mockClient) Delete(key string) (err error) { |
| 42 | + for _, expect := range m.expected { |
| 43 | + if expect.key == key { |
| 44 | + return nil |
| 45 | + } |
| 46 | + } |
| 47 | + return fmt.Errorf("%s does not exist", m.expected) |
| 48 | +} |
| 49 | + |
| 50 | +func TestGetConfig(t *testing.T) { |
| 51 | + |
| 52 | + testMock := mockClient{expected: MockStore{{key: "/deis/controller/testing", value: "foo"}, {key: "/deis/controller/port", value: "8000"}}} |
| 53 | + testWriter := bytes.Buffer{} |
| 54 | + |
| 55 | + err := doConfig("controller", "get", []string{"testing", "port"}, testMock, &testWriter) |
| 56 | + |
| 57 | + if err != nil { |
| 58 | + t.Fatal(err) |
| 59 | + } |
| 60 | + |
| 61 | + expected := "foo\n8000\n" |
| 62 | + output := testWriter.String() |
| 63 | + if output != expected { |
| 64 | + t.Error(fmt.Errorf("Expected: '%s', Got:'%s'", expected, output)) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestGetConfigError(t *testing.T) { |
| 69 | + testMock := mockClient{expected: MockStore{{key: "/deis/controller/testing", value: "foo"}}} |
| 70 | + testWriter := bytes.Buffer{} |
| 71 | + |
| 72 | + err := doConfig("controller", "get", []string{"port"}, testMock, &testWriter) |
| 73 | + |
| 74 | + if err == nil { |
| 75 | + t.Fatal("Error Expected") |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func TestSetConfig(t *testing.T) { |
| 80 | + testMock := mockClient{expected: MockStore{{key: "/deis/controller/testing", value: "foo"}, {key: "/deis/controller/port", value: "8000"}}} |
| 81 | + testWriter := bytes.Buffer{} |
| 82 | + |
| 83 | + err := doConfig("controller", "set", []string{"testing=bar", "port=1000"}, testMock, &testWriter) |
| 84 | + |
| 85 | + if err != nil { |
| 86 | + t.Fatal(err) |
| 87 | + } |
| 88 | + |
| 89 | + expected := "bar\n1000\n" |
| 90 | + output := testWriter.String() |
| 91 | + if output != expected { |
| 92 | + t.Error(fmt.Errorf("Expected: '%s', Got:'%s'", expected, output)) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestDeleteConfig(t *testing.T) { |
| 97 | + testMock := mockClient{expected: MockStore{{key: "/deis/controller/testing", value: "foo"}, {key: "/deis/controller/port", value: "8000"}}} |
| 98 | + testWriter := bytes.Buffer{} |
| 99 | + |
| 100 | + err := doConfig("controller", "rm", []string{"testing", "port"}, testMock, &testWriter) |
| 101 | + |
| 102 | + if err != nil { |
| 103 | + t.Fatal(err) |
| 104 | + } |
| 105 | + |
| 106 | + expected := "testing\nport\n" |
| 107 | + output := testWriter.String() |
| 108 | + if output != expected { |
| 109 | + t.Error(fmt.Errorf("Expected: '%s', Got:'%s'", expected, output)) |
| 110 | + } |
| 111 | +} |
| 112 | + |
10 | 113 | // TestConfigSSHPrivateKey ensures private keys are base64 encoded from file path |
11 | 114 | func TestConfigSSHPrivateKey(t *testing.T) { |
12 | 115 |
|
|
0 commit comments