This file is indexed.

/usr/share/gocode/src/github.com/docker/libnetwork/client/client_test.go is in golang-github-docker-libnetwork-dev 0.8.0-dev.2+git20170202.599.45b4086-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package client

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"os"
	"strings"
	"testing"

	_ "github.com/docker/libnetwork/testutils"
)

// nopCloser is used to provide a dummy CallFunc for Cmd()
type nopCloser struct {
	io.Reader
}

func (nopCloser) Close() error { return nil }

func TestMain(m *testing.M) {
	setupMockHTTPCallback()
	os.Exit(m.Run())
}

var callbackFunc func(method, path string, data interface{}, headers map[string][]string) (io.ReadCloser, http.Header, int, error)
var mockNwJSON, mockNwListJSON, mockServiceJSON, mockServiceListJSON, mockSbJSON, mockSbListJSON []byte
var mockNwName = "test"
var mockNwID = "2a3456789"
var mockServiceName = "testSrv"
var mockServiceID = "2a3456789"
var mockContainerID = "2a3456789"
var mockSandboxID = "2b3456789"

func setupMockHTTPCallback() {
	var list []networkResource
	nw := networkResource{Name: mockNwName, ID: mockNwID}
	mockNwJSON, _ = json.Marshal(nw)
	list = append(list, nw)
	mockNwListJSON, _ = json.Marshal(list)

	var srvList []serviceResource
	ep := serviceResource{Name: mockServiceName, ID: mockServiceID, Network: mockNwName}
	mockServiceJSON, _ = json.Marshal(ep)
	srvList = append(srvList, ep)
	mockServiceListJSON, _ = json.Marshal(srvList)

	var sbxList []SandboxResource
	sb := SandboxResource{ID: mockSandboxID, ContainerID: mockContainerID}
	mockSbJSON, _ = json.Marshal(sb)
	sbxList = append(sbxList, sb)
	mockSbListJSON, _ = json.Marshal(sbxList)

	dummyHTTPHdr := http.Header{}

	callbackFunc = func(method, path string, data interface{}, headers map[string][]string) (io.ReadCloser, http.Header, int, error) {
		var rsp string
		switch method {
		case "GET":
			if strings.Contains(path, fmt.Sprintf("networks?name=%s", mockNwName)) {
				rsp = string(mockNwListJSON)
			} else if strings.Contains(path, "networks?name=") {
				rsp = "[]"
			} else if strings.Contains(path, fmt.Sprintf("networks?partial-id=%s", mockNwID)) {
				rsp = string(mockNwListJSON)
			} else if strings.Contains(path, "networks?partial-id=") {
				rsp = "[]"
			} else if strings.HasSuffix(path, "networks") {
				rsp = string(mockNwListJSON)
			} else if strings.HasSuffix(path, "networks/"+mockNwID) {
				rsp = string(mockNwJSON)
			} else if strings.Contains(path, fmt.Sprintf("services?name=%s", mockServiceName)) {
				rsp = string(mockServiceListJSON)
			} else if strings.Contains(path, "services?name=") {
				rsp = "[]"
			} else if strings.Contains(path, fmt.Sprintf("services?partial-id=%s", mockServiceID)) {
				rsp = string(mockServiceListJSON)
			} else if strings.Contains(path, "services?partial-id=") {
				rsp = "[]"
			} else if strings.HasSuffix(path, "services") {
				rsp = string(mockServiceListJSON)
			} else if strings.HasSuffix(path, "services/"+mockServiceID) {
				rsp = string(mockServiceJSON)
			} else if strings.Contains(path, "containers") {
				return nopCloser{bytes.NewBufferString("")}, dummyHTTPHdr, 400, fmt.Errorf("Bad Request")
			} else if strings.Contains(path, fmt.Sprintf("sandboxes?container-id=%s", mockContainerID)) {
				rsp = string(mockSbListJSON)
			} else if strings.Contains(path, fmt.Sprintf("sandboxes?partial-container-id=%s", mockContainerID)) {
				rsp = string(mockSbListJSON)
			}
		case "POST":
			var data []byte
			if strings.HasSuffix(path, "networks") {
				data, _ = json.Marshal(mockNwID)
			} else if strings.HasSuffix(path, "services") {
				data, _ = json.Marshal(mockServiceID)
			} else if strings.HasSuffix(path, "backend") {
				data, _ = json.Marshal(mockSandboxID)
			}
			rsp = string(data)
		case "PUT":
		case "DELETE":
			rsp = ""
		}
		return nopCloser{bytes.NewBufferString(rsp)}, dummyHTTPHdr, 200, nil
	}
}

