@@ -16,9 +16,10 @@ import (
1616)
1717
1818type Input struct {
19- Image string `toml:"image" comment:"Fake service image, usually can be found in our ECR with $project-fakes name"`
20- Port int `toml:"port" validate:"required" comment:"The port which Docker container is exposing"`
21- Out * Output `toml:"out" comment:"Fakes service config output"`
19+ Image string `toml:"image" comment:"Fake service image, usually can be found in our ECR with $project-fakes name"`
20+ ContainerName string `toml:"container_name" comment:"Docker container name"`
21+ Port int `toml:"port" validate:"required" comment:"The port which Docker container is exposing"`
22+ Out * Output `toml:"out" comment:"Fakes service config output"`
2223}
2324
2425type Output struct {
@@ -34,18 +35,27 @@ func NewDockerFakeDataProvider(in *Input) (*Output, error) {
3435 return NewWithContext (context .Background (), in )
3536}
3637
38+ func defaultFake (in * Input ) {
39+ if in .Port == 0 {
40+ in .Port = 9111
41+ }
42+ if in .ContainerName == "" {
43+ in .ContainerName = "fake"
44+ }
45+ }
46+
3747// NewWithContext creates new fake data provider in Docker using testcontainers-go
3848func NewWithContext (ctx context.Context , in * Input ) (* Output , error ) {
3949 if in .Out != nil && in .Out .UseCache {
4050 return in .Out , nil
4151 }
52+ defaultFake (in )
4253 bindPort := fmt .Sprintf ("%d/tcp" , in .Port )
43- containerName := framework .DefaultTCName ("fake" )
4454 if pods .K8sEnabled () {
4555 _ , svc , err := pods .Run (ctx , & pods.Config {
4656 Pods : []* pods.PodConfig {
4757 {
48- Name : pods .Ptr (containerName ),
58+ Name : pods .Ptr (in . ContainerName ),
4959 Image : & in .Image ,
5060 Ports : []string {fmt .Sprintf ("%d:%d" , in .Port , in .Port )},
5161 Requests : pods .ResourcesSmall (),
@@ -63,17 +73,17 @@ func NewWithContext(ctx context.Context, in *Input) (*Output, error) {
6373 in .Out = & Output {
6474 K8sService : svc ,
6575 BaseURLHost : fmt .Sprintf ("http://%s:%d" , "localhost" , in .Port ),
66- BaseURLDocker : fmt .Sprintf ("http://%s:%d" , fmt .Sprintf ("%s-svc" , containerName ), in .Port ),
76+ BaseURLDocker : fmt .Sprintf ("http://%s:%d" , fmt .Sprintf ("%s-svc" , in . ContainerName ), in .Port ),
6777 }
6878 return in .Out , nil
6979 }
7080 req := tc.ContainerRequest {
71- Name : containerName ,
81+ Name : in . ContainerName ,
7282 Image : in .Image ,
7383 Labels : framework .DefaultTCLabels (),
7484 Networks : []string {framework .DefaultNetworkName },
7585 NetworkAliases : map [string ][]string {
76- framework .DefaultNetworkName : {containerName },
86+ framework .DefaultNetworkName : {in . ContainerName },
7787 },
7888 ExposedPorts : []string {bindPort },
7989 HostConfigModifier : func (h * container.HostConfig ) {
@@ -92,7 +102,7 @@ func NewWithContext(ctx context.Context, in *Input) (*Output, error) {
92102 }
93103 in .Out = & Output {
94104 BaseURLHost : fmt .Sprintf ("http://localhost:%d" , in .Port ),
95- BaseURLDocker : fmt .Sprintf ("http://%s:%d" , containerName , in .Port ),
105+ BaseURLDocker : fmt .Sprintf ("http://%s:%d" , in . ContainerName , in .Port ),
96106 }
97107 return in .Out , nil
98108}
0 commit comments