12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234 |
- package main
- import (
- "archive/tar"
- "bytes"
- "context"
- "encoding/json"
- "fmt"
- "io"
- "io/ioutil"
- "net/http"
- "os"
- "path/filepath"
- "regexp"
- "runtime"
- "strings"
- "testing"
- "time"
- "github.com/docker/docker/api/types"
- containertypes "github.com/docker/docker/api/types/container"
- mounttypes "github.com/docker/docker/api/types/mount"
- networktypes "github.com/docker/docker/api/types/network"
- "github.com/docker/docker/api/types/versions"
- "github.com/docker/docker/client"
- "github.com/docker/docker/integration-cli/cli"
- "github.com/docker/docker/integration-cli/cli/build"
- "github.com/docker/docker/pkg/ioutils"
- "github.com/docker/docker/pkg/mount"
- "github.com/docker/docker/pkg/stringid"
- "github.com/docker/docker/testutil/request"
- "github.com/docker/docker/volume"
- "github.com/docker/go-connections/nat"
- "gotest.tools/assert"
- is "gotest.tools/assert/cmp"
- "gotest.tools/poll"
- )
- func (s *DockerSuite) TestContainerAPIGetAll(c *testing.T) {
- startCount := getContainerCount(c)
- name := "getall"
- dockerCmd(c, "run", "--name", name, "busybox", "true")
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- options := types.ContainerListOptions{
- All: true,
- }
- containers, err := cli.ContainerList(context.Background(), options)
- assert.NilError(c, err)
- assert.Equal(c, len(containers), startCount+1)
- actual := containers[0].Names[0]
- assert.Equal(c, actual, "/"+name)
- }
- // regression test for empty json field being omitted #13691
- func (s *DockerSuite) TestContainerAPIGetJSONNoFieldsOmitted(c *testing.T) {
- startCount := getContainerCount(c)
- dockerCmd(c, "run", "busybox", "true")
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- options := types.ContainerListOptions{
- All: true,
- }
- containers, err := cli.ContainerList(context.Background(), options)
- assert.NilError(c, err)
- assert.Equal(c, len(containers), startCount+1)
- actual := fmt.Sprintf("%+v", containers[0])
- // empty Labels field triggered this bug, make sense to check for everything
- // cause even Ports for instance can trigger this bug
- // better safe than sorry..
- fields := []string{
- "ID",
- "Names",
- "Image",
- "Command",
- "Created",
- "Ports",
- "Labels",
- "Status",
- "NetworkSettings",
- }
- // decoding into types.Container do not work since it eventually unmarshal
- // and empty field to an empty go map, so we just check for a string
- for _, f := range fields {
- if !strings.Contains(actual, f) {
- c.Fatalf("Field %s is missing and it shouldn't", f)
- }
- }
- }
- func (s *DockerSuite) TestContainerAPIGetExport(c *testing.T) {
- // Not supported on Windows as Windows does not support docker export
- testRequires(c, DaemonIsLinux)
- name := "exportcontainer"
- dockerCmd(c, "run", "--name", name, "busybox", "touch", "/test")
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- body, err := cli.ContainerExport(context.Background(), name)
- assert.NilError(c, err)
- defer body.Close()
- found := false
- for tarReader := tar.NewReader(body); ; {
- h, err := tarReader.Next()
- if err != nil && err == io.EOF {
- break
- }
- if h.Name == "test" {
- found = true
- break
- }
- }
- assert.Assert(c, found, "The created test file has not been found in the exported image")
- }
- func (s *DockerSuite) TestContainerAPIGetChanges(c *testing.T) {
- // Not supported on Windows as Windows does not support docker diff (/containers/name/changes)
- testRequires(c, DaemonIsLinux)
- name := "changescontainer"
- dockerCmd(c, "run", "--name", name, "busybox", "rm", "/etc/passwd")
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- changes, err := cli.ContainerDiff(context.Background(), name)
- assert.NilError(c, err)
- // Check the changelog for removal of /etc/passwd
- success := false
- for _, elem := range changes {
- if elem.Path == "/etc/passwd" && elem.Kind == 2 {
- success = true
- }
- }
- assert.Assert(c, success, "/etc/passwd has been removed but is not present in the diff")
- }
- func (s *DockerSuite) TestGetContainerStats(c *testing.T) {
- var (
- name = "statscontainer"
- )
- runSleepingContainer(c, "--name", name)
- type b struct {
- stats types.ContainerStats
- err error
- }
- bc := make(chan b, 1)
- go func() {
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- stats, err := cli.ContainerStats(context.Background(), name, true)
- assert.NilError(c, err)
- bc <- b{stats, err}
- }()
- // allow some time to stream the stats from the container
- time.Sleep(4 * time.Second)
- dockerCmd(c, "rm", "-f", name)
- // collect the results from the stats stream or timeout and fail
- // if the stream was not disconnected.
- select {
- case <-time.After(2 * time.Second):
- c.Fatal("stream was not closed after container was removed")
- case sr := <-bc:
- dec := json.NewDecoder(sr.stats.Body)
- defer sr.stats.Body.Close()
- var s *types.Stats
- // decode only one object from the stream
- assert.NilError(c, dec.Decode(&s))
- }
- }
- func (s *DockerSuite) TestGetContainerStatsRmRunning(c *testing.T) {
- out := runSleepingContainer(c)
- id := strings.TrimSpace(out)
- buf := &ChannelBuffer{C: make(chan []byte, 1)}
- defer buf.Close()
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- stats, err := cli.ContainerStats(context.Background(), id, true)
- assert.NilError(c, err)
- defer stats.Body.Close()
- chErr := make(chan error, 1)
- go func() {
- _, err = io.Copy(buf, stats.Body)
- chErr <- err
- }()
- b := make([]byte, 32)
- // make sure we've got some stats
- _, err = buf.ReadTimeout(b, 2*time.Second)
- assert.NilError(c, err)
- // Now remove without `-f` and make sure we are still pulling stats
- _, _, err = dockerCmdWithError("rm", id)
- assert.Assert(c, err != nil, "rm should have failed but didn't")
- _, err = buf.ReadTimeout(b, 2*time.Second)
- assert.NilError(c, err)
- dockerCmd(c, "rm", "-f", id)
- assert.Assert(c, <-chErr == nil)
- }
- // ChannelBuffer holds a chan of byte array that can be populate in a goroutine.
- type ChannelBuffer struct {
- C chan []byte
- }
- // Write implements Writer.
- func (c *ChannelBuffer) Write(b []byte) (int, error) {
- c.C <- b
- return len(b), nil
- }
- // Close closes the go channel.
- func (c *ChannelBuffer) Close() error {
- close(c.C)
- return nil
- }
- // ReadTimeout reads the content of the channel in the specified byte array with
- // the specified duration as timeout.
- func (c *ChannelBuffer) ReadTimeout(p []byte, n time.Duration) (int, error) {
- select {
- case b := <-c.C:
- return copy(p[0:], b), nil
- case <-time.After(n):
- return -1, fmt.Errorf("timeout reading from channel")
- }
- }
- // regression test for gh13421
- // previous test was just checking one stat entry so it didn't fail (stats with
- // stream false always return one stat)
- func (s *DockerSuite) TestGetContainerStatsStream(c *testing.T) {
- name := "statscontainer"
- runSleepingContainer(c, "--name", name)
- type b struct {
- stats types.ContainerStats
- err error
- }
- bc := make(chan b, 1)
- go func() {
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- stats, err := cli.ContainerStats(context.Background(), name, true)
- assert.NilError(c, err)
- bc <- b{stats, err}
- }()
- // allow some time to stream the stats from the container
- time.Sleep(4 * time.Second)
- dockerCmd(c, "rm", "-f", name)
- // collect the results from the stats stream or timeout and fail
- // if the stream was not disconnected.
- select {
- case <-time.After(2 * time.Second):
- c.Fatal("stream was not closed after container was removed")
- case sr := <-bc:
- b, err := ioutil.ReadAll(sr.stats.Body)
- defer sr.stats.Body.Close()
- assert.NilError(c, err)
- s := string(b)
- // count occurrences of "read" of types.Stats
- if l := strings.Count(s, "read"); l < 2 {
- c.Fatalf("Expected more than one stat streamed, got %d", l)
- }
- }
- }
- func (s *DockerSuite) TestGetContainerStatsNoStream(c *testing.T) {
- name := "statscontainer"
- runSleepingContainer(c, "--name", name)
- type b struct {
- stats types.ContainerStats
- err error
- }
- bc := make(chan b, 1)
- go func() {
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- stats, err := cli.ContainerStats(context.Background(), name, false)
- assert.NilError(c, err)
- bc <- b{stats, err}
- }()
- // allow some time to stream the stats from the container
- time.Sleep(4 * time.Second)
- dockerCmd(c, "rm", "-f", name)
- // collect the results from the stats stream or timeout and fail
- // if the stream was not disconnected.
- select {
- case <-time.After(2 * time.Second):
- c.Fatal("stream was not closed after container was removed")
- case sr := <-bc:
- b, err := ioutil.ReadAll(sr.stats.Body)
- defer sr.stats.Body.Close()
- assert.NilError(c, err)
- s := string(b)
- // count occurrences of `"read"` of types.Stats
- assert.Assert(c, strings.Count(s, `"read"`) == 1, "Expected only one stat streamed, got %d", strings.Count(s, `"read"`))
- }
- }
- func (s *DockerSuite) TestGetStoppedContainerStats(c *testing.T) {
- name := "statscontainer"
- dockerCmd(c, "create", "--name", name, "busybox", "ps")
- chResp := make(chan error)
- // We expect an immediate response, but if it's not immediate, the test would hang, so put it in a goroutine
- // below we'll check this on a timeout.
- go func() {
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- resp, err := cli.ContainerStats(context.Background(), name, false)
- defer resp.Body.Close()
- chResp <- err
- }()
- select {
- case err := <-chResp:
- assert.NilError(c, err)
- case <-time.After(10 * time.Second):
- c.Fatal("timeout waiting for stats response for stopped container")
- }
- }
- func (s *DockerSuite) TestContainerAPIPause(c *testing.T) {
- // Problematic on Windows as Windows does not support pause
- testRequires(c, DaemonIsLinux)
- getPaused := func(c *testing.T) []string {
- return strings.Fields(cli.DockerCmd(c, "ps", "-f", "status=paused", "-q", "-a").Combined())
- }
- out := cli.DockerCmd(c, "run", "-d", "busybox", "sleep", "30").Combined()
- ContainerID := strings.TrimSpace(out)
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- err = cli.ContainerPause(context.Background(), ContainerID)
- assert.NilError(c, err)
- pausedContainers := getPaused(c)
- if len(pausedContainers) != 1 || stringid.TruncateID(ContainerID) != pausedContainers[0] {
- c.Fatalf("there should be one paused container and not %d", len(pausedContainers))
- }
- err = cli.ContainerUnpause(context.Background(), ContainerID)
- assert.NilError(c, err)
- pausedContainers = getPaused(c)
- assert.Equal(c, len(pausedContainers), 0, "There should be no paused container.")
- }
- func (s *DockerSuite) TestContainerAPITop(c *testing.T) {
- testRequires(c, DaemonIsLinux)
- out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "top")
- id := strings.TrimSpace(out)
- assert.NilError(c, waitRun(id))
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- // sort by comm[andline] to make sure order stays the same in case of PID rollover
- top, err := cli.ContainerTop(context.Background(), id, []string{"aux", "--sort=comm"})
- assert.NilError(c, err)
- assert.Equal(c, len(top.Titles), 11, fmt.Sprintf("expected 11 titles, found %d: %v", len(top.Titles), top.Titles))
- if top.Titles[0] != "USER" || top.Titles[10] != "COMMAND" {
- c.Fatalf("expected `USER` at `Titles[0]` and `COMMAND` at Titles[10]: %v", top.Titles)
- }
- assert.Equal(c, len(top.Processes), 2, fmt.Sprintf("expected 2 processes, found %d: %v", len(top.Processes), top.Processes))
- assert.Equal(c, top.Processes[0][10], "/bin/sh -c top")
- assert.Equal(c, top.Processes[1][10], "top")
- }
- func (s *DockerSuite) TestContainerAPITopWindows(c *testing.T) {
- testRequires(c, DaemonIsWindows)
- out := runSleepingContainer(c, "-d")
- id := strings.TrimSpace(out)
- assert.NilError(c, waitRun(id))
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- top, err := cli.ContainerTop(context.Background(), id, nil)
- assert.NilError(c, err)
- assert.Equal(c, len(top.Titles), 4, "expected 4 titles, found %d: %v", len(top.Titles), top.Titles)
- if top.Titles[0] != "Name" || top.Titles[3] != "Private Working Set" {
- c.Fatalf("expected `Name` at `Titles[0]` and `Private Working Set` at Titles[3]: %v", top.Titles)
- }
- assert.Assert(c, len(top.Processes) >= 2, "expected at least 2 processes, found %d: %v", len(top.Processes), top.Processes)
- foundProcess := false
- expectedProcess := "busybox.exe"
- for _, process := range top.Processes {
- if process[0] == expectedProcess {
- foundProcess = true
- break
- }
- }
- assert.Assert(c, foundProcess, "expected to find %s: %v", expectedProcess, top.Processes)
- }
- func (s *DockerSuite) TestContainerAPICommit(c *testing.T) {
- cName := "testapicommit"
- dockerCmd(c, "run", "--name="+cName, "busybox", "/bin/sh", "-c", "touch /test")
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- options := types.ContainerCommitOptions{
- Reference: "testcontainerapicommit:testtag",
- }
- img, err := cli.ContainerCommit(context.Background(), cName, options)
- assert.NilError(c, err)
- cmd := inspectField(c, img.ID, "Config.Cmd")
- assert.Equal(c, cmd, "[/bin/sh -c touch /test]", fmt.Sprintf("got wrong Cmd from commit: %q", cmd))
- // sanity check, make sure the image is what we think it is
- dockerCmd(c, "run", img.ID, "ls", "/test")
- }
- func (s *DockerSuite) TestContainerAPICommitWithLabelInConfig(c *testing.T) {
- cName := "testapicommitwithconfig"
- dockerCmd(c, "run", "--name="+cName, "busybox", "/bin/sh", "-c", "touch /test")
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- config := containertypes.Config{
- Labels: map[string]string{"key1": "value1", "key2": "value2"}}
- options := types.ContainerCommitOptions{
- Reference: "testcontainerapicommitwithconfig",
- Config: &config,
- }
- img, err := cli.ContainerCommit(context.Background(), cName, options)
- assert.NilError(c, err)
- label1 := inspectFieldMap(c, img.ID, "Config.Labels", "key1")
- assert.Equal(c, label1, "value1")
- label2 := inspectFieldMap(c, img.ID, "Config.Labels", "key2")
- assert.Equal(c, label2, "value2")
- cmd := inspectField(c, img.ID, "Config.Cmd")
- assert.Equal(c, cmd, "[/bin/sh -c touch /test]", fmt.Sprintf("got wrong Cmd from commit: %q", cmd))
- // sanity check, make sure the image is what we think it is
- dockerCmd(c, "run", img.ID, "ls", "/test")
- }
- func (s *DockerSuite) TestContainerAPIBadPort(c *testing.T) {
- // TODO Windows to Windows CI - Port this test
- testRequires(c, DaemonIsLinux)
- config := containertypes.Config{
- Image: "busybox",
- Cmd: []string{"/bin/sh", "-c", "echo test"},
- }
- hostConfig := containertypes.HostConfig{
- PortBindings: nat.PortMap{
- "8080/tcp": []nat.PortBinding{
- {
- HostIP: "",
- HostPort: "aa80"},
- },
- },
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- _, err = cli.ContainerCreate(context.Background(), &config, &hostConfig, &networktypes.NetworkingConfig{}, "")
- assert.ErrorContains(c, err, `invalid port specification: "aa80"`)
- }
- func (s *DockerSuite) TestContainerAPICreate(c *testing.T) {
- config := containertypes.Config{
- Image: "busybox",
- Cmd: []string{"/bin/sh", "-c", "touch /test && ls /test"},
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- container, err := cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, "")
- assert.NilError(c, err)
- out, _ := dockerCmd(c, "start", "-a", container.ID)
- assert.Equal(c, strings.TrimSpace(out), "/test")
- }
- func (s *DockerSuite) TestContainerAPICreateEmptyConfig(c *testing.T) {
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- _, err = cli.ContainerCreate(context.Background(), &containertypes.Config{}, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, "")
- expected := "No command specified"
- assert.ErrorContains(c, err, expected)
- }
- func (s *DockerSuite) TestContainerAPICreateMultipleNetworksConfig(c *testing.T) {
- // Container creation must fail if client specified configurations for more than one network
- config := containertypes.Config{
- Image: "busybox",
- }
- networkingConfig := networktypes.NetworkingConfig{
- EndpointsConfig: map[string]*networktypes.EndpointSettings{
- "net1": {},
- "net2": {},
- "net3": {},
- },
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- _, err = cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networkingConfig, "")
- msg := err.Error()
- // network name order in error message is not deterministic
- assert.Assert(c, strings.Contains(msg, "Container cannot be connected to network endpoints"))
- assert.Assert(c, strings.Contains(msg, "net1"))
- assert.Assert(c, strings.Contains(msg, "net2"))
- assert.Assert(c, strings.Contains(msg, "net3"))
- }
- func (s *DockerSuite) TestContainerAPICreateBridgeNetworkMode(c *testing.T) {
- // Windows does not support bridge
- testRequires(c, DaemonIsLinux)
- UtilCreateNetworkMode(c, "bridge")
- }
- func (s *DockerSuite) TestContainerAPICreateOtherNetworkModes(c *testing.T) {
- // Windows does not support these network modes
- testRequires(c, DaemonIsLinux, NotUserNamespace)
- UtilCreateNetworkMode(c, "host")
- UtilCreateNetworkMode(c, "container:web1")
- }
- func UtilCreateNetworkMode(c *testing.T, networkMode containertypes.NetworkMode) {
- config := containertypes.Config{
- Image: "busybox",
- }
- hostConfig := containertypes.HostConfig{
- NetworkMode: networkMode,
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- container, err := cli.ContainerCreate(context.Background(), &config, &hostConfig, &networktypes.NetworkingConfig{}, "")
- assert.NilError(c, err)
- containerJSON, err := cli.ContainerInspect(context.Background(), container.ID)
- assert.NilError(c, err)
- assert.Equal(c, containerJSON.HostConfig.NetworkMode, networkMode, "Mismatched NetworkMode")
- }
- func (s *DockerSuite) TestContainerAPICreateWithCpuSharesCpuset(c *testing.T) {
- // TODO Windows to Windows CI. The CpuShares part could be ported.
- testRequires(c, DaemonIsLinux)
- config := containertypes.Config{
- Image: "busybox",
- }
- hostConfig := containertypes.HostConfig{
- Resources: containertypes.Resources{
- CPUShares: 512,
- CpusetCpus: "0",
- },
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- container, err := cli.ContainerCreate(context.Background(), &config, &hostConfig, &networktypes.NetworkingConfig{}, "")
- assert.NilError(c, err)
- containerJSON, err := cli.ContainerInspect(context.Background(), container.ID)
- assert.NilError(c, err)
- out := inspectField(c, containerJSON.ID, "HostConfig.CpuShares")
- assert.Equal(c, out, "512")
- outCpuset := inspectField(c, containerJSON.ID, "HostConfig.CpusetCpus")
- assert.Equal(c, outCpuset, "0")
- }
- func (s *DockerSuite) TestContainerAPIVerifyHeader(c *testing.T) {
- config := map[string]interface{}{
- "Image": "busybox",
- }
- create := func(ct string) (*http.Response, io.ReadCloser, error) {
- jsonData := bytes.NewBuffer(nil)
- assert.Assert(c, json.NewEncoder(jsonData).Encode(config) == nil)
- return request.Post("/containers/create", request.RawContent(ioutil.NopCloser(jsonData)), request.ContentType(ct))
- }
- // Try with no content-type
- res, body, err := create("")
- assert.NilError(c, err)
- // todo: we need to figure out a better way to compare between dockerd versions
- // comparing between daemon API version is not precise.
- if versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), "1.32") {
- assert.Equal(c, res.StatusCode, http.StatusBadRequest)
- } else {
- assert.Assert(c, res.StatusCode != http.StatusOK)
- }
- body.Close()
- // Try with wrong content-type
- res, body, err = create("application/xml")
- assert.NilError(c, err)
- if versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), "1.32") {
- assert.Equal(c, res.StatusCode, http.StatusBadRequest)
- } else {
- assert.Assert(c, res.StatusCode != http.StatusOK)
- }
- body.Close()
- // now application/json
- res, body, err = create("application/json")
- assert.NilError(c, err)
- assert.Equal(c, res.StatusCode, http.StatusCreated)
- body.Close()
- }
- //Issue 14230. daemon should return 500 for invalid port syntax
- func (s *DockerSuite) TestContainerAPIInvalidPortSyntax(c *testing.T) {
- config := `{
- "Image": "busybox",
- "HostConfig": {
- "NetworkMode": "default",
- "PortBindings": {
- "19039;1230": [
- {}
- ]
- }
- }
- }`
- res, body, err := request.Post("/containers/create", request.RawString(config), request.JSON)
- assert.NilError(c, err)
- if versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), "1.32") {
- assert.Equal(c, res.StatusCode, http.StatusBadRequest)
- } else {
- assert.Assert(c, res.StatusCode != http.StatusOK)
- }
- b, err := request.ReadBody(body)
- assert.NilError(c, err)
- assert.Assert(c, strings.Contains(string(b[:]), "invalid port"))
- }
- func (s *DockerSuite) TestContainerAPIRestartPolicyInvalidPolicyName(c *testing.T) {
- config := `{
- "Image": "busybox",
- "HostConfig": {
- "RestartPolicy": {
- "Name": "something",
- "MaximumRetryCount": 0
- }
- }
- }`
- res, body, err := request.Post("/containers/create", request.RawString(config), request.JSON)
- assert.NilError(c, err)
- if versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), "1.32") {
- assert.Equal(c, res.StatusCode, http.StatusBadRequest)
- } else {
- assert.Assert(c, res.StatusCode != http.StatusOK)
- }
- b, err := request.ReadBody(body)
- assert.NilError(c, err)
- assert.Assert(c, strings.Contains(string(b[:]), "invalid restart policy"))
- }
- func (s *DockerSuite) TestContainerAPIRestartPolicyRetryMismatch(c *testing.T) {
- config := `{
- "Image": "busybox",
- "HostConfig": {
- "RestartPolicy": {
- "Name": "always",
- "MaximumRetryCount": 2
- }
- }
- }`
- res, body, err := request.Post("/containers/create", request.RawString(config), request.JSON)
- assert.NilError(c, err)
- if versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), "1.32") {
- assert.Equal(c, res.StatusCode, http.StatusBadRequest)
- } else {
- assert.Assert(c, res.StatusCode != http.StatusOK)
- }
- b, err := request.ReadBody(body)
- assert.NilError(c, err)
- assert.Assert(c, strings.Contains(string(b[:]), "maximum retry count cannot be used with restart policy"))
- }
- func (s *DockerSuite) TestContainerAPIRestartPolicyNegativeRetryCount(c *testing.T) {
- config := `{
- "Image": "busybox",
- "HostConfig": {
- "RestartPolicy": {
- "Name": "on-failure",
- "MaximumRetryCount": -2
- }
- }
- }`
- res, body, err := request.Post("/containers/create", request.RawString(config), request.JSON)
- assert.NilError(c, err)
- if versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), "1.32") {
- assert.Equal(c, res.StatusCode, http.StatusBadRequest)
- } else {
- assert.Assert(c, res.StatusCode != http.StatusOK)
- }
- b, err := request.ReadBody(body)
- assert.NilError(c, err)
- assert.Assert(c, strings.Contains(string(b[:]), "maximum retry count cannot be negative"))
- }
- func (s *DockerSuite) TestContainerAPIRestartPolicyDefaultRetryCount(c *testing.T) {
- config := `{
- "Image": "busybox",
- "HostConfig": {
- "RestartPolicy": {
- "Name": "on-failure",
- "MaximumRetryCount": 0
- }
- }
- }`
- res, _, err := request.Post("/containers/create", request.RawString(config), request.JSON)
- assert.NilError(c, err)
- assert.Equal(c, res.StatusCode, http.StatusCreated)
- }
- // Issue 7941 - test to make sure a "null" in JSON is just ignored.
- // W/o this fix a null in JSON would be parsed into a string var as "null"
- func (s *DockerSuite) TestContainerAPIPostCreateNull(c *testing.T) {
- config := `{
- "Hostname":"",
- "Domainname":"",
- "Memory":0,
- "MemorySwap":0,
- "CpuShares":0,
- "Cpuset":null,
- "AttachStdin":true,
- "AttachStdout":true,
- "AttachStderr":true,
- "ExposedPorts":{},
- "Tty":true,
- "OpenStdin":true,
- "StdinOnce":true,
- "Env":[],
- "Cmd":"ls",
- "Image":"busybox",
- "Volumes":{},
- "WorkingDir":"",
- "Entrypoint":null,
- "NetworkDisabled":false,
- "OnBuild":null}`
- res, body, err := request.Post("/containers/create", request.RawString(config), request.JSON)
- assert.NilError(c, err)
- assert.Equal(c, res.StatusCode, http.StatusCreated)
- b, err := request.ReadBody(body)
- assert.NilError(c, err)
- type createResp struct {
- ID string
- }
- var container createResp
- assert.Assert(c, json.Unmarshal(b, &container) == nil)
- out := inspectField(c, container.ID, "HostConfig.CpusetCpus")
- assert.Equal(c, out, "")
- outMemory := inspectField(c, container.ID, "HostConfig.Memory")
- assert.Equal(c, outMemory, "0")
- outMemorySwap := inspectField(c, container.ID, "HostConfig.MemorySwap")
- assert.Equal(c, outMemorySwap, "0")
- }
- func (s *DockerSuite) TestCreateWithTooLowMemoryLimit(c *testing.T) {
- // TODO Windows: Port once memory is supported
- testRequires(c, DaemonIsLinux)
- config := `{
- "Image": "busybox",
- "Cmd": "ls",
- "OpenStdin": true,
- "CpuShares": 100,
- "Memory": 524287
- }`
- res, body, err := request.Post("/containers/create", request.RawString(config), request.JSON)
- assert.NilError(c, err)
- b, err2 := request.ReadBody(body)
- assert.Assert(c, err2 == nil)
- if versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), "1.32") {
- assert.Equal(c, res.StatusCode, http.StatusBadRequest)
- } else {
- assert.Assert(c, res.StatusCode != http.StatusOK)
- }
- assert.Assert(c, strings.Contains(string(b), "Minimum memory limit allowed is 4MB"))
- }
- func (s *DockerSuite) TestContainerAPIRename(c *testing.T) {
- out, _ := dockerCmd(c, "run", "--name", "TestContainerAPIRename", "-d", "busybox", "sh")
- containerID := strings.TrimSpace(out)
- newName := "TestContainerAPIRenameNew"
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- err = cli.ContainerRename(context.Background(), containerID, newName)
- assert.NilError(c, err)
- name := inspectField(c, containerID, "Name")
- assert.Equal(c, name, "/"+newName, "Failed to rename container")
- }
- func (s *DockerSuite) TestContainerAPIKill(c *testing.T) {
- name := "test-api-kill"
- runSleepingContainer(c, "-i", "--name", name)
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- err = cli.ContainerKill(context.Background(), name, "SIGKILL")
- assert.NilError(c, err)
- state := inspectField(c, name, "State.Running")
- assert.Equal(c, state, "false", fmt.Sprintf("got wrong State from container %s: %q", name, state))
- }
- func (s *DockerSuite) TestContainerAPIRestart(c *testing.T) {
- name := "test-api-restart"
- runSleepingContainer(c, "-di", "--name", name)
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- timeout := 1 * time.Second
- err = cli.ContainerRestart(context.Background(), name, &timeout)
- assert.NilError(c, err)
- assert.Assert(c, waitInspect(name, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 15*time.Second) == nil)
- }
- func (s *DockerSuite) TestContainerAPIRestartNotimeoutParam(c *testing.T) {
- name := "test-api-restart-no-timeout-param"
- out := runSleepingContainer(c, "-di", "--name", name)
- id := strings.TrimSpace(out)
- assert.NilError(c, waitRun(id))
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- err = cli.ContainerRestart(context.Background(), name, nil)
- assert.NilError(c, err)
- assert.Assert(c, waitInspect(name, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 15*time.Second) == nil)
- }
- func (s *DockerSuite) TestContainerAPIStart(c *testing.T) {
- name := "testing-start"
- config := containertypes.Config{
- Image: "busybox",
- Cmd: append([]string{"/bin/sh", "-c"}, sleepCommandForDaemonPlatform()...),
- OpenStdin: true,
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- _, err = cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, name)
- assert.NilError(c, err)
- err = cli.ContainerStart(context.Background(), name, types.ContainerStartOptions{})
- assert.NilError(c, err)
- // second call to start should give 304
- // maybe add ContainerStartWithRaw to test it
- err = cli.ContainerStart(context.Background(), name, types.ContainerStartOptions{})
- assert.NilError(c, err)
- // TODO(tibor): figure out why this doesn't work on windows
- }
- func (s *DockerSuite) TestContainerAPIStop(c *testing.T) {
- name := "test-api-stop"
- runSleepingContainer(c, "-i", "--name", name)
- timeout := 30 * time.Second
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- err = cli.ContainerStop(context.Background(), name, &timeout)
- assert.NilError(c, err)
- assert.Assert(c, waitInspect(name, "{{ .State.Running }}", "false", 60*time.Second) == nil)
- // second call to start should give 304
- // maybe add ContainerStartWithRaw to test it
- err = cli.ContainerStop(context.Background(), name, &timeout)
- assert.NilError(c, err)
- }
- func (s *DockerSuite) TestContainerAPIWait(c *testing.T) {
- name := "test-api-wait"
- sleepCmd := "/bin/sleep"
- if testEnv.OSType == "windows" {
- sleepCmd = "sleep"
- }
- dockerCmd(c, "run", "--name", name, "busybox", sleepCmd, "2")
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- waitresC, errC := cli.ContainerWait(context.Background(), name, "")
- select {
- case err = <-errC:
- assert.NilError(c, err)
- case waitres := <-waitresC:
- assert.Equal(c, waitres.StatusCode, int64(0))
- }
- }
- func (s *DockerSuite) TestContainerAPICopyNotExistsAnyMore(c *testing.T) {
- name := "test-container-api-copy"
- dockerCmd(c, "run", "--name", name, "busybox", "touch", "/test.txt")
- postData := types.CopyConfig{
- Resource: "/test.txt",
- }
- // no copy in client/
- res, _, err := request.Post("/containers/"+name+"/copy", request.JSONBody(postData))
- assert.NilError(c, err)
- assert.Equal(c, res.StatusCode, http.StatusNotFound)
- }
- func (s *DockerSuite) TestContainerAPICopyPre124(c *testing.T) {
- testRequires(c, DaemonIsLinux) // Windows only supports 1.25 or later
- name := "test-container-api-copy"
- dockerCmd(c, "run", "--name", name, "busybox", "touch", "/test.txt")
- postData := types.CopyConfig{
- Resource: "/test.txt",
- }
- res, body, err := request.Post("/v1.23/containers/"+name+"/copy", request.JSONBody(postData))
- assert.NilError(c, err)
- assert.Equal(c, res.StatusCode, http.StatusOK)
- found := false
- for tarReader := tar.NewReader(body); ; {
- h, err := tarReader.Next()
- if err != nil {
- if err == io.EOF {
- break
- }
- c.Fatal(err)
- }
- if h.Name == "test.txt" {
- found = true
- break
- }
- }
- assert.Assert(c, found)
- }
- func (s *DockerSuite) TestContainerAPICopyResourcePathEmptyPre124(c *testing.T) {
- testRequires(c, DaemonIsLinux) // Windows only supports 1.25 or later
- name := "test-container-api-copy-resource-empty"
- dockerCmd(c, "run", "--name", name, "busybox", "touch", "/test.txt")
- postData := types.CopyConfig{
- Resource: "",
- }
- res, body, err := request.Post("/v1.23/containers/"+name+"/copy", request.JSONBody(postData))
- assert.NilError(c, err)
- if versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), "1.32") {
- assert.Equal(c, res.StatusCode, http.StatusBadRequest)
- } else {
- assert.Assert(c, res.StatusCode != http.StatusOK)
- }
- b, err := request.ReadBody(body)
- assert.NilError(c, err)
- assert.Assert(c, is.Regexp("^Path cannot be empty\n$", string(b)))
- }
- func (s *DockerSuite) TestContainerAPICopyResourcePathNotFoundPre124(c *testing.T) {
- testRequires(c, DaemonIsLinux) // Windows only supports 1.25 or later
- name := "test-container-api-copy-resource-not-found"
- dockerCmd(c, "run", "--name", name, "busybox")
- postData := types.CopyConfig{
- Resource: "/notexist",
- }
- res, body, err := request.Post("/v1.23/containers/"+name+"/copy", request.JSONBody(postData))
- assert.NilError(c, err)
- if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
- assert.Equal(c, res.StatusCode, http.StatusInternalServerError)
- } else {
- assert.Equal(c, res.StatusCode, http.StatusNotFound)
- }
- b, err := request.ReadBody(body)
- assert.NilError(c, err)
- assert.Assert(c, is.Regexp("^Could not find the file /notexist in container "+name+"\n$", string(b)))
- }
- func (s *DockerSuite) TestContainerAPICopyContainerNotFoundPr124(c *testing.T) {
- testRequires(c, DaemonIsLinux) // Windows only supports 1.25 or later
- postData := types.CopyConfig{
- Resource: "/something",
- }
- res, _, err := request.Post("/v1.23/containers/notexists/copy", request.JSONBody(postData))
- assert.NilError(c, err)
- assert.Equal(c, res.StatusCode, http.StatusNotFound)
- }
- func (s *DockerSuite) TestContainerAPIDelete(c *testing.T) {
- out := runSleepingContainer(c)
- id := strings.TrimSpace(out)
- assert.NilError(c, waitRun(id))
- dockerCmd(c, "stop", id)
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- err = cli.ContainerRemove(context.Background(), id, types.ContainerRemoveOptions{})
- assert.NilError(c, err)
- }
- func (s *DockerSuite) TestContainerAPIDeleteNotExist(c *testing.T) {
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- err = cli.ContainerRemove(context.Background(), "doesnotexist", types.ContainerRemoveOptions{})
- assert.ErrorContains(c, err, "No such container: doesnotexist")
- }
- func (s *DockerSuite) TestContainerAPIDeleteForce(c *testing.T) {
- out := runSleepingContainer(c)
- id := strings.TrimSpace(out)
- assert.NilError(c, waitRun(id))
- removeOptions := types.ContainerRemoveOptions{
- Force: true,
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- err = cli.ContainerRemove(context.Background(), id, removeOptions)
- assert.NilError(c, err)
- }
- func (s *DockerSuite) TestContainerAPIDeleteRemoveLinks(c *testing.T) {
- // Windows does not support links
- testRequires(c, DaemonIsLinux)
- out, _ := dockerCmd(c, "run", "-d", "--name", "tlink1", "busybox", "top")
- id := strings.TrimSpace(out)
- assert.NilError(c, waitRun(id))
- out, _ = dockerCmd(c, "run", "--link", "tlink1:tlink1", "--name", "tlink2", "-d", "busybox", "top")
- id2 := strings.TrimSpace(out)
- assert.Assert(c, waitRun(id2) == nil)
- links := inspectFieldJSON(c, id2, "HostConfig.Links")
- assert.Equal(c, links, "[\"/tlink1:/tlink2/tlink1\"]", "expected to have links between containers")
- removeOptions := types.ContainerRemoveOptions{
- RemoveLinks: true,
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- err = cli.ContainerRemove(context.Background(), "tlink2/tlink1", removeOptions)
- assert.NilError(c, err)
- linksPostRm := inspectFieldJSON(c, id2, "HostConfig.Links")
- assert.Equal(c, linksPostRm, "null", "call to api deleteContainer links should have removed the specified links")
- }
- func (s *DockerSuite) TestContainerAPIDeleteConflict(c *testing.T) {
- out := runSleepingContainer(c)
- id := strings.TrimSpace(out)
- assert.NilError(c, waitRun(id))
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- err = cli.ContainerRemove(context.Background(), id, types.ContainerRemoveOptions{})
- expected := "cannot remove a running container"
- assert.ErrorContains(c, err, expected)
- }
- func (s *DockerSuite) TestContainerAPIDeleteRemoveVolume(c *testing.T) {
- testRequires(c, testEnv.IsLocalDaemon)
- vol := "/testvolume"
- if testEnv.OSType == "windows" {
- vol = `c:\testvolume`
- }
- out := runSleepingContainer(c, "-v", vol)
- id := strings.TrimSpace(out)
- assert.NilError(c, waitRun(id))
- source, err := inspectMountSourceField(id, vol)
- assert.NilError(c, err)
- _, err = os.Stat(source)
- assert.NilError(c, err)
- removeOptions := types.ContainerRemoveOptions{
- Force: true,
- RemoveVolumes: true,
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- err = cli.ContainerRemove(context.Background(), id, removeOptions)
- assert.NilError(c, err)
- _, err = os.Stat(source)
- assert.Assert(c, os.IsNotExist(err), "expected to get ErrNotExist error, got %v", err)
- }
- // Regression test for https://github.com/docker/docker/issues/6231
- func (s *DockerSuite) TestContainerAPIChunkedEncoding(c *testing.T) {
- config := map[string]interface{}{
- "Image": "busybox",
- "Cmd": append([]string{"/bin/sh", "-c"}, sleepCommandForDaemonPlatform()...),
- "OpenStdin": true,
- }
- resp, _, err := request.Post("/containers/create", request.JSONBody(config), request.With(func(req *http.Request) error {
- // This is a cheat to make the http request do chunked encoding
- // Otherwise (just setting the Content-Encoding to chunked) net/http will overwrite
- // https://golang.org/src/pkg/net/http/request.go?s=11980:12172
- req.ContentLength = -1
- return nil
- }))
- assert.Assert(c, err == nil, "error creating container with chunked encoding")
- defer resp.Body.Close()
- assert.Equal(c, resp.StatusCode, http.StatusCreated)
- }
- func (s *DockerSuite) TestContainerAPIPostContainerStop(c *testing.T) {
- out := runSleepingContainer(c)
- containerID := strings.TrimSpace(out)
- assert.Assert(c, waitRun(containerID) == nil)
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- err = cli.ContainerStop(context.Background(), containerID, nil)
- assert.NilError(c, err)
- assert.Assert(c, waitInspect(containerID, "{{ .State.Running }}", "false", 60*time.Second) == nil)
- }
- // #14170
- func (s *DockerSuite) TestPostContainerAPICreateWithStringOrSliceEntrypoint(c *testing.T) {
- config := containertypes.Config{
- Image: "busybox",
- Entrypoint: []string{"echo"},
- Cmd: []string{"hello", "world"},
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- _, err = cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, "echotest")
- assert.NilError(c, err)
- out, _ := dockerCmd(c, "start", "-a", "echotest")
- assert.Equal(c, strings.TrimSpace(out), "hello world")
- config2 := struct {
- Image string
- Entrypoint string
- Cmd []string
- }{"busybox", "echo", []string{"hello", "world"}}
- _, _, err = request.Post("/containers/create?name=echotest2", request.JSONBody(config2))
- assert.NilError(c, err)
- out, _ = dockerCmd(c, "start", "-a", "echotest2")
- assert.Equal(c, strings.TrimSpace(out), "hello world")
- }
- // #14170
- func (s *DockerSuite) TestPostContainersCreateWithStringOrSliceCmd(c *testing.T) {
- config := containertypes.Config{
- Image: "busybox",
- Cmd: []string{"echo", "hello", "world"},
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- _, err = cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, "echotest")
- assert.NilError(c, err)
- out, _ := dockerCmd(c, "start", "-a", "echotest")
- assert.Equal(c, strings.TrimSpace(out), "hello world")
- config2 := struct {
- Image string
- Entrypoint string
- Cmd string
- }{"busybox", "echo", "hello world"}
- _, _, err = request.Post("/containers/create?name=echotest2", request.JSONBody(config2))
- assert.NilError(c, err)
- out, _ = dockerCmd(c, "start", "-a", "echotest2")
- assert.Equal(c, strings.TrimSpace(out), "hello world")
- }
- // regression #14318
- // for backward compatibility testing with and without CAP_ prefix
- // and with upper and lowercase
- func (s *DockerSuite) TestPostContainersCreateWithStringOrSliceCapAddDrop(c *testing.T) {
- // Windows doesn't support CapAdd/CapDrop
- testRequires(c, DaemonIsLinux)
- config := struct {
- Image string
- CapAdd string
- CapDrop string
- }{"busybox", "NET_ADMIN", "cap_sys_admin"}
- res, _, err := request.Post("/containers/create?name=capaddtest0", request.JSONBody(config))
- assert.NilError(c, err)
- assert.Equal(c, res.StatusCode, http.StatusCreated)
- config2 := containertypes.Config{
- Image: "busybox",
- }
- hostConfig := containertypes.HostConfig{
- CapAdd: []string{"net_admin", "SYS_ADMIN"},
- CapDrop: []string{"SETGID", "CAP_SETPCAP"},
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- _, err = cli.ContainerCreate(context.Background(), &config2, &hostConfig, &networktypes.NetworkingConfig{}, "capaddtest1")
- assert.NilError(c, err)
- }
- // #14915
- func (s *DockerSuite) TestContainerAPICreateNoHostConfig118(c *testing.T) {
- testRequires(c, DaemonIsLinux) // Windows only support 1.25 or later
- config := containertypes.Config{
- Image: "busybox",
- }
- cli, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion("v1.18"))
- assert.NilError(c, err)
- _, err = cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, "")
- assert.NilError(c, err)
- }
- // Ensure an error occurs when you have a container read-only rootfs but you
- // extract an archive to a symlink in a writable volume which points to a
- // directory outside of the volume.
- func (s *DockerSuite) TestPutContainerArchiveErrSymlinkInVolumeToReadOnlyRootfs(c *testing.T) {
- // Windows does not support read-only rootfs
- // Requires local volume mount bind.
- // --read-only + userns has remount issues
- testRequires(c, testEnv.IsLocalDaemon, NotUserNamespace, DaemonIsLinux)
- testVol := getTestDir(c, "test-put-container-archive-err-symlink-in-volume-to-read-only-rootfs-")
- defer os.RemoveAll(testVol)
- makeTestContentInDir(c, testVol)
- cID := makeTestContainer(c, testContainerOptions{
- readOnly: true,
- volumes: defaultVolumes(testVol), // Our bind mount is at /vol2
- })
- // Attempt to extract to a symlink in the volume which points to a
- // directory outside the volume. This should cause an error because the
- // rootfs is read-only.
- cli, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion("v1.20"))
- assert.NilError(c, err)
- err = cli.CopyToContainer(context.Background(), cID, "/vol2/symlinkToAbsDir", nil, types.CopyToContainerOptions{})
- assert.ErrorContains(c, err, "container rootfs is marked read-only")
- }
- func (s *DockerSuite) TestPostContainersCreateWithWrongCpusetValues(c *testing.T) {
- // Not supported on Windows
- testRequires(c, DaemonIsLinux)
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- config := containertypes.Config{
- Image: "busybox",
- }
- hostConfig1 := containertypes.HostConfig{
- Resources: containertypes.Resources{
- CpusetCpus: "1-42,,",
- },
- }
- name := "wrong-cpuset-cpus"
- _, err = cli.ContainerCreate(context.Background(), &config, &hostConfig1, &networktypes.NetworkingConfig{}, name)
- expected := "Invalid value 1-42,, for cpuset cpus"
- assert.ErrorContains(c, err, expected)
- hostConfig2 := containertypes.HostConfig{
- Resources: containertypes.Resources{
- CpusetMems: "42-3,1--",
- },
- }
- name = "wrong-cpuset-mems"
- _, err = cli.ContainerCreate(context.Background(), &config, &hostConfig2, &networktypes.NetworkingConfig{}, name)
- expected = "Invalid value 42-3,1-- for cpuset mems"
- assert.ErrorContains(c, err, expected)
- }
- func (s *DockerSuite) TestPostContainersCreateShmSizeNegative(c *testing.T) {
- // ShmSize is not supported on Windows
- testRequires(c, DaemonIsLinux)
- config := containertypes.Config{
- Image: "busybox",
- }
- hostConfig := containertypes.HostConfig{
- ShmSize: -1,
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- _, err = cli.ContainerCreate(context.Background(), &config, &hostConfig, &networktypes.NetworkingConfig{}, "")
- assert.ErrorContains(c, err, "SHM size can not be less than 0")
- }
- func (s *DockerSuite) TestPostContainersCreateShmSizeHostConfigOmitted(c *testing.T) {
- // ShmSize is not supported on Windows
- testRequires(c, DaemonIsLinux)
- var defaultSHMSize int64 = 67108864
- config := containertypes.Config{
- Image: "busybox",
- Cmd: []string{"mount"},
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- container, err := cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, "")
- assert.NilError(c, err)
- containerJSON, err := cli.ContainerInspect(context.Background(), container.ID)
- assert.NilError(c, err)
- assert.Equal(c, containerJSON.HostConfig.ShmSize, defaultSHMSize)
- out, _ := dockerCmd(c, "start", "-i", containerJSON.ID)
- shmRegexp := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=65536k`)
- if !shmRegexp.MatchString(out) {
- c.Fatalf("Expected shm of 64MB in mount command, got %v", out)
- }
- }
- func (s *DockerSuite) TestPostContainersCreateShmSizeOmitted(c *testing.T) {
- // ShmSize is not supported on Windows
- testRequires(c, DaemonIsLinux)
- config := containertypes.Config{
- Image: "busybox",
- Cmd: []string{"mount"},
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- container, err := cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, "")
- assert.NilError(c, err)
- containerJSON, err := cli.ContainerInspect(context.Background(), container.ID)
- assert.NilError(c, err)
- assert.Equal(c, containerJSON.HostConfig.ShmSize, int64(67108864))
- out, _ := dockerCmd(c, "start", "-i", containerJSON.ID)
- shmRegexp := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=65536k`)
- if !shmRegexp.MatchString(out) {
- c.Fatalf("Expected shm of 64MB in mount command, got %v", out)
- }
- }
- func (s *DockerSuite) TestPostContainersCreateWithShmSize(c *testing.T) {
- // ShmSize is not supported on Windows
- testRequires(c, DaemonIsLinux)
- config := containertypes.Config{
- Image: "busybox",
- Cmd: []string{"mount"},
- }
- hostConfig := containertypes.HostConfig{
- ShmSize: 1073741824,
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- container, err := cli.ContainerCreate(context.Background(), &config, &hostConfig, &networktypes.NetworkingConfig{}, "")
- assert.NilError(c, err)
- containerJSON, err := cli.ContainerInspect(context.Background(), container.ID)
- assert.NilError(c, err)
- assert.Equal(c, containerJSON.HostConfig.ShmSize, int64(1073741824))
- out, _ := dockerCmd(c, "start", "-i", containerJSON.ID)
- shmRegex := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=1048576k`)
- if !shmRegex.MatchString(out) {
- c.Fatalf("Expected shm of 1GB in mount command, got %v", out)
- }
- }
- func (s *DockerSuite) TestPostContainersCreateMemorySwappinessHostConfigOmitted(c *testing.T) {
- // Swappiness is not supported on Windows
- testRequires(c, DaemonIsLinux)
- config := containertypes.Config{
- Image: "busybox",
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- container, err := cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, "")
- assert.NilError(c, err)
- containerJSON, err := cli.ContainerInspect(context.Background(), container.ID)
- assert.NilError(c, err)
- if versions.LessThan(testEnv.DaemonAPIVersion(), "1.31") {
- assert.Equal(c, *containerJSON.HostConfig.MemorySwappiness, int64(-1))
- } else {
- assert.Assert(c, containerJSON.HostConfig.MemorySwappiness == nil)
- }
- }
- // check validation is done daemon side and not only in cli
- func (s *DockerSuite) TestPostContainersCreateWithOomScoreAdjInvalidRange(c *testing.T) {
- // OomScoreAdj is not supported on Windows
- testRequires(c, DaemonIsLinux)
- config := containertypes.Config{
- Image: "busybox",
- }
- hostConfig := containertypes.HostConfig{
- OomScoreAdj: 1001,
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- name := "oomscoreadj-over"
- _, err = cli.ContainerCreate(context.Background(), &config, &hostConfig, &networktypes.NetworkingConfig{}, name)
- expected := "Invalid value 1001, range for oom score adj is [-1000, 1000]"
- assert.ErrorContains(c, err, expected)
- hostConfig = containertypes.HostConfig{
- OomScoreAdj: -1001,
- }
- name = "oomscoreadj-low"
- _, err = cli.ContainerCreate(context.Background(), &config, &hostConfig, &networktypes.NetworkingConfig{}, name)
- expected = "Invalid value -1001, range for oom score adj is [-1000, 1000]"
- assert.ErrorContains(c, err, expected)
- }
- // test case for #22210 where an empty container name caused panic.
- func (s *DockerSuite) TestContainerAPIDeleteWithEmptyName(c *testing.T) {
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- err = cli.ContainerRemove(context.Background(), "", types.ContainerRemoveOptions{})
- assert.ErrorContains(c, err, "No such container")
- }
- func (s *DockerSuite) TestContainerAPIStatsWithNetworkDisabled(c *testing.T) {
- // Problematic on Windows as Windows does not support stats
- testRequires(c, DaemonIsLinux)
- name := "testing-network-disabled"
- config := containertypes.Config{
- Image: "busybox",
- Cmd: []string{"top"},
- NetworkDisabled: true,
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- _, err = cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, name)
- assert.NilError(c, err)
- err = cli.ContainerStart(context.Background(), name, types.ContainerStartOptions{})
- assert.NilError(c, err)
- assert.Assert(c, waitRun(name) == nil)
- type b struct {
- stats types.ContainerStats
- err error
- }
- bc := make(chan b, 1)
- go func() {
- stats, err := cli.ContainerStats(context.Background(), name, false)
- bc <- b{stats, err}
- }()
- // allow some time to stream the stats from the container
- time.Sleep(4 * time.Second)
- dockerCmd(c, "rm", "-f", name)
- // collect the results from the stats stream or timeout and fail
- // if the stream was not disconnected.
- select {
- case <-time.After(2 * time.Second):
- c.Fatal("stream was not closed after container was removed")
- case sr := <-bc:
- assert.Assert(c, sr.err == nil)
- sr.stats.Body.Close()
- }
- }
- func (s *DockerSuite) TestContainersAPICreateMountsValidation(c *testing.T) {
- type testCase struct {
- config containertypes.Config
- hostConfig containertypes.HostConfig
- msg string
- }
- prefix, slash := getPrefixAndSlashFromDaemonPlatform()
- destPath := prefix + slash + "foo"
- notExistPath := prefix + slash + "notexist"
- cases := []testCase{
- {
- config: containertypes.Config{
- Image: "busybox",
- },
- hostConfig: containertypes.HostConfig{
- Mounts: []mounttypes.Mount{{
- Type: "notreal",
- Target: destPath,
- },
- },
- },
- msg: "mount type unknown",
- },
- {
- config: containertypes.Config{
- Image: "busybox",
- },
- hostConfig: containertypes.HostConfig{
- Mounts: []mounttypes.Mount{{
- Type: "bind"}}},
- msg: "Target must not be empty",
- },
- {
- config: containertypes.Config{
- Image: "busybox",
- },
- hostConfig: containertypes.HostConfig{
- Mounts: []mounttypes.Mount{{
- Type: "bind",
- Target: destPath}}},
- msg: "Source must not be empty",
- },
- {
- config: containertypes.Config{
- Image: "busybox",
- },
- hostConfig: containertypes.HostConfig{
- Mounts: []mounttypes.Mount{{
- Type: "bind",
- Source: notExistPath,
- Target: destPath}}},
- msg: "source path does not exist",
- // FIXME(vdemeester) fails into e2e, migrate to integration/container anyway
- // msg: "source path does not exist: " + notExistPath,
- },
- {
- config: containertypes.Config{
- Image: "busybox",
- },
- hostConfig: containertypes.HostConfig{
- Mounts: []mounttypes.Mount{{
- Type: "volume"}}},
- msg: "Target must not be empty",
- },
- {
- config: containertypes.Config{
- Image: "busybox",
- },
- hostConfig: containertypes.HostConfig{
- Mounts: []mounttypes.Mount{{
- Type: "volume",
- Source: "hello",
- Target: destPath}}},
- msg: "",
- },
- {
- config: containertypes.Config{
- Image: "busybox",
- },
- hostConfig: containertypes.HostConfig{
- Mounts: []mounttypes.Mount{{
- Type: "volume",
- Source: "hello2",
- Target: destPath,
- VolumeOptions: &mounttypes.VolumeOptions{
- DriverConfig: &mounttypes.Driver{
- Name: "local"}}}}},
- msg: "",
- },
- }
- if testEnv.IsLocalDaemon() {
- tmpDir, err := ioutils.TempDir("", "test-mounts-api")
- assert.NilError(c, err)
- defer os.RemoveAll(tmpDir)
- cases = append(cases, []testCase{
- {
- config: containertypes.Config{
- Image: "busybox",
- },
- hostConfig: containertypes.HostConfig{
- Mounts: []mounttypes.Mount{{
- Type: "bind",
- Source: tmpDir,
- Target: destPath}}},
- msg: "",
- },
- {
- config: containertypes.Config{
- Image: "busybox",
- },
- hostConfig: containertypes.HostConfig{
- Mounts: []mounttypes.Mount{{
- Type: "bind",
- Source: tmpDir,
- Target: destPath,
- VolumeOptions: &mounttypes.VolumeOptions{}}}},
- msg: "VolumeOptions must not be specified",
- },
- }...)
- }
- if DaemonIsWindows() {
- cases = append(cases, []testCase{
- {
- config: containertypes.Config{
- Image: "busybox",
- },
- hostConfig: containertypes.HostConfig{
- Mounts: []mounttypes.Mount{
- {
- Type: "volume",
- Source: "not-supported-on-windows",
- Target: destPath,
- VolumeOptions: &mounttypes.VolumeOptions{
- DriverConfig: &mounttypes.Driver{
- Name: "local",
- Options: map[string]string{"type": "tmpfs"},
- },
- },
- },
- },
- },
- msg: `options are not supported on this platform`,
- },
- }...)
- }
- if DaemonIsLinux() {
- cases = append(cases, []testCase{
- {
- config: containertypes.Config{
- Image: "busybox",
- },
- hostConfig: containertypes.HostConfig{
- Mounts: []mounttypes.Mount{
- {
- Type: "volume",
- Source: "missing-device-opt",
- Target: destPath,
- VolumeOptions: &mounttypes.VolumeOptions{
- DriverConfig: &mounttypes.Driver{
- Name: "local",
- Options: map[string]string{"foobar": "foobaz"},
- },
- },
- },
- },
- },
- msg: `invalid option: "foobar"`,
- },
- {
- config: containertypes.Config{
- Image: "busybox",
- },
- hostConfig: containertypes.HostConfig{
- Mounts: []mounttypes.Mount{
- {
- Type: "volume",
- Source: "missing-device-opt",
- Target: destPath,
- VolumeOptions: &mounttypes.VolumeOptions{
- DriverConfig: &mounttypes.Driver{
- Name: "local",
- Options: map[string]string{"type": "tmpfs"},
- },
- },
- },
- },
- },
- msg: `missing required option: "device"`,
- },
- {
- config: containertypes.Config{
- Image: "busybox",
- },
- hostConfig: containertypes.HostConfig{
- Mounts: []mounttypes.Mount{
- {
- Type: "volume",
- Source: "missing-type-opt",
- Target: destPath,
- VolumeOptions: &mounttypes.VolumeOptions{
- DriverConfig: &mounttypes.Driver{
- Name: "local",
- Options: map[string]string{"device": "tmpfs"},
- },
- },
- },
- },
- },
- msg: `missing required option: "type"`,
- },
- {
- config: containertypes.Config{
- Image: "busybox",
- },
- hostConfig: containertypes.HostConfig{
- Mounts: []mounttypes.Mount{
- {
- Type: "volume",
- Source: "hello4",
- Target: destPath,
- VolumeOptions: &mounttypes.VolumeOptions{
- DriverConfig: &mounttypes.Driver{
- Name: "local",
- Options: map[string]string{"o": "size=1", "type": "tmpfs", "device": "tmpfs"},
- },
- },
- },
- },
- },
- msg: "",
- },
- {
- config: containertypes.Config{
- Image: "busybox",
- },
- hostConfig: containertypes.HostConfig{
- Mounts: []mounttypes.Mount{{
- Type: "tmpfs",
- Target: destPath}}},
- msg: "",
- },
- {
- config: containertypes.Config{
- Image: "busybox",
- },
- hostConfig: containertypes.HostConfig{
- Mounts: []mounttypes.Mount{{
- Type: "tmpfs",
- Target: destPath,
- TmpfsOptions: &mounttypes.TmpfsOptions{
- SizeBytes: 4096 * 1024,
- Mode: 0700,
- }}}},
- msg: "",
- },
- {
- config: containertypes.Config{
- Image: "busybox",
- },
- hostConfig: containertypes.HostConfig{
- Mounts: []mounttypes.Mount{{
- Type: "tmpfs",
- Source: "/shouldnotbespecified",
- Target: destPath}}},
- msg: "Source must not be specified",
- },
- }...)
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- // TODO add checks for statuscode returned by API
- for i, x := range cases {
- c.Logf("case %d", i)
- _, err = cli.ContainerCreate(context.Background(), &x.config, &x.hostConfig, &networktypes.NetworkingConfig{}, "")
- if len(x.msg) > 0 {
- assert.ErrorContains(c, err, x.msg, "%v", cases[i].config)
- } else {
- assert.NilError(c, err)
- }
- }
- }
- func (s *DockerSuite) TestContainerAPICreateMountsBindRead(c *testing.T) {
- testRequires(c, NotUserNamespace, testEnv.IsLocalDaemon)
- // also with data in the host side
- prefix, slash := getPrefixAndSlashFromDaemonPlatform()
- destPath := prefix + slash + "foo"
- tmpDir, err := ioutil.TempDir("", "test-mounts-api-bind")
- assert.NilError(c, err)
- defer os.RemoveAll(tmpDir)
- err = ioutil.WriteFile(filepath.Join(tmpDir, "bar"), []byte("hello"), 0666)
- assert.NilError(c, err)
- config := containertypes.Config{
- Image: "busybox",
- Cmd: []string{"/bin/sh", "-c", "cat /foo/bar"},
- }
- hostConfig := containertypes.HostConfig{
- Mounts: []mounttypes.Mount{
- {Type: "bind", Source: tmpDir, Target: destPath},
- },
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- _, err = cli.ContainerCreate(context.Background(), &config, &hostConfig, &networktypes.NetworkingConfig{}, "test")
- assert.NilError(c, err)
- out, _ := dockerCmd(c, "start", "-a", "test")
- assert.Equal(c, out, "hello")
- }
- // Test Mounts comes out as expected for the MountPoint
- func (s *DockerSuite) TestContainersAPICreateMountsCreate(c *testing.T) {
- prefix, slash := getPrefixAndSlashFromDaemonPlatform()
- destPath := prefix + slash + "foo"
- var (
- testImg string
- )
- if testEnv.OSType != "windows" {
- testImg = "test-mount-config"
- buildImageSuccessfully(c, testImg, build.WithDockerfile(`
- FROM busybox
- RUN mkdir `+destPath+` && touch `+destPath+slash+`bar
- CMD cat `+destPath+slash+`bar
- `))
- } else {
- testImg = "busybox"
- }
- type testCase struct {
- spec mounttypes.Mount
- expected types.MountPoint
- }
- var selinuxSharedLabel string
- // this test label was added after a bug fix in 1.32, thus add requirements min API >= 1.32
- // for the sake of making test pass in earlier versions
- // bug fixed in https://github.com/moby/moby/pull/34684
- if !versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
- if runtime.GOOS == "linux" {
- selinuxSharedLabel = "z"
- }
- }
- cases := []testCase{
- // use literal strings here for `Type` instead of the defined constants in the volume package to keep this honest
- // Validation of the actual `Mount` struct is done in another test is not needed here
- {
- spec: mounttypes.Mount{Type: "volume", Target: destPath},
- expected: types.MountPoint{Driver: volume.DefaultDriverName, Type: "volume", RW: true, Destination: destPath, Mode: selinuxSharedLabel},
- },
- {
- spec: mounttypes.Mount{Type: "volume", Target: destPath + slash},
- expected: types.MountPoint{Driver: volume.DefaultDriverName, Type: "volume", RW: true, Destination: destPath, Mode: selinuxSharedLabel},
- },
- {
- spec: mounttypes.Mount{Type: "volume", Target: destPath, Source: "test1"},
- expected: types.MountPoint{Type: "volume", Name: "test1", RW: true, Destination: destPath, Mode: selinuxSharedLabel},
- },
- {
- spec: mounttypes.Mount{Type: "volume", Target: destPath, ReadOnly: true, Source: "test2"},
- expected: types.MountPoint{Type: "volume", Name: "test2", RW: false, Destination: destPath, Mode: selinuxSharedLabel},
- },
- {
- spec: mounttypes.Mount{Type: "volume", Target: destPath, Source: "test3", VolumeOptions: &mounttypes.VolumeOptions{DriverConfig: &mounttypes.Driver{Name: volume.DefaultDriverName}}},
- expected: types.MountPoint{Driver: volume.DefaultDriverName, Type: "volume", Name: "test3", RW: true, Destination: destPath, Mode: selinuxSharedLabel},
- },
- }
- if testEnv.IsLocalDaemon() {
- // setup temp dir for testing binds
- tmpDir1, err := ioutil.TempDir("", "test-mounts-api-1")
- assert.NilError(c, err)
- defer os.RemoveAll(tmpDir1)
- cases = append(cases, []testCase{
- {
- spec: mounttypes.Mount{
- Type: "bind",
- Source: tmpDir1,
- Target: destPath,
- },
- expected: types.MountPoint{
- Type: "bind",
- RW: true,
- Destination: destPath,
- Source: tmpDir1,
- },
- },
- {
- spec: mounttypes.Mount{Type: "bind", Source: tmpDir1, Target: destPath, ReadOnly: true},
- expected: types.MountPoint{Type: "bind", RW: false, Destination: destPath, Source: tmpDir1},
- },
- }...)
- // for modes only supported on Linux
- if DaemonIsLinux() {
- tmpDir3, err := ioutils.TempDir("", "test-mounts-api-3")
- assert.NilError(c, err)
- defer os.RemoveAll(tmpDir3)
- assert.Assert(c, mount.Mount(tmpDir3, tmpDir3, "none", "bind,shared") == nil)
- cases = append(cases, []testCase{
- {
- spec: mounttypes.Mount{Type: "bind", Source: tmpDir3, Target: destPath},
- expected: types.MountPoint{Type: "bind", RW: true, Destination: destPath, Source: tmpDir3},
- },
- {
- spec: mounttypes.Mount{Type: "bind", Source: tmpDir3, Target: destPath, ReadOnly: true},
- expected: types.MountPoint{Type: "bind", RW: false, Destination: destPath, Source: tmpDir3},
- },
- {
- spec: mounttypes.Mount{Type: "bind", Source: tmpDir3, Target: destPath, ReadOnly: true, BindOptions: &mounttypes.BindOptions{Propagation: "shared"}},
- expected: types.MountPoint{Type: "bind", RW: false, Destination: destPath, Source: tmpDir3, Propagation: "shared"},
- },
- }...)
- }
- }
- if testEnv.OSType != "windows" { // Windows does not support volume populate
- cases = append(cases, []testCase{
- {
- spec: mounttypes.Mount{Type: "volume", Target: destPath, VolumeOptions: &mounttypes.VolumeOptions{NoCopy: true}},
- expected: types.MountPoint{Driver: volume.DefaultDriverName, Type: "volume", RW: true, Destination: destPath, Mode: selinuxSharedLabel},
- },
- {
- spec: mounttypes.Mount{Type: "volume", Target: destPath + slash, VolumeOptions: &mounttypes.VolumeOptions{NoCopy: true}},
- expected: types.MountPoint{Driver: volume.DefaultDriverName, Type: "volume", RW: true, Destination: destPath, Mode: selinuxSharedLabel},
- },
- {
- spec: mounttypes.Mount{Type: "volume", Target: destPath, Source: "test4", VolumeOptions: &mounttypes.VolumeOptions{NoCopy: true}},
- expected: types.MountPoint{Type: "volume", Name: "test4", RW: true, Destination: destPath, Mode: selinuxSharedLabel},
- },
- {
- spec: mounttypes.Mount{Type: "volume", Target: destPath, Source: "test5", ReadOnly: true, VolumeOptions: &mounttypes.VolumeOptions{NoCopy: true}},
- expected: types.MountPoint{Type: "volume", Name: "test5", RW: false, Destination: destPath, Mode: selinuxSharedLabel},
- },
- }...)
- }
- ctx := context.Background()
- apiclient := testEnv.APIClient()
- for i, x := range cases {
- c.Logf("case %d - config: %v", i, x.spec)
- container, err := apiclient.ContainerCreate(
- ctx,
- &containertypes.Config{Image: testImg},
- &containertypes.HostConfig{Mounts: []mounttypes.Mount{x.spec}},
- &networktypes.NetworkingConfig{},
- "")
- assert.NilError(c, err)
- containerInspect, err := apiclient.ContainerInspect(ctx, container.ID)
- assert.NilError(c, err)
- mps := containerInspect.Mounts
- assert.Assert(c, is.Len(mps, 1))
- mountPoint := mps[0]
- if x.expected.Source != "" {
- assert.Check(c, is.Equal(x.expected.Source, mountPoint.Source))
- }
- if x.expected.Name != "" {
- assert.Check(c, is.Equal(x.expected.Name, mountPoint.Name))
- }
- if x.expected.Driver != "" {
- assert.Check(c, is.Equal(x.expected.Driver, mountPoint.Driver))
- }
- if x.expected.Propagation != "" {
- assert.Check(c, is.Equal(x.expected.Propagation, mountPoint.Propagation))
- }
- assert.Check(c, is.Equal(x.expected.RW, mountPoint.RW))
- assert.Check(c, is.Equal(x.expected.Type, mountPoint.Type))
- assert.Check(c, is.Equal(x.expected.Mode, mountPoint.Mode))
- assert.Check(c, is.Equal(x.expected.Destination, mountPoint.Destination))
- err = apiclient.ContainerStart(ctx, container.ID, types.ContainerStartOptions{})
- assert.NilError(c, err)
- poll.WaitOn(c, containerExit(apiclient, container.ID), poll.WithDelay(time.Second))
- err = apiclient.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{
- RemoveVolumes: true,
- Force: true,
- })
- assert.NilError(c, err)
- switch {
- // Named volumes still exist after the container is removed
- case x.spec.Type == "volume" && len(x.spec.Source) > 0:
- _, err := apiclient.VolumeInspect(ctx, mountPoint.Name)
- assert.NilError(c, err)
- // Bind mounts are never removed with the container
- case x.spec.Type == "bind":
- // anonymous volumes are removed
- default:
- _, err := apiclient.VolumeInspect(ctx, mountPoint.Name)
- assert.Check(c, client.IsErrNotFound(err))
- }
- }
- }
- func containerExit(apiclient client.APIClient, name string) func(poll.LogT) poll.Result {
- return func(logT poll.LogT) poll.Result {
- container, err := apiclient.ContainerInspect(context.Background(), name)
- if err != nil {
- return poll.Error(err)
- }
- switch container.State.Status {
- case "created", "running":
- return poll.Continue("container %s is %s, waiting for exit", name, container.State.Status)
- }
- return poll.Success()
- }
- }
- func (s *DockerSuite) TestContainersAPICreateMountsTmpfs(c *testing.T) {
- testRequires(c, DaemonIsLinux)
- type testCase struct {
- cfg mounttypes.Mount
- expectedOptions []string
- }
- target := "/foo"
- cases := []testCase{
- {
- cfg: mounttypes.Mount{
- Type: "tmpfs",
- Target: target},
- expectedOptions: []string{"rw", "nosuid", "nodev", "noexec", "relatime"},
- },
- {
- cfg: mounttypes.Mount{
- Type: "tmpfs",
- Target: target,
- TmpfsOptions: &mounttypes.TmpfsOptions{
- SizeBytes: 4096 * 1024, Mode: 0700}},
- expectedOptions: []string{"rw", "nosuid", "nodev", "noexec", "relatime", "size=4096k", "mode=700"},
- },
- }
- cli, err := client.NewClientWithOpts(client.FromEnv)
- assert.NilError(c, err)
- defer cli.Close()
- config := containertypes.Config{
- Image: "busybox",
- Cmd: []string{"/bin/sh", "-c", fmt.Sprintf("mount | grep 'tmpfs on %s'", target)},
- }
- for i, x := range cases {
- cName := fmt.Sprintf("test-tmpfs-%d", i)
- hostConfig := containertypes.HostConfig{
- Mounts: []mounttypes.Mount{x.cfg},
- }
- _, err = cli.ContainerCreate(context.Background(), &config, &hostConfig, &networktypes.NetworkingConfig{}, cName)
- assert.NilError(c, err)
- out, _ := dockerCmd(c, "start", "-a", cName)
- for _, option := range x.expectedOptions {
- assert.Assert(c, strings.Contains(out, option))
- }
- }
- }
- // Regression test for #33334
- // Makes sure that when a container which has a custom stop signal + restart=always
- // gets killed (with SIGKILL) by the kill API, that the restart policy is cancelled.
- func (s *DockerSuite) TestContainerKillCustomStopSignal(c *testing.T) {
- id := strings.TrimSpace(runSleepingContainer(c, "--stop-signal=SIGTERM", "--restart=always"))
- res, _, err := request.Post("/containers/" + id + "/kill")
- assert.NilError(c, err)
- defer res.Body.Close()
- b, err := ioutil.ReadAll(res.Body)
- assert.NilError(c, err)
- assert.Equal(c, res.StatusCode, http.StatusNoContent, string(b))
- err = waitInspect(id, "{{.State.Running}} {{.State.Restarting}}", "false false", 30*time.Second)
- assert.NilError(c, err)
- }
|