12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394 |
- package docker
- import (
- "archive/tar"
- "bufio"
- "bytes"
- "encoding/json"
- "github.com/dotcloud/docker/auth"
- "github.com/dotcloud/docker/registry"
- "github.com/dotcloud/docker/utils"
- "io"
- "net"
- "net/http"
- "net/http/httptest"
- "os"
- "path"
- "testing"
- "time"
- )
- func TestGetAuth(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{
- runtime: runtime,
- }
- r := httptest.NewRecorder()
- authConfig := &auth.AuthConfig{
- Username: "utest",
- Password: "utest",
- Email: "utest@yopmail.com",
- }
- authConfigJSON, err := json.Marshal(authConfig)
- if err != nil {
- t.Fatal(err)
- }
- req, err := http.NewRequest("POST", "/auth", bytes.NewReader(authConfigJSON))
- if err != nil {
- t.Fatal(err)
- }
- if err := postAuth(srv, APIVERSION, r, req, nil); err != nil {
- t.Fatal(err)
- }
- if r.Code != http.StatusOK && r.Code != 0 {
- t.Fatalf("%d OK or 0 expected, received %d\n", http.StatusOK, r.Code)
- }
- newAuthConfig := registry.NewRegistry(runtime.root).GetAuthConfig(false)
- if newAuthConfig.Username != authConfig.Username ||
- newAuthConfig.Email != authConfig.Email {
- t.Fatalf("The auth configuration hasn't been set correctly")
- }
- }
- func TestGetVersion(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- r := httptest.NewRecorder()
- if err := getVersion(srv, APIVERSION, r, nil, nil); err != nil {
- t.Fatal(err)
- }
- v := &APIVersion{}
- if err = json.Unmarshal(r.Body.Bytes(), v); err != nil {
- t.Fatal(err)
- }
- if v.Version != VERSION {
- t.Errorf("Excepted version %s, %s found", VERSION, v.Version)
- }
- }
- func TestGetInfo(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- r := httptest.NewRecorder()
- if err := getInfo(srv, APIVERSION, r, nil, nil); err != nil {
- t.Fatal(err)
- }
- infos := &APIInfo{}
- err = json.Unmarshal(r.Body.Bytes(), infos)
- if err != nil {
- t.Fatal(err)
- }
- if infos.Images != 1 {
- t.Errorf("Excepted images: %d, %d found", 1, infos.Images)
- }
- }
- func TestGetImagesJSON(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- // all=0
- req, err := http.NewRequest("GET", "/images/json?all=0", nil)
- if err != nil {
- t.Fatal(err)
- }
- r := httptest.NewRecorder()
- if err := getImagesJSON(srv, APIVERSION, r, req, nil); err != nil {
- t.Fatal(err)
- }
- images := []APIImages{}
- if err := json.Unmarshal(r.Body.Bytes(), &images); err != nil {
- t.Fatal(err)
- }
- if len(images) != 1 {
- t.Errorf("Excepted 1 image, %d found", len(images))
- }
- if images[0].Repository != unitTestImageName {
- t.Errorf("Excepted image %s, %s found", unitTestImageName, images[0].Repository)
- }
- r2 := httptest.NewRecorder()
- // all=1
- req2, err := http.NewRequest("GET", "/images/json?all=true", nil)
- if err != nil {
- t.Fatal(err)
- }
- if err := getImagesJSON(srv, APIVERSION, r2, req2, nil); err != nil {
- t.Fatal(err)
- }
- images2 := []APIImages{}
- if err := json.Unmarshal(r2.Body.Bytes(), &images2); err != nil {
- t.Fatal(err)
- }
- if len(images2) != 1 {
- t.Errorf("Excepted 1 image, %d found", len(images2))
- }
- if images2[0].ID != GetTestImage(runtime).ID {
- t.Errorf("Retrieved image Id differs, expected %s, received %s", GetTestImage(runtime).ID, images2[0].ID)
- }
- r3 := httptest.NewRecorder()
- // filter=a
- req3, err := http.NewRequest("GET", "/images/json?filter=a", nil)
- if err != nil {
- t.Fatal(err)
- }
- if err := getImagesJSON(srv, APIVERSION, r3, req3, nil); err != nil {
- t.Fatal(err)
- }
- images3 := []APIImages{}
- if err := json.Unmarshal(r3.Body.Bytes(), &images3); err != nil {
- t.Fatal(err)
- }
- if len(images3) != 0 {
- t.Errorf("Excepted 1 image, %d found", len(images3))
- }
- r4 := httptest.NewRecorder()
- // all=foobar
- req4, err := http.NewRequest("GET", "/images/json?all=foobar", nil)
- if err != nil {
- t.Fatal(err)
- }
- err = getImagesJSON(srv, APIVERSION, r4, req4, nil)
- if err == nil {
- t.Fatalf("Error expected, received none")
- }
- httpError(r4, err)
- if r4.Code != http.StatusBadRequest {
- t.Fatalf("%d Bad Request expected, received %d\n", http.StatusBadRequest, r4.Code)
- }
- }
- func TestGetImagesViz(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- r := httptest.NewRecorder()
- if err := getImagesViz(srv, APIVERSION, r, nil, nil); err != nil {
- t.Fatal(err)
- }
- if r.Code != http.StatusOK {
- t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
- }
- reader := bufio.NewReader(r.Body)
- line, err := reader.ReadString('\n')
- if err != nil {
- t.Fatal(err)
- }
- if line != "digraph docker {\n" {
- t.Errorf("Excepted digraph docker {\n, %s found", line)
- }
- }
- func TestGetImagesSearch(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{
- runtime: runtime,
- }
- r := httptest.NewRecorder()
- req, err := http.NewRequest("GET", "/images/search?term=redis", nil)
- if err != nil {
- t.Fatal(err)
- }
- if err := getImagesSearch(srv, APIVERSION, r, req, nil); err != nil {
- t.Fatal(err)
- }
- results := []APISearch{}
- if err := json.Unmarshal(r.Body.Bytes(), &results); err != nil {
- t.Fatal(err)
- }
- if len(results) < 2 {
- t.Errorf("Excepted at least 2 lines, %d found", len(results))
- }
- }
- func TestGetImagesHistory(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- r := httptest.NewRecorder()
- if err := getImagesHistory(srv, APIVERSION, r, nil, map[string]string{"name": unitTestImageName}); err != nil {
- t.Fatal(err)
- }
- history := []APIHistory{}
- if err := json.Unmarshal(r.Body.Bytes(), &history); err != nil {
- t.Fatal(err)
- }
- if len(history) != 1 {
- t.Errorf("Excepted 1 line, %d found", len(history))
- }
- }
- func TestGetImagesByName(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- r := httptest.NewRecorder()
- if err := getImagesByName(srv, APIVERSION, r, nil, map[string]string{"name": unitTestImageName}); err != nil {
- t.Fatal(err)
- }
- img := &Image{}
- if err := json.Unmarshal(r.Body.Bytes(), img); err != nil {
- t.Fatal(err)
- }
- if img.ID != GetTestImage(runtime).ID || img.Comment != "Imported from http://get.docker.io/images/busybox" {
- t.Errorf("Error inspecting image")
- }
- }
- func TestGetContainersJSON(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- container, err := NewBuilder(runtime).Create(&Config{
- Image: GetTestImage(runtime).ID,
- Cmd: []string{"echo", "test"},
- })
- if err != nil {
- t.Fatal(err)
- }
- defer runtime.Destroy(container)
- req, err := http.NewRequest("GET", "/containers/json?all=1", nil)
- if err != nil {
- t.Fatal(err)
- }
- r := httptest.NewRecorder()
- if err := getContainersJSON(srv, APIVERSION, r, req, nil); err != nil {
- t.Fatal(err)
- }
- containers := []APIContainers{}
- if err := json.Unmarshal(r.Body.Bytes(), &containers); err != nil {
- t.Fatal(err)
- }
- if len(containers) != 1 {
- t.Fatalf("Excepted %d container, %d found", 1, len(containers))
- }
- if containers[0].ID != container.ID {
- t.Fatalf("Container ID mismatch. Expected: %s, received: %s\n", container.ID, containers[0].ID)
- }
- }
- func TestGetContainersExport(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- builder := NewBuilder(runtime)
- // Create a container and remove a file
- container, err := builder.Create(
- &Config{
- Image: GetTestImage(runtime).ID,
- Cmd: []string{"touch", "/test"},
- },
- )
- if err != nil {
- t.Fatal(err)
- }
- defer runtime.Destroy(container)
- if err := container.Run(); err != nil {
- t.Fatal(err)
- }
- r := httptest.NewRecorder()
- if err = getContainersExport(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
- t.Fatal(err)
- }
- if r.Code != http.StatusOK {
- t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
- }
- found := false
- for tarReader := tar.NewReader(r.Body); ; {
- h, err := tarReader.Next()
- if err != nil {
- if err == io.EOF {
- break
- }
- t.Fatal(err)
- }
- if h.Name == "./test" {
- found = true
- break
- }
- }
- if !found {
- t.Fatalf("The created test file has not been found in the exported image")
- }
- }
- func TestGetContainersChanges(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- builder := NewBuilder(runtime)
- // Create a container and remove a file
- container, err := builder.Create(
- &Config{
- Image: GetTestImage(runtime).ID,
- Cmd: []string{"/bin/rm", "/etc/passwd"},
- },
- )
- if err != nil {
- t.Fatal(err)
- }
- defer runtime.Destroy(container)
- if err := container.Run(); err != nil {
- t.Fatal(err)
- }
- r := httptest.NewRecorder()
- if err := getContainersChanges(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
- t.Fatal(err)
- }
- changes := []Change{}
- if err := json.Unmarshal(r.Body.Bytes(), &changes); err != nil {
- t.Fatal(err)
- }
- // Check the changelog
- success := false
- for _, elem := range changes {
- if elem.Path == "/etc/passwd" && elem.Kind == 2 {
- success = true
- }
- }
- if !success {
- t.Fatalf("/etc/passwd as been removed but is not present in the diff")
- }
- }
- func TestGetContainersByName(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- builder := NewBuilder(runtime)
- // Create a container and remove a file
- container, err := builder.Create(
- &Config{
- Image: GetTestImage(runtime).ID,
- Cmd: []string{"echo", "test"},
- },
- )
- if err != nil {
- t.Fatal(err)
- }
- defer runtime.Destroy(container)
- r := httptest.NewRecorder()
- if err := getContainersByName(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
- t.Fatal(err)
- }
- outContainer := &Container{}
- if err := json.Unmarshal(r.Body.Bytes(), outContainer); err != nil {
- t.Fatal(err)
- }
- if outContainer.ID != container.ID {
- t.Fatalf("Wrong containers retrieved. Expected %s, recieved %s", container.ID, outContainer.ID)
- }
- }
- func TestPostAuth(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{
- runtime: runtime,
- }
- config := &auth.AuthConfig{
- Username: "utest",
- Email: "utest@yopmail.com",
- }
- authStr := auth.EncodeAuth(config)
- auth.SaveConfig(runtime.root, authStr, config.Email)
- r := httptest.NewRecorder()
- if err := getAuth(srv, APIVERSION, r, nil, nil); err != nil {
- t.Fatal(err)
- }
- authConfig := &auth.AuthConfig{}
- if err := json.Unmarshal(r.Body.Bytes(), authConfig); err != nil {
- t.Fatal(err)
- }
- if authConfig.Username != config.Username || authConfig.Email != config.Email {
- t.Errorf("The retrieve auth mismatch with the one set.")
- }
- }
- func TestPostCommit(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- builder := NewBuilder(runtime)
- // Create a container and remove a file
- container, err := builder.Create(
- &Config{
- Image: GetTestImage(runtime).ID,
- Cmd: []string{"touch", "/test"},
- },
- )
- if err != nil {
- t.Fatal(err)
- }
- defer runtime.Destroy(container)
- if err := container.Run(); err != nil {
- t.Fatal(err)
- }
- req, err := http.NewRequest("POST", "/commit?repo=testrepo&testtag=tag&container="+container.ID, bytes.NewReader([]byte{}))
- if err != nil {
- t.Fatal(err)
- }
- r := httptest.NewRecorder()
- if err := postCommit(srv, APIVERSION, r, req, nil); err != nil {
- t.Fatal(err)
- }
- if r.Code != http.StatusCreated {
- t.Fatalf("%d Created expected, received %d\n", http.StatusCreated, r.Code)
- }
- apiID := &APIID{}
- if err := json.Unmarshal(r.Body.Bytes(), apiID); err != nil {
- t.Fatal(err)
- }
- if _, err := runtime.graph.Get(apiID.ID); err != nil {
- t.Fatalf("The image has not been commited")
- }
- }
- func TestPostImagesCreate(t *testing.T) {
- // FIXME: Use the staging in order to perform tests
- // runtime, err := newTestRuntime()
- // if err != nil {
- // t.Fatal(err)
- // }
- // defer nuke(runtime)
- // srv := &Server{runtime: runtime}
- // stdin, stdinPipe := io.Pipe()
- // stdout, stdoutPipe := io.Pipe()
- // c1 := make(chan struct{})
- // go func() {
- // defer close(c1)
- // r := &hijackTester{
- // ResponseRecorder: httptest.NewRecorder(),
- // in: stdin,
- // out: stdoutPipe,
- // }
- // req, err := http.NewRequest("POST", "/images/create?fromImage="+unitTestImageName, bytes.NewReader([]byte{}))
- // if err != nil {
- // t.Fatal(err)
- // }
- // body, err := postImagesCreate(srv, r, req, nil)
- // if err != nil {
- // t.Fatal(err)
- // }
- // if body != nil {
- // t.Fatalf("No body expected, received: %s\n", body)
- // }
- // }()
- // // Acknowledge hijack
- // setTimeout(t, "hijack acknowledge timed out", 2*time.Second, func() {
- // stdout.Read([]byte{})
- // stdout.Read(make([]byte, 4096))
- // })
- // setTimeout(t, "Waiting for imagesCreate output", 5*time.Second, func() {
- // reader := bufio.NewReader(stdout)
- // line, err := reader.ReadString('\n')
- // if err != nil {
- // t.Fatal(err)
- // }
- // if !strings.HasPrefix(line, "Pulling repository d from") {
- // t.Fatalf("Expected Pulling repository docker-ut from..., found %s", line)
- // }
- // })
- // // Close pipes (client disconnects)
- // if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
- // t.Fatal(err)
- // }
- // // Wait for imagesCreate to finish, the client disconnected, therefore, Create finished his job
- // setTimeout(t, "Waiting for imagesCreate timed out", 10*time.Second, func() {
- // <-c1
- // })
- }
- func TestPostImagesInsert(t *testing.T) {
- // runtime, err := newTestRuntime()
- // if err != nil {
- // t.Fatal(err)
- // }
- // defer nuke(runtime)
- // srv := &Server{runtime: runtime}
- // stdin, stdinPipe := io.Pipe()
- // stdout, stdoutPipe := io.Pipe()
- // // Attach to it
- // c1 := make(chan struct{})
- // go func() {
- // defer close(c1)
- // r := &hijackTester{
- // ResponseRecorder: httptest.NewRecorder(),
- // in: stdin,
- // out: stdoutPipe,
- // }
- // req, err := http.NewRequest("POST", "/images/"+unitTestImageName+"/insert?path=%2Ftest&url=https%3A%2F%2Fraw.github.com%2Fdotcloud%2Fdocker%2Fmaster%2FREADME.md", bytes.NewReader([]byte{}))
- // if err != nil {
- // t.Fatal(err)
- // }
- // if err := postContainersCreate(srv, r, req, nil); err != nil {
- // t.Fatal(err)
- // }
- // }()
- // // Acknowledge hijack
- // setTimeout(t, "hijack acknowledge timed out", 5*time.Second, func() {
- // stdout.Read([]byte{})
- // stdout.Read(make([]byte, 4096))
- // })
- // id := ""
- // setTimeout(t, "Waiting for imagesInsert output", 10*time.Second, func() {
- // for {
- // reader := bufio.NewReader(stdout)
- // id, err = reader.ReadString('\n')
- // if err != nil {
- // t.Fatal(err)
- // }
- // }
- // })
- // // Close pipes (client disconnects)
- // if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
- // t.Fatal(err)
- // }
- // // Wait for attach to finish, the client disconnected, therefore, Attach finished his job
- // setTimeout(t, "Waiting for CmdAttach timed out", 2*time.Second, func() {
- // <-c1
- // })
- // img, err := srv.runtime.repositories.LookupImage(id)
- // if err != nil {
- // t.Fatalf("New image %s expected but not found", id)
- // }
- // layer, err := img.layer()
- // if err != nil {
- // t.Fatal(err)
- // }
- // if _, err := os.Stat(path.Join(layer, "test")); err != nil {
- // t.Fatalf("The test file has not been found")
- // }
- // if err := srv.runtime.graph.Delete(img.ID); err != nil {
- // t.Fatal(err)
- // }
- }
- func TestPostImagesPush(t *testing.T) {
- //FIXME: Use staging in order to perform tests
- // runtime, err := newTestRuntime()
- // if err != nil {
- // t.Fatal(err)
- // }
- // defer nuke(runtime)
- // srv := &Server{runtime: runtime}
- // stdin, stdinPipe := io.Pipe()
- // stdout, stdoutPipe := io.Pipe()
- // c1 := make(chan struct{})
- // go func() {
- // r := &hijackTester{
- // ResponseRecorder: httptest.NewRecorder(),
- // in: stdin,
- // out: stdoutPipe,
- // }
- // req, err := http.NewRequest("POST", "/images/docker-ut/push", bytes.NewReader([]byte{}))
- // if err != nil {
- // t.Fatal(err)
- // }
- // body, err := postImagesPush(srv, r, req, map[string]string{"name": "docker-ut"})
- // close(c1)
- // if err != nil {
- // t.Fatal(err)
- // }
- // if body != nil {
- // t.Fatalf("No body expected, received: %s\n", body)
- // }
- // }()
- // // Acknowledge hijack
- // setTimeout(t, "hijack acknowledge timed out", 2*time.Second, func() {
- // stdout.Read([]byte{})
- // stdout.Read(make([]byte, 4096))
- // })
- // setTimeout(t, "Waiting for imagesCreate output", 5*time.Second, func() {
- // reader := bufio.NewReader(stdout)
- // line, err := reader.ReadString('\n')
- // if err != nil {
- // t.Fatal(err)
- // }
- // if !strings.HasPrefix(line, "Processing checksum") {
- // t.Fatalf("Processing checksum..., found %s", line)
- // }
- // })
- // // Close pipes (client disconnects)
- // if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
- // t.Fatal(err)
- // }
- // // Wait for imagesPush to finish, the client disconnected, therefore, Push finished his job
- // setTimeout(t, "Waiting for imagesPush timed out", 10*time.Second, func() {
- // <-c1
- // })
- }
- func TestPostImagesTag(t *testing.T) {
- // FIXME: Use staging in order to perform tests
- // runtime, err := newTestRuntime()
- // if err != nil {
- // t.Fatal(err)
- // }
- // defer nuke(runtime)
- // srv := &Server{runtime: runtime}
- // r := httptest.NewRecorder()
- // req, err := http.NewRequest("POST", "/images/docker-ut/tag?repo=testrepo&tag=testtag", bytes.NewReader([]byte{}))
- // if err != nil {
- // t.Fatal(err)
- // }
- // body, err := postImagesTag(srv, r, req, map[string]string{"name": "docker-ut"})
- // if err != nil {
- // t.Fatal(err)
- // }
- // if body != nil {
- // t.Fatalf("No body expected, received: %s\n", body)
- // }
- // if r.Code != http.StatusCreated {
- // t.Fatalf("%d Created expected, received %d\n", http.StatusCreated, r.Code)
- // }
- }
- func TestPostContainersCreate(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- configJSON, err := json.Marshal(&Config{
- Image: GetTestImage(runtime).ID,
- Memory: 33554432,
- Cmd: []string{"touch", "/test"},
- })
- if err != nil {
- t.Fatal(err)
- }
- req, err := http.NewRequest("POST", "/containers/create", bytes.NewReader(configJSON))
- if err != nil {
- t.Fatal(err)
- }
- r := httptest.NewRecorder()
- if err := postContainersCreate(srv, APIVERSION, r, req, nil); err != nil {
- t.Fatal(err)
- }
- if r.Code != http.StatusCreated {
- t.Fatalf("%d Created expected, received %d\n", http.StatusCreated, r.Code)
- }
- apiRun := &APIRun{}
- if err := json.Unmarshal(r.Body.Bytes(), apiRun); err != nil {
- t.Fatal(err)
- }
- container := srv.runtime.Get(apiRun.ID)
- if container == nil {
- t.Fatalf("Container not created")
- }
- if err := container.Run(); err != nil {
- t.Fatal(err)
- }
- if _, err := os.Stat(path.Join(container.rwPath(), "test")); err != nil {
- if os.IsNotExist(err) {
- utils.Debugf("Err: %s", err)
- t.Fatalf("The test file has not been created")
- }
- t.Fatal(err)
- }
- }
- func TestPostContainersKill(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- container, err := NewBuilder(runtime).Create(
- &Config{
- Image: GetTestImage(runtime).ID,
- Cmd: []string{"/bin/cat"},
- OpenStdin: true,
- },
- )
- if err != nil {
- t.Fatal(err)
- }
- defer runtime.Destroy(container)
- if err := container.Start(); err != nil {
- t.Fatal(err)
- }
- // Give some time to the process to start
- container.WaitTimeout(500 * time.Millisecond)
- if !container.State.Running {
- t.Errorf("Container should be running")
- }
- r := httptest.NewRecorder()
- if err := postContainersKill(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
- t.Fatal(err)
- }
- if r.Code != http.StatusNoContent {
- t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
- }
- if container.State.Running {
- t.Fatalf("The container hasn't been killed")
- }
- }
- func TestPostContainersRestart(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- container, err := NewBuilder(runtime).Create(
- &Config{
- Image: GetTestImage(runtime).ID,
- Cmd: []string{"/bin/cat"},
- OpenStdin: true,
- },
- )
- if err != nil {
- t.Fatal(err)
- }
- defer runtime.Destroy(container)
- if err := container.Start(); err != nil {
- t.Fatal(err)
- }
- // Give some time to the process to start
- container.WaitTimeout(500 * time.Millisecond)
- if !container.State.Running {
- t.Errorf("Container should be running")
- }
- req, err := http.NewRequest("POST", "/containers/"+container.ID+"/restart?t=1", bytes.NewReader([]byte{}))
- if err != nil {
- t.Fatal(err)
- }
- r := httptest.NewRecorder()
- if err := postContainersRestart(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
- t.Fatal(err)
- }
- if r.Code != http.StatusNoContent {
- t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
- }
- // Give some time to the process to restart
- container.WaitTimeout(500 * time.Millisecond)
- if !container.State.Running {
- t.Fatalf("Container should be running")
- }
- if err := container.Kill(); err != nil {
- t.Fatal(err)
- }
- }
- func TestPostContainersStart(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- container, err := NewBuilder(runtime).Create(
- &Config{
- Image: GetTestImage(runtime).ID,
- Cmd: []string{"/bin/cat"},
- OpenStdin: true,
- },
- )
- if err != nil {
- t.Fatal(err)
- }
- defer runtime.Destroy(container)
- r := httptest.NewRecorder()
- if err := postContainersStart(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
- t.Fatal(err)
- }
- if r.Code != http.StatusNoContent {
- t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
- }
- // Give some time to the process to start
- container.WaitTimeout(500 * time.Millisecond)
- if !container.State.Running {
- t.Errorf("Container should be running")
- }
- r = httptest.NewRecorder()
- if err = postContainersStart(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err == nil {
- t.Fatalf("A running containter should be able to be started")
- }
- if err := container.Kill(); err != nil {
- t.Fatal(err)
- }
- }
- func TestPostContainersStop(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- container, err := NewBuilder(runtime).Create(
- &Config{
- Image: GetTestImage(runtime).ID,
- Cmd: []string{"/bin/cat"},
- OpenStdin: true,
- },
- )
- if err != nil {
- t.Fatal(err)
- }
- defer runtime.Destroy(container)
- if err := container.Start(); err != nil {
- t.Fatal(err)
- }
- // Give some time to the process to start
- container.WaitTimeout(500 * time.Millisecond)
- if !container.State.Running {
- t.Errorf("Container should be running")
- }
- // Note: as it is a POST request, it requires a body.
- req, err := http.NewRequest("POST", "/containers/"+container.ID+"/stop?t=1", bytes.NewReader([]byte{}))
- if err != nil {
- t.Fatal(err)
- }
- r := httptest.NewRecorder()
- if err := postContainersStop(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
- t.Fatal(err)
- }
- if r.Code != http.StatusNoContent {
- t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
- }
- if container.State.Running {
- t.Fatalf("The container hasn't been stopped")
- }
- }
- func TestPostContainersWait(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- container, err := NewBuilder(runtime).Create(
- &Config{
- Image: GetTestImage(runtime).ID,
- Cmd: []string{"/bin/sleep", "1"},
- OpenStdin: true,
- },
- )
- if err != nil {
- t.Fatal(err)
- }
- defer runtime.Destroy(container)
- if err := container.Start(); err != nil {
- t.Fatal(err)
- }
- setTimeout(t, "Wait timed out", 3*time.Second, func() {
- r := httptest.NewRecorder()
- if err := postContainersWait(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
- t.Fatal(err)
- }
- apiWait := &APIWait{}
- if err := json.Unmarshal(r.Body.Bytes(), apiWait); err != nil {
- t.Fatal(err)
- }
- if apiWait.StatusCode != 0 {
- t.Fatalf("Non zero exit code for sleep: %d\n", apiWait.StatusCode)
- }
- })
- if container.State.Running {
- t.Fatalf("The container should be stopped after wait")
- }
- }
- func TestPostContainersAttach(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- container, err := NewBuilder(runtime).Create(
- &Config{
- Image: GetTestImage(runtime).ID,
- Cmd: []string{"/bin/cat"},
- OpenStdin: true,
- },
- )
- if err != nil {
- t.Fatal(err)
- }
- defer runtime.Destroy(container)
- // Start the process
- if err := container.Start(); err != nil {
- t.Fatal(err)
- }
- stdin, stdinPipe := io.Pipe()
- stdout, stdoutPipe := io.Pipe()
- // Attach to it
- c1 := make(chan struct{})
- go func() {
- defer close(c1)
- r := &hijackTester{
- ResponseRecorder: httptest.NewRecorder(),
- in: stdin,
- out: stdoutPipe,
- }
- req, err := http.NewRequest("POST", "/containers/"+container.ID+"/attach?stream=1&stdin=1&stdout=1&stderr=1", bytes.NewReader([]byte{}))
- if err != nil {
- t.Fatal(err)
- }
- if err := postContainersAttach(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
- t.Fatal(err)
- }
- }()
- // Acknowledge hijack
- setTimeout(t, "hijack acknowledge timed out", 2*time.Second, func() {
- stdout.Read([]byte{})
- stdout.Read(make([]byte, 4096))
- })
- setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
- if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
- t.Fatal(err)
- }
- })
- // Close pipes (client disconnects)
- if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
- t.Fatal(err)
- }
- // Wait for attach to finish, the client disconnected, therefore, Attach finished his job
- setTimeout(t, "Waiting for CmdAttach timed out", 2*time.Second, func() {
- <-c1
- })
- // We closed stdin, expect /bin/cat to still be running
- // Wait a little bit to make sure container.monitor() did his thing
- err = container.WaitTimeout(500 * time.Millisecond)
- if err == nil || !container.State.Running {
- t.Fatalf("/bin/cat is not running after closing stdin")
- }
- // Try to avoid the timeoout in destroy. Best effort, don't check error
- cStdin, _ := container.StdinPipe()
- cStdin.Close()
- container.Wait()
- }
- // FIXME: Test deleting running container
- // FIXME: Test deleting container with volume
- // FIXME: Test deleting volume in use by other container
- func TestDeleteContainers(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- container, err := NewBuilder(runtime).Create(&Config{
- Image: GetTestImage(runtime).ID,
- Cmd: []string{"touch", "/test"},
- })
- if err != nil {
- t.Fatal(err)
- }
- defer runtime.Destroy(container)
- if err := container.Run(); err != nil {
- t.Fatal(err)
- }
- req, err := http.NewRequest("DELETE", "/containers/"+container.ID, nil)
- if err != nil {
- t.Fatal(err)
- }
- r := httptest.NewRecorder()
- if err := deleteContainers(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
- t.Fatal(err)
- }
- if r.Code != http.StatusNoContent {
- t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
- }
- if c := runtime.Get(container.ID); c != nil {
- t.Fatalf("The container as not been deleted")
- }
- if _, err := os.Stat(path.Join(container.rwPath(), "test")); err == nil {
- t.Fatalf("The test file has not been deleted")
- }
- }
- func TestOptionsRoute(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime, enableCors: true}
- r := httptest.NewRecorder()
- router, err := createRouter(srv, false)
- if err != nil {
- t.Fatal(err)
- }
- req, err := http.NewRequest("OPTIONS", "/", nil)
- if err != nil {
- t.Fatal(err)
- }
- router.ServeHTTP(r, req)
- if r.Code != http.StatusOK {
- t.Errorf("Expected response for OPTIONS request to be \"200\", %v found.", r.Code)
- }
- }
- func TestGetEnabledCors(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime, enableCors: true}
- r := httptest.NewRecorder()
- router, err := createRouter(srv, false)
- if err != nil {
- t.Fatal(err)
- }
- req, err := http.NewRequest("GET", "/version", nil)
- if err != nil {
- t.Fatal(err)
- }
- router.ServeHTTP(r, req)
- if r.Code != http.StatusOK {
- t.Errorf("Expected response for OPTIONS request to be \"200\", %v found.", r.Code)
- }
- allowOrigin := r.Header().Get("Access-Control-Allow-Origin")
- allowHeaders := r.Header().Get("Access-Control-Allow-Headers")
- allowMethods := r.Header().Get("Access-Control-Allow-Methods")
- if allowOrigin != "*" {
- t.Errorf("Expected header Access-Control-Allow-Origin to be \"*\", %s found.", allowOrigin)
- }
- if allowHeaders != "Origin, X-Requested-With, Content-Type, Accept" {
- t.Errorf("Expected header Access-Control-Allow-Headers to be \"Origin, X-Requested-With, Content-Type, Accept\", %s found.", allowHeaders)
- }
- if allowMethods != "GET, POST, DELETE, PUT, OPTIONS" {
- t.Errorf("Expected hearder Access-Control-Allow-Methods to be \"GET, POST, DELETE, PUT, OPTIONS\", %s found.", allowMethods)
- }
- }
- func TestDeleteImages(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- if err := srv.runtime.repositories.Set("test", "test", unitTestImageName, true); err != nil {
- t.Fatal(err)
- }
- images, err := srv.Images(false, "")
- if err != nil {
- t.Fatal(err)
- }
- if len(images) != 2 {
- t.Errorf("Excepted 2 images, %d found", len(images))
- }
- req, err := http.NewRequest("DELETE", "/images/test:test", nil)
- if err != nil {
- t.Fatal(err)
- }
- r := httptest.NewRecorder()
- if err := deleteImages(srv, APIVERSION, r, req, map[string]string{"name": "test:test"}); err != nil {
- t.Fatal(err)
- }
- if r.Code != http.StatusOK {
- t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
- }
- var outs []APIRmi
- if err := json.Unmarshal(r.Body.Bytes(), &outs); err != nil {
- t.Fatal(err)
- }
- if len(outs) != 1 {
- t.Fatalf("Expected %d event (untagged), got %d", 1, len(outs))
- }
- images, err = srv.Images(false, "")
- if err != nil {
- t.Fatal(err)
- }
- if len(images) != 1 {
- t.Errorf("Excepted 1 image, %d found", len(images))
- }
- /* if c := runtime.Get(container.Id); c != nil {
- t.Fatalf("The container as not been deleted")
- }
- if _, err := os.Stat(path.Join(container.rwPath(), "test")); err == nil {
- t.Fatalf("The test file has not been deleted")
- } */
- }
- // Mocked types for tests
- type NopConn struct {
- io.ReadCloser
- io.Writer
- }
- func (c *NopConn) LocalAddr() net.Addr { return nil }
- func (c *NopConn) RemoteAddr() net.Addr { return nil }
- func (c *NopConn) SetDeadline(t time.Time) error { return nil }
- func (c *NopConn) SetReadDeadline(t time.Time) error { return nil }
- func (c *NopConn) SetWriteDeadline(t time.Time) error { return nil }
- type hijackTester struct {
- *httptest.ResponseRecorder
- in io.ReadCloser
- out io.Writer
- }
- func (t *hijackTester) Hijack() (net.Conn, *bufio.ReadWriter, error) {
- bufrw := bufio.NewReadWriter(bufio.NewReader(t.in), bufio.NewWriter(t.out))
- conn := &NopConn{
- ReadCloser: t.in,
- Writer: t.out,
- }
- return conn, bufrw, nil
- }
|