func TestClientDummyCommand(t *testing.T) {
	var out, errOut bytes.Buffer
	cli := NewNetworkCli(&out, &errOut, callbackFunc)

	err := cli.Cmd("docker", "dummy")
	if err == nil {
		t.Fatalf("Incorrect Command must fail")
	}
}

func TestClientNetworkInvalidCommand(t *testing.T) {
	var out, errOut bytes.Buffer
	cli := NewNetworkCli(&out, &errOut, callbackFunc)

	err := cli.Cmd("docker", "network", "invalid")
	if err == nil {
		t.Fatalf("Passing invalid commands must fail")
	}
}

func TestClientNetworkCreate(t *testing.T) {
	var out, errOut bytes.Buffer
	cli := NewNetworkCli(&out, &errOut, callbackFunc)

	err := cli.Cmd("docker", "network", "create", mockNwName)
	if err != nil {
		t.Fatal(err.Error())
	}
}

func TestClientNetworkCreateWithDriver(t *testing.T) {
	var out, errOut bytes.Buffer
	cli := NewNetworkCli(&out, &errOut, callbackFunc)

	err := cli.Cmd("docker", "network", "create", "-f=dummy", mockNwName)
	if err == nil {
		t.Fatalf("Passing incorrect flags to the create command must fail")
	}

	err = cli.Cmd("docker", "network", "create", "-d=dummy", mockNwName)
	if err != nil {
		t.Fatalf(err.Error())
	}
}

func TestClientNetworkRm(t *testing.T) {
	var out, errOut bytes.Buffer
	cli := NewNetworkCli(&out, &errOut, callbackFunc)

	err := cli.Cmd("docker", "network", "rm", mockNwName)
	if err != nil {
		t.Fatal(err.Error())
	}
}

func TestClientNetworkLs(t *testing.T) {
	var out, errOut bytes.Buffer
	cli := NewNetworkCli(&out, &errOut, callbackFunc)

	err := cli.Cmd("docker", "network", "ls")
	if err != nil {
		t.Fatal(err.Error())
	}
}

func TestClientNetworkInfo(t *testing.T) {
	var out, errOut bytes.Buffer
	cli := NewNetworkCli(&out, &errOut, callbackFunc)

	err := cli.Cmd("docker", "network", "info", mockNwName)
	if err != nil {
		t.Fatal(err.Error())
	}
}

func TestClientNetworkInfoById(t *testing.T) {
	var out, errOut bytes.Buffer
	cli := NewNetworkCli(&out, &errOut, callbackFunc)

	err := cli.Cmd("docker", "network", "info", mockNwID)
	if err != nil {
		t.Fatal(err.Error())
	}
}

// Docker Flag processing in flag.go uses os.Exit() frequently, even for --help
// TODO : Handle the --help test-case in the IT when CLI is available
/*
func TestClientNetworkServiceCreateHelp(t *testing.T) {
	var out, errOut bytes.Buffer
	cFunc := func(method, path string, data interface{}, headers map[string][]string) (io.ReadCloser, int, error) {
		return nil, 0, nil
	}
	cli := NewNetworkCli(&out, &errOut, callbackFunc)

	err := cli.Cmd("docker", "network", "create", "--help")
	if err != nil {
		t.Fatalf(err.Error())
	}
}
*/

// Docker flag processing in flag.go uses os.Exit(1) for incorrect parameter case.
// TODO : Handle the missing argument case in the IT when CLI is available
/*
func TestClientNetworkServiceCreateMissingArgument(t *testing.T) {
	var out, errOut bytes.Buffer
	cFunc := func(method, path string, data interface{}, headers map[string][]string) (io.ReadCloser, int, error) {
		return nil, 0, nil
	}
	cli := NewNetworkCli(&out, &errOut, callbackFunc)

	err := cli.Cmd("docker", "network", "create")
	if err != nil {
		t.Fatal(err.Error())
	}
}
*/