forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_file_test.go
73 lines (68 loc) · 1.96 KB
/
container_file_test.go
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
73
// This test is testing very internal logic that should not be exported away from this package. We'll
// leave it in the main testcontainers package. Do not use for user facing examples.
package testcontainers
import (
"errors"
"os"
"path/filepath"
"testing"
)
func TestContainerFileValidation(t *testing.T) {
type ContainerFileValidationTestCase struct {
Name string
ExpectedError error
File ContainerFile
}
f, err := os.Open(filepath.Join(".", "testdata", "hello.sh"))
if err != nil {
t.Fatal(err)
}
testTable := []ContainerFileValidationTestCase{
{
Name: "valid container file: has hostfilepath",
File: ContainerFile{
HostFilePath: "/path/to/host",
ContainerFilePath: "/path/to/container",
},
},
{
Name: "valid container file: has reader",
File: ContainerFile{
Reader: f,
ContainerFilePath: "/path/to/container",
},
},
{
Name: "invalid container file",
ExpectedError: errors.New("either HostFilePath or Reader must be specified"),
File: ContainerFile{
HostFilePath: "",
Reader: nil,
ContainerFilePath: "/path/to/container",
},
},
{
Name: "invalid container file",
ExpectedError: errors.New("ContainerFilePath must be specified"),
File: ContainerFile{
HostFilePath: "/path/to/host",
ContainerFilePath: "",
},
},
}
for _, testCase := range testTable {
t.Run(testCase.Name, func(t *testing.T) {
err := testCase.File.validate()
switch {
case err == nil && testCase.ExpectedError == nil:
return
case err == nil && testCase.ExpectedError != nil:
t.Errorf("did not receive expected error: %s", testCase.ExpectedError.Error())
case err != nil && testCase.ExpectedError == nil:
t.Errorf("received unexpected error: %s", err.Error())
case err.Error() != testCase.ExpectedError.Error():
t.Errorf("errors mismatch: %s != %s", err.Error(), testCase.ExpectedError.Error())
}
})
}
}