File: container_test.go

package info (click to toggle)
golang-github-fsouza-go-dockerclient 1.6.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,204 kB
  • sloc: makefile: 23
file content (94 lines) | stat: -rw-r--r-- 2,881 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2013 go-dockerclient authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package docker

import (
	"errors"
	"net/http"
	"testing"
	"time"
)

func TestStateString(t *testing.T) {
	t.Parallel()
	started := time.Now().Add(-3 * time.Hour)
	tests := []struct {
		name     string
		input    State
		expected string
	}{
		{"paused", State{Running: true, Paused: true, StartedAt: started}, "Up 3 hours (Paused)"},
		{"restarting", State{Running: true, Restarting: true, ExitCode: 7, FinishedAt: started}, "Restarting (7) 3 hours ago"},
		{"up", State{Running: true, StartedAt: started}, "Up 3 hours"},
		{"being removed", State{RemovalInProgress: true}, "Removal In Progress"},
		{"dead", State{Dead: true}, "Dead"},
		{"created", State{}, "Created"},
		{"no creation info", State{StartedAt: started}, ""},
		{"erro code", State{ExitCode: 7, StartedAt: started, FinishedAt: started}, "Exited (7) 3 hours ago"},
	}
	for _, tt := range tests {
		test := tt
		t.Run(test.name, func(t *testing.T) {
			t.Parallel()
			if got := test.input.String(); got != test.expected {
				t.Errorf("State.String(): wrong result. Want %q. Got %q.", test.expected, got)
			}
		})
	}
}

func TestStateStateString(t *testing.T) {
	t.Parallel()
	started := time.Now().Add(-3 * time.Hour)
	tests := []struct {
		input    State
		expected string
	}{
		{State{Running: true, Paused: true}, "paused"},
		{State{Running: true, Restarting: true}, "restarting"},
		{State{Running: true}, "running"},
		{State{Dead: true}, "dead"},
		{State{}, "created"},
		{State{StartedAt: started}, "exited"},
	}
	for _, tt := range tests {
		test := tt
		t.Run(test.expected, func(t *testing.T) {
			t.Parallel()
			if got := test.input.StateString(); got != test.expected {
				t.Errorf("State.String(): wrong result. Want %q. Got %q.", test.expected, got)
			}
		})
	}
}

// sleepyRoundTripper implements the http.RoundTripper interface. It sleeps
// for the 'sleep' duration and then returns an error for RoundTrip method.
type sleepyRoudTripper struct {
	sleepDuration time.Duration
}

func (rt *sleepyRoudTripper) RoundTrip(r *http.Request) (*http.Response, error) {
	time.Sleep(rt.sleepDuration)
	return nil, errors.New("Can't complete round trip")
}

func TestNoSuchContainerError(t *testing.T) {
	t.Parallel()
	err := &NoSuchContainer{ID: "i345"}
	expected := "No such container: i345"
	if got := err.Error(); got != expected {
		t.Errorf("NoSuchContainer: wrong message. Want %q. Got %q.", expected, got)
	}
}

func TestNoSuchContainerErrorMessage(t *testing.T) {
	t.Parallel()
	err := &NoSuchContainer{ID: "i345", Err: errors.New("some advanced error info")}
	expected := "some advanced error info"
	if got := err.Error(); got != expected {
		t.Errorf("NoSuchContainer: wrong message. Want %q. Got %q.", expected, got)
	}
}