@@ -9,17 +9,19 @@ import (
99 "io"
1010 "net/http"
1111
12+ "github.com/docker/go-connections/nat"
1213 . "github.com/onsi/ginkgo/v2"
1314 . "github.com/onsi/gomega"
1415 "github.com/runfinch/common-tests/command"
1516 "github.com/runfinch/common-tests/option"
1617
1718 "github.com/runfinch/finch-daemon/api/types"
1819 "github.com/runfinch/finch-daemon/e2e/client"
20+ "github.com/runfinch/finch-daemon/e2e/util"
1921)
2022
2123// ContainerInspect tests the `GET containers/{id}/json` API.
22- func ContainerInspect (opt * option.Option ) {
24+ func ContainerInspect (opt * option.Option , pOpt util. NewOpt ) {
2325 Describe ("inspect container" , func () {
2426 var (
2527 uClient * http.Client
@@ -74,6 +76,96 @@ func ContainerInspect(opt *option.Option) {
7476 Expect (got .SizeRootFs ).ShouldNot (BeNil ())
7577 })
7678
79+ It ("should return hostconfig with proper values" , func () {
80+ // setup cpu/mem options
81+ createOptions := types.ContainerCreateRequest {}
82+ createOptions .Image = defaultImage
83+ createOptions .Cmd = []string {"sleep" , "Infinity" }
84+ createOptions .HostConfig .ShmSize = 134217728
85+ createOptions .HostConfig .CPUSetCPUs = "0,1"
86+ createOptions .HostConfig .CPUSetMems = "0"
87+ createOptions .HostConfig .CPUShares = 2048
88+ createOptions .HostConfig .CPUPeriod = 100000
89+ createOptions .HostConfig .Memory = 134217728
90+ createOptions .HostConfig .MemorySwap = 514288000
91+
92+ // setup port mappings
93+ hostPort := "8001"
94+ ctrPort := "8000"
95+ hostPort2 := "9001"
96+ ctrPort2 := "9000"
97+ tcpPort := nat .Port (fmt .Sprintf ("%s/tcp" , ctrPort ))
98+ tcpPortBinding := nat.PortBinding {HostIP : "0.0.0.0" , HostPort : hostPort }
99+ udpPort := nat .Port (fmt .Sprintf ("%s/udp" , ctrPort2 ))
100+ udpPortBinding := nat.PortBinding {HostIP : "0.0.0.0" , HostPort : hostPort2 }
101+ createOptions .HostConfig .PortBindings = nat.PortMap {
102+ tcpPort : []nat.PortBinding {tcpPortBinding },
103+ udpPort : []nat.PortBinding {udpPortBinding },
104+ }
105+
106+ // setup devices
107+ tmpFileOpt , _ := pOpt ([]string {"touch" , "/tmp/loopdev" })
108+ command .Run (tmpFileOpt )
109+ defer func () {
110+ rmOpt , _ := pOpt ([]string {"rm" , "-f" , "/tmp/loopdev" })
111+ command .Run (rmOpt )
112+ }()
113+ ddOpt , _ := pOpt ([]string {"dd" , "if=/dev/zero" , "of=/tmp/loopdev" , "bs=4096" , "count=1" })
114+ command .Run (ddOpt )
115+ loopDevOpt , _ := pOpt ([]string {"losetup" , "-f" , "--show" , "/tmp/loopdev" })
116+ loopDev := command .StdoutStr (loopDevOpt )
117+ Expect (loopDev ).ShouldNot (BeEmpty ())
118+ defer func () {
119+ detachOpt , _ := pOpt ([]string {"losetup" , "-d" , loopDev })
120+ command .Run (detachOpt )
121+ }()
122+ createOptions .HostConfig .Devices = []types.DeviceMapping {
123+ {
124+ PathOnHost : loopDev ,
125+ PathInContainer : loopDev ,
126+ CgroupPermissions : "rwm" ,
127+ },
128+ }
129+
130+ // create and start container with options
131+ containerCreateUrl := client .ConvertToFinchUrl (version , "/containers/create" )
132+ statusCode , ctr := createContainer (uClient , containerCreateUrl , testContainerName2 , createOptions )
133+ Expect (statusCode ).Should (Equal (http .StatusCreated ))
134+ Expect (ctr .ID ).ShouldNot (BeEmpty ())
135+ command .Run (opt , "start" , testContainerName2 )
136+
137+ // inspect container
138+ res , err := uClient .Get (client .ConvertToFinchUrl (version , fmt .Sprintf ("/containers/%s/json" , ctr .ID )))
139+ Expect (err ).Should (BeNil ())
140+ Expect (res .StatusCode ).Should (Equal (http .StatusOK ))
141+ var got types.Container
142+ err = json .NewDecoder (res .Body ).Decode (& got )
143+ Expect (err ).Should (BeNil ())
144+
145+ // verify hostconfig
146+ Expect (got .HostConfig ).ShouldNot (BeNil ())
147+ Expect (got .HostConfig .ContainerIDFile ).Should (BeEmpty ())
148+ Expect (got .HostConfig .PidMode ).Should (BeEmpty ())
149+ Expect (got .HostConfig .IpcMode ).Should (Equal ("private" ))
150+ Expect (got .HostConfig .ReadonlyRootfs ).Should (BeFalse ())
151+ Expect (got .HostConfig .Sysctls ).Should (BeEmpty ())
152+ Expect (got .HostConfig .Devices ).Should (HaveLen (1 ))
153+ Expect (got .HostConfig .Devices [0 ]).Should (Equal (createOptions .HostConfig .Devices [0 ]))
154+ Expect (got .HostConfig .ShmSize ).Should (Equal (createOptions .HostConfig .ShmSize ))
155+ Expect (got .HostConfig .CPUSetCPUs ).Should (Equal (createOptions .HostConfig .CPUSetCPUs ))
156+ Expect (got .HostConfig .CPUSetMems ).Should (Equal (createOptions .HostConfig .CPUSetMems ))
157+ Expect (got .HostConfig .CPUShares ).Should (Equal (createOptions .HostConfig .CPUShares ))
158+ Expect (got .HostConfig .CPUPeriod ).Should (Equal (createOptions .HostConfig .CPUPeriod ))
159+ Expect (got .HostConfig .Memory ).Should (Equal (createOptions .HostConfig .Memory ))
160+ Expect (got .HostConfig .MemorySwap ).Should (Equal (createOptions .HostConfig .MemorySwap ))
161+ Expect (got .HostConfig .PortBindings ).Should (HaveLen (2 ))
162+ Expect (got .HostConfig .PortBindings [tcpPort ]).Should (HaveLen (1 ))
163+ Expect (got .HostConfig .PortBindings [tcpPort ]).Should (HaveLen (1 ))
164+ Expect (got .HostConfig .PortBindings [tcpPort ][0 ]).Should (Equal (tcpPortBinding ))
165+ Expect (got .HostConfig .PortBindings [udpPort ]).Should (HaveLen (1 ))
166+ Expect (got .HostConfig .PortBindings [udpPort ][0 ]).Should (Equal (udpPortBinding ))
167+ })
168+
77169 It ("should return 404 error when container does not exist" , func () {
78170 res , err := uClient .Get (client .ConvertToFinchUrl (version , "/containers/nonexistent/json" ))
79171 Expect (err ).Should (BeNil ())
0 commit comments