github.com/raychaser/docker@v1.5.0/engine/job_test.go (about)

     1  package engine
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"testing"
     7  )
     8  
     9  func TestJobStatusOK(t *testing.T) {
    10  	eng := New()
    11  	eng.Register("return_ok", func(job *Job) Status { return StatusOK })
    12  	err := eng.Job("return_ok").Run()
    13  	if err != nil {
    14  		t.Fatalf("Expected: err=%v\nReceived: err=%v", nil, err)
    15  	}
    16  }
    17  
    18  func TestJobStatusErr(t *testing.T) {
    19  	eng := New()
    20  	eng.Register("return_err", func(job *Job) Status { return StatusErr })
    21  	err := eng.Job("return_err").Run()
    22  	if err == nil {
    23  		t.Fatalf("When a job returns StatusErr, Run() should return an error")
    24  	}
    25  }
    26  
    27  func TestJobStatusNotFound(t *testing.T) {
    28  	eng := New()
    29  	eng.Register("return_not_found", func(job *Job) Status { return StatusNotFound })
    30  	err := eng.Job("return_not_found").Run()
    31  	if err == nil {
    32  		t.Fatalf("When a job returns StatusNotFound, Run() should return an error")
    33  	}
    34  }
    35  
    36  func TestJobStdoutString(t *testing.T) {
    37  	eng := New()
    38  	// FIXME: test multiple combinations of output and status
    39  	eng.Register("say_something_in_stdout", func(job *Job) Status {
    40  		job.Printf("Hello world\n")
    41  		return StatusOK
    42  	})
    43  
    44  	job := eng.Job("say_something_in_stdout")
    45  	var outputBuffer = bytes.NewBuffer(nil)
    46  	job.Stdout.Add(outputBuffer)
    47  	if err := job.Run(); err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	fmt.Println(outputBuffer)
    51  	var output = Tail(outputBuffer, 1)
    52  	if expectedOutput := "Hello world"; output != expectedOutput {
    53  		t.Fatalf("Stdout last line:\nExpected: %v\nReceived: %v", expectedOutput, output)
    54  	}
    55  }
    56  
    57  func TestJobStderrString(t *testing.T) {
    58  	eng := New()
    59  	// FIXME: test multiple combinations of output and status
    60  	eng.Register("say_something_in_stderr", func(job *Job) Status {
    61  		job.Errorf("Warning, something might happen\nHere it comes!\nOh no...\nSomething happened\n")
    62  		return StatusOK
    63  	})
    64  
    65  	job := eng.Job("say_something_in_stderr")
    66  	var outputBuffer = bytes.NewBuffer(nil)
    67  	job.Stderr.Add(outputBuffer)
    68  	if err := job.Run(); err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	var output = Tail(outputBuffer, 1)
    72  	if expectedOutput := "Something happened"; output != expectedOutput {
    73  		t.Fatalf("Stderr last line:\nExpected: %v\nReceived: %v", expectedOutput, output)
    74  	}
    75  }