11package container_test
22
33import (
4- "fmt"
5- "io"
6- "os"
74 "time"
85
96 "github.com/docker/go-connections/nat"
@@ -30,13 +27,7 @@ type File struct {
3027}
3128
3229type ContainerInterface interface {
33- AddFiles (file []File ) error
3430 GetProps () Props
35- Clear () error
36- Terminate () error
37- NginxToConfigDefault () error
38- ChangeNginxConfigReadFile (nginxPath string ) error
39- ChangeNginxConfig (nginxConfig []byte ) error
4031}
4132
4233var _ ContainerInterface = (* ContainerTest )(nil )
@@ -71,71 +62,9 @@ func New(ctx context.Context, context string, dockerfile string) (*ContainerTest
7162 }
7263 container .Props .Port = port .Port ()
7364 container .Container = nginxContainer
74- if err := container .NginxToConfigDefault (); err != nil {
75- return nil , err
76- }
7765 return & container , nil
7866}
7967
80- func (c * ContainerTest ) AddFiles (files []File ) error {
81- for _ , file := range files {
82- c .FilesPath = append (c .FilesPath , file .Path )
83- err := c .Container .CopyToContainer (context .Background (), []byte (file .File ), file .Path , 0644 )
84- if err != nil {
85- return err
86- }
87- }
88- return nil
89- }
90-
91- func (c * ContainerTest ) Clear () error {
92- for _ , file := range c .FilesPath {
93- _ , _ , err := c .Container .Exec (context .Background (), []string {"rm" , file })
94- if err != nil {
95- return err
96- }
97- }
98- return c .NginxToConfigDefault ()
99- }
100-
101- func (c * ContainerTest ) NginxToConfigDefault () error {
102- srcFile , err := os .Open ("./container/nginx.conf" )
103- if err != nil {
104- return fmt .Errorf ("error opening nginx.conf: %w" , err )
105- }
106- defer srcFile .Close ()
107- fileContent , err := io .ReadAll (srcFile )
108- if err != nil {
109- return fmt .Errorf ("error reading nginx.conf: %w" , err )
110- }
111- return c .ChangeNginxConfig (fileContent )
112- }
113-
114- func (c * ContainerTest ) ChangeNginxConfigReadFile (nginxPath string ) error {
115- nginxConf , err := os .ReadFile (nginxPath )
116- if err != nil {
117- return err
118- }
119- return c .ChangeNginxConfig (nginxConf )
120- }
121-
122- func (c * ContainerTest ) ChangeNginxConfig (nginxConfig []byte ) error {
123- _ , _ , err := c .Container .Exec (context .Background (), []string {"sh" , "-c" , "rm " , "/usr/local/openresty/nginx/conf/nginx.conf" })
124- if err != nil {
125- return err
126- }
127- err = c .Container .CopyToContainer (context .Background (), nginxConfig , "/usr/local/openresty/nginx/conf/nginx.conf" , 0644 )
128- if err != nil {
129- return err
130- }
131- _ , _ , err = c .Container .Exec (context .Background (), []string {"sh" , "-c" , "pkill -HUP openresty || openresty" })
132- return err
133- }
134-
135- func (c * ContainerTest ) Terminate () error {
136- return c .Container .Terminate (context .Background ())
137- }
138-
13968func (c * ContainerTest ) GetProps () Props {
14069 return c .Props
14170}
0 commit comments