|
1 | 1 | package dockergen
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "log" |
| 7 | + "os" |
4 | 8 | "testing"
|
5 | 9 |
|
6 | 10 | "github.com/stretchr/testify/assert"
|
@@ -129,3 +133,61 @@ func TestParseHostTCPDefault(t *testing.T) {
|
129 | 133 | assert.Equal(t, "tcp", proto, "failed to parse tcp://:4243")
|
130 | 134 | assert.Equal(t, "127.0.0.1:4243", addr, "failed to parse tcp://:4243")
|
131 | 135 | }
|
| 136 | + |
| 137 | +func TestParseHostSystemd(t *testing.T) { |
| 138 | + proto, addr, err := parseHost("fd://") |
| 139 | + assert.NoError(t, err) |
| 140 | + assert.Equal(t, "fd", proto, "failed to parse fd://") |
| 141 | + assert.Equal(t, "fd://", addr, "failed to parse fd://") |
| 142 | +} |
| 143 | + |
| 144 | +func assertParseHostError(t *testing.T, address string) { |
| 145 | + proto, addr, err := parseHost(address) |
| 146 | + message := fmt.Sprintf("should have failed to parse %v", address) |
| 147 | + assert.Error(t, err, message) |
| 148 | + assert.Equal(t, "", proto, message) |
| 149 | + assert.Equal(t, "", addr, message) |
| 150 | +} |
| 151 | + |
| 152 | +func TestParseHostTCPNoAddressError(t *testing.T) { |
| 153 | + assertParseHostError(t, "tcp://") |
| 154 | +} |
| 155 | + |
| 156 | +func TestParseHostTCPIncorrectBindAddressError(t *testing.T) { |
| 157 | + incorrectBindAdresses := []string{ |
| 158 | + "tcp://127.0.0.1:4243:80", |
| 159 | + "tcp://127.0.0.1:", |
| 160 | + "tcp://127.0.0.1", |
| 161 | + } |
| 162 | + |
| 163 | + for _, address := range incorrectBindAdresses { |
| 164 | + assertParseHostError(t, address) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +func TestParseHostWrongProtocolError(t *testing.T) { |
| 169 | + assertParseHostError(t, "foo://") |
| 170 | +} |
| 171 | + |
| 172 | +func TestTlsEnabled(t *testing.T) { |
| 173 | + tls := tlsEnabled("foo", "bar", "baz") |
| 174 | + assert.False(t, tls) |
| 175 | + |
| 176 | + filepaths := map[string]string{ |
| 177 | + "cert": "", |
| 178 | + "caCert": "", |
| 179 | + "key": "", |
| 180 | + } |
| 181 | + // Create temporary files |
| 182 | + for key := range filepaths { |
| 183 | + file, err := ioutil.TempFile("", key) |
| 184 | + if err != nil { |
| 185 | + log.Fatal(err) |
| 186 | + } |
| 187 | + defer os.Remove(file.Name()) |
| 188 | + filepaths[key] = file.Name() |
| 189 | + } |
| 190 | + |
| 191 | + tls = tlsEnabled(filepaths["cert"], filepaths["caCert"], filepaths["key"]) |
| 192 | + assert.True(t, tls) |
| 193 | +} |
0 commit comments