Merge pull request #174 from monnand/kubelet-cadvisor
Letting kubelet retrieve container stats from cAdvisor
This commit is contained in:
commit
3a9a3b1114
@ -68,6 +68,20 @@ type Container struct {
|
||||
VolumeMounts []VolumeMount `yaml:"volumeMounts,omitempty" json:"volumeMounts,omitempty"`
|
||||
}
|
||||
|
||||
// Percentile represents a pair which contains a percentage from 0 to 100 and
|
||||
// its corresponding value.
|
||||
type Percentile struct {
|
||||
Percentage int `json:"percentage,omitempty"`
|
||||
Value uint64 `json:"value,omitempty"`
|
||||
}
|
||||
|
||||
// ContainerStats represents statistical information of a container
|
||||
type ContainerStats struct {
|
||||
CpuUsagePercentiles []Percentile `json:"cpu_usage_percentiles,omitempty"`
|
||||
MemoryUsagePercentiles []Percentile `json:"memory_usage_percentiles,omitempty"`
|
||||
MaxMemoryUsage uint64 `json:"max_memory_usage,omitempty"`
|
||||
}
|
||||
|
||||
// Event is the representation of an event logged to etcd backends
|
||||
type Event struct {
|
||||
Event string `json:"event,omitempty"`
|
||||
|
@ -36,6 +36,7 @@ import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
"github.com/coreos/go-etcd/etcd"
|
||||
"github.com/fsouza/go-dockerclient"
|
||||
"github.com/google/cadvisor/info"
|
||||
"gopkg.in/v1/yaml"
|
||||
)
|
||||
|
||||
@ -58,11 +59,17 @@ type DockerInterface interface {
|
||||
StopContainer(id string, timeout uint) error
|
||||
}
|
||||
|
||||
type CadvisorInterface interface {
|
||||
ContainerInfo(name string) (*info.ContainerInfo, error)
|
||||
MachineInfo() (*info.MachineInfo, error)
|
||||
}
|
||||
|
||||
// The main kubelet implementation
|
||||
type Kubelet struct {
|
||||
Hostname string
|
||||
Client util.EtcdClient
|
||||
DockerClient DockerInterface
|
||||
CadvisorClient CadvisorInterface
|
||||
FileCheckFrequency time.Duration
|
||||
SyncFrequency time.Duration
|
||||
HTTPCheckFrequency time.Duration
|
||||
@ -641,3 +648,43 @@ func (kl *Kubelet) GetContainerInfo(name string) (string, error) {
|
||||
data, err := json.Marshal(info)
|
||||
return string(data), err
|
||||
}
|
||||
|
||||
func (kl *Kubelet) GetContainerStats(name string) (*api.ContainerStats, error) {
|
||||
if kl.CadvisorClient == nil {
|
||||
return nil, nil
|
||||
}
|
||||
id, found, err := kl.GetContainerID(name)
|
||||
if err != nil || !found {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info, err := kl.CadvisorClient.ContainerInfo(fmt.Sprintf("/docker/%v", id))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// When the stats data for the container is not available yet.
|
||||
if info.StatsPercentiles == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
ret := new(api.ContainerStats)
|
||||
ret.MaxMemoryUsage = info.StatsPercentiles.MaxMemoryUsage
|
||||
if len(info.StatsPercentiles.CpuUsagePercentiles) > 0 {
|
||||
percentiles := make([]api.Percentile, len(info.StatsPercentiles.CpuUsagePercentiles))
|
||||
for i, p := range info.StatsPercentiles.CpuUsagePercentiles {
|
||||
percentiles[i].Percentage = p.Percentage
|
||||
percentiles[i].Value = p.Value
|
||||
}
|
||||
ret.CpuUsagePercentiles = percentiles
|
||||
}
|
||||
if len(info.StatsPercentiles.MemoryUsagePercentiles) > 0 {
|
||||
percentiles := make([]api.Percentile, len(info.StatsPercentiles.MemoryUsagePercentiles))
|
||||
for i, p := range info.StatsPercentiles.MemoryUsagePercentiles {
|
||||
percentiles[i].Percentage = p.Percentage
|
||||
percentiles[i].Value = p.Value
|
||||
}
|
||||
ret.MemoryUsagePercentiles = percentiles
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ limitations under the License.
|
||||
package kubelet
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
@ -35,6 +36,7 @@ type KubeletServer struct {
|
||||
// For testablitiy.
|
||||
type kubeletInterface interface {
|
||||
GetContainerID(name string) (string, bool, error)
|
||||
GetContainerStats(name string) (*api.ContainerStats, error)
|
||||
GetContainerInfo(name string) (string, error)
|
||||
}
|
||||
|
||||
@ -64,6 +66,33 @@ func (s *KubeletServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
||||
s.UpdateChannel <- manifest
|
||||
case u.Path == "/containerStats":
|
||||
container := u.Query().Get("container")
|
||||
if len(container) == 0 {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
fmt.Fprint(w, "Missing container query arg.")
|
||||
return
|
||||
}
|
||||
stats, err := s.Kubelet.GetContainerStats(container)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprintf(w, "Internal Error: %#v", err)
|
||||
return
|
||||
}
|
||||
if stats == nil {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprint(w, "{}")
|
||||
return
|
||||
}
|
||||
data, err := json.Marshal(stats)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprintf(w, "Internal Error: %#v", err)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Add("Content-type", "application/json")
|
||||
w.Write(data)
|
||||
case u.Path == "/containerInfo":
|
||||
container := u.Query().Get("container")
|
||||
if len(container) == 0 {
|
||||
|
@ -2,6 +2,7 @@ package kubelet
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
@ -15,8 +16,9 @@ import (
|
||||
)
|
||||
|
||||
type fakeKubelet struct {
|
||||
infoFunc func(name string) (string, error)
|
||||
idFunc func(name string) (string, bool, error)
|
||||
infoFunc func(name string) (string, error)
|
||||
idFunc func(name string) (string, bool, error)
|
||||
statsFunc func(name string) (*api.ContainerStats, error)
|
||||
}
|
||||
|
||||
func (fk *fakeKubelet) GetContainerInfo(name string) (string, error) {
|
||||
@ -27,6 +29,10 @@ func (fk *fakeKubelet) GetContainerID(name string) (string, bool, error) {
|
||||
return fk.idFunc(name)
|
||||
}
|
||||
|
||||
func (fk *fakeKubelet) GetContainerStats(name string) (*api.ContainerStats, error) {
|
||||
return fk.statsFunc(name)
|
||||
}
|
||||
|
||||
// If we made everything distribute a list of ContainerManifests, we could just use
|
||||
// channelReader.
|
||||
type channelReaderSingle struct {
|
||||
@ -129,3 +135,42 @@ func TestContainerInfo(t *testing.T) {
|
||||
t.Errorf("Expected: '%v', got: '%v'", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerStats(t *testing.T) {
|
||||
fw := makeServerTest()
|
||||
expectedStats := &api.ContainerStats{
|
||||
MaxMemoryUsage: 1024001,
|
||||
CpuUsagePercentiles: []api.Percentile{
|
||||
{50, 150},
|
||||
{80, 180},
|
||||
{90, 190},
|
||||
},
|
||||
MemoryUsagePercentiles: []api.Percentile{
|
||||
{50, 150},
|
||||
{80, 180},
|
||||
{90, 190},
|
||||
},
|
||||
}
|
||||
expectedContainerName := "goodcontainer"
|
||||
fw.fakeKubelet.statsFunc = func(name string) (*api.ContainerStats, error) {
|
||||
if name != expectedContainerName {
|
||||
return nil, fmt.Errorf("bad container name: %v", name)
|
||||
}
|
||||
return expectedStats, nil
|
||||
}
|
||||
|
||||
resp, err := http.Get(fw.testHttpServer.URL + fmt.Sprintf("/containerStats?container=%v", expectedContainerName))
|
||||
if err != nil {
|
||||
t.Fatalf("Got error GETing: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var receivedStats api.ContainerStats
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
err = decoder.Decode(&receivedStats)
|
||||
if err != nil {
|
||||
t.Fatalf("received invalid json data: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(&receivedStats, expectedStats) {
|
||||
t.Errorf("received wrong data: %#v", receivedStats)
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,8 @@ import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
"github.com/coreos/go-etcd/etcd"
|
||||
"github.com/fsouza/go-dockerclient"
|
||||
"github.com/google/cadvisor/info"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// TODO: This doesn't reduce typing enough to make it worth the less readable errors. Remove.
|
||||
@ -905,3 +907,183 @@ func TestWatchEtcd(t *testing.T) {
|
||||
t.Errorf("Unexpected manifest(s) %#v %#v", read[0], manifest)
|
||||
}
|
||||
}
|
||||
|
||||
type mockCadvisorClient struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (self *mockCadvisorClient) ContainerInfo(name string) (*info.ContainerInfo, error) {
|
||||
args := self.Called(name)
|
||||
return args.Get(0).(*info.ContainerInfo), args.Error(1)
|
||||
}
|
||||
|
||||
func (self *mockCadvisorClient) MachineInfo() (*info.MachineInfo, error) {
|
||||
args := self.Called()
|
||||
return args.Get(0).(*info.MachineInfo), args.Error(1)
|
||||
}
|
||||
|
||||
func areSamePercentiles(
|
||||
cadvisorPercentiles []info.Percentile,
|
||||
kubePercentiles []api.Percentile,
|
||||
t *testing.T,
|
||||
) {
|
||||
if len(cadvisorPercentiles) != len(kubePercentiles) {
|
||||
t.Errorf("cadvisor gives %v percentiles; kubelet got %v", len(cadvisorPercentiles), len(kubePercentiles))
|
||||
return
|
||||
}
|
||||
for _, ap := range cadvisorPercentiles {
|
||||
found := false
|
||||
for _, kp := range kubePercentiles {
|
||||
if ap.Percentage == kp.Percentage {
|
||||
found = true
|
||||
if ap.Value != kp.Value {
|
||||
t.Errorf("%v percentile from cadvisor is %v; kubelet got %v",
|
||||
ap.Percentage,
|
||||
ap.Value,
|
||||
kp.Value)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("Unable to find %v percentile in kubelet's data", ap.Percentage)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetContainerStats(t *testing.T) {
|
||||
containerId := "ab2cdf"
|
||||
containerPath := fmt.Sprintf("/docker/%v", containerId)
|
||||
containerInfo := &info.ContainerInfo{
|
||||
ContainerReference: info.ContainerReference{
|
||||
Name: containerPath,
|
||||
},
|
||||
StatsPercentiles: &info.ContainerStatsPercentiles{
|
||||
MaxMemoryUsage: 1024000,
|
||||
MemoryUsagePercentiles: []info.Percentile{
|
||||
{50, 100},
|
||||
{80, 180},
|
||||
{90, 190},
|
||||
},
|
||||
CpuUsagePercentiles: []info.Percentile{
|
||||
{51, 101},
|
||||
{81, 181},
|
||||
{91, 191},
|
||||
},
|
||||
},
|
||||
}
|
||||
fakeDocker := FakeDockerClient{
|
||||
err: nil,
|
||||
}
|
||||
|
||||
mockCadvisor := &mockCadvisorClient{}
|
||||
mockCadvisor.On("ContainerInfo", containerPath).Return(containerInfo, nil)
|
||||
|
||||
kubelet := Kubelet{
|
||||
DockerClient: &fakeDocker,
|
||||
CadvisorClient: mockCadvisor,
|
||||
}
|
||||
fakeDocker.containerList = []docker.APIContainers{
|
||||
{
|
||||
Names: []string{"foo"},
|
||||
ID: containerId,
|
||||
},
|
||||
}
|
||||
|
||||
stats, err := kubelet.GetContainerStats("foo")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if stats.MaxMemoryUsage != containerInfo.StatsPercentiles.MaxMemoryUsage {
|
||||
t.Errorf("wrong max memory usage")
|
||||
}
|
||||
areSamePercentiles(containerInfo.StatsPercentiles.CpuUsagePercentiles, stats.CpuUsagePercentiles, t)
|
||||
areSamePercentiles(containerInfo.StatsPercentiles.MemoryUsagePercentiles, stats.MemoryUsagePercentiles, t)
|
||||
mockCadvisor.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestGetContainerStatsWithoutCadvisor(t *testing.T) {
|
||||
fakeDocker := FakeDockerClient{
|
||||
err: nil,
|
||||
}
|
||||
|
||||
kubelet := Kubelet{
|
||||
DockerClient: &fakeDocker,
|
||||
}
|
||||
fakeDocker.containerList = []docker.APIContainers{
|
||||
{
|
||||
Names: []string{"foo"},
|
||||
},
|
||||
}
|
||||
|
||||
stats, _ := kubelet.GetContainerStats("foo")
|
||||
// When there's no cAdvisor, the stats should be either nil or empty
|
||||
if stats == nil {
|
||||
return
|
||||
}
|
||||
if stats.MaxMemoryUsage != 0 {
|
||||
t.Errorf("MaxMemoryUsage is %v even if there's no cadvisor", stats.MaxMemoryUsage)
|
||||
}
|
||||
if len(stats.CpuUsagePercentiles) > 0 {
|
||||
t.Errorf("Cpu usage percentiles is not empty (%+v) even if there's no cadvisor", stats.CpuUsagePercentiles)
|
||||
}
|
||||
if len(stats.MemoryUsagePercentiles) > 0 {
|
||||
t.Errorf("Memory usage percentiles is not empty (%+v) even if there's no cadvisor", stats.MemoryUsagePercentiles)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetContainerStatsWhenCadvisorFailed(t *testing.T) {
|
||||
containerId := "ab2cdf"
|
||||
containerPath := fmt.Sprintf("/docker/%v", containerId)
|
||||
fakeDocker := FakeDockerClient{
|
||||
err: nil,
|
||||
}
|
||||
|
||||
containerInfo := &info.ContainerInfo{}
|
||||
mockCadvisor := &mockCadvisorClient{}
|
||||
expectedErr := fmt.Errorf("some error")
|
||||
mockCadvisor.On("ContainerInfo", containerPath).Return(containerInfo, expectedErr)
|
||||
|
||||
kubelet := Kubelet{
|
||||
DockerClient: &fakeDocker,
|
||||
CadvisorClient: mockCadvisor,
|
||||
}
|
||||
fakeDocker.containerList = []docker.APIContainers{
|
||||
{
|
||||
Names: []string{"foo"},
|
||||
ID: containerId,
|
||||
},
|
||||
}
|
||||
|
||||
stats, err := kubelet.GetContainerStats("foo")
|
||||
if stats != nil {
|
||||
t.Errorf("non-nil stats on error")
|
||||
}
|
||||
if err == nil {
|
||||
t.Errorf("expect error but received nil error")
|
||||
return
|
||||
}
|
||||
if err.Error() != expectedErr.Error() {
|
||||
t.Errorf("wrong error message. expect %v, got %v", err, expectedErr)
|
||||
}
|
||||
mockCadvisor.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestGetContainerStatsOnNonExistContainer(t *testing.T) {
|
||||
fakeDocker := FakeDockerClient{
|
||||
err: nil,
|
||||
}
|
||||
|
||||
mockCadvisor := &mockCadvisorClient{}
|
||||
|
||||
kubelet := Kubelet{
|
||||
DockerClient: &fakeDocker,
|
||||
CadvisorClient: mockCadvisor,
|
||||
}
|
||||
fakeDocker.containerList = []docker.APIContainers{}
|
||||
|
||||
stats, _ := kubelet.GetContainerStats("foo")
|
||||
if stats != nil {
|
||||
t.Errorf("non-nil stats on non exist container")
|
||||
}
|
||||
mockCadvisor.AssertExpectations(t)
|
||||
}
|
||||
|
1
third_party/deps.sh
vendored
1
third_party/deps.sh
vendored
@ -12,6 +12,7 @@ DEP_PACKAGES="
|
||||
code.google.com/p/google-api-go-client/googleapi
|
||||
github.com/coreos/go-log/log
|
||||
github.com/coreos/go-systemd/journal
|
||||
github.com/google/cadvisor/info
|
||||
"
|
||||
|
||||
PACKAGES="$TOP_PACKAGES $DEP_PACKAGES"
|
||||
|
13
third_party/src/github.com/google/cadvisor/.travis.yml
vendored
Normal file
13
third_party/src/github.com/google/cadvisor/.travis.yml
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
language: go
|
||||
go:
|
||||
- 1.2
|
||||
before_script:
|
||||
- go get github.com/stretchr/testify/mock
|
||||
- go get github.com/kr/pretty
|
||||
script:
|
||||
- go test -v -race github.com/google/cadvisor/container
|
||||
- go test -v github.com/google/cadvisor/info
|
||||
- go test -v github.com/google/cadvisor/client
|
||||
- go test -v github.com/google/cadvisor/sampling
|
||||
- go test -v github.com/google/cadvisor/storage/memory
|
||||
- go build github.com/google/cadvisor
|
11
third_party/src/github.com/google/cadvisor/AUTHORS
vendored
Normal file
11
third_party/src/github.com/google/cadvisor/AUTHORS
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
# This is the official list of cAdvisor authors for copyright purposes.
|
||||
# This file is distinct from the CONTRIBUTORS files.
|
||||
# See the latter for an explanation.
|
||||
|
||||
# Names should be added to this file as
|
||||
# Name or Organization <email address>
|
||||
# The email address is not required for organizations.
|
||||
|
||||
# Please keep the list sorted.
|
||||
|
||||
Google Inc.
|
14
third_party/src/github.com/google/cadvisor/CONTRIBUTING.md
vendored
Normal file
14
third_party/src/github.com/google/cadvisor/CONTRIBUTING.md
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
## How to contribute
|
||||
|
||||
Open an issue or a pull request, its that easy!
|
||||
|
||||
## Contributor License Agreements
|
||||
|
||||
We'd love to accept your pull requests! Before we can merge them, we have to jump a couple of legal hurdles.
|
||||
|
||||
Please fill out either the individual or corporate Contributor License Agreement (CLA).
|
||||
|
||||
* If you are an individual writing original source code and you're sure you own the intellectual property, then you'll need to sign an [individual CLA](http://code.google.com/legal/individual-cla-v1.0.html).
|
||||
* If you work for a company that wants to allow you to contribute your work, then you'll need to sign a [corporate CLA](http://code.google.com/legal/corporate-cla-v1.0.html).
|
||||
|
||||
Follow either of the two links above to access the appropriate CLA and instructions for how to sign and return it. Once we receive it, we'll be able to accept your pull requests.
|
11
third_party/src/github.com/google/cadvisor/CONTRIBUTORS
vendored
Normal file
11
third_party/src/github.com/google/cadvisor/CONTRIBUTORS
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
# This is the official list of people who have contributed to the project. The
|
||||
# copyright is held by those individuals or organizations in the AUTHORS file.
|
||||
#
|
||||
# Names should be added to this file like so:
|
||||
# Name <email address>
|
||||
|
||||
# Please keep the list sorted by first name.
|
||||
|
||||
Kamil Yurtsever <kyurtsever@google.com>
|
||||
Nan Deng <dengnan@google.com>
|
||||
Victor Marmol <vmarmol@google.com>
|
16
third_party/src/github.com/google/cadvisor/Dockerfile
vendored
Normal file
16
third_party/src/github.com/google/cadvisor/Dockerfile
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
FROM google/golang-runtime
|
||||
MAINTAINER dengnan@google.com vmarmol@google.com proppy@google.com
|
||||
|
||||
# TODO(vmarmol): Build from source.
|
||||
# Get lmctfy and its dependencies.
|
||||
RUN apt-get update -y --force-yes && apt-get install -y --no-install-recommends --force-yes pkg-config libapparmor1
|
||||
ADD http://storage.googleapis.com/cadvisor-bin/lmctfy/libre2.so.0.0.0 /usr/lib/libre2.so.0
|
||||
ADD http://storage.googleapis.com/cadvisor-bin/lmctfy/lmctfy /usr/bin/lmctfy
|
||||
RUN chmod +x /usr/bin/lmctfy
|
||||
|
||||
# Install libprotobuf8.
|
||||
ADD http://storage.googleapis.com/cadvisor-bin/lmctfy/libprotobuf8_2.5.0-9_amd64.deb /tmp/libprotobuf8_2.5.0-9_amd64.deb
|
||||
ADD http://storage.googleapis.com/cadvisor-bin/lmctfy/libc6_2.19-1_amd64.deb /tmp/libc6_2.19-1_amd64.deb
|
||||
RUN dpkg -i /tmp/libc6_2.19-1_amd64.deb /tmp/libprotobuf8_2.5.0-9_amd64.deb
|
||||
|
||||
# The image builds the app and exposes it on 8080.
|
190
third_party/src/github.com/google/cadvisor/LICENSE
vendored
Normal file
190
third_party/src/github.com/google/cadvisor/LICENSE
vendored
Normal file
@ -0,0 +1,190 @@
|
||||
Copyright 2014 The cAdvisor Authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
93
third_party/src/github.com/google/cadvisor/README.md
vendored
Normal file
93
third_party/src/github.com/google/cadvisor/README.md
vendored
Normal file
@ -0,0 +1,93 @@
|
||||
# cAdvisor
|
||||
|
||||
cAdvisor (Container Advisor) provides container users an understanding of the resource usage and performance characteristics of their running containers. It is a running daemon that collects, aggregates, processes, and exports information about running containers. Specifically, for each container it keeps resource isolation parameters, historical resource usage, and histograms of complete historical resource usage. This data is exported by container and machine-wide.
|
||||
|
||||
cAdvisor currently supports lmctfy containers as well as Docker containers (those that use the default libcontainer execdriver). Other container backends can also be added. cAdvisor's container abstraction is based on lmctfy's so containers are inherently nested hierarchically.
|
||||
|
||||

|
||||
|
||||
#### Quick Start: Running cAdvisor in a Docker Container
|
||||
|
||||
To quickly tryout cAdvisor on your machine with Docker (version 0.11 or above), we have a Docker image that includes everything you need to get started. Simply run:
|
||||
|
||||
```
|
||||
sudo docker run \
|
||||
--volume=/var/run:/var/run:rw \
|
||||
--volume=/sys/fs/cgroup/:/sys/fs/cgroup:ro \
|
||||
--volume=/var/lib/docker/:/var/lib/docker:ro \
|
||||
--publish=8080:8080 \
|
||||
--detach=true \
|
||||
google/cadvisor
|
||||
```
|
||||
|
||||
cAdvisor is now running (in the background) on `http://localhost:8080`. The setup includes directories with Docker state cAdvisor needs to observe.
|
||||
|
||||
If you want to build your own cAdvisor Docker image, take a look at the Dockerfile which can build a cAdvisor docker container under `quickstart/Dockerfile`.
|
||||
|
||||
## Web UI
|
||||
|
||||
cAdvisor exposes a web UI at its port:
|
||||
|
||||
`http://<hostname>:<port>/`
|
||||
|
||||
## Remote REST API
|
||||
|
||||
cAdvisor exposes its raw and processed stats via a versioned remote REST API:
|
||||
|
||||
`http://<hostname>:<port>/api/<version>/<request>`
|
||||
|
||||
The current (and only) version of the API is `v1.0`.
|
||||
|
||||
### Version 1.0
|
||||
|
||||
This version exposes two main endpoints, one for container information and the other for machine information. Both endpoints are read-only in v1.0.
|
||||
|
||||
#### Container Information
|
||||
|
||||
The resource name for container information is as follows:
|
||||
|
||||
`/api/v1.0/containers/<absolute container name>`
|
||||
|
||||
Where the absolute container name follows the lmctfy naming convention. For example:
|
||||
|
||||
| Container Name | Resource Name |
|
||||
|----------------------|-------------------------------------------|
|
||||
| / | /api/v1.0/containers/ |
|
||||
| /foo | /api/v1.0/containers/foo |
|
||||
| /docker/2c4dee605d22 | /api/v1.0/containers/docker/2c4dee605d22 |
|
||||
|
||||
Note that the root container (`/`) contains usage for the entire machine. All Docker containers are listed under `/docker`.
|
||||
|
||||
The container information is returned as a JSON object containing:
|
||||
|
||||
- Absolute container name
|
||||
- List of subcontainers
|
||||
- ContainerSpec which describes the resource isolation enabled in the container
|
||||
- Detailed resource usage statistics of the container for the last `N` seconds (`N` is globally configurable in cAdvisor)
|
||||
- Histogram of resource usage from the creation of the container
|
||||
|
||||
The actual object is the marshalled JSON of the `ContainerInfo` struct found in [info/container.go](info/container.go)
|
||||
|
||||
#### Machine Information
|
||||
|
||||
The resource name for machine information is as follows:
|
||||
|
||||
`/api/v1.0/machine`
|
||||
|
||||
This resource is read-only. The machine information is returned as a JSON object containing:
|
||||
|
||||
- Number of schedulable logical CPU cores
|
||||
- Memory capacity (in bytes)
|
||||
|
||||
The actual object is the marshalled JSON of the `MachineInfo` struct found in [info/machine.go](info/machine.go)
|
||||
|
||||
## Roadmap
|
||||
|
||||
cAdvisor aims to improve the resource usage and performance characteristics of running containers. Today, we gather and expose this information to users. In our roadmap:
|
||||
- Advise on the performance of a container (e.g.: when it is being negatively affected by another, when it is not receiving the resources it requires, etc)
|
||||
- Auto-tune the performance of the container based on previous advise.
|
||||
- Provide usage prediction to cluster schedulers and orchestration layers.
|
||||
|
||||
## Community
|
||||
|
||||
Contributions, questions, and comments are all welcomed and encouraged! cAdvisor developers hand out in [#google-containers](http://webchat.freenode.net/?channels=google-containers) room on freenode.net. We also have the [google-containers Google Groups mailing list](https://groups.google.com/forum/#!forum/google-containers).
|
2
third_party/src/github.com/google/cadvisor/TODO.txt
vendored
Normal file
2
third_party/src/github.com/google/cadvisor/TODO.txt
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
- Allow us to have different tracking policies: only top-level, only specified, all containers
|
||||
- Add ability to checkpoint state (and plugable medium for this)
|
88
third_party/src/github.com/google/cadvisor/api/handler.go
vendored
Normal file
88
third_party/src/github.com/google/cadvisor/api/handler.go
vendored
Normal file
@ -0,0 +1,88 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Handler for /api/
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/cadvisor/manager"
|
||||
)
|
||||
|
||||
const (
|
||||
ApiResource = "/api/v1.0/"
|
||||
ContainersApi = "containers"
|
||||
MachineApi = "machine"
|
||||
)
|
||||
|
||||
func HandleRequest(m manager.Manager, w http.ResponseWriter, u *url.URL) error {
|
||||
start := time.Now()
|
||||
|
||||
// Get API request type.
|
||||
requestType := u.Path[len(ApiResource):]
|
||||
i := strings.Index(requestType, "/")
|
||||
requestArgs := ""
|
||||
if i != -1 {
|
||||
requestArgs = requestType[i:]
|
||||
requestType = requestType[:i]
|
||||
}
|
||||
|
||||
if requestType == MachineApi {
|
||||
log.Printf("Api - Machine")
|
||||
|
||||
// Get the MachineInfo
|
||||
machineInfo, err := m.GetMachineInfo()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
out, err := json.Marshal(machineInfo)
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "Failed to marshall MachineInfo with error: %s", err)
|
||||
}
|
||||
w.Write(out)
|
||||
} else if requestType == ContainersApi {
|
||||
// The container name is the path after the requestType
|
||||
containerName := requestArgs
|
||||
|
||||
log.Printf("Api - Container(%s)", containerName)
|
||||
|
||||
// Get the container.
|
||||
cont, err := m.GetContainerInfo(containerName)
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "Failed to get container \"%s\" with error: %s", containerName, err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Only output the container as JSON.
|
||||
out, err := json.Marshal(cont)
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "Failed to marshall container %q with error: %s", containerName, err)
|
||||
}
|
||||
w.Write(out)
|
||||
} else {
|
||||
return fmt.Errorf("unknown API request type %q", requestType)
|
||||
}
|
||||
|
||||
log.Printf("Request took %s", time.Since(start))
|
||||
return nil
|
||||
}
|
96
third_party/src/github.com/google/cadvisor/cadvisor.go
vendored
Normal file
96
third_party/src/github.com/google/cadvisor/cadvisor.go
vendored
Normal file
@ -0,0 +1,96 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/cadvisor/api"
|
||||
"github.com/google/cadvisor/container/docker"
|
||||
"github.com/google/cadvisor/container/lmctfy"
|
||||
"github.com/google/cadvisor/info"
|
||||
"github.com/google/cadvisor/manager"
|
||||
"github.com/google/cadvisor/pages"
|
||||
"github.com/google/cadvisor/pages/static"
|
||||
"github.com/google/cadvisor/storage/memory"
|
||||
)
|
||||
|
||||
var argPort = flag.Int("port", 8080, "port to listen")
|
||||
var argSampleSize = flag.Int("samples", 1024, "number of samples we want to keep")
|
||||
var argHistoryDuration = flag.Int("history_duration", 60, "number of seconds of container history to keep")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
storage := memory.New(*argSampleSize, *argHistoryDuration)
|
||||
// TODO(monnand): Add stats writer for manager
|
||||
containerManager, err := manager.New(storage)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create a Container Manager: %s", err)
|
||||
}
|
||||
|
||||
if err := lmctfy.Register("/"); err != nil {
|
||||
log.Printf("lmctfy registration failed: %v.", err)
|
||||
log.Print("Running in docker only mode.")
|
||||
if err := docker.Register(containerManager, "/"); err != nil {
|
||||
log.Printf("Docker registration failed: %v.", err)
|
||||
log.Fatalf("Unable to continue without docker or lmctfy.")
|
||||
}
|
||||
}
|
||||
|
||||
if err := docker.Register(containerManager, "/docker"); err != nil {
|
||||
// Ignore this error because we should work with lmctfy only
|
||||
log.Printf("Docker registration failed: %v.", err)
|
||||
log.Print("Running in lmctfy only mode.")
|
||||
}
|
||||
|
||||
// Handler for static content.
|
||||
http.HandleFunc(static.StaticResource, func(w http.ResponseWriter, r *http.Request) {
|
||||
err := static.HandleRequest(w, r.URL)
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "%s", err)
|
||||
}
|
||||
})
|
||||
|
||||
// Handler for the API.
|
||||
http.HandleFunc(api.ApiResource, func(w http.ResponseWriter, r *http.Request) {
|
||||
err := api.HandleRequest(containerManager, w, r.URL)
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "%s", err)
|
||||
}
|
||||
})
|
||||
|
||||
// Redirect / to containers page.
|
||||
http.Handle("/", http.RedirectHandler(pages.ContainersPage, http.StatusTemporaryRedirect))
|
||||
|
||||
// Register the handler for the containers page.
|
||||
http.HandleFunc(pages.ContainersPage, func(w http.ResponseWriter, r *http.Request) {
|
||||
err := pages.ServerContainersPage(containerManager, w, r.URL)
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "%s", err)
|
||||
}
|
||||
})
|
||||
|
||||
go containerManager.Start()
|
||||
|
||||
log.Printf("Starting cAdvisor version: %q", info.VERSION)
|
||||
log.Print("About to serve on port ", *argPort)
|
||||
|
||||
addr := fmt.Sprintf(":%v", *argPort)
|
||||
log.Fatal(http.ListenAndServe(addr, nil))
|
||||
}
|
96
third_party/src/github.com/google/cadvisor/client/client.go
vendored
Normal file
96
third_party/src/github.com/google/cadvisor/client/client.go
vendored
Normal file
@ -0,0 +1,96 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package cadvisor
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/google/cadvisor/info"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
baseUrl string
|
||||
}
|
||||
|
||||
func NewClient(URL string) (*Client, error) {
|
||||
c := &Client{
|
||||
baseUrl: strings.Join([]string{
|
||||
URL,
|
||||
"api/v1.0",
|
||||
}, "/"),
|
||||
}
|
||||
_, err := c.MachineInfo()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (self *Client) machineInfoUrl() string {
|
||||
return strings.Join([]string{self.baseUrl, "machine"}, "/")
|
||||
}
|
||||
|
||||
func (self *Client) MachineInfo() (minfo *info.MachineInfo, err error) {
|
||||
u := self.machineInfoUrl()
|
||||
ret := new(info.MachineInfo)
|
||||
err = self.httpGetJsonData(ret, u, "machine info")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
minfo = ret
|
||||
return
|
||||
}
|
||||
|
||||
func (self *Client) containerInfoUrl(name string) string {
|
||||
if name[0] == '/' {
|
||||
name = name[1:]
|
||||
}
|
||||
return strings.Join([]string{self.baseUrl, "containers", name}, "/")
|
||||
}
|
||||
|
||||
func (self *Client) httpGetJsonData(data interface{}, url, infoName string) error {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("unable to get %v: %v", infoName, err)
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("unable to read all %v: %v", infoName, err)
|
||||
return err
|
||||
}
|
||||
err = json.Unmarshal(body, data)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("unable to unmarshal %v (%v): %v", infoName, string(body), err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *Client) ContainerInfo(name string) (cinfo *info.ContainerInfo, err error) {
|
||||
u := self.containerInfoUrl(name)
|
||||
ret := new(info.ContainerInfo)
|
||||
err = self.httpGetJsonData(ret, u, fmt.Sprintf("container info for %v", name))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
cinfo = ret
|
||||
return
|
||||
}
|
756
third_party/src/github.com/google/cadvisor/client/client_test.go
vendored
Normal file
756
third_party/src/github.com/google/cadvisor/client/client_test.go
vendored
Normal file
@ -0,0 +1,756 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package cadvisor
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/google/cadvisor/info"
|
||||
)
|
||||
|
||||
func testGetJsonData(
|
||||
strRep string,
|
||||
emptyData interface{},
|
||||
f func() (interface{}, error),
|
||||
) error {
|
||||
err := json.Unmarshal([]byte(strRep), emptyData)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid json input: %v", err)
|
||||
}
|
||||
reply, err := f()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to retrieve data: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(reply, emptyData) {
|
||||
return fmt.Errorf("retrieved wrong data: %+v != %+v", reply, emptyData)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func cadvisorTestClient(path, reply string) (*Client, *httptest.Server, error) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == path {
|
||||
fmt.Fprint(w, reply)
|
||||
} else if r.URL.Path == "/api/v1.0/machine" {
|
||||
fmt.Fprint(w, `{"num_cores":8,"memory_capacity":31625871360}`)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
fmt.Fprintf(w, "Page not found.")
|
||||
}
|
||||
}))
|
||||
client, err := NewClient(ts.URL)
|
||||
if err != nil {
|
||||
ts.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
return client, ts, err
|
||||
}
|
||||
|
||||
func TestGetMachineinfo(t *testing.T) {
|
||||
respStr := `{"num_cores":8,"memory_capacity":31625871360}`
|
||||
client, server, err := cadvisorTestClient("/api/v1.0/machine", respStr)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to get a client %v", err)
|
||||
}
|
||||
defer server.Close()
|
||||
err = testGetJsonData(respStr, &info.MachineInfo{}, func() (interface{}, error) {
|
||||
return client.MachineInfo()
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetContainerInfo(t *testing.T) {
|
||||
respStr := `
|
||||
{
|
||||
"name": "%v",
|
||||
"spec": {
|
||||
"cpu": {
|
||||
"limit": 18446744073709551000,
|
||||
"max_limit": 0,
|
||||
"mask": {
|
||||
"data": [
|
||||
18446744073709551000
|
||||
]
|
||||
}
|
||||
},
|
||||
"memory": {
|
||||
"limit": 18446744073709551000,
|
||||
"swap_limit": 18446744073709551000
|
||||
}
|
||||
},
|
||||
"stats": [
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:26.434981825Z",
|
||||
"cpu": {
|
||||
"usage": {
|
||||
"total": 56896502,
|
||||
"per_cpu": [
|
||||
20479682,
|
||||
13579420,
|
||||
6025040,
|
||||
2255123,
|
||||
3635661,
|
||||
2489368,
|
||||
5158288,
|
||||
3273920
|
||||
],
|
||||
"user": 10000000,
|
||||
"system": 30000000
|
||||
},
|
||||
"load": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616,
|
||||
"container_data": {
|
||||
"pgfault": 2279
|
||||
},
|
||||
"hierarchical_data": {
|
||||
"pgfault": 2279
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:27.538394608Z",
|
||||
"cpu": {
|
||||
"usage": {
|
||||
"total": 56896502,
|
||||
"per_cpu": [
|
||||
20479682,
|
||||
13579420,
|
||||
6025040,
|
||||
2255123,
|
||||
3635661,
|
||||
2489368,
|
||||
5158288,
|
||||
3273920
|
||||
],
|
||||
"user": 10000000,
|
||||
"system": 30000000
|
||||
},
|
||||
"load": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616,
|
||||
"container_data": {
|
||||
"pgfault": 2279
|
||||
},
|
||||
"hierarchical_data": {
|
||||
"pgfault": 2279
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:28.640302072Z",
|
||||
"cpu": {
|
||||
"usage": {
|
||||
"total": 56896502,
|
||||
"per_cpu": [
|
||||
20479682,
|
||||
13579420,
|
||||
6025040,
|
||||
2255123,
|
||||
3635661,
|
||||
2489368,
|
||||
5158288,
|
||||
3273920
|
||||
],
|
||||
"user": 10000000,
|
||||
"system": 30000000
|
||||
},
|
||||
"load": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616,
|
||||
"container_data": {
|
||||
"pgfault": 2279
|
||||
},
|
||||
"hierarchical_data": {
|
||||
"pgfault": 2279
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:29.74247308Z",
|
||||
"cpu": {
|
||||
"usage": {
|
||||
"total": 56896502,
|
||||
"per_cpu": [
|
||||
20479682,
|
||||
13579420,
|
||||
6025040,
|
||||
2255123,
|
||||
3635661,
|
||||
2489368,
|
||||
5158288,
|
||||
3273920
|
||||
],
|
||||
"user": 10000000,
|
||||
"system": 30000000
|
||||
},
|
||||
"load": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616,
|
||||
"container_data": {
|
||||
"pgfault": 2279
|
||||
},
|
||||
"hierarchical_data": {
|
||||
"pgfault": 2279
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:30.844494537Z",
|
||||
"cpu": {
|
||||
"usage": {
|
||||
"total": 56896502,
|
||||
"per_cpu": [
|
||||
20479682,
|
||||
13579420,
|
||||
6025040,
|
||||
2255123,
|
||||
3635661,
|
||||
2489368,
|
||||
5158288,
|
||||
3273920
|
||||
],
|
||||
"user": 10000000,
|
||||
"system": 30000000
|
||||
},
|
||||
"load": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616,
|
||||
"container_data": {
|
||||
"pgfault": 2279
|
||||
},
|
||||
"hierarchical_data": {
|
||||
"pgfault": 2279
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:31.946757066Z",
|
||||
"cpu": {
|
||||
"usage": {
|
||||
"total": 56896502,
|
||||
"per_cpu": [
|
||||
20479682,
|
||||
13579420,
|
||||
6025040,
|
||||
2255123,
|
||||
3635661,
|
||||
2489368,
|
||||
5158288,
|
||||
3273920
|
||||
],
|
||||
"user": 10000000,
|
||||
"system": 30000000
|
||||
},
|
||||
"load": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616,
|
||||
"container_data": {
|
||||
"pgfault": 2279
|
||||
},
|
||||
"hierarchical_data": {
|
||||
"pgfault": 2279
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:33.050214062Z",
|
||||
"cpu": {
|
||||
"usage": {
|
||||
"total": 56896502,
|
||||
"per_cpu": [
|
||||
20479682,
|
||||
13579420,
|
||||
6025040,
|
||||
2255123,
|
||||
3635661,
|
||||
2489368,
|
||||
5158288,
|
||||
3273920
|
||||
],
|
||||
"user": 10000000,
|
||||
"system": 30000000
|
||||
},
|
||||
"load": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616,
|
||||
"container_data": {
|
||||
"pgfault": 2279
|
||||
},
|
||||
"hierarchical_data": {
|
||||
"pgfault": 2279
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:34.15222186Z",
|
||||
"cpu": {
|
||||
"usage": {
|
||||
"total": 56896502,
|
||||
"per_cpu": [
|
||||
20479682,
|
||||
13579420,
|
||||
6025040,
|
||||
2255123,
|
||||
3635661,
|
||||
2489368,
|
||||
5158288,
|
||||
3273920
|
||||
],
|
||||
"user": 10000000,
|
||||
"system": 30000000
|
||||
},
|
||||
"load": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616,
|
||||
"container_data": {
|
||||
"pgfault": 2279
|
||||
},
|
||||
"hierarchical_data": {
|
||||
"pgfault": 2279
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:35.25417391Z",
|
||||
"cpu": {
|
||||
"usage": {
|
||||
"total": 56896502,
|
||||
"per_cpu": [
|
||||
20479682,
|
||||
13579420,
|
||||
6025040,
|
||||
2255123,
|
||||
3635661,
|
||||
2489368,
|
||||
5158288,
|
||||
3273920
|
||||
],
|
||||
"user": 10000000,
|
||||
"system": 30000000
|
||||
},
|
||||
"load": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616,
|
||||
"container_data": {
|
||||
"pgfault": 2279
|
||||
},
|
||||
"hierarchical_data": {
|
||||
"pgfault": 2279
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:36.355902169Z",
|
||||
"cpu": {
|
||||
"usage": {
|
||||
"total": 56896502,
|
||||
"per_cpu": [
|
||||
20479682,
|
||||
13579420,
|
||||
6025040,
|
||||
2255123,
|
||||
3635661,
|
||||
2489368,
|
||||
5158288,
|
||||
3273920
|
||||
],
|
||||
"user": 10000000,
|
||||
"system": 30000000
|
||||
},
|
||||
"load": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616,
|
||||
"container_data": {
|
||||
"pgfault": 2279
|
||||
},
|
||||
"hierarchical_data": {
|
||||
"pgfault": 2279
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:37.457585928Z",
|
||||
"cpu": {
|
||||
"usage": {
|
||||
"total": 56896502,
|
||||
"per_cpu": [
|
||||
20479682,
|
||||
13579420,
|
||||
6025040,
|
||||
2255123,
|
||||
3635661,
|
||||
2489368,
|
||||
5158288,
|
||||
3273920
|
||||
],
|
||||
"user": 10000000,
|
||||
"system": 30000000
|
||||
},
|
||||
"load": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616,
|
||||
"container_data": {
|
||||
"pgfault": 2279
|
||||
},
|
||||
"hierarchical_data": {
|
||||
"pgfault": 2279
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:38.559417379Z",
|
||||
"cpu": {
|
||||
"usage": {
|
||||
"total": 56896502,
|
||||
"per_cpu": [
|
||||
20479682,
|
||||
13579420,
|
||||
6025040,
|
||||
2255123,
|
||||
3635661,
|
||||
2489368,
|
||||
5158288,
|
||||
3273920
|
||||
],
|
||||
"user": 10000000,
|
||||
"system": 30000000
|
||||
},
|
||||
"load": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616,
|
||||
"container_data": {
|
||||
"pgfault": 2279
|
||||
},
|
||||
"hierarchical_data": {
|
||||
"pgfault": 2279
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:39.662978029Z",
|
||||
"cpu": {
|
||||
"usage": {
|
||||
"total": 56896502,
|
||||
"per_cpu": [
|
||||
20479682,
|
||||
13579420,
|
||||
6025040,
|
||||
2255123,
|
||||
3635661,
|
||||
2489368,
|
||||
5158288,
|
||||
3273920
|
||||
],
|
||||
"user": 10000000,
|
||||
"system": 30000000
|
||||
},
|
||||
"load": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616,
|
||||
"container_data": {
|
||||
"pgfault": 2279
|
||||
},
|
||||
"hierarchical_data": {
|
||||
"pgfault": 2279
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:40.764671232Z",
|
||||
"cpu": {
|
||||
"usage": {
|
||||
"total": 56896502,
|
||||
"per_cpu": [
|
||||
20479682,
|
||||
13579420,
|
||||
6025040,
|
||||
2255123,
|
||||
3635661,
|
||||
2489368,
|
||||
5158288,
|
||||
3273920
|
||||
],
|
||||
"user": 10000000,
|
||||
"system": 30000000
|
||||
},
|
||||
"load": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616,
|
||||
"container_data": {
|
||||
"pgfault": 2279
|
||||
},
|
||||
"hierarchical_data": {
|
||||
"pgfault": 2279
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:41.866456459Z",
|
||||
"cpu": {
|
||||
"usage": {
|
||||
"total": 56896502,
|
||||
"per_cpu": [
|
||||
20479682,
|
||||
13579420,
|
||||
6025040,
|
||||
2255123,
|
||||
3635661,
|
||||
2489368,
|
||||
5158288,
|
||||
3273920
|
||||
],
|
||||
"user": 10000000,
|
||||
"system": 30000000
|
||||
},
|
||||
"load": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616,
|
||||
"container_data": {
|
||||
"pgfault": 2279
|
||||
},
|
||||
"hierarchical_data": {
|
||||
"pgfault": 2279
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"stats_summary": {
|
||||
"max_memory_usage": 495616,
|
||||
"samples": [
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:27.538394608Z",
|
||||
"duration": 1103412783,
|
||||
"cpu": {
|
||||
"usage": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:28.640302072Z",
|
||||
"duration": 1101907464,
|
||||
"cpu": {
|
||||
"usage": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:29.74247308Z",
|
||||
"duration": 1102171008,
|
||||
"cpu": {
|
||||
"usage": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:30.844494537Z",
|
||||
"duration": 1102021457,
|
||||
"cpu": {
|
||||
"usage": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:31.946757066Z",
|
||||
"duration": 1102262529,
|
||||
"cpu": {
|
||||
"usage": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:33.050214062Z",
|
||||
"duration": 1103456996,
|
||||
"cpu": {
|
||||
"usage": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:34.15222186Z",
|
||||
"duration": 1102007798,
|
||||
"cpu": {
|
||||
"usage": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:35.25417391Z",
|
||||
"duration": 1101952050,
|
||||
"cpu": {
|
||||
"usage": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:36.355902169Z",
|
||||
"duration": 1101728259,
|
||||
"cpu": {
|
||||
"usage": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:37.457585928Z",
|
||||
"duration": 1101683759,
|
||||
"cpu": {
|
||||
"usage": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:38.559417379Z",
|
||||
"duration": 1101831451,
|
||||
"cpu": {
|
||||
"usage": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:39.662978029Z",
|
||||
"duration": 1103560650,
|
||||
"cpu": {
|
||||
"usage": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:40.764671232Z",
|
||||
"duration": 1101693203,
|
||||
"cpu": {
|
||||
"usage": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2014-06-13T01:03:41.866456459Z",
|
||||
"duration": 1101785227,
|
||||
"cpu": {
|
||||
"usage": 0
|
||||
},
|
||||
"memory": {
|
||||
"usage": 495616
|
||||
}
|
||||
}
|
||||
],
|
||||
"memory_usage_percentiles": [
|
||||
{
|
||||
"percentage": 50,
|
||||
"value": 495616
|
||||
},
|
||||
{
|
||||
"percentage": 80,
|
||||
"value": 495616
|
||||
},
|
||||
{
|
||||
"percentage": 90,
|
||||
"value": 495616
|
||||
},
|
||||
{
|
||||
"percentage": 95,
|
||||
"value": 495616
|
||||
},
|
||||
{
|
||||
"percentage": 99,
|
||||
"value": 495616
|
||||
}
|
||||
],
|
||||
"cpu_usage_percentiles": [
|
||||
{
|
||||
"percentage": 50,
|
||||
"value": 0
|
||||
},
|
||||
{
|
||||
"percentage": 80,
|
||||
"value": 0
|
||||
},
|
||||
{
|
||||
"percentage": 90,
|
||||
"value": 0
|
||||
},
|
||||
{
|
||||
"percentage": 95,
|
||||
"value": 0
|
||||
},
|
||||
{
|
||||
"percentage": 99,
|
||||
"value": 0
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
`
|
||||
containerName := "/some/container"
|
||||
respStr = fmt.Sprintf(respStr, containerName)
|
||||
client, server, err := cadvisorTestClient(fmt.Sprintf("/api/v1.0/containers%v", containerName), respStr)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to get a client %v", err)
|
||||
}
|
||||
defer server.Close()
|
||||
err = testGetJsonData(respStr, &info.ContainerInfo{}, func() (interface{}, error) {
|
||||
return client.ContainerInfo(containerName)
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
35
third_party/src/github.com/google/cadvisor/container/container.go
vendored
Normal file
35
third_party/src/github.com/google/cadvisor/container/container.go
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package container
|
||||
|
||||
import "github.com/google/cadvisor/info"
|
||||
|
||||
// Listing types.
|
||||
const (
|
||||
LIST_SELF = iota
|
||||
LIST_RECURSIVE
|
||||
)
|
||||
|
||||
type ListType int
|
||||
|
||||
// Interface for container operation handlers.
|
||||
type ContainerHandler interface {
|
||||
ContainerReference() (info.ContainerReference, error)
|
||||
GetSpec() (*info.ContainerSpec, error)
|
||||
GetStats() (*info.ContainerStats, error)
|
||||
ListContainers(listType ListType) ([]info.ContainerReference, error)
|
||||
ListThreads(listType ListType) ([]int, error)
|
||||
ListProcesses(listType ListType) ([]int, error)
|
||||
}
|
103
third_party/src/github.com/google/cadvisor/container/docker/factory.go
vendored
Normal file
103
third_party/src/github.com/google/cadvisor/container/docker/factory.go
vendored
Normal file
@ -0,0 +1,103 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package docker
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
||||
"github.com/fsouza/go-dockerclient"
|
||||
"github.com/google/cadvisor/container"
|
||||
"github.com/google/cadvisor/info"
|
||||
)
|
||||
|
||||
var ArgDockerEndpoint = flag.String("docker", "unix:///var/run/docker.sock", "docker endpoint")
|
||||
|
||||
type dockerFactory struct {
|
||||
machineInfoFactory info.MachineInfoFactory
|
||||
}
|
||||
|
||||
func (self *dockerFactory) String() string {
|
||||
return "docker"
|
||||
}
|
||||
|
||||
func (self *dockerFactory) NewContainerHandler(name string) (handler container.ContainerHandler, err error) {
|
||||
client, err := docker.NewClient(*ArgDockerEndpoint)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
handler, err = newDockerContainerHandler(
|
||||
client,
|
||||
name,
|
||||
self.machineInfoFactory,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
func parseDockerVersion(full_version_string string) ([]int, error) {
|
||||
version_regexp_string := "(\\d+)\\.(\\d+)\\.(\\d+)"
|
||||
version_re := regexp.MustCompile(version_regexp_string)
|
||||
matches := version_re.FindAllStringSubmatch(full_version_string, -1)
|
||||
if len(matches) != 1 {
|
||||
return nil, fmt.Errorf("Version string \"%v\" doesn't match expected regular expression: \"%v\"", full_version_string, version_regexp_string)
|
||||
}
|
||||
version_string_array := matches[0][1:]
|
||||
version_array := make([]int, 3)
|
||||
for index, version_string := range version_string_array {
|
||||
version, err := strconv.Atoi(version_string)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error while parsing \"%v\" in \"%v\"", version_string, full_version_string)
|
||||
}
|
||||
version_array[index] = version
|
||||
}
|
||||
return version_array, nil
|
||||
}
|
||||
|
||||
// Register root container before running this function!
|
||||
func Register(factory info.MachineInfoFactory, paths ...string) error {
|
||||
client, err := docker.NewClient(*ArgDockerEndpoint)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to communicate with docker daemon: %v", err)
|
||||
}
|
||||
if version, err := client.Version(); err != nil {
|
||||
return fmt.Errorf("unable to communicate with docker daemon: %v", err)
|
||||
} else {
|
||||
expected_version := []int{0, 11, 1}
|
||||
version_string := version.Get("Version")
|
||||
version, err := parseDockerVersion(version_string)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Couldn't parse docker version: %v", err)
|
||||
}
|
||||
for index, number := range version {
|
||||
if number > expected_version[index] {
|
||||
break
|
||||
} else if number < expected_version[index] {
|
||||
return fmt.Errorf("cAdvisor requires docker version above %v but we have found version %v reported as \"%v\"", expected_version, version, version_string)
|
||||
}
|
||||
}
|
||||
}
|
||||
f := &dockerFactory{
|
||||
machineInfoFactory: factory,
|
||||
}
|
||||
for _, p := range paths {
|
||||
if p != "/" && p != "/docker" {
|
||||
return fmt.Errorf("%v cannot be managed by docker", p)
|
||||
}
|
||||
container.RegisterContainerHandlerFactory(p, f)
|
||||
}
|
||||
return nil
|
||||
}
|
281
third_party/src/github.com/google/cadvisor/container/docker/handler.go
vendored
Normal file
281
third_party/src/github.com/google/cadvisor/container/docker/handler.go
vendored
Normal file
@ -0,0 +1,281 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package docker
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/docker/libcontainer"
|
||||
"github.com/docker/libcontainer/cgroups"
|
||||
"github.com/docker/libcontainer/cgroups/fs"
|
||||
"github.com/fsouza/go-dockerclient"
|
||||
"github.com/google/cadvisor/container"
|
||||
"github.com/google/cadvisor/info"
|
||||
)
|
||||
|
||||
type dockerContainerHandler struct {
|
||||
client *docker.Client
|
||||
name string
|
||||
aliases []string
|
||||
machineInfoFactory info.MachineInfoFactory
|
||||
}
|
||||
|
||||
func newDockerContainerHandler(
|
||||
client *docker.Client,
|
||||
name string,
|
||||
machineInfoFactory info.MachineInfoFactory,
|
||||
) (container.ContainerHandler, error) {
|
||||
handler := &dockerContainerHandler{
|
||||
client: client,
|
||||
name: name,
|
||||
machineInfoFactory: machineInfoFactory,
|
||||
}
|
||||
if !handler.isDockerContainer() {
|
||||
return handler, nil
|
||||
}
|
||||
_, id, err := handler.splitName()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid docker container %v: %v", name, err)
|
||||
}
|
||||
ctnr, err := client.InspectContainer(id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to inspect container %v: %v", name, err)
|
||||
}
|
||||
handler.aliases = append(handler.aliases, path.Join("/docker", ctnr.Name))
|
||||
return handler, nil
|
||||
}
|
||||
|
||||
func (self *dockerContainerHandler) ContainerReference() (info.ContainerReference, error) {
|
||||
return info.ContainerReference{
|
||||
Name: self.name,
|
||||
Aliases: self.aliases,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (self *dockerContainerHandler) splitName() (string, string, error) {
|
||||
parent, id := path.Split(self.name)
|
||||
cgroupSelf, err := os.Open("/proc/self/cgroup")
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
scanner := bufio.NewScanner(cgroupSelf)
|
||||
|
||||
subsys := []string{"memory", "cpu"}
|
||||
nestedLevels := 0
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
elems := strings.Split(line, ":")
|
||||
if len(elems) < 3 {
|
||||
continue
|
||||
}
|
||||
for _, s := range subsys {
|
||||
if elems[1] == s {
|
||||
// count how many nested docker containers are there.
|
||||
nestedLevels = strings.Count(elems[2], "/docker")
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if nestedLevels > 0 {
|
||||
// we are running inside a docker container
|
||||
upperLevel := strings.Repeat("../../", nestedLevels)
|
||||
//parent = strings.Join([]string{parent, upperLevel}, "/")
|
||||
parent = fmt.Sprintf("%v%v", upperLevel, parent)
|
||||
}
|
||||
return parent, id, nil
|
||||
}
|
||||
|
||||
func (self *dockerContainerHandler) isDockerRoot() bool {
|
||||
// TODO(dengnan): Should we consider other cases?
|
||||
return self.name == "/docker"
|
||||
}
|
||||
|
||||
func (self *dockerContainerHandler) isRootContainer() bool {
|
||||
return self.name == "/"
|
||||
}
|
||||
|
||||
func (self *dockerContainerHandler) isDockerContainer() bool {
|
||||
return (!self.isDockerRoot()) && (!self.isRootContainer())
|
||||
}
|
||||
|
||||
// TODO(vmarmol): Switch to getting this from libcontainer once we have a solid API.
|
||||
func readLibcontainerSpec(id string) (spec *libcontainer.Container, err error) {
|
||||
dir := "/var/lib/docker/execdriver/native"
|
||||
configPath := path.Join(dir, id, "container.json")
|
||||
f, err := os.Open(configPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
d := json.NewDecoder(f)
|
||||
ret := new(libcontainer.Container)
|
||||
err = d.Decode(ret)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
spec = ret
|
||||
return
|
||||
}
|
||||
|
||||
func libcontainerConfigToContainerSpec(config *libcontainer.Container, mi *info.MachineInfo) *info.ContainerSpec {
|
||||
spec := new(info.ContainerSpec)
|
||||
spec.Memory = new(info.MemorySpec)
|
||||
spec.Memory.Limit = math.MaxUint64
|
||||
spec.Memory.SwapLimit = math.MaxUint64
|
||||
if config.Cgroups.Memory > 0 {
|
||||
spec.Memory.Limit = uint64(config.Cgroups.Memory)
|
||||
}
|
||||
if config.Cgroups.MemorySwap > 0 {
|
||||
spec.Memory.SwapLimit = uint64(config.Cgroups.MemorySwap)
|
||||
}
|
||||
|
||||
// Get CPU info
|
||||
spec.Cpu = new(info.CpuSpec)
|
||||
spec.Cpu.Limit = 1024
|
||||
if config.Cgroups.CpuShares != 0 {
|
||||
spec.Cpu.Limit = uint64(config.Cgroups.CpuShares)
|
||||
}
|
||||
n := (mi.NumCores + 63) / 64
|
||||
spec.Cpu.Mask.Data = make([]uint64, n)
|
||||
for i := 0; i < n; i++ {
|
||||
spec.Cpu.Mask.Data[i] = math.MaxUint64
|
||||
}
|
||||
// TODO(vmarmol): Get CPUs from config.Cgroups.CpusetCpus
|
||||
return spec
|
||||
}
|
||||
|
||||
func (self *dockerContainerHandler) GetSpec() (spec *info.ContainerSpec, err error) {
|
||||
if !self.isDockerContainer() {
|
||||
spec = new(info.ContainerSpec)
|
||||
return
|
||||
}
|
||||
mi, err := self.machineInfoFactory.GetMachineInfo()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, id, err := self.splitName()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
libcontainerSpec, err := readLibcontainerSpec(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
spec = libcontainerConfigToContainerSpec(libcontainerSpec, mi)
|
||||
return
|
||||
}
|
||||
|
||||
func libcontainerToContainerStats(s *cgroups.Stats, mi *info.MachineInfo) *info.ContainerStats {
|
||||
ret := new(info.ContainerStats)
|
||||
ret.Timestamp = time.Now()
|
||||
ret.Cpu = new(info.CpuStats)
|
||||
ret.Cpu.Usage.User = s.CpuStats.CpuUsage.UsageInUsermode
|
||||
ret.Cpu.Usage.System = s.CpuStats.CpuUsage.UsageInKernelmode
|
||||
n := len(s.CpuStats.CpuUsage.PercpuUsage)
|
||||
ret.Cpu.Usage.PerCpu = make([]uint64, n)
|
||||
|
||||
ret.Cpu.Usage.Total = 0
|
||||
for i := 0; i < n; i++ {
|
||||
ret.Cpu.Usage.PerCpu[i] = s.CpuStats.CpuUsage.PercpuUsage[i]
|
||||
ret.Cpu.Usage.Total += s.CpuStats.CpuUsage.PercpuUsage[i]
|
||||
}
|
||||
ret.Memory = new(info.MemoryStats)
|
||||
ret.Memory.Usage = s.MemoryStats.Usage
|
||||
if v, ok := s.MemoryStats.Stats["pgfault"]; ok {
|
||||
ret.Memory.ContainerData.Pgfault = v
|
||||
ret.Memory.HierarchicalData.Pgfault = v
|
||||
}
|
||||
if v, ok := s.MemoryStats.Stats["pgmajfault"]; ok {
|
||||
ret.Memory.ContainerData.Pgmajfault = v
|
||||
ret.Memory.HierarchicalData.Pgmajfault = v
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (self *dockerContainerHandler) GetStats() (stats *info.ContainerStats, err error) {
|
||||
if !self.isDockerContainer() {
|
||||
// Return empty stats for root containers.
|
||||
stats = new(info.ContainerStats)
|
||||
stats.Timestamp = time.Now()
|
||||
return
|
||||
}
|
||||
mi, err := self.machineInfoFactory.GetMachineInfo()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
parent, id, err := self.splitName()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
cg := &cgroups.Cgroup{
|
||||
Parent: parent,
|
||||
Name: id,
|
||||
}
|
||||
s, err := fs.GetStats(cg)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
stats = libcontainerToContainerStats(s, mi)
|
||||
return
|
||||
}
|
||||
|
||||
func (self *dockerContainerHandler) ListContainers(listType container.ListType) ([]info.ContainerReference, error) {
|
||||
if self.isDockerContainer() {
|
||||
return nil, nil
|
||||
}
|
||||
if self.isRootContainer() && listType == container.LIST_SELF {
|
||||
return []info.ContainerReference{info.ContainerReference{Name: "/docker"}}, nil
|
||||
}
|
||||
opt := docker.ListContainersOptions{
|
||||
All: true,
|
||||
}
|
||||
containers, err := self.client.ListContainers(opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ret := make([]info.ContainerReference, 0, len(containers)+1)
|
||||
for _, c := range containers {
|
||||
if !strings.HasPrefix(c.Status, "Up ") {
|
||||
continue
|
||||
}
|
||||
path := fmt.Sprintf("/docker/%v", c.ID)
|
||||
aliases := c.Names
|
||||
ref := info.ContainerReference{
|
||||
Name: path,
|
||||
Aliases: aliases,
|
||||
}
|
||||
ret = append(ret, ref)
|
||||
}
|
||||
if self.isRootContainer() {
|
||||
ret = append(ret, info.ContainerReference{Name: "/docker"})
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (self *dockerContainerHandler) ListThreads(listType container.ListType) ([]int, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (self *dockerContainerHandler) ListProcesses(listType container.ListType) ([]int, error) {
|
||||
return nil, nil
|
||||
}
|
129
third_party/src/github.com/google/cadvisor/container/factory.go
vendored
Normal file
129
third_party/src/github.com/google/cadvisor/container/factory.go
vendored
Normal file
@ -0,0 +1,129 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package container
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type ContainerHandlerFactory interface {
|
||||
NewContainerHandler(name string) (ContainerHandler, error)
|
||||
|
||||
// for testability
|
||||
String() string
|
||||
}
|
||||
|
||||
type factoryTreeNode struct {
|
||||
defaultFactory ContainerHandlerFactory
|
||||
children map[string]*factoryTreeNode
|
||||
}
|
||||
|
||||
func (self *factoryTreeNode) find(elems ...string) ContainerHandlerFactory {
|
||||
node := self
|
||||
for _, elem := range elems {
|
||||
if len(node.children) == 0 {
|
||||
break
|
||||
}
|
||||
if child, ok := node.children[elem]; ok {
|
||||
node = child
|
||||
} else {
|
||||
return node.defaultFactory
|
||||
}
|
||||
}
|
||||
|
||||
return node.defaultFactory
|
||||
}
|
||||
|
||||
func (self *factoryTreeNode) add(factory ContainerHandlerFactory, elems ...string) {
|
||||
node := self
|
||||
for _, elem := range elems {
|
||||
if node.children == nil {
|
||||
node.children = make(map[string]*factoryTreeNode, 16)
|
||||
}
|
||||
child, ok := self.children[elem]
|
||||
if !ok {
|
||||
child = &factoryTreeNode{
|
||||
defaultFactory: node.defaultFactory,
|
||||
children: make(map[string]*factoryTreeNode, 16),
|
||||
}
|
||||
node.children[elem] = child
|
||||
}
|
||||
node = child
|
||||
}
|
||||
node.defaultFactory = factory
|
||||
}
|
||||
|
||||
type factoryManager struct {
|
||||
root *factoryTreeNode
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func dropEmptyString(elems ...string) []string {
|
||||
ret := make([]string, 0, len(elems))
|
||||
for _, e := range elems {
|
||||
if len(e) > 0 {
|
||||
ret = append(ret, e)
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// Must register factory for root container!
|
||||
func (self *factoryManager) Register(path string, factory ContainerHandlerFactory) {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
|
||||
if self.root == nil {
|
||||
self.root = &factoryTreeNode{
|
||||
defaultFactory: nil,
|
||||
children: make(map[string]*factoryTreeNode, 10),
|
||||
}
|
||||
}
|
||||
|
||||
elems := dropEmptyString(strings.Split(path, "/")...)
|
||||
self.root.add(factory, elems...)
|
||||
}
|
||||
|
||||
func (self *factoryManager) NewContainerHandler(path string) (ContainerHandler, error) {
|
||||
self.lock.RLock()
|
||||
defer self.lock.RUnlock()
|
||||
|
||||
if self.root == nil {
|
||||
err := fmt.Errorf("nil factory for container %v: no factory registered", path)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
elems := dropEmptyString(strings.Split(path, "/")...)
|
||||
factory := self.root.find(elems...)
|
||||
if factory == nil {
|
||||
err := fmt.Errorf("nil factory for container %v", path)
|
||||
return nil, err
|
||||
}
|
||||
log.Printf("Container handler factory for %v is %v\n", path, factory)
|
||||
return factory.NewContainerHandler(path)
|
||||
}
|
||||
|
||||
var globalFactoryManager factoryManager
|
||||
|
||||
func RegisterContainerHandlerFactory(path string, factory ContainerHandlerFactory) {
|
||||
globalFactoryManager.Register(path, factory)
|
||||
}
|
||||
|
||||
func NewContainerHandler(path string) (ContainerHandler, error) {
|
||||
return globalFactoryManager.NewContainerHandler(path)
|
||||
}
|
76
third_party/src/github.com/google/cadvisor/container/factory_test.go
vendored
Normal file
76
third_party/src/github.com/google/cadvisor/container/factory_test.go
vendored
Normal file
@ -0,0 +1,76 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package container
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type mockContainerHandlerFactory struct {
|
||||
mock.Mock
|
||||
Name string
|
||||
}
|
||||
|
||||
func (self *mockContainerHandlerFactory) String() string {
|
||||
return self.Name
|
||||
}
|
||||
|
||||
func (self *mockContainerHandlerFactory) NewContainerHandler(name string) (ContainerHandler, error) {
|
||||
args := self.Called(name)
|
||||
return args.Get(0).(ContainerHandler), args.Error(1)
|
||||
}
|
||||
|
||||
func testExpectedFactory(root *factoryTreeNode, path, expectedFactory string, t *testing.T) {
|
||||
elems := dropEmptyString(strings.Split(path, "/")...)
|
||||
factory := root.find(elems...)
|
||||
if factory.String() != expectedFactory {
|
||||
t.Errorf("factory %v should be used to create container %v. but %v is selected",
|
||||
expectedFactory,
|
||||
path,
|
||||
factory)
|
||||
}
|
||||
}
|
||||
|
||||
func testAddFactory(root *factoryTreeNode, path string) *factoryTreeNode {
|
||||
elems := dropEmptyString(strings.Split(path, "/")...)
|
||||
if root == nil {
|
||||
root = &factoryTreeNode{
|
||||
defaultFactory: nil,
|
||||
}
|
||||
}
|
||||
f := &mockContainerHandlerFactory{
|
||||
Name: path,
|
||||
}
|
||||
root.add(f, elems...)
|
||||
return root
|
||||
}
|
||||
|
||||
func TestFactoryTree(t *testing.T) {
|
||||
root := testAddFactory(nil, "/")
|
||||
root = testAddFactory(root, "/docker")
|
||||
root = testAddFactory(root, "/user")
|
||||
root = testAddFactory(root, "/user/special/containers")
|
||||
|
||||
testExpectedFactory(root, "/docker/container", "/docker", t)
|
||||
testExpectedFactory(root, "/docker", "/docker", t)
|
||||
testExpectedFactory(root, "/", "/", t)
|
||||
testExpectedFactory(root, "/user/deep/level/container", "/user", t)
|
||||
testExpectedFactory(root, "/user/special/containers", "/user/special/containers", t)
|
||||
testExpectedFactory(root, "/user/special/containers/container", "/user/special/containers", t)
|
||||
testExpectedFactory(root, "/other", "/", t)
|
||||
}
|
93
third_party/src/github.com/google/cadvisor/container/filter.go
vendored
Normal file
93
third_party/src/github.com/google/cadvisor/container/filter.go
vendored
Normal file
@ -0,0 +1,93 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package container
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/google/cadvisor/info"
|
||||
)
|
||||
|
||||
type containerListFilter struct {
|
||||
filter func(string) bool
|
||||
handler ContainerHandler
|
||||
}
|
||||
|
||||
func (self *containerListFilter) ContainerReference() (info.ContainerReference, error) {
|
||||
return self.handler.ContainerReference()
|
||||
}
|
||||
|
||||
func (self *containerListFilter) GetSpec() (*info.ContainerSpec, error) {
|
||||
return self.handler.GetSpec()
|
||||
}
|
||||
|
||||
func (self *containerListFilter) GetStats() (*info.ContainerStats, error) {
|
||||
return self.handler.GetStats()
|
||||
}
|
||||
|
||||
func (self *containerListFilter) ListContainers(listType ListType) ([]info.ContainerReference, error) {
|
||||
containers, err := self.handler.ListContainers(listType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(containers) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
ret := make([]info.ContainerReference, 0, len(containers))
|
||||
for _, c := range containers {
|
||||
if self.filter(c.Name) {
|
||||
ret = append(ret, c)
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (self *containerListFilter) ListThreads(listType ListType) ([]int, error) {
|
||||
return self.handler.ListThreads(listType)
|
||||
}
|
||||
|
||||
func (self *containerListFilter) ListProcesses(listType ListType) ([]int, error) {
|
||||
return self.handler.ListProcesses(listType)
|
||||
}
|
||||
|
||||
func NewWhiteListFilter(handler ContainerHandler, acceptedPaths ...string) ContainerHandler {
|
||||
filter := func(p string) bool {
|
||||
for _, path := range acceptedPaths {
|
||||
if strings.HasPrefix(p, path) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
return &containerListFilter{
|
||||
filter: filter,
|
||||
handler: handler,
|
||||
}
|
||||
}
|
||||
|
||||
func NewBlackListFilter(handler ContainerHandler, forbiddenPaths ...string) ContainerHandler {
|
||||
filter := func(p string) bool {
|
||||
for _, path := range forbiddenPaths {
|
||||
if strings.HasPrefix(p, path) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
return &containerListFilter{
|
||||
filter: filter,
|
||||
handler: handler,
|
||||
}
|
||||
}
|
127
third_party/src/github.com/google/cadvisor/container/filter_test.go
vendored
Normal file
127
third_party/src/github.com/google/cadvisor/container/filter_test.go
vendored
Normal file
@ -0,0 +1,127 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package container
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/cadvisor/info"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type mockContainerHandler struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (self *mockContainerHandler) GetSpec() (*info.ContainerSpec, error) {
|
||||
args := self.Called()
|
||||
return args.Get(0).(*info.ContainerSpec), args.Error(1)
|
||||
}
|
||||
|
||||
func (self *mockContainerHandler) ContainerReference() (info.ContainerReference, error) {
|
||||
args := self.Called()
|
||||
return args.Get(0).(info.ContainerReference), args.Error(1)
|
||||
}
|
||||
|
||||
func (self *mockContainerHandler) GetStats() (*info.ContainerStats, error) {
|
||||
args := self.Called()
|
||||
return args.Get(0).(*info.ContainerStats), args.Error(1)
|
||||
}
|
||||
|
||||
func (self *mockContainerHandler) ListContainers(listType ListType) ([]info.ContainerReference, error) {
|
||||
args := self.Called(listType)
|
||||
return args.Get(0).([]info.ContainerReference), args.Error(1)
|
||||
}
|
||||
|
||||
func (self *mockContainerHandler) ListThreads(listType ListType) ([]int, error) {
|
||||
args := self.Called(listType)
|
||||
return args.Get(0).([]int), args.Error(1)
|
||||
}
|
||||
|
||||
func (self *mockContainerHandler) ListProcesses(listType ListType) ([]int, error) {
|
||||
args := self.Called(listType)
|
||||
return args.Get(0).([]int), args.Error(1)
|
||||
}
|
||||
|
||||
func TestWhiteListContainerFilter(t *testing.T) {
|
||||
mockc := &mockContainerHandler{}
|
||||
mockc.On("ListContainers", LIST_RECURSIVE).Return(
|
||||
[]info.ContainerReference{
|
||||
info.ContainerReference{Name: "/docker/ee0103"},
|
||||
info.ContainerReference{Name: "/container/created/by/lmctfy"},
|
||||
info.ContainerReference{Name: "/user/something"},
|
||||
},
|
||||
nil,
|
||||
)
|
||||
|
||||
filterPaths := []string{
|
||||
"/docker",
|
||||
"/container",
|
||||
}
|
||||
|
||||
fc := NewWhiteListFilter(mockc, filterPaths...)
|
||||
containers, err := fc.ListContainers(LIST_RECURSIVE)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, c := range containers {
|
||||
legal := false
|
||||
for _, prefix := range filterPaths {
|
||||
if strings.HasPrefix(c.Name, prefix) {
|
||||
legal = true
|
||||
}
|
||||
}
|
||||
if !legal {
|
||||
t.Errorf("%v is not in the white list", c)
|
||||
}
|
||||
}
|
||||
mockc.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestBlackListContainerFilter(t *testing.T) {
|
||||
mockc := &mockContainerHandler{}
|
||||
mockc.On("ListContainers", LIST_RECURSIVE).Return(
|
||||
[]info.ContainerReference{
|
||||
info.ContainerReference{Name: "/docker/ee0103"},
|
||||
info.ContainerReference{Name: "/container/created/by/lmctfy"},
|
||||
info.ContainerReference{Name: "/user/something"},
|
||||
},
|
||||
nil,
|
||||
)
|
||||
|
||||
filterPaths := []string{
|
||||
"/docker",
|
||||
"/container",
|
||||
}
|
||||
|
||||
fc := NewBlackListFilter(mockc, filterPaths...)
|
||||
containers, err := fc.ListContainers(LIST_RECURSIVE)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, c := range containers {
|
||||
legal := true
|
||||
for _, prefix := range filterPaths {
|
||||
if strings.HasPrefix(c.Name, prefix) {
|
||||
legal = false
|
||||
}
|
||||
}
|
||||
if !legal {
|
||||
t.Errorf("%v is in the black list", c)
|
||||
}
|
||||
}
|
||||
mockc.AssertExpectations(t)
|
||||
}
|
52
third_party/src/github.com/google/cadvisor/container/lmctfy/factory.go
vendored
Normal file
52
third_party/src/github.com/google/cadvisor/container/lmctfy/factory.go
vendored
Normal file
@ -0,0 +1,52 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package lmctfy
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"os/exec"
|
||||
|
||||
"github.com/google/cadvisor/container"
|
||||
)
|
||||
|
||||
func Register(paths ...string) error {
|
||||
if _, err := exec.LookPath("lmctfy"); err != nil {
|
||||
return errors.New("cannot find lmctfy")
|
||||
}
|
||||
f := &lmctfyFactory{}
|
||||
for _, path := range paths {
|
||||
log.Printf("register lmctfy under %v", path)
|
||||
container.RegisterContainerHandlerFactory(path, f)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type lmctfyFactory struct {
|
||||
}
|
||||
|
||||
func (self *lmctfyFactory) String() string {
|
||||
return "lmctfy"
|
||||
}
|
||||
|
||||
func (self *lmctfyFactory) NewContainerHandler(name string) (container.ContainerHandler, error) {
|
||||
c, err := New(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// XXX(dengnan): /user is created by ubuntu 14.04. Not sure if we should list it
|
||||
handler := container.NewBlackListFilter(c, "/user")
|
||||
return handler, nil
|
||||
}
|
2103
third_party/src/github.com/google/cadvisor/container/lmctfy/lmctfy.pb.go
vendored
Normal file
2103
third_party/src/github.com/google/cadvisor/container/lmctfy/lmctfy.pb.go
vendored
Normal file
File diff suppressed because it is too large
Load Diff
199
third_party/src/github.com/google/cadvisor/container/lmctfy/lmctfy_container.go
vendored
Normal file
199
third_party/src/github.com/google/cadvisor/container/lmctfy/lmctfy_container.go
vendored
Normal file
@ -0,0 +1,199 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// A container object
|
||||
|
||||
package lmctfy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"code.google.com/p/goprotobuf/proto"
|
||||
"github.com/google/cadvisor/container"
|
||||
"github.com/google/cadvisor/info"
|
||||
)
|
||||
|
||||
type lmctfyContainerHandler struct {
|
||||
// Container name
|
||||
Name string
|
||||
}
|
||||
|
||||
const (
|
||||
lmctfyBinary = "lmctfy"
|
||||
notFoundExitCode = 5
|
||||
)
|
||||
|
||||
// Create a new
|
||||
func New(name string) (container.ContainerHandler, error) {
|
||||
el := &lmctfyContainerHandler{
|
||||
Name: name,
|
||||
}
|
||||
return el, nil
|
||||
}
|
||||
|
||||
func (self *lmctfyContainerHandler) ContainerReference() (info.ContainerReference, error) {
|
||||
return info.ContainerReference{Name: self.Name}, nil
|
||||
}
|
||||
|
||||
func getExitCode(err error) int {
|
||||
msg, ok := err.(*exec.ExitError)
|
||||
if ok {
|
||||
return msg.Sys().(syscall.WaitStatus).ExitStatus()
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func protobufToContainerSpec(pspec *ContainerSpec) *info.ContainerSpec {
|
||||
ret := new(info.ContainerSpec)
|
||||
if pspec.GetCpu() != nil {
|
||||
cpuspec := new(info.CpuSpec)
|
||||
cpuspec.Limit = pspec.GetCpu().GetLimit()
|
||||
cpuspec.MaxLimit = pspec.GetCpu().GetMaxLimit()
|
||||
if pspec.GetCpu().GetMask() != nil {
|
||||
cpuspec.Mask.Data = pspec.GetCpu().GetMask().GetData()
|
||||
}
|
||||
ret.Cpu = cpuspec
|
||||
}
|
||||
if pspec.GetMemory() != nil {
|
||||
pmem := pspec.GetMemory()
|
||||
memspec := new(info.MemorySpec)
|
||||
memspec.Limit = uint64(pmem.GetLimit())
|
||||
memspec.Reservation = uint64(pmem.GetReservation())
|
||||
memspec.SwapLimit = uint64(pmem.GetSwapLimit())
|
||||
ret.Memory = memspec
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// Gets spec.
|
||||
func (c *lmctfyContainerHandler) GetSpec() (*info.ContainerSpec, error) {
|
||||
// Run lmctfy spec "container_name" and get spec.
|
||||
// Ignore if the container was not found.
|
||||
cmd := exec.Command(lmctfyBinary, "spec", string(c.Name))
|
||||
data, err := cmd.Output()
|
||||
if err != nil && getExitCode(err) != notFoundExitCode {
|
||||
return nil, fmt.Errorf("unable to run command %v spec %v: %v", lmctfyBinary, c.Name, err)
|
||||
}
|
||||
|
||||
// Parse output into a protobuf.
|
||||
pspec := &ContainerSpec{}
|
||||
err = proto.UnmarshalText(string(data), pspec)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
spec := protobufToContainerSpec(pspec)
|
||||
return spec, nil
|
||||
}
|
||||
|
||||
func protobufToMemoryData(pmd *MemoryStats_MemoryData, data *info.MemoryStatsMemoryData) {
|
||||
if pmd == nil {
|
||||
return
|
||||
}
|
||||
data.Pgfault = uint64(pmd.GetPgfault())
|
||||
data.Pgmajfault = uint64(pmd.GetPgmajfault())
|
||||
return
|
||||
}
|
||||
|
||||
func protobufToContainerStats(pstats *ContainerStats) *info.ContainerStats {
|
||||
ret := new(info.ContainerStats)
|
||||
if pstats.GetCpu() != nil {
|
||||
pcpu := pstats.GetCpu()
|
||||
cpustats := new(info.CpuStats)
|
||||
cpustats.Usage.Total = pcpu.GetUsage().GetTotal()
|
||||
percpu := pcpu.GetUsage().GetPerCpu()
|
||||
if len(percpu) > 0 {
|
||||
cpustats.Usage.PerCpu = make([]uint64, len(percpu))
|
||||
for i, p := range percpu {
|
||||
cpustats.Usage.PerCpu[i] = uint64(p)
|
||||
}
|
||||
}
|
||||
cpustats.Usage.User = uint64(pcpu.GetUsage().GetUser())
|
||||
cpustats.Usage.System = uint64(pcpu.GetUsage().GetSystem())
|
||||
cpustats.Load = pcpu.GetLoad()
|
||||
ret.Cpu = cpustats
|
||||
}
|
||||
if pstats.GetMemory() != nil {
|
||||
pmem := pstats.GetMemory()
|
||||
memstats := new(info.MemoryStats)
|
||||
memstats.Limit = uint64(pmem.GetLimit())
|
||||
memstats.Usage = uint64(pmem.GetUsage())
|
||||
protobufToMemoryData(pmem.GetContainerData(), &memstats.ContainerData)
|
||||
protobufToMemoryData(pmem.GetHierarchicalData(), &memstats.HierarchicalData)
|
||||
ret.Memory = memstats
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// Gets full stats.
|
||||
func (c *lmctfyContainerHandler) GetStats() (*info.ContainerStats, error) {
|
||||
// Ignore if the container was not found.
|
||||
cmd := exec.Command(lmctfyBinary, "stats", "full", string(c.Name))
|
||||
data, err := cmd.Output()
|
||||
if err != nil && getExitCode(err) != notFoundExitCode {
|
||||
return nil, fmt.Errorf("unable to run command %v stats full %v: %v", lmctfyBinary, c.Name, err)
|
||||
}
|
||||
|
||||
// Parse output into a protobuf.
|
||||
pstats := &ContainerStats{}
|
||||
err = proto.UnmarshalText(string(data), pstats)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stats := protobufToContainerStats(pstats)
|
||||
stats.Timestamp = time.Now()
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// Gets all subcontainers.
|
||||
func (c *lmctfyContainerHandler) ListContainers(listType container.ListType) ([]info.ContainerReference, error) {
|
||||
// Prepare the arguments.
|
||||
args := []string{"list", "containers", "-v"}
|
||||
if listType == container.LIST_RECURSIVE {
|
||||
args = append(args, "-r")
|
||||
}
|
||||
args = append(args, c.Name)
|
||||
|
||||
// Run the command.
|
||||
cmd := exec.Command(lmctfyBinary, args...)
|
||||
data, err := cmd.Output()
|
||||
if err != nil && getExitCode(err) != notFoundExitCode {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Parse lines as container names.
|
||||
if len(data) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
names := strings.Split(string(data), "\n")
|
||||
containerNames := make([]info.ContainerReference, 0, len(names))
|
||||
for _, name := range names {
|
||||
if len(name) != 0 {
|
||||
ref := info.ContainerReference{Name: name}
|
||||
containerNames = append(containerNames, ref)
|
||||
}
|
||||
}
|
||||
return containerNames, nil
|
||||
}
|
||||
|
||||
// TODO(vmarmol): Implement
|
||||
func (c *lmctfyContainerHandler) ListThreads(listType container.ListType) ([]int, error) {
|
||||
return []int{}, nil
|
||||
}
|
||||
func (c *lmctfyContainerHandler) ListProcesses(listType container.ListType) ([]int, error) {
|
||||
return []int{}, nil
|
||||
}
|
273
third_party/src/github.com/google/cadvisor/container/lmctfy/virtual_host.pb.go
vendored
Normal file
273
third_party/src/github.com/google/cadvisor/container/lmctfy/virtual_host.pb.go
vendored
Normal file
@ -0,0 +1,273 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Code generated by protoc-gen-go.
|
||||
// source: virtual_host.proto
|
||||
// DO NOT EDIT!
|
||||
|
||||
package lmctfy
|
||||
|
||||
import proto "code.google.com/p/goprotobuf/proto"
|
||||
import json "encoding/json"
|
||||
import math "math"
|
||||
|
||||
// Reference proto, json, and math imports to suppress error if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = &json.SyntaxError{}
|
||||
var _ = math.Inf
|
||||
|
||||
type Network_Bridge_Type int32
|
||||
|
||||
const (
|
||||
Network_Bridge_ETH Network_Bridge_Type = 0
|
||||
Network_Bridge_OVS Network_Bridge_Type = 1
|
||||
)
|
||||
|
||||
var Network_Bridge_Type_name = map[int32]string{
|
||||
0: "ETH",
|
||||
1: "OVS",
|
||||
}
|
||||
var Network_Bridge_Type_value = map[string]int32{
|
||||
"ETH": 0,
|
||||
"OVS": 1,
|
||||
}
|
||||
|
||||
func (x Network_Bridge_Type) Enum() *Network_Bridge_Type {
|
||||
p := new(Network_Bridge_Type)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
func (x Network_Bridge_Type) String() string {
|
||||
return proto.EnumName(Network_Bridge_Type_name, int32(x))
|
||||
}
|
||||
func (x *Network_Bridge_Type) UnmarshalJSON(data []byte) error {
|
||||
value, err := proto.UnmarshalJSONEnum(Network_Bridge_Type_value, data, "Network_Bridge_Type")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*x = Network_Bridge_Type(value)
|
||||
return nil
|
||||
}
|
||||
|
||||
type Network struct {
|
||||
Interface *string `protobuf:"bytes,1,opt,name=interface" json:"interface,omitempty"`
|
||||
Connection *Network_Connection `protobuf:"bytes,3,opt,name=connection" json:"connection,omitempty"`
|
||||
VirtualIp *Network_VirtualIp `protobuf:"bytes,2,opt,name=virtual_ip" json:"virtual_ip,omitempty"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
}
|
||||
|
||||
func (m *Network) Reset() { *m = Network{} }
|
||||
func (m *Network) String() string { return proto.CompactTextString(m) }
|
||||
func (*Network) ProtoMessage() {}
|
||||
|
||||
func (m *Network) GetInterface() string {
|
||||
if m != nil && m.Interface != nil {
|
||||
return *m.Interface
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *Network) GetConnection() *Network_Connection {
|
||||
if m != nil {
|
||||
return m.Connection
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Network) GetVirtualIp() *Network_VirtualIp {
|
||||
if m != nil {
|
||||
return m.VirtualIp
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type Network_VethPair struct {
|
||||
Outside *string `protobuf:"bytes,1,opt,name=outside" json:"outside,omitempty"`
|
||||
Inside *string `protobuf:"bytes,2,opt,name=inside" json:"inside,omitempty"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
}
|
||||
|
||||
func (m *Network_VethPair) Reset() { *m = Network_VethPair{} }
|
||||
func (m *Network_VethPair) String() string { return proto.CompactTextString(m) }
|
||||
func (*Network_VethPair) ProtoMessage() {}
|
||||
|
||||
func (m *Network_VethPair) GetOutside() string {
|
||||
if m != nil && m.Outside != nil {
|
||||
return *m.Outside
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *Network_VethPair) GetInside() string {
|
||||
if m != nil && m.Inside != nil {
|
||||
return *m.Inside
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type Network_Bridge struct {
|
||||
Name *string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
|
||||
Type *Network_Bridge_Type `protobuf:"varint,2,opt,name=type,enum=containers.Network_Bridge_Type" json:"type,omitempty"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
}
|
||||
|
||||
func (m *Network_Bridge) Reset() { *m = Network_Bridge{} }
|
||||
func (m *Network_Bridge) String() string { return proto.CompactTextString(m) }
|
||||
func (*Network_Bridge) ProtoMessage() {}
|
||||
|
||||
func (m *Network_Bridge) GetName() string {
|
||||
if m != nil && m.Name != nil {
|
||||
return *m.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *Network_Bridge) GetType() Network_Bridge_Type {
|
||||
if m != nil && m.Type != nil {
|
||||
return *m.Type
|
||||
}
|
||||
return Network_Bridge_ETH
|
||||
}
|
||||
|
||||
type Network_Connection struct {
|
||||
VethPair *Network_VethPair `protobuf:"bytes,1,opt,name=veth_pair" json:"veth_pair,omitempty"`
|
||||
Bridge *Network_Bridge `protobuf:"bytes,2,opt,name=bridge" json:"bridge,omitempty"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
}
|
||||
|
||||
func (m *Network_Connection) Reset() { *m = Network_Connection{} }
|
||||
func (m *Network_Connection) String() string { return proto.CompactTextString(m) }
|
||||
func (*Network_Connection) ProtoMessage() {}
|
||||
|
||||
func (m *Network_Connection) GetVethPair() *Network_VethPair {
|
||||
if m != nil {
|
||||
return m.VethPair
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Network_Connection) GetBridge() *Network_Bridge {
|
||||
if m != nil {
|
||||
return m.Bridge
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type Network_VirtualIp struct {
|
||||
Ip *string `protobuf:"bytes,1,opt,name=ip" json:"ip,omitempty"`
|
||||
Netmask *string `protobuf:"bytes,2,opt,name=netmask" json:"netmask,omitempty"`
|
||||
Gateway *string `protobuf:"bytes,3,opt,name=gateway" json:"gateway,omitempty"`
|
||||
Mtu *int32 `protobuf:"varint,4,opt,name=mtu" json:"mtu,omitempty"`
|
||||
IpForward *bool `protobuf:"varint,5,opt,name=ip_forward" json:"ip_forward,omitempty"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
}
|
||||
|
||||
func (m *Network_VirtualIp) Reset() { *m = Network_VirtualIp{} }
|
||||
func (m *Network_VirtualIp) String() string { return proto.CompactTextString(m) }
|
||||
func (*Network_VirtualIp) ProtoMessage() {}
|
||||
|
||||
func (m *Network_VirtualIp) GetIp() string {
|
||||
if m != nil && m.Ip != nil {
|
||||
return *m.Ip
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *Network_VirtualIp) GetNetmask() string {
|
||||
if m != nil && m.Netmask != nil {
|
||||
return *m.Netmask
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *Network_VirtualIp) GetGateway() string {
|
||||
if m != nil && m.Gateway != nil {
|
||||
return *m.Gateway
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *Network_VirtualIp) GetMtu() int32 {
|
||||
if m != nil && m.Mtu != nil {
|
||||
return *m.Mtu
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (m *Network_VirtualIp) GetIpForward() bool {
|
||||
if m != nil && m.IpForward != nil {
|
||||
return *m.IpForward
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type Mounts struct {
|
||||
Mount []*Mounts_Mount `protobuf:"bytes,1,rep,name=mount" json:"mount,omitempty"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
}
|
||||
|
||||
func (m *Mounts) Reset() { *m = Mounts{} }
|
||||
func (m *Mounts) String() string { return proto.CompactTextString(m) }
|
||||
func (*Mounts) ProtoMessage() {}
|
||||
|
||||
func (m *Mounts) GetMount() []*Mounts_Mount {
|
||||
if m != nil {
|
||||
return m.Mount
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type Mounts_Mount struct {
|
||||
Source *string `protobuf:"bytes,1,opt,name=source" json:"source,omitempty"`
|
||||
Target *string `protobuf:"bytes,2,opt,name=target" json:"target,omitempty"`
|
||||
ReadOnly *bool `protobuf:"varint,3,opt,name=read_only" json:"read_only,omitempty"`
|
||||
Private *bool `protobuf:"varint,4,opt,name=private" json:"private,omitempty"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
}
|
||||
|
||||
func (m *Mounts_Mount) Reset() { *m = Mounts_Mount{} }
|
||||
func (m *Mounts_Mount) String() string { return proto.CompactTextString(m) }
|
||||
func (*Mounts_Mount) ProtoMessage() {}
|
||||
|
||||
func (m *Mounts_Mount) GetSource() string {
|
||||
if m != nil && m.Source != nil {
|
||||
return *m.Source
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *Mounts_Mount) GetTarget() string {
|
||||
if m != nil && m.Target != nil {
|
||||
return *m.Target
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *Mounts_Mount) GetReadOnly() bool {
|
||||
if m != nil && m.ReadOnly != nil {
|
||||
return *m.ReadOnly
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (m *Mounts_Mount) GetPrivate() bool {
|
||||
if m != nil && m.Private != nil {
|
||||
return *m.Private
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterEnum("containers.Network_Bridge_Type", Network_Bridge_Type_name, Network_Bridge_Type_value)
|
||||
}
|
9
third_party/src/github.com/google/cadvisor/deploy/docker-only/Dockerfile
vendored
Normal file
9
third_party/src/github.com/google/cadvisor/deploy/docker-only/Dockerfile
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
FROM busybox:ubuntu-14.04
|
||||
MAINTAINER kyurtsever@google.com dengnan@google.com vmarmol@google.com jason@swindle.me
|
||||
|
||||
# Get cAdvisor binaries.
|
||||
ADD http://storage.googleapis.com/cadvisor-bin/cadvisor /usr/bin/cadvisor
|
||||
RUN chmod +x /usr/bin/cadvisor
|
||||
|
||||
EXPOSE 8080
|
||||
ENTRYPOINT ["/usr/bin/cadvisor"]
|
14
third_party/src/github.com/google/cadvisor/deploy/lmctfy-docker/Dockerfile
vendored
Normal file
14
third_party/src/github.com/google/cadvisor/deploy/lmctfy-docker/Dockerfile
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
FROM ubuntu:14.04
|
||||
MAINTAINER kyurtsever@google.com dengnan@google.com vmarmol@google.com
|
||||
|
||||
# Get the lmctfy dependencies.
|
||||
RUN apt-get update && apt-get install -y -q --no-install-recommends pkg-config libprotobuf8 libapparmor1
|
||||
|
||||
# Get the lcmtfy and cAdvisor binaries.
|
||||
ADD http://storage.googleapis.com/cadvisor-bin/lmctfy/lmctfy /usr/bin/lmctfy
|
||||
ADD http://storage.googleapis.com/cadvisor-bin/lmctfy/libre2.so.0.0.0 /usr/lib/libre2.so.0
|
||||
ADD http://storage.googleapis.com/cadvisor-bin/cadvisor /usr/bin/cadvisor
|
||||
RUN chmod +x /usr/bin/lmctfy && chmod +x /usr/bin/cadvisor
|
||||
|
||||
EXPOSE 8080
|
||||
ENTRYPOINT ["/usr/bin/cadvisor"]
|
280
third_party/src/github.com/google/cadvisor/info/container.go
vendored
Normal file
280
third_party/src/github.com/google/cadvisor/info/container.go
vendored
Normal file
@ -0,0 +1,280 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package info
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CpuSpecMask struct {
|
||||
Data []uint64 `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
type CpuSpec struct {
|
||||
Limit uint64 `json:"limit"`
|
||||
MaxLimit uint64 `json:"max_limit"`
|
||||
Mask CpuSpecMask `json:"mask,omitempty"`
|
||||
}
|
||||
|
||||
type MemorySpec struct {
|
||||
// The amount of memory requested. Default is unlimited (-1).
|
||||
// Units: bytes.
|
||||
Limit uint64 `json:"limit,omitempty"`
|
||||
|
||||
// The amount of guaranteed memory. Default is 0.
|
||||
// Units: bytes.
|
||||
Reservation uint64 `json:"reservation,omitempty"`
|
||||
|
||||
// The amount of swap space requested. Default is unlimited (-1).
|
||||
// Units: bytes.
|
||||
SwapLimit uint64 `json:"swap_limit,omitempty"`
|
||||
}
|
||||
|
||||
type ContainerSpec struct {
|
||||
Cpu *CpuSpec `json:"cpu,omitempty"`
|
||||
Memory *MemorySpec `json:"memory,omitempty"`
|
||||
}
|
||||
|
||||
// Container reference contains enough information to uniquely identify a container
|
||||
type ContainerReference struct {
|
||||
// The absolute name of the container.
|
||||
Name string `json:"name"`
|
||||
|
||||
Aliases []string `json:"aliases,omitempty"`
|
||||
}
|
||||
|
||||
type ContainerInfo struct {
|
||||
ContainerReference
|
||||
|
||||
// The direct subcontainers of the current container.
|
||||
Subcontainers []ContainerReference `json:"subcontainers,omitempty"`
|
||||
|
||||
// The isolation used in the container.
|
||||
Spec *ContainerSpec `json:"spec,omitempty"`
|
||||
|
||||
// Historical statistics gathered from the container.
|
||||
Stats []*ContainerStats `json:"stats,omitempty"`
|
||||
|
||||
// Randomly sampled container states.
|
||||
Samples []*ContainerStatsSample `json:"samples,omitempty"`
|
||||
|
||||
StatsPercentiles *ContainerStatsPercentiles `json:"stats_summary,omitempty"`
|
||||
}
|
||||
|
||||
func (self *ContainerInfo) StatsAfter(ref time.Time) []*ContainerStats {
|
||||
n := len(self.Stats) + 1
|
||||
for i, s := range self.Stats {
|
||||
if s.Timestamp.After(ref) {
|
||||
n = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if n > len(self.Stats) {
|
||||
return nil
|
||||
}
|
||||
return self.Stats[n:]
|
||||
}
|
||||
|
||||
func (self *ContainerInfo) StatsStartTime() time.Time {
|
||||
var ret time.Time
|
||||
for _, s := range self.Stats {
|
||||
if s.Timestamp.Before(ret) || ret.IsZero() {
|
||||
ret = s.Timestamp
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (self *ContainerInfo) StatsEndTime() time.Time {
|
||||
var ret time.Time
|
||||
for i := len(self.Stats) - 1; i >= 0; i-- {
|
||||
s := self.Stats[i]
|
||||
if s.Timestamp.After(ret) {
|
||||
ret = s.Timestamp
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// All CPU usage metrics are cumulative from the creation of the container
|
||||
type CpuStats struct {
|
||||
Usage struct {
|
||||
// Total CPU usage.
|
||||
// Units: nanoseconds
|
||||
Total uint64 `json:"total"`
|
||||
|
||||
// Per CPU/core usage of the container.
|
||||
// Unit: nanoseconds.
|
||||
PerCpu []uint64 `json:"per_cpu,omitempty"`
|
||||
|
||||
// Time spent in user space.
|
||||
// Unit: nanoseconds
|
||||
User uint64 `json:"user"`
|
||||
|
||||
// Time spent in kernel space.
|
||||
// Unit: nanoseconds
|
||||
System uint64 `json:"system"`
|
||||
} `json:"usage"`
|
||||
Load int32 `json:"load"`
|
||||
}
|
||||
|
||||
type MemoryStats struct {
|
||||
// Memory limit, equivalent to "limit" in MemorySpec.
|
||||
// Units: Bytes.
|
||||
Limit uint64 `json:"limit,omitempty"`
|
||||
|
||||
// Usage statistics.
|
||||
|
||||
// Current memory usage, this includes all memory regardless of when it was
|
||||
// accessed.
|
||||
// Units: Bytes.
|
||||
Usage uint64 `json:"usage,omitempty"`
|
||||
|
||||
// The amount of working set memory, this includes recently accessed memory,
|
||||
// dirty memory, and kernel memory. Working set is <= "usage".
|
||||
// Units: Bytes.
|
||||
WorkingSet uint64 `json:"working_set,omitempty"`
|
||||
|
||||
ContainerData MemoryStatsMemoryData `json:"container_data,omitempty"`
|
||||
HierarchicalData MemoryStatsMemoryData `json:"hierarchical_data,omitempty"`
|
||||
}
|
||||
|
||||
type MemoryStatsMemoryData struct {
|
||||
Pgfault uint64 `json:"pgfault,omitempty"`
|
||||
Pgmajfault uint64 `json:"pgmajfault,omitempty"`
|
||||
}
|
||||
|
||||
type ContainerStats struct {
|
||||
// The time of this stat point.
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Cpu *CpuStats `json:"cpu,omitempty"`
|
||||
Memory *MemoryStats `json:"memory,omitempty"`
|
||||
}
|
||||
|
||||
type ContainerStatsSample struct {
|
||||
// Timetamp of the end of the sample period
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
// Duration of the sample period
|
||||
Duration time.Duration `json:"duration"`
|
||||
Cpu struct {
|
||||
// number of nanoseconds of CPU time used by the container
|
||||
Usage uint64 `json:"usage"`
|
||||
} `json:"cpu"`
|
||||
Memory struct {
|
||||
// Units: Bytes.
|
||||
Usage uint64 `json:"usage"`
|
||||
} `json:"memory"`
|
||||
}
|
||||
|
||||
type Percentile struct {
|
||||
Percentage int `json:"percentage"`
|
||||
Value uint64 `json:"value"`
|
||||
}
|
||||
|
||||
type ContainerStatsPercentiles struct {
|
||||
MaxMemoryUsage uint64 `json:"max_memory_usage,omitempty"`
|
||||
MemoryUsagePercentiles []Percentile `json:"memory_usage_percentiles,omitempty"`
|
||||
CpuUsagePercentiles []Percentile `json:"cpu_usage_percentiles,omitempty"`
|
||||
}
|
||||
|
||||
// Each sample needs two stats because the cpu usage in ContainerStats is
|
||||
// cumulative.
|
||||
// prev should be an earlier observation than current.
|
||||
// This method is not thread/goroutine safe.
|
||||
func NewSample(prev, current *ContainerStats) (*ContainerStatsSample, error) {
|
||||
if prev == nil || current == nil {
|
||||
return nil, fmt.Errorf("empty stats")
|
||||
}
|
||||
// Ignore this sample if it is incomplete
|
||||
if prev.Cpu == nil || prev.Memory == nil || current.Cpu == nil || current.Memory == nil {
|
||||
return nil, fmt.Errorf("incomplete stats")
|
||||
}
|
||||
// prev must be an early observation
|
||||
if !current.Timestamp.After(prev.Timestamp) {
|
||||
return nil, fmt.Errorf("wrong stats order")
|
||||
}
|
||||
// This data is invalid.
|
||||
if current.Cpu.Usage.Total < prev.Cpu.Usage.Total {
|
||||
return nil, fmt.Errorf("current CPU usage is less than prev CPU usage (cumulative).")
|
||||
}
|
||||
sample := new(ContainerStatsSample)
|
||||
// Calculate the diff to get the CPU usage within the time interval.
|
||||
sample.Cpu.Usage = current.Cpu.Usage.Total - prev.Cpu.Usage.Total
|
||||
// Memory usage is current memory usage
|
||||
sample.Memory.Usage = current.Memory.Usage
|
||||
sample.Timestamp = current.Timestamp
|
||||
sample.Duration = current.Timestamp.Sub(prev.Timestamp)
|
||||
|
||||
return sample, nil
|
||||
}
|
||||
|
||||
type uint64Slice []uint64
|
||||
|
||||
func (self uint64Slice) Len() int {
|
||||
return len(self)
|
||||
}
|
||||
|
||||
func (self uint64Slice) Less(i, j int) bool {
|
||||
return self[i] < self[j]
|
||||
}
|
||||
|
||||
func (self uint64Slice) Swap(i, j int) {
|
||||
self[i], self[j] = self[j], self[i]
|
||||
}
|
||||
|
||||
func (self uint64Slice) Percentiles(requestedPercentiles ...int) []Percentile {
|
||||
if len(self) == 0 {
|
||||
return nil
|
||||
}
|
||||
ret := make([]Percentile, 0, len(requestedPercentiles))
|
||||
sort.Sort(self)
|
||||
for _, p := range requestedPercentiles {
|
||||
idx := (len(self) * p / 100) - 1
|
||||
if idx < 0 {
|
||||
idx = 0
|
||||
}
|
||||
ret = append(
|
||||
ret,
|
||||
Percentile{
|
||||
Percentage: p,
|
||||
Value: self[idx],
|
||||
},
|
||||
)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func NewPercentiles(samples []*ContainerStatsSample, cpuPercentages, memoryPercentages []int) *ContainerStatsPercentiles {
|
||||
if len(samples) == 0 {
|
||||
return nil
|
||||
}
|
||||
cpuUsages := make([]uint64, 0, len(samples))
|
||||
memUsages := make([]uint64, 0, len(samples))
|
||||
|
||||
for _, sample := range samples {
|
||||
if sample == nil {
|
||||
continue
|
||||
}
|
||||
cpuUsages = append(cpuUsages, sample.Cpu.Usage)
|
||||
memUsages = append(memUsages, sample.Memory.Usage)
|
||||
}
|
||||
|
||||
ret := new(ContainerStatsPercentiles)
|
||||
ret.CpuUsagePercentiles = uint64Slice(cpuUsages).Percentiles(cpuPercentages...)
|
||||
ret.MemoryUsagePercentiles = uint64Slice(memUsages).Percentiles(memoryPercentages...)
|
||||
return ret
|
||||
}
|
232
third_party/src/github.com/google/cadvisor/info/container_test.go
vendored
Normal file
232
third_party/src/github.com/google/cadvisor/info/container_test.go
vendored
Normal file
@ -0,0 +1,232 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package info
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestStatsStartTime(t *testing.T) {
|
||||
N := 10
|
||||
stats := make([]*ContainerStats, 0, N)
|
||||
ct := time.Now()
|
||||
for i := 0; i < N; i++ {
|
||||
s := &ContainerStats{
|
||||
Timestamp: ct.Add(time.Duration(i) * time.Second),
|
||||
}
|
||||
stats = append(stats, s)
|
||||
}
|
||||
cinfo := &ContainerInfo{
|
||||
ContainerReference: ContainerReference{
|
||||
Name: "/some/container",
|
||||
},
|
||||
Stats: stats,
|
||||
}
|
||||
ref := ct.Add(time.Duration(N-1) * time.Second)
|
||||
end := cinfo.StatsEndTime()
|
||||
|
||||
if !ref.Equal(end) {
|
||||
t.Errorf("end time is %v; should be %v", end, ref)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatsEndTime(t *testing.T) {
|
||||
N := 10
|
||||
stats := make([]*ContainerStats, 0, N)
|
||||
ct := time.Now()
|
||||
for i := 0; i < N; i++ {
|
||||
s := &ContainerStats{
|
||||
Timestamp: ct.Add(time.Duration(i) * time.Second),
|
||||
}
|
||||
stats = append(stats, s)
|
||||
}
|
||||
cinfo := &ContainerInfo{
|
||||
ContainerReference: ContainerReference{
|
||||
Name: "/some/container",
|
||||
},
|
||||
Stats: stats,
|
||||
}
|
||||
ref := ct
|
||||
start := cinfo.StatsStartTime()
|
||||
|
||||
if !ref.Equal(start) {
|
||||
t.Errorf("start time is %v; should be %v", start, ref)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPercentiles(t *testing.T) {
|
||||
N := 100
|
||||
data := make([]uint64, N)
|
||||
|
||||
for i := 1; i < N+1; i++ {
|
||||
data[i-1] = uint64(i)
|
||||
}
|
||||
percentages := []int{
|
||||
80,
|
||||
90,
|
||||
50,
|
||||
}
|
||||
percentiles := uint64Slice(data).Percentiles(percentages...)
|
||||
for _, s := range percentiles {
|
||||
if s.Value != uint64(s.Percentage) {
|
||||
t.Errorf("%v percentile data should be %v, but got %v", s.Percentage, s.Percentage, s.Value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPercentilesSmallDataSet(t *testing.T) {
|
||||
var value uint64 = 11
|
||||
data := []uint64{value}
|
||||
|
||||
percentages := []int{
|
||||
80,
|
||||
90,
|
||||
50,
|
||||
}
|
||||
percentiles := uint64Slice(data).Percentiles(percentages...)
|
||||
for _, s := range percentiles {
|
||||
if s.Value != value {
|
||||
t.Errorf("%v percentile data should be %v, but got %v", s.Percentage, value, s.Value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewSampleNilStats(t *testing.T) {
|
||||
stats := &ContainerStats{
|
||||
Cpu: &CpuStats{},
|
||||
Memory: &MemoryStats{},
|
||||
}
|
||||
stats.Cpu.Usage.PerCpu = []uint64{uint64(10)}
|
||||
stats.Cpu.Usage.Total = uint64(10)
|
||||
stats.Cpu.Usage.System = uint64(2)
|
||||
stats.Cpu.Usage.User = uint64(8)
|
||||
stats.Memory.Usage = uint64(200)
|
||||
|
||||
sample, err := NewSample(nil, stats)
|
||||
if err == nil {
|
||||
t.Errorf("generated an unexpected sample: %+v", sample)
|
||||
}
|
||||
|
||||
sample, err = NewSample(stats, nil)
|
||||
if err == nil {
|
||||
t.Errorf("generated an unexpected sample: %+v", sample)
|
||||
}
|
||||
}
|
||||
|
||||
func createStats(cpuUsage, memUsage uint64, timestamp time.Time) *ContainerStats {
|
||||
stats := &ContainerStats{
|
||||
Cpu: &CpuStats{},
|
||||
Memory: &MemoryStats{},
|
||||
}
|
||||
stats.Cpu.Usage.PerCpu = []uint64{cpuUsage}
|
||||
stats.Cpu.Usage.Total = cpuUsage
|
||||
stats.Cpu.Usage.System = 0
|
||||
stats.Cpu.Usage.User = cpuUsage
|
||||
stats.Memory.Usage = memUsage
|
||||
stats.Timestamp = timestamp
|
||||
return stats
|
||||
}
|
||||
|
||||
func TestAddSample(t *testing.T) {
|
||||
cpuPrevUsage := uint64(10)
|
||||
cpuCurrentUsage := uint64(15)
|
||||
memCurrentUsage := uint64(200)
|
||||
prevTime := time.Now()
|
||||
|
||||
prev := createStats(cpuPrevUsage, memCurrentUsage, prevTime)
|
||||
current := createStats(cpuCurrentUsage, memCurrentUsage, prevTime.Add(1*time.Second))
|
||||
|
||||
sample, err := NewSample(prev, current)
|
||||
if err != nil {
|
||||
t.Errorf("should be able to generate a sample. but received error: %v", err)
|
||||
}
|
||||
if sample == nil {
|
||||
t.Fatalf("nil sample and nil error. unexpected result!")
|
||||
}
|
||||
|
||||
if sample.Memory.Usage != memCurrentUsage {
|
||||
t.Errorf("wrong memory usage: %v. should be %v", sample.Memory.Usage, memCurrentUsage)
|
||||
}
|
||||
|
||||
if sample.Cpu.Usage != cpuCurrentUsage-cpuPrevUsage {
|
||||
t.Errorf("wrong CPU usage: %v. should be %v", sample.Cpu.Usage, cpuCurrentUsage-cpuPrevUsage)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddSampleIncompleteStats(t *testing.T) {
|
||||
cpuPrevUsage := uint64(10)
|
||||
cpuCurrentUsage := uint64(15)
|
||||
memCurrentUsage := uint64(200)
|
||||
prevTime := time.Now()
|
||||
|
||||
prev := createStats(cpuPrevUsage, memCurrentUsage, prevTime)
|
||||
current := createStats(cpuCurrentUsage, memCurrentUsage, prevTime.Add(1*time.Second))
|
||||
stats := &ContainerStats{
|
||||
Cpu: prev.Cpu,
|
||||
Memory: nil,
|
||||
}
|
||||
sample, err := NewSample(stats, current)
|
||||
if err == nil {
|
||||
t.Errorf("generated an unexpected sample: %+v", sample)
|
||||
}
|
||||
sample, err = NewSample(prev, stats)
|
||||
if err == nil {
|
||||
t.Errorf("generated an unexpected sample: %+v", sample)
|
||||
}
|
||||
|
||||
stats = &ContainerStats{
|
||||
Cpu: nil,
|
||||
Memory: prev.Memory,
|
||||
}
|
||||
sample, err = NewSample(stats, current)
|
||||
if err == nil {
|
||||
t.Errorf("generated an unexpected sample: %+v", sample)
|
||||
}
|
||||
sample, err = NewSample(prev, stats)
|
||||
if err == nil {
|
||||
t.Errorf("generated an unexpected sample: %+v", sample)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddSampleWrongOrder(t *testing.T) {
|
||||
cpuPrevUsage := uint64(10)
|
||||
cpuCurrentUsage := uint64(15)
|
||||
memCurrentUsage := uint64(200)
|
||||
prevTime := time.Now()
|
||||
|
||||
prev := createStats(cpuPrevUsage, memCurrentUsage, prevTime)
|
||||
current := createStats(cpuCurrentUsage, memCurrentUsage, prevTime.Add(1*time.Second))
|
||||
|
||||
sample, err := NewSample(current, prev)
|
||||
if err == nil {
|
||||
t.Errorf("generated an unexpected sample: %+v", sample)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddSampleWrongCpuUsage(t *testing.T) {
|
||||
cpuPrevUsage := uint64(15)
|
||||
cpuCurrentUsage := uint64(10)
|
||||
memCurrentUsage := uint64(200)
|
||||
prevTime := time.Now()
|
||||
|
||||
prev := createStats(cpuPrevUsage, memCurrentUsage, prevTime)
|
||||
current := createStats(cpuCurrentUsage, memCurrentUsage, prevTime.Add(1*time.Second))
|
||||
|
||||
sample, err := NewSample(prev, current)
|
||||
if err == nil {
|
||||
t.Errorf("generated an unexpected sample: %+v", sample)
|
||||
}
|
||||
}
|
42
third_party/src/github.com/google/cadvisor/info/machine.go
vendored
Normal file
42
third_party/src/github.com/google/cadvisor/info/machine.go
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package info
|
||||
|
||||
type MachineInfo struct {
|
||||
// The number of cores in this machine.
|
||||
NumCores int `json:"num_cores"`
|
||||
|
||||
// The amount of memory (in bytes) in this machine
|
||||
MemoryCapacity int64 `json:"memory_capacity"`
|
||||
}
|
||||
|
||||
type VersionInfo struct {
|
||||
// Kernel version.
|
||||
KernelVersion string `json:"kernel_version"`
|
||||
|
||||
// OS image being used for cadvisor container, or host image if running on host directly.
|
||||
ContainerOsVersion string `json:"container_os_version"`
|
||||
|
||||
// Docker version.
|
||||
DockerVersion string `json:"docker_version"`
|
||||
|
||||
// cAdvisor version.
|
||||
CadvisorVersion string `json:"cadvisor_version"`
|
||||
}
|
||||
|
||||
type MachineInfoFactory interface {
|
||||
GetMachineInfo() (*MachineInfo, error)
|
||||
GetVersionInfo() (*VersionInfo, error)
|
||||
}
|
18
third_party/src/github.com/google/cadvisor/info/version.go
vendored
Normal file
18
third_party/src/github.com/google/cadvisor/info/version.go
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package info
|
||||
|
||||
// Version of cAdvisor.
|
||||
const VERSION = "0.1.0"
|
BIN
third_party/src/github.com/google/cadvisor/logo.png
vendored
Normal file
BIN
third_party/src/github.com/google/cadvisor/logo.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 92 KiB |
174
third_party/src/github.com/google/cadvisor/manager/container.go
vendored
Normal file
174
third_party/src/github.com/google/cadvisor/manager/container.go
vendored
Normal file
@ -0,0 +1,174 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Per-container manager.
|
||||
|
||||
package manager
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/cadvisor/container"
|
||||
"github.com/google/cadvisor/info"
|
||||
"github.com/google/cadvisor/storage"
|
||||
)
|
||||
|
||||
// Internal mirror of the external data structure.
|
||||
type containerStat struct {
|
||||
Timestamp time.Time
|
||||
Data *info.ContainerStats
|
||||
}
|
||||
type containerInfo struct {
|
||||
info.ContainerReference
|
||||
Subcontainers []info.ContainerReference
|
||||
Spec *info.ContainerSpec
|
||||
}
|
||||
|
||||
type containerData struct {
|
||||
handler container.ContainerHandler
|
||||
info containerInfo
|
||||
storageDriver storage.StorageDriver
|
||||
lock sync.Mutex
|
||||
|
||||
// Tells the container to stop.
|
||||
stop chan bool
|
||||
}
|
||||
|
||||
func (c *containerData) Start() error {
|
||||
// Force the first update.
|
||||
c.housekeepingTick()
|
||||
log.Printf("Start housekeeping for container %q\n", c.info.Name)
|
||||
|
||||
go c.housekeeping()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *containerData) Stop() error {
|
||||
c.stop <- true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *containerData) GetInfo() (*containerInfo, error) {
|
||||
// TODO(vmarmol): Consider caching this.
|
||||
// Get spec and subcontainers.
|
||||
err := c.updateSpec()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = c.updateSubcontainers()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Make a copy of the info for the user.
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
ret := c.info
|
||||
return &ret, nil
|
||||
}
|
||||
|
||||
func NewContainerData(containerName string, driver storage.StorageDriver) (*containerData, error) {
|
||||
if driver == nil {
|
||||
return nil, fmt.Errorf("nil storage driver")
|
||||
}
|
||||
cont := &containerData{}
|
||||
handler, err := container.NewContainerHandler(containerName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cont.handler = handler
|
||||
ref, err := handler.ContainerReference()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cont.info.Name = ref.Name
|
||||
cont.info.Aliases = ref.Aliases
|
||||
cont.storageDriver = driver
|
||||
cont.stop = make(chan bool, 1)
|
||||
|
||||
return cont, nil
|
||||
}
|
||||
|
||||
func (c *containerData) housekeeping() {
|
||||
// Housekeep every second.
|
||||
ticker := time.NewTicker(1 * time.Second)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-c.stop:
|
||||
// Stop housekeeping when signaled.
|
||||
return
|
||||
case <-ticker.C:
|
||||
start := time.Now()
|
||||
c.housekeepingTick()
|
||||
|
||||
// Log if housekeeping took longer than 120ms.
|
||||
duration := time.Since(start)
|
||||
if duration >= 120*time.Millisecond {
|
||||
log.Printf("Housekeeping(%s) took %s", c.info.Name, duration)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *containerData) housekeepingTick() {
|
||||
err := c.updateStats()
|
||||
if err != nil {
|
||||
log.Printf("Failed to update stats for container \"%s\": %s", c.info.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *containerData) updateSpec() error {
|
||||
spec, err := c.handler.GetSpec()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
c.info.Spec = spec
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *containerData) updateStats() error {
|
||||
stats, err := c.handler.GetStats()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if stats == nil {
|
||||
return nil
|
||||
}
|
||||
ref, err := c.handler.ContainerReference()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = c.storageDriver.AddStats(ref, stats)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *containerData) updateSubcontainers() error {
|
||||
subcontainers, err := c.handler.ListContainers(container.LIST_SELF)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
c.info.Subcontainers = subcontainers
|
||||
return nil
|
||||
}
|
127
third_party/src/github.com/google/cadvisor/manager/machine.go
vendored
Normal file
127
third_party/src/github.com/google/cadvisor/manager/machine.go
vendored
Normal file
@ -0,0 +1,127 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package manager
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
dclient "github.com/fsouza/go-dockerclient"
|
||||
"github.com/google/cadvisor/container/docker"
|
||||
"github.com/google/cadvisor/info"
|
||||
)
|
||||
|
||||
var numCpuRegexp = regexp.MustCompile("processor\\t*: +[0-9]+")
|
||||
var memoryCapacityRegexp = regexp.MustCompile("MemTotal: *([0-9]+) kB")
|
||||
|
||||
func getMachineInfo() (*info.MachineInfo, error) {
|
||||
// Get the number of CPUs from /proc/cpuinfo.
|
||||
out, err := ioutil.ReadFile("/proc/cpuinfo")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
numCores := len(numCpuRegexp.FindAll(out, -1))
|
||||
if numCores == 0 {
|
||||
return nil, fmt.Errorf("failed to count cores in output: %s", string(out))
|
||||
}
|
||||
|
||||
// Get the amount of usable memory from /proc/meminfo.
|
||||
out, err = ioutil.ReadFile("/proc/meminfo")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
matches := memoryCapacityRegexp.FindSubmatch(out)
|
||||
if len(matches) != 2 {
|
||||
return nil, fmt.Errorf("failed to find memory capacity in output: %s", string(out))
|
||||
}
|
||||
memoryCapacity, err := strconv.ParseInt(string(matches[1]), 10, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Capacity is in KB, convert it to bytes.
|
||||
memoryCapacity = memoryCapacity * 1024
|
||||
|
||||
return &info.MachineInfo{
|
||||
NumCores: numCores,
|
||||
MemoryCapacity: memoryCapacity,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func getVersionInfo() (*info.VersionInfo, error) {
|
||||
|
||||
kernel_version := getKernelVersion()
|
||||
container_os := getContainerOsVersion()
|
||||
docker_version := getDockerVersion()
|
||||
|
||||
return &info.VersionInfo{
|
||||
KernelVersion: kernel_version,
|
||||
ContainerOsVersion: container_os,
|
||||
DockerVersion: docker_version,
|
||||
CadvisorVersion: info.VERSION,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func getContainerOsVersion() string {
|
||||
container_os := "Unknown"
|
||||
os_release, err := ioutil.ReadFile("/etc/os-release")
|
||||
if err == nil {
|
||||
// We might be running in a busybox or some hand-crafted image.
|
||||
// It's useful to know why cadvisor didn't come up.
|
||||
for _, line := range strings.Split(string(os_release), "\n") {
|
||||
parsed := strings.Split(line, "\"")
|
||||
if len(parsed) == 3 && parsed[0] == "PRETTY_NAME=" {
|
||||
container_os = parsed[1]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return container_os
|
||||
}
|
||||
|
||||
func getDockerVersion() string {
|
||||
docker_version := "Unknown"
|
||||
client, err := dclient.NewClient(*docker.ArgDockerEndpoint)
|
||||
if err == nil {
|
||||
version, err := client.Version()
|
||||
if err == nil {
|
||||
docker_version = version.Get("Version")
|
||||
}
|
||||
}
|
||||
return docker_version
|
||||
}
|
||||
|
||||
func getKernelVersion() string {
|
||||
uname := &syscall.Utsname{}
|
||||
|
||||
if err := syscall.Uname(uname); err != nil {
|
||||
return "Unknown"
|
||||
}
|
||||
|
||||
release := make([]byte, len(uname.Release))
|
||||
i := 0
|
||||
for _, c := range uname.Release {
|
||||
release[i] = byte(c)
|
||||
i++
|
||||
}
|
||||
release = release[:bytes.IndexByte(release, 0)]
|
||||
|
||||
return string(release)
|
||||
}
|
300
third_party/src/github.com/google/cadvisor/manager/manager.go
vendored
Normal file
300
third_party/src/github.com/google/cadvisor/manager/manager.go
vendored
Normal file
@ -0,0 +1,300 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package manager
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/cadvisor/container"
|
||||
"github.com/google/cadvisor/info"
|
||||
"github.com/google/cadvisor/storage"
|
||||
)
|
||||
|
||||
type Manager interface {
|
||||
// Start the manager, blocks forever.
|
||||
Start() error
|
||||
|
||||
// Get information about a container.
|
||||
GetContainerInfo(containerName string) (*info.ContainerInfo, error)
|
||||
|
||||
// Get information about the machine.
|
||||
GetMachineInfo() (*info.MachineInfo, error)
|
||||
|
||||
// Get version information about different components we depend on.
|
||||
GetVersionInfo() (*info.VersionInfo, error)
|
||||
}
|
||||
|
||||
func New(driver storage.StorageDriver) (Manager, error) {
|
||||
if driver == nil {
|
||||
return nil, fmt.Errorf("nil storage driver!")
|
||||
}
|
||||
newManager := &manager{}
|
||||
newManager.containers = make(map[string]*containerData)
|
||||
|
||||
machineInfo, err := getMachineInfo()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
newManager.machineInfo = *machineInfo
|
||||
log.Printf("Machine: %+v", newManager.machineInfo)
|
||||
|
||||
versionInfo, err := getVersionInfo()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
newManager.versionInfo = *versionInfo
|
||||
log.Printf("Version: %+v", newManager.versionInfo)
|
||||
newManager.storageDriver = driver
|
||||
|
||||
return newManager, nil
|
||||
}
|
||||
|
||||
type manager struct {
|
||||
containers map[string]*containerData
|
||||
containersLock sync.RWMutex
|
||||
storageDriver storage.StorageDriver
|
||||
machineInfo info.MachineInfo
|
||||
versionInfo info.VersionInfo
|
||||
}
|
||||
|
||||
// Start the container manager.
|
||||
func (m *manager) Start() error {
|
||||
// Create root and then recover all containers.
|
||||
_, err := m.createContainer("/")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("Starting recovery of all containers")
|
||||
err = m.detectContainers()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("Recovery completed")
|
||||
|
||||
// Look for new containers in the main housekeeping thread.
|
||||
for t := range time.Tick(time.Second) {
|
||||
start := time.Now()
|
||||
|
||||
// Check for new containers.
|
||||
err = m.detectContainers()
|
||||
if err != nil {
|
||||
log.Printf("Failed to detect containers: %s", err)
|
||||
}
|
||||
|
||||
// Log if housekeeping took more than 100ms.
|
||||
duration := time.Since(start)
|
||||
if duration >= 100*time.Millisecond {
|
||||
log.Printf("Global Housekeeping(%d) took %s", t.Unix(), duration)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get a container by name.
|
||||
func (m *manager) GetContainerInfo(containerName string) (*info.ContainerInfo, error) {
|
||||
log.Printf("Get(%s)", containerName)
|
||||
var cont *containerData
|
||||
var ok bool
|
||||
func() {
|
||||
m.containersLock.RLock()
|
||||
defer m.containersLock.RUnlock()
|
||||
|
||||
// Ensure we have the container.
|
||||
cont, ok = m.containers[containerName]
|
||||
}()
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown container \"%s\"", containerName)
|
||||
}
|
||||
|
||||
// Get the info from the container.
|
||||
cinfo, err := cont.GetInfo()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var percentiles *info.ContainerStatsPercentiles
|
||||
var samples []*info.ContainerStatsSample
|
||||
var stats []*info.ContainerStats
|
||||
// TODO(monnand): These numbers should not be hard coded
|
||||
percentiles, err = m.storageDriver.Percentiles(
|
||||
cinfo.Name,
|
||||
[]int{50, 80, 90, 99},
|
||||
[]int{50, 80, 90, 99},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
samples, err = m.storageDriver.Samples(cinfo.Name, 1024)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stats, err = m.storageDriver.RecentStats(cinfo.Name, 1024)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Make a copy of the info for the user.
|
||||
ret := &info.ContainerInfo{
|
||||
ContainerReference: info.ContainerReference{
|
||||
Name: cinfo.Name,
|
||||
Aliases: cinfo.Aliases,
|
||||
},
|
||||
Subcontainers: cinfo.Subcontainers,
|
||||
Spec: cinfo.Spec,
|
||||
StatsPercentiles: percentiles,
|
||||
Samples: samples,
|
||||
Stats: stats,
|
||||
}
|
||||
|
||||
// Set default value to an actual value
|
||||
if ret.Spec.Memory != nil {
|
||||
// Memory.Limit is 0 means there's no limit
|
||||
if ret.Spec.Memory.Limit == 0 {
|
||||
ret.Spec.Memory.Limit = uint64(m.machineInfo.MemoryCapacity)
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (m *manager) GetMachineInfo() (*info.MachineInfo, error) {
|
||||
// Copy and return the MachineInfo.
|
||||
ret := m.machineInfo
|
||||
return &ret, nil
|
||||
}
|
||||
|
||||
func (m *manager) GetVersionInfo() (*info.VersionInfo, error) {
|
||||
ret := m.versionInfo
|
||||
return &ret, nil
|
||||
}
|
||||
|
||||
// Create a container. This expects to only be called from the global manager thread.
|
||||
func (m *manager) createContainer(containerName string) (*containerData, error) {
|
||||
cont, err := NewContainerData(containerName, m.storageDriver)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Add to the containers map.
|
||||
func() {
|
||||
m.containersLock.Lock()
|
||||
defer m.containersLock.Unlock()
|
||||
|
||||
// Add the container name and all its aliases.
|
||||
m.containers[containerName] = cont
|
||||
for _, alias := range cont.info.Aliases {
|
||||
m.containers[alias] = cont
|
||||
}
|
||||
}()
|
||||
log.Printf("Added container: %s (aliases: %s)", containerName, cont.info.Aliases)
|
||||
|
||||
// Start the container's housekeeping.
|
||||
cont.Start()
|
||||
return cont, nil
|
||||
}
|
||||
|
||||
func (m *manager) destroyContainer(containerName string) error {
|
||||
m.containersLock.Lock()
|
||||
defer m.containersLock.Unlock()
|
||||
|
||||
cont, ok := m.containers[containerName]
|
||||
if !ok {
|
||||
return fmt.Errorf("Expected container \"%s\" to exist during destroy", containerName)
|
||||
}
|
||||
|
||||
// Tell the container to stop.
|
||||
err := cont.Stop()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Remove the container from our records (and all its aliases).
|
||||
delete(m.containers, containerName)
|
||||
for _, alias := range cont.info.Aliases {
|
||||
delete(m.containers, alias)
|
||||
}
|
||||
log.Printf("Destroyed container: %s (aliases: %s)", containerName, cont.info.Aliases)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Detect all containers that have been added or deleted.
|
||||
func (m *manager) getContainersDiff() (added []info.ContainerReference, removed []info.ContainerReference, err error) {
|
||||
// TODO(vmarmol): We probably don't need to lock around / since it will always be there.
|
||||
m.containersLock.RLock()
|
||||
defer m.containersLock.RUnlock()
|
||||
|
||||
// Get all containers on the system.
|
||||
cont, ok := m.containers["/"]
|
||||
if !ok {
|
||||
return nil, nil, fmt.Errorf("Failed to find container \"/\" while checking for new containers")
|
||||
}
|
||||
allContainers, err := cont.handler.ListContainers(container.LIST_RECURSIVE)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
allContainers = append(allContainers, info.ContainerReference{Name: "/"})
|
||||
|
||||
// Determine which were added and which were removed.
|
||||
allContainersSet := make(map[string]*containerData)
|
||||
for name, d := range m.containers {
|
||||
// Only add the canonical name.
|
||||
if d.info.Name == name {
|
||||
allContainersSet[name] = d
|
||||
}
|
||||
}
|
||||
for _, c := range allContainers {
|
||||
delete(allContainersSet, c.Name)
|
||||
_, ok := m.containers[c.Name]
|
||||
if !ok {
|
||||
added = append(added, c)
|
||||
}
|
||||
}
|
||||
|
||||
// Removed ones are no longer in the container listing.
|
||||
for _, d := range allContainersSet {
|
||||
removed = append(removed, d.info.ContainerReference)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Detect the existing containers and reflect the setup here.
|
||||
func (m *manager) detectContainers() error {
|
||||
added, removed, err := m.getContainersDiff()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add the new containers.
|
||||
for _, container := range added {
|
||||
_, err = m.createContainer(container.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to create existing container: %s: %s", container.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the old containers.
|
||||
for _, container := range removed {
|
||||
err = m.destroyContainer(container.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to destroy existing container: %s: %s", container.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
222
third_party/src/github.com/google/cadvisor/pages/containers.go
vendored
Normal file
222
third_party/src/github.com/google/cadvisor/pages/containers.go
vendored
Normal file
@ -0,0 +1,222 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Page for /containers/
|
||||
package pages
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/cadvisor/info"
|
||||
"github.com/google/cadvisor/manager"
|
||||
)
|
||||
|
||||
const ContainersPage = "/containers/"
|
||||
|
||||
var funcMap = template.FuncMap{
|
||||
"containerLink": containerLink,
|
||||
"printMask": printMask,
|
||||
"printCores": printCores,
|
||||
"printMegabytes": printMegabytes,
|
||||
"getMemoryUsage": getMemoryUsage,
|
||||
"getMemoryUsagePercent": getMemoryUsagePercent,
|
||||
"getHotMemoryPercent": getHotMemoryPercent,
|
||||
"getColdMemoryPercent": getColdMemoryPercent,
|
||||
}
|
||||
|
||||
// TODO(vmarmol): Consider housekeeping Spec too so we can show changes through time. We probably don't need it ever second though.
|
||||
|
||||
var pageTemplate *template.Template
|
||||
|
||||
type pageData struct {
|
||||
ContainerName string
|
||||
ParentContainers []info.ContainerReference
|
||||
Subcontainers []info.ContainerReference
|
||||
Spec *info.ContainerSpec
|
||||
Stats []*info.ContainerStats
|
||||
MachineInfo *info.MachineInfo
|
||||
ResourcesAvailable bool
|
||||
CpuAvailable bool
|
||||
MemoryAvailable bool
|
||||
}
|
||||
|
||||
func init() {
|
||||
pageTemplate = template.New("containersTemplate").Funcs(funcMap)
|
||||
_, err := pageTemplate.Parse(containersHtmlTemplate)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse template: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(vmarmol): Escape this correctly.
|
||||
func containerLink(container info.ContainerReference, basenameOnly bool, cssClasses string) interface{} {
|
||||
var displayName string
|
||||
containerName := container.Name
|
||||
if len(container.Aliases) > 0 {
|
||||
displayName = container.Aliases[0]
|
||||
} else if basenameOnly {
|
||||
displayName = path.Base(string(container.Name))
|
||||
} else {
|
||||
displayName = string(container.Name)
|
||||
}
|
||||
if container.Name == "root" {
|
||||
containerName = "/"
|
||||
} else if strings.Contains(container.Name, " ") {
|
||||
// If it has a space, it is an a.k.a, so keep the base-name
|
||||
containerName = container.Name[:strings.Index(container.Name, " ")]
|
||||
}
|
||||
return template.HTML(fmt.Sprintf("<a class=\"%s\" href=\"%s%s\">%s</a>", cssClasses, ContainersPage[:len(ContainersPage)-1], containerName, displayName))
|
||||
}
|
||||
|
||||
func printMask(mask *info.CpuSpecMask, numCores int) interface{} {
|
||||
// TODO(vmarmol): Detect this correctly.
|
||||
// TODO(vmarmol): Support more than 64 cores.
|
||||
rawMask := uint64(0)
|
||||
if len(mask.Data) > 0 {
|
||||
rawMask = mask.Data[0]
|
||||
}
|
||||
masks := make([]string, numCores)
|
||||
for i := uint(0); i < uint(numCores); i++ {
|
||||
coreClass := "inactive-cpu"
|
||||
// by default, all cores are active
|
||||
if ((0x1<<i)&rawMask) != 0 || len(mask.Data) == 0 {
|
||||
coreClass = "active-cpu"
|
||||
}
|
||||
masks[i] = fmt.Sprintf("<span class=\"%s\">%d</span>", coreClass, i)
|
||||
}
|
||||
return template.HTML(strings.Join(masks, " "))
|
||||
}
|
||||
|
||||
func printCores(millicores *uint64) string {
|
||||
// TODO(vmarmol): Detect this correctly
|
||||
if *millicores > 1024*1000 {
|
||||
return "unlimited"
|
||||
}
|
||||
cores := float64(*millicores) / 1000
|
||||
return strconv.FormatFloat(cores, 'f', 3, 64)
|
||||
}
|
||||
|
||||
func toMegabytes(bytes uint64) float64 {
|
||||
return float64(bytes) / (1 << 20)
|
||||
}
|
||||
|
||||
func printMegabytes(bytes uint64) string {
|
||||
// TODO(vmarmol): Detect this correctly
|
||||
if bytes > (100 << 30) {
|
||||
return "unlimited"
|
||||
}
|
||||
megabytes := toMegabytes(bytes)
|
||||
return strconv.FormatFloat(megabytes, 'f', 3, 64)
|
||||
}
|
||||
|
||||
func toMemoryPercent(usage uint64, spec *info.ContainerSpec, machine *info.MachineInfo) int {
|
||||
// Saturate limit to the machine size.
|
||||
limit := uint64(spec.Memory.Limit)
|
||||
if limit > uint64(machine.MemoryCapacity) {
|
||||
limit = uint64(machine.MemoryCapacity)
|
||||
}
|
||||
|
||||
return int((usage * 100) / limit)
|
||||
}
|
||||
|
||||
func getMemoryUsage(stats []*info.ContainerStats) string {
|
||||
return strconv.FormatFloat(toMegabytes((stats[len(stats)-1].Memory.Usage)), 'f', 2, 64)
|
||||
}
|
||||
|
||||
func getMemoryUsagePercent(spec *info.ContainerSpec, stats []*info.ContainerStats, machine *info.MachineInfo) int {
|
||||
return toMemoryPercent((stats[len(stats)-1].Memory.Usage), spec, machine)
|
||||
}
|
||||
|
||||
func getHotMemoryPercent(spec *info.ContainerSpec, stats []*info.ContainerStats, machine *info.MachineInfo) int {
|
||||
return toMemoryPercent((stats[len(stats)-1].Memory.WorkingSet), spec, machine)
|
||||
}
|
||||
|
||||
func getColdMemoryPercent(spec *info.ContainerSpec, stats []*info.ContainerStats, machine *info.MachineInfo) int {
|
||||
latestStats := stats[len(stats)-1].Memory
|
||||
return toMemoryPercent((latestStats.Usage)-(latestStats.WorkingSet), spec, machine)
|
||||
}
|
||||
|
||||
func ServerContainersPage(m manager.Manager, w http.ResponseWriter, u *url.URL) error {
|
||||
start := time.Now()
|
||||
|
||||
// The container name is the path after the handler
|
||||
containerName := u.Path[len(ContainersPage)-1:]
|
||||
|
||||
// Get the container.
|
||||
cont, err := m.GetContainerInfo(containerName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to get container \"%s\" with error: %s", containerName, err)
|
||||
}
|
||||
|
||||
// Get the MachineInfo
|
||||
machineInfo, err := m.GetMachineInfo()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Make a list of the parent containers and their links
|
||||
var parentContainers []info.ContainerReference
|
||||
parentContainers = append(parentContainers, info.ContainerReference{Name: "root"})
|
||||
parentName := ""
|
||||
for _, part := range strings.Split(string(cont.Name), "/") {
|
||||
if part == "" {
|
||||
continue
|
||||
}
|
||||
parentName += "/" + part
|
||||
parentContainers = append(parentContainers, info.ContainerReference{Name: parentName})
|
||||
}
|
||||
|
||||
// Pick the shortest name of the container as the display name.
|
||||
displayName := cont.Name
|
||||
for _, alias := range cont.Aliases {
|
||||
if len(displayName) >= len(alias) {
|
||||
displayName = alias
|
||||
}
|
||||
}
|
||||
|
||||
// Replace the last part of the parent containers with the displayName.
|
||||
if displayName != cont.Name {
|
||||
parentContainers[len(parentContainers)-1] = info.ContainerReference{
|
||||
Name: fmt.Sprintf("%s (%s)", displayName, path.Base(cont.Name)),
|
||||
}
|
||||
}
|
||||
|
||||
data := &pageData{
|
||||
ContainerName: displayName,
|
||||
// TODO(vmarmol): Only use strings for this.
|
||||
ParentContainers: parentContainers,
|
||||
Subcontainers: cont.Subcontainers,
|
||||
Spec: cont.Spec,
|
||||
Stats: cont.Stats,
|
||||
MachineInfo: machineInfo,
|
||||
ResourcesAvailable: cont.Spec.Cpu != nil || cont.Spec.Memory != nil,
|
||||
CpuAvailable: cont.Spec.Cpu != nil,
|
||||
MemoryAvailable: cont.Spec.Memory != nil,
|
||||
}
|
||||
err = pageTemplate.Execute(w, data)
|
||||
if err != nil {
|
||||
log.Printf("Failed to apply template: %s", err)
|
||||
}
|
||||
|
||||
log.Printf("Request took %s", time.Since(start))
|
||||
return nil
|
||||
}
|
160
third_party/src/github.com/google/cadvisor/pages/containers_html.go
vendored
Normal file
160
third_party/src/github.com/google/cadvisor/pages/containers_html.go
vendored
Normal file
@ -0,0 +1,160 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package pages
|
||||
|
||||
const containersHtmlTemplate = `
|
||||
<html>
|
||||
<head>
|
||||
<title>cAdvisor - Container {{.ContainerName}}</title>
|
||||
<!-- Latest compiled and minified CSS -->
|
||||
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
|
||||
|
||||
<!-- Optional theme -->
|
||||
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">
|
||||
|
||||
<link rel="stylesheet" href="/static/containers.css">
|
||||
|
||||
<!-- Latest compiled and minified JavaScript -->
|
||||
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
|
||||
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
|
||||
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
|
||||
|
||||
<script type="text/javascript" src="/static/containers.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container theme-showcase" >
|
||||
<div class="col-sm-12" id="logo">
|
||||
</div>
|
||||
<div class="col-sm-12">
|
||||
<div class="page-header">
|
||||
<h1>{{.ContainerName}}</h1>
|
||||
</div>
|
||||
<ol class="breadcrumb">
|
||||
{{range $parentContainer := .ParentContainers}}
|
||||
<li>{{containerLink $parentContainer true ""}}</li>
|
||||
{{end}}
|
||||
</ol>
|
||||
</div>
|
||||
{{if .Subcontainers}}
|
||||
<div class="col-sm-12">
|
||||
<div class="page-header">
|
||||
<h3>Subcontainers</h3>
|
||||
</div>
|
||||
<div class="list-group">
|
||||
{{range $subcontainer := .Subcontainers}}
|
||||
{{containerLink $subcontainer false "list-group-item"}}
|
||||
{{end}}
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
{{if .ResourcesAvailable}}
|
||||
<div class="col-sm-12">
|
||||
<div class="page-header">
|
||||
<h3>Isolation</h3>
|
||||
</div>
|
||||
{{if .CpuAvailable}}
|
||||
<ul class="list-group">
|
||||
<li class="list-group-item active isolation-title panel-title">CPU</li>
|
||||
{{if .Spec.Cpu.Limit}}
|
||||
<li class="list-group-item"><span class="stat-label">Limit</span> {{printCores .Spec.Cpu.Limit}} <span class="unit-label">cores</span></li>
|
||||
{{end}}
|
||||
{{if .Spec.Cpu.MaxLimit}}
|
||||
<li class="list-group-item"><span class="stat-label">Max Limit</span> {{printCores .Spec.Cpu.MaxLimit}} <span class="unit-label">cores</span></li>
|
||||
{{end}}
|
||||
{{if .Spec.Cpu.Mask}}
|
||||
<li class="list-group-item"><span class="stat-label">Allowed Cores</span> {{printMask .Spec.Cpu.Mask .MachineInfo.NumCores}}</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
{{end}}
|
||||
{{if .MemoryAvailable}}
|
||||
<ul class="list-group">
|
||||
<li class="list-group-item active isolation-title panel-title">Memory</li>
|
||||
{{if .Spec.Memory.Reservation}}
|
||||
<li class="list-group-item"><span class="stat-label">Reservation</span> {{printMegabytes .Spec.Memory.Reservation}} <span class="unit-label">MB</span></li>
|
||||
{{end}}
|
||||
{{if .Spec.Memory.Limit}}
|
||||
<li class="list-group-item"><span class="stat-label">Limit</span> {{printMegabytes .Spec.Memory.Limit}} <span class="unit-label">MB</span></li>
|
||||
{{end}}
|
||||
{{if .Spec.Memory.SwapLimit}}
|
||||
<li class="list-group-item"><span class="stat-label">Swap Limit</span> {{printMegabytes .Spec.Memory.SwapLimit}} <span class="unit-label">MB</span></li>
|
||||
{{end}}
|
||||
</ul>
|
||||
{{end}}
|
||||
</div>
|
||||
<div class="col-sm-12">
|
||||
<div class="page-header">
|
||||
<h3>Usage</h3>
|
||||
</div>
|
||||
<div class="panel panel-primary">
|
||||
<div class="panel-heading">
|
||||
<h3 class="panel-title">Overview</h3>
|
||||
</div>
|
||||
<div id="usage-gauge" class="panel-body">
|
||||
</div>
|
||||
</div>
|
||||
{{if .CpuAvailable}}
|
||||
<div class="panel panel-primary">
|
||||
<div class="panel-heading">
|
||||
<h3 class="panel-title">CPU</h3>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<h4>Total Usage</h4>
|
||||
<div id="cpu-total-usage-chart"></div>
|
||||
<h4>Usage per Core</h4>
|
||||
<div id="cpu-per-core-usage-chart"></div>
|
||||
<h4>Usage Breakdown</h4>
|
||||
<div id="cpu-usage-breakdown-chart"></div>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
{{if .MemoryAvailable}}
|
||||
<div class="panel panel-primary">
|
||||
<div class="panel-heading">
|
||||
<h3 class="panel-title">Memory</h3>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<h4>Total Usage</h4>
|
||||
<div id="memory-usage-chart"></div>
|
||||
<br/>
|
||||
<div class="row col-sm-12">
|
||||
<h4>Usage Breakdown</h4>
|
||||
<div class="col-sm-9">
|
||||
<div class="progress">
|
||||
<div class="progress-bar progress-bar-danger" style="width: {{getHotMemoryPercent .Spec .Stats .MachineInfo}}%">
|
||||
<span class="sr-only">Hot Memory</span>
|
||||
</div>
|
||||
<div class="progress-bar progress-bar-info" style="width: {{getColdMemoryPercent .Spec .Stats .MachineInfo}}%">
|
||||
<span class="sr-only">Cold Memory</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-sm-3">
|
||||
{{ getMemoryUsage .Stats }} MB ({{ getMemoryUsagePercent .Spec .Stats .MachineInfo}}%)
|
||||
</div>
|
||||
</div>
|
||||
<h4>Page Faults</h4>
|
||||
<div id="memory-page-faults-chart"></div>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
</div>
|
||||
{{end}}
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
startPage({{.ContainerName}}, {{.CpuAvailable}}, {{.MemoryAvailable}});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
`
|
47
third_party/src/github.com/google/cadvisor/pages/static/containers_css.go
vendored
Normal file
47
third_party/src/github.com/google/cadvisor/pages/static/containers_css.go
vendored
Normal file
File diff suppressed because one or more lines are too long
295
third_party/src/github.com/google/cadvisor/pages/static/containers_js.go
vendored
Normal file
295
third_party/src/github.com/google/cadvisor/pages/static/containers_js.go
vendored
Normal file
@ -0,0 +1,295 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package static
|
||||
|
||||
const containersJs = `
|
||||
google.load("visualization", "1", {packages: ["corechart", "gauge"]});
|
||||
|
||||
// Draw a line chart.
|
||||
function drawLineChart(seriesTitles, data, elementId, unit) {
|
||||
// Convert the first column to a Date.
|
||||
for (var i = 0; i < data.length; i++) {
|
||||
if (data[i] != null) {
|
||||
data[i][0] = new Date(data[i][0]);
|
||||
}
|
||||
}
|
||||
|
||||
// Add the definition of each column and the necessary data.
|
||||
var dataTable = new google.visualization.DataTable();
|
||||
dataTable.addColumn('datetime', seriesTitles[0]);
|
||||
for (var i = 1; i < seriesTitles.length; i++) {
|
||||
dataTable.addColumn('number', seriesTitles[i]);
|
||||
}
|
||||
dataTable.addRows(data);
|
||||
|
||||
// Create and draw the visualization.
|
||||
var ac = null;
|
||||
var opts = null;
|
||||
// TODO(vmarmol): Remove this hack, it is to support the old charts and the new charts during the transition.
|
||||
if (window.charts) {
|
||||
if (!(elementId in window.charts)) {
|
||||
ac = new google.visualization.LineChart(document.getElementById(elementId));
|
||||
window.charts[elementId] = ac;
|
||||
}
|
||||
ac = window.charts[elementId];
|
||||
opts = window.chartOptions;
|
||||
} else {
|
||||
ac = new google.visualization.LineChart(document.getElementById(elementId));
|
||||
opts = {};
|
||||
}
|
||||
opts.vAxis = {title: unit};
|
||||
opts.legend = {position: 'bottom'};
|
||||
ac.draw(dataTable, window.chartOptions);
|
||||
}
|
||||
|
||||
// Draw a gauge.
|
||||
function drawGauge(elementId, cpuUsage, memoryUsage) {
|
||||
var gauges = [['Label', 'Value']];
|
||||
if (cpuUsage >= 0) {
|
||||
gauges.push(['CPU', cpuUsage]);
|
||||
}
|
||||
if (memoryUsage >= 0) {
|
||||
gauges.push(['Memory', memoryUsage]);
|
||||
}
|
||||
// Create and populate the data table.
|
||||
var data = google.visualization.arrayToDataTable(gauges);
|
||||
|
||||
// Create and draw the visualization.
|
||||
var options = {
|
||||
width: 400, height: 120,
|
||||
redFrom: 90, redTo: 100,
|
||||
yellowFrom:75, yellowTo: 90,
|
||||
minorTicks: 5,
|
||||
animation: {
|
||||
duration: 900,
|
||||
easing: 'linear'
|
||||
}
|
||||
};
|
||||
var chart = new google.visualization.Gauge(document.getElementById(elementId));
|
||||
chart.draw(data, options);
|
||||
}
|
||||
|
||||
// Get the machine info.
|
||||
function getMachineInfo(callback) {
|
||||
$.getJSON("/api/v1.0/machine", function(data) {
|
||||
callback(data);
|
||||
});
|
||||
}
|
||||
|
||||
// Get the container stats for the specified container.
|
||||
function getStats(containerName, callback) {
|
||||
$.getJSON("/api/v1.0/containers" + containerName, function(data) {
|
||||
callback(data);
|
||||
});
|
||||
}
|
||||
|
||||
// Draw the graph for CPU usage.
|
||||
function drawCpuTotalUsage(elementId, machineInfo, stats) {
|
||||
var titles = ["Time", "Total"];
|
||||
var data = [];
|
||||
for (var i = 1; i < stats.stats.length; i++) {
|
||||
var cur = stats.stats[i];
|
||||
var prev = stats.stats[i - 1];
|
||||
|
||||
// TODO(vmarmol): This assumes we sample every second, use the timestamps.
|
||||
var elements = [];
|
||||
elements.push(cur.timestamp);
|
||||
elements.push((cur.cpu.usage.total - prev.cpu.usage.total) / 1000000000);
|
||||
data.push(elements);
|
||||
}
|
||||
drawLineChart(titles, data, elementId, "Cores");
|
||||
}
|
||||
|
||||
// Draw the graph for per-core CPU usage.
|
||||
function drawCpuPerCoreUsage(elementId, machineInfo, stats) {
|
||||
// Add a title for each core.
|
||||
var titles = ["Time"];
|
||||
for (var i = 0; i < machineInfo.num_cores; i++) {
|
||||
titles.push("Core " + i);
|
||||
}
|
||||
var data = [];
|
||||
for (var i = 1; i < stats.stats.length; i++) {
|
||||
var cur = stats.stats[i];
|
||||
var prev = stats.stats[i - 1];
|
||||
|
||||
var elements = [];
|
||||
elements.push(cur.timestamp);
|
||||
for (var j = 0; j < machineInfo.num_cores; j++) {
|
||||
// TODO(vmarmol): This assumes we sample every second, use the timestamps.
|
||||
elements.push((cur.cpu.usage.per_cpu[j] - prev.cpu.usage.per_cpu[j]) / 1000000000);
|
||||
}
|
||||
data.push(elements);
|
||||
}
|
||||
drawLineChart(titles, data, elementId, "Cores");
|
||||
}
|
||||
|
||||
// Draw the graph for CPU usage breakdown.
|
||||
function drawCpuUsageBreakdown(elementId, containerInfo) {
|
||||
var titles = ["Time", "User", "Kernel"];
|
||||
var data = [];
|
||||
for (var i = 1; i < containerInfo.stats.length; i++) {
|
||||
var cur = containerInfo.stats[i];
|
||||
var prev = containerInfo.stats[i - 1];
|
||||
|
||||
// TODO(vmarmol): This assumes we sample every second, use the timestamps.
|
||||
var elements = [];
|
||||
elements.push(cur.timestamp);
|
||||
elements.push((cur.cpu.usage.user - prev.cpu.usage.user) / 1000000000);
|
||||
elements.push((cur.cpu.usage.system - prev.cpu.usage.system) / 1000000000);
|
||||
data.push(elements);
|
||||
}
|
||||
drawLineChart(titles, data, elementId, "Cores");
|
||||
}
|
||||
|
||||
// Draw the gauges for overall resource usage.
|
||||
function drawOverallUsage(elementId, machineInfo, containerInfo) {
|
||||
var cur = containerInfo.stats[containerInfo.stats.length - 1];
|
||||
|
||||
var cpuUsage = 0;
|
||||
if (containerInfo.spec.cpu && containerInfo.stats.length >= 2) {
|
||||
var prev = containerInfo.stats[containerInfo.stats.length - 2];
|
||||
var rawUsage = cur.cpu.usage.total - prev.cpu.usage.total;
|
||||
|
||||
// Convert to millicores and take the percentage
|
||||
cpuUsage = Math.round(((rawUsage / 1000000) / containerInfo.spec.cpu.limit) * 100);
|
||||
if (cpuUsage > 100) {
|
||||
cpuUsage = 100;
|
||||
}
|
||||
}
|
||||
|
||||
var memoryUsage = 0;
|
||||
if (containerInfo.spec.memory) {
|
||||
// Saturate to the machine size.
|
||||
var limit = containerInfo.spec.memory.limit;
|
||||
if (limit > machineInfo.memory_capacity) {
|
||||
limit = machineInfo.memory_capacity;
|
||||
}
|
||||
|
||||
memoryUsage = Math.round((cur.memory.usage / limit) * 100);
|
||||
}
|
||||
|
||||
drawGauge(elementId, cpuUsage, memoryUsage);
|
||||
}
|
||||
|
||||
var oneMegabyte = 1024 * 1024;
|
||||
|
||||
function drawMemoryUsage(elementId, containerInfo) {
|
||||
var titles = ["Time", "Total"];
|
||||
var data = [];
|
||||
for (var i = 0; i < containerInfo.stats.length; i++) {
|
||||
var cur = containerInfo.stats[i];
|
||||
|
||||
// TODO(vmarmol): This assumes we sample every second, use the timestamps.
|
||||
var elements = [];
|
||||
elements.push(cur.timestamp);
|
||||
elements.push(cur.memory.usage / oneMegabyte);
|
||||
data.push(elements);
|
||||
}
|
||||
drawLineChart(titles, data, elementId, "Megabytes");
|
||||
}
|
||||
|
||||
function drawMemoryPageFaults(elementId, containerInfo) {
|
||||
var titles = ["Time", "Faults", "Major Faults"];
|
||||
var data = [];
|
||||
for (var i = 1; i < containerInfo.stats.length; i++) {
|
||||
var cur = containerInfo.stats[i];
|
||||
var prev = containerInfo.stats[i - 1];
|
||||
|
||||
// TODO(vmarmol): This assumes we sample every second, use the timestamps.
|
||||
var elements = [];
|
||||
elements.push(cur.timestamp);
|
||||
elements.push(cur.memory.hierarchical_data.pgfault - prev.memory.hierarchical_data.pgfault);
|
||||
// TODO(vmarmol): Fix to expose this data.
|
||||
//elements.push(cur.memory.hierarchical_data.pgmajfault - prev.memory.hierarchical_data.pgmajfault);
|
||||
elements.push(0);
|
||||
data.push(elements);
|
||||
}
|
||||
drawLineChart(titles, data, elementId, "Faults");
|
||||
}
|
||||
|
||||
// Expects an array of closures to call. After each execution the JS runtime is given control back before continuing.
|
||||
// This function returns asynchronously
|
||||
function stepExecute(steps) {
|
||||
// No steps, stop.
|
||||
if (steps.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get a step and execute it.
|
||||
var step = steps.shift();
|
||||
step();
|
||||
|
||||
// Schedule the next step.
|
||||
setTimeout(function() {
|
||||
stepExecute(steps);
|
||||
}, 0);
|
||||
}
|
||||
|
||||
// Draw all the charts on the page.
|
||||
function drawCharts(machineInfo, containerInfo) {
|
||||
var steps = [];
|
||||
|
||||
steps.push(function() {
|
||||
drawOverallUsage("usage-gauge", machineInfo, containerInfo)
|
||||
});
|
||||
|
||||
// CPU.
|
||||
steps.push(function() {
|
||||
drawCpuTotalUsage("cpu-total-usage-chart", machineInfo, containerInfo);
|
||||
});
|
||||
steps.push(function() {
|
||||
drawCpuPerCoreUsage("cpu-per-core-usage-chart", machineInfo, containerInfo);
|
||||
});
|
||||
steps.push(function() {
|
||||
drawCpuUsageBreakdown("cpu-usage-breakdown-chart", containerInfo);
|
||||
});
|
||||
|
||||
// Memory.
|
||||
steps.push(function() {
|
||||
drawMemoryUsage("memory-usage-chart", containerInfo);
|
||||
});
|
||||
steps.push(function() {
|
||||
drawMemoryPageFaults("memory-page-faults-chart", containerInfo);
|
||||
});
|
||||
|
||||
stepExecute(steps);
|
||||
}
|
||||
|
||||
// Executed when the page finishes loading.
|
||||
function startPage(containerName, hasCpu, hasMemory) {
|
||||
// Don't fetch data if we don't have any resource.
|
||||
if (!hasCpu && !hasMemory) {
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO(vmarmol): Look into changing the view window to get a smoother animation.
|
||||
window.chartOptions = {
|
||||
curveType: 'function',
|
||||
height: 300,
|
||||
legend:{position:"none"},
|
||||
focusTarget: "category",
|
||||
};
|
||||
window.charts = {};
|
||||
|
||||
// Get machine info, then get the stats every 1s.
|
||||
getMachineInfo(function(machineInfo) {
|
||||
setInterval(function() {
|
||||
getStats(containerName, function(stats){
|
||||
drawCharts(machineInfo, stats);
|
||||
});
|
||||
}, 1000);
|
||||
});
|
||||
}
|
||||
`
|
46
third_party/src/github.com/google/cadvisor/pages/static/static.go
vendored
Normal file
46
third_party/src/github.com/google/cadvisor/pages/static/static.go
vendored
Normal file
@ -0,0 +1,46 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Handler for /static content.
|
||||
|
||||
package static
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const StaticResource = "/static/"
|
||||
|
||||
var staticFiles = map[string]string{
|
||||
"containers.css": containersCss,
|
||||
"containers.js": containersJs,
|
||||
}
|
||||
|
||||
func HandleRequest(w http.ResponseWriter, u *url.URL) error {
|
||||
if len(u.Path) <= len(StaticResource) {
|
||||
return fmt.Errorf("unknown static resource %q", u.Path)
|
||||
}
|
||||
|
||||
// Get the static content if it exists.
|
||||
resource := u.Path[len(StaticResource):]
|
||||
content, ok := staticFiles[resource]
|
||||
if !ok {
|
||||
return fmt.Errorf("unknown static resource %q", resource)
|
||||
}
|
||||
|
||||
_, err := w.Write([]byte(content))
|
||||
return err
|
||||
}
|
51
third_party/src/github.com/google/cadvisor/sampling/autofilter.go
vendored
Normal file
51
third_party/src/github.com/google/cadvisor/sampling/autofilter.go
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package sampling
|
||||
|
||||
type autoFilterSampler struct {
|
||||
// filter will run to remove elements before adding every observation
|
||||
filter func(d interface{}) bool
|
||||
sampler Sampler
|
||||
}
|
||||
|
||||
func (self *autoFilterSampler) Len() int {
|
||||
return self.sampler.Len()
|
||||
}
|
||||
|
||||
func (self *autoFilterSampler) Reset() {
|
||||
self.sampler.Reset()
|
||||
}
|
||||
|
||||
func (self *autoFilterSampler) Map(f func(d interface{})) {
|
||||
self.sampler.Map(f)
|
||||
}
|
||||
|
||||
func (self *autoFilterSampler) Filter(filter func(d interface{}) bool) {
|
||||
self.sampler.Filter(filter)
|
||||
}
|
||||
|
||||
func (self *autoFilterSampler) Update(d interface{}) {
|
||||
self.Filter(self.filter)
|
||||
self.sampler.Update(d)
|
||||
}
|
||||
|
||||
// Add a decorator for sampler. Whenever an Update() is called, the sampler will
|
||||
// call filter() first to remove elements in the decorated sampler.
|
||||
func NewAutoFilterSampler(sampler Sampler, filter func(d interface{}) bool) Sampler {
|
||||
return &autoFilterSampler{
|
||||
filter: filter,
|
||||
sampler: sampler,
|
||||
}
|
||||
}
|
60
third_party/src/github.com/google/cadvisor/sampling/autoreset.go
vendored
Normal file
60
third_party/src/github.com/google/cadvisor/sampling/autoreset.go
vendored
Normal file
@ -0,0 +1,60 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package sampling
|
||||
|
||||
import "time"
|
||||
|
||||
type autoResetSampler struct {
|
||||
shouldReset func(d interface{}) bool
|
||||
sampler Sampler
|
||||
}
|
||||
|
||||
func (self *autoResetSampler) Len() int {
|
||||
return self.sampler.Len()
|
||||
}
|
||||
|
||||
func (self *autoResetSampler) Reset() {
|
||||
self.sampler.Reset()
|
||||
}
|
||||
|
||||
func (self *autoResetSampler) Map(f func(d interface{})) {
|
||||
self.sampler.Map(f)
|
||||
}
|
||||
|
||||
func (self *autoResetSampler) Filter(filter func(d interface{}) bool) {
|
||||
self.sampler.Filter(filter)
|
||||
}
|
||||
|
||||
func (self *autoResetSampler) Update(d interface{}) {
|
||||
if self.shouldReset(d) {
|
||||
self.sampler.Reset()
|
||||
}
|
||||
self.sampler.Update(d)
|
||||
}
|
||||
|
||||
func NewPeriodicallyResetSampler(period time.Duration, sampler Sampler) Sampler {
|
||||
lastRest := time.Now()
|
||||
shouldReset := func(d interface{}) bool {
|
||||
if time.Now().Sub(lastRest) > period {
|
||||
lastRest = time.Now()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
return &autoResetSampler{
|
||||
shouldReset: shouldReset,
|
||||
sampler: sampler,
|
||||
}
|
||||
}
|
171
third_party/src/github.com/google/cadvisor/sampling/chainsample.go
vendored
Normal file
171
third_party/src/github.com/google/cadvisor/sampling/chainsample.go
vendored
Normal file
@ -0,0 +1,171 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package sampling
|
||||
|
||||
import (
|
||||
"log"
|
||||
"math/rand"
|
||||
"sync"
|
||||
|
||||
"github.com/kr/pretty"
|
||||
)
|
||||
|
||||
type empty struct{}
|
||||
|
||||
// Randomly generate number [start,end) except @except.
|
||||
func randInt64Except(start, end int64, except map[int64]empty) int64 {
|
||||
n := end - start
|
||||
ret := rand.Int63n(n) + start
|
||||
for _, ok := except[ret]; ok; _, ok = except[ret] {
|
||||
ret = rand.Int63n(n) + start
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// Basic idea:
|
||||
// Every observation will have a sequence number as its id.
|
||||
// Suppose we want to sample k observations within latest n observations
|
||||
// At first, we generated k random numbers in [0,n). These random numbers
|
||||
// will be used as ids of observations that will be sampled.
|
||||
type chainSampler struct {
|
||||
sampleSize int
|
||||
windowSize int64
|
||||
|
||||
// Every observation will have a sequence number starting from 1.
|
||||
// The sequence number must increase by one for each observation.
|
||||
numObservations int64
|
||||
|
||||
// All samples stored as id -> value.
|
||||
samples map[int64]interface{}
|
||||
|
||||
// The set of id of future observations.
|
||||
futureSamples map[int64]empty
|
||||
|
||||
// The chain of samples: old observation id -> future observation id.
|
||||
// When the old observation expires, the future observation will be
|
||||
// stored as a sample.
|
||||
sampleChain map[int64]int64
|
||||
|
||||
// Replacements are: observations whose previous sample is not expired
|
||||
// id->value.
|
||||
replacements map[int64]interface{}
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func (self *chainSampler) initFutureSamples() {
|
||||
for i := 0; i < self.sampleSize; i++ {
|
||||
n := randInt64Except(1, self.windowSize+1, self.futureSamples)
|
||||
self.futureSamples[n] = empty{}
|
||||
}
|
||||
}
|
||||
|
||||
func (self *chainSampler) arrive(seqNum int64, obv interface{}) {
|
||||
if _, ok := self.futureSamples[seqNum]; !ok {
|
||||
// If this observation is not selected, ignore it.
|
||||
return
|
||||
}
|
||||
|
||||
delete(self.futureSamples, seqNum)
|
||||
|
||||
if len(self.samples) < self.sampleSize {
|
||||
self.samples[seqNum] = obv
|
||||
}
|
||||
self.replacements[seqNum] = obv
|
||||
|
||||
// Select a future observation which will replace current observation
|
||||
// when it expires.
|
||||
futureSeqNum := randInt64Except(seqNum+1, seqNum+self.windowSize+1, self.futureSamples)
|
||||
self.futureSamples[futureSeqNum] = empty{}
|
||||
self.sampleChain[seqNum] = futureSeqNum
|
||||
}
|
||||
|
||||
func (self *chainSampler) expireAndReplace() {
|
||||
expSeqNum := self.numObservations - self.windowSize
|
||||
if _, ok := self.samples[expSeqNum]; !ok {
|
||||
// No sample expires
|
||||
return
|
||||
}
|
||||
delete(self.samples, expSeqNum)
|
||||
// There must be a replacement, otherwise panic.
|
||||
replacementSeqNum := self.sampleChain[expSeqNum]
|
||||
// The sequence number must increase by one for each observation.
|
||||
replacement, ok := self.replacements[replacementSeqNum]
|
||||
if !ok {
|
||||
log.Printf("cannot find %v. which is the replacement of %v\n", replacementSeqNum, expSeqNum)
|
||||
pretty.Printf("chain: %# v\n", self)
|
||||
panic("Should never occur!")
|
||||
}
|
||||
// This observation must have arrived before.
|
||||
self.samples[replacementSeqNum] = replacement
|
||||
}
|
||||
|
||||
func (self *chainSampler) Update(obv interface{}) {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
|
||||
self.numObservations++
|
||||
self.arrive(self.numObservations, obv)
|
||||
self.expireAndReplace()
|
||||
}
|
||||
|
||||
func (self *chainSampler) Len() int {
|
||||
self.lock.RLock()
|
||||
defer self.lock.RUnlock()
|
||||
return len(self.samples)
|
||||
}
|
||||
|
||||
func (self *chainSampler) Reset() {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
self.numObservations = 0
|
||||
self.samples = make(map[int64]interface{}, self.sampleSize)
|
||||
self.futureSamples = make(map[int64]empty, self.sampleSize*2)
|
||||
self.sampleChain = make(map[int64]int64, self.sampleSize*2)
|
||||
self.replacements = make(map[int64]interface{}, self.sampleSize*2)
|
||||
self.initFutureSamples()
|
||||
}
|
||||
|
||||
func (self *chainSampler) Map(f func(d interface{})) {
|
||||
self.lock.RLock()
|
||||
defer self.lock.RUnlock()
|
||||
|
||||
for seqNum, obv := range self.samples {
|
||||
if _, ok := obv.(int); !ok {
|
||||
pretty.Printf("Seq %v. WAT: %# v\n", seqNum, obv)
|
||||
}
|
||||
f(obv)
|
||||
}
|
||||
}
|
||||
|
||||
// NOT SUPPORTED
|
||||
func (self *chainSampler) Filter(filter func(d interface{}) bool) {
|
||||
return
|
||||
}
|
||||
|
||||
// Chain sampler described in
|
||||
// Brian Babcok, Mayur Datar and Rajeev Motwani,
|
||||
// Sampling From a Moving Window Over Streaming Data
|
||||
func NewChainSampler(sampleSize, windowSize int) Sampler {
|
||||
sampler := &chainSampler{
|
||||
sampleSize: sampleSize,
|
||||
windowSize: int64(windowSize),
|
||||
samples: make(map[int64]interface{}, sampleSize),
|
||||
futureSamples: make(map[int64]empty, sampleSize*2),
|
||||
sampleChain: make(map[int64]int64, sampleSize*2),
|
||||
replacements: make(map[int64]interface{}, sampleSize*2),
|
||||
}
|
||||
sampler.initFutureSamples()
|
||||
return sampler
|
||||
}
|
43
third_party/src/github.com/google/cadvisor/sampling/chainsample_test.go
vendored
Normal file
43
third_party/src/github.com/google/cadvisor/sampling/chainsample_test.go
vendored
Normal file
@ -0,0 +1,43 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package sampling
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestChainSampler(t *testing.T) {
|
||||
numSamples := 10
|
||||
windowSize := 10 * numSamples
|
||||
numObservations := 10 * windowSize
|
||||
numSampleRounds := 10 * numObservations
|
||||
|
||||
s := NewChainSampler(numSamples, windowSize)
|
||||
hist := make(map[int]int, numSamples)
|
||||
for i := 0; i < numSampleRounds; i++ {
|
||||
sampleStream(hist, numObservations, s)
|
||||
}
|
||||
ratio := histStddev(hist) / histMean(hist)
|
||||
if ratio > 1.05 {
|
||||
// XXX(dengnan): better sampler?
|
||||
t.Errorf("std dev: %v; mean: %v. Either we have a really bad PRNG, or a bad implementation", histStddev(hist), histMean(hist))
|
||||
}
|
||||
if len(hist) > windowSize {
|
||||
t.Errorf("sampled %v data. larger than window size %v", len(hist), windowSize)
|
||||
}
|
||||
for seqNum, freq := range hist {
|
||||
if seqNum < numObservations-windowSize && freq > 0 {
|
||||
t.Errorf("observation with seqnum %v is sampled %v times", seqNum, freq)
|
||||
}
|
||||
}
|
||||
}
|
17
third_party/src/github.com/google/cadvisor/sampling/doc.go
vendored
Normal file
17
third_party/src/github.com/google/cadvisor/sampling/doc.go
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package sampling provides several sampling algorithms.
|
||||
// These algorithms will be used to sample containers' stats information
|
||||
package sampling
|
143
third_party/src/github.com/google/cadvisor/sampling/es.go
vendored
Normal file
143
third_party/src/github.com/google/cadvisor/sampling/es.go
vendored
Normal file
@ -0,0 +1,143 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package sampling
|
||||
|
||||
import (
|
||||
"container/heap"
|
||||
"math"
|
||||
"math/rand"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type esSampleItem struct {
|
||||
data interface{}
|
||||
key float64
|
||||
}
|
||||
|
||||
type esSampleHeap []esSampleItem
|
||||
|
||||
func (self esSampleHeap) Len() int {
|
||||
return len(self)
|
||||
}
|
||||
|
||||
func (self esSampleHeap) Less(i, j int) bool {
|
||||
return self[i].key < self[j].key
|
||||
}
|
||||
|
||||
func (self esSampleHeap) Swap(i, j int) {
|
||||
self[i], self[j] = self[j], self[i]
|
||||
}
|
||||
|
||||
func (self *esSampleHeap) Push(x interface{}) {
|
||||
item := x.(esSampleItem)
|
||||
*self = append(*self, item)
|
||||
}
|
||||
|
||||
func (self *esSampleHeap) Pop() interface{} {
|
||||
old := *self
|
||||
item := old[len(old)-1]
|
||||
*self = old[:len(old)-1]
|
||||
return item
|
||||
}
|
||||
|
||||
type esSampler struct {
|
||||
weight func(interface{}) float64
|
||||
samples *esSampleHeap
|
||||
maxSize int
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func (self *esSampler) Update(d interface{}) {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
|
||||
u := rand.Float64()
|
||||
key := math.Pow(u, 1.0/self.weight(d))
|
||||
|
||||
if self.samples.Len() < self.maxSize {
|
||||
heap.Push(self.samples, esSampleItem{
|
||||
data: d,
|
||||
key: key,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
s := *(self.samples)
|
||||
min := s[0]
|
||||
|
||||
// The key of the new item is larger than a key in existing item.
|
||||
// Add this new item.
|
||||
if key > min.key {
|
||||
heap.Pop(self.samples)
|
||||
heap.Push(self.samples, esSampleItem{
|
||||
data: d,
|
||||
key: key,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (self *esSampler) Len() int {
|
||||
self.lock.RLock()
|
||||
defer self.lock.RUnlock()
|
||||
return len(*self.samples)
|
||||
}
|
||||
|
||||
func (self *esSampler) Reset() {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
self.samples = &esSampleHeap{}
|
||||
heap.Init(self.samples)
|
||||
}
|
||||
|
||||
func (self *esSampler) Map(f func(interface{})) {
|
||||
self.lock.RLock()
|
||||
defer self.lock.RUnlock()
|
||||
|
||||
for _, d := range *self.samples {
|
||||
f(d.data)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *esSampler) Filter(filter func(d interface{}) bool) {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
|
||||
rmlist := make([]int, 0, len(*self.samples))
|
||||
for i, d := range *self.samples {
|
||||
if filter(d.data) {
|
||||
rmlist = append(rmlist, i)
|
||||
}
|
||||
}
|
||||
|
||||
for _, i := range rmlist {
|
||||
heap.Remove(self.samples, i)
|
||||
}
|
||||
}
|
||||
|
||||
// ES sampling algorithm described in
|
||||
//
|
||||
// Pavlos S. Efraimidis and Paul G. Spirakis. Weighted random sampling with a
|
||||
// reservoir. Information Processing Letters, 97(5):181 – 185, 2006.
|
||||
//
|
||||
// http://dl.acm.org/citation.cfm?id=1138834
|
||||
func NewESSampler(size int, weight func(interface{}) float64) Sampler {
|
||||
s := &esSampleHeap{}
|
||||
heap.Init(s)
|
||||
return &esSampler{
|
||||
maxSize: size,
|
||||
samples: s,
|
||||
weight: weight,
|
||||
}
|
||||
}
|
81
third_party/src/github.com/google/cadvisor/sampling/es_test.go
vendored
Normal file
81
third_party/src/github.com/google/cadvisor/sampling/es_test.go
vendored
Normal file
@ -0,0 +1,81 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package sampling
|
||||
|
||||
import (
|
||||
"container/heap"
|
||||
"math/rand"
|
||||
"testing"
|
||||
|
||||
"github.com/kr/pretty"
|
||||
)
|
||||
|
||||
// This should be a min heap
|
||||
func TestESSampleHeap(t *testing.T) {
|
||||
h := &esSampleHeap{}
|
||||
heap.Init(h)
|
||||
min := 5.0
|
||||
N := 10
|
||||
|
||||
for i := 0; i < N; i++ {
|
||||
key := rand.Float64()
|
||||
if key < min {
|
||||
min = key
|
||||
}
|
||||
heap.Push(h, esSampleItem{nil, key})
|
||||
}
|
||||
l := *h
|
||||
if l[0].key != min {
|
||||
t.Errorf("not a min heap")
|
||||
pretty.Printf("min=%v\nheap=%# v\n", min, l)
|
||||
}
|
||||
}
|
||||
|
||||
func TestESSampler(t *testing.T) {
|
||||
reservoirSize := 10
|
||||
numObvs := 10 * reservoirSize
|
||||
numSampleRounds := 100 * numObvs
|
||||
|
||||
weight := func(d interface{}) float64 {
|
||||
n := d.(int)
|
||||
return float64(n + 1)
|
||||
}
|
||||
s := NewESSampler(reservoirSize, weight)
|
||||
hist := make(map[int]int, numObvs)
|
||||
for i := 0; i < numSampleRounds; i++ {
|
||||
sampleStream(hist, numObvs, s)
|
||||
}
|
||||
|
||||
diff := 2
|
||||
wrongOrderedItems := make([]int, 0, numObvs)
|
||||
threshold := 1.05
|
||||
for i := 0; i < numObvs-diff; i++ {
|
||||
// Item with smaller weight should have lower probability to be selected.
|
||||
n1 := hist[i]
|
||||
n2 := hist[i+diff]
|
||||
if n1 > n2 {
|
||||
if float64(n1) > float64(n2)*threshold {
|
||||
wrongOrderedItems = append(wrongOrderedItems, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
if float64(len(wrongOrderedItems)) > float64(numObvs)*0.05 {
|
||||
for _, i := range wrongOrderedItems {
|
||||
n1 := hist[i]
|
||||
n2 := hist[i+diff]
|
||||
t.Errorf("item with weight %v is selected %v times; while item with weight %v is selected %v times", i, n1, i+diff, n2)
|
||||
}
|
||||
}
|
||||
}
|
99
third_party/src/github.com/google/cadvisor/sampling/reservoir.go
vendored
Normal file
99
third_party/src/github.com/google/cadvisor/sampling/reservoir.go
vendored
Normal file
@ -0,0 +1,99 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package sampling
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Reservoir sampling algorithm.
|
||||
// http://en.wikipedia.org/wiki/Reservoir_sampling
|
||||
type reservoirSampler struct {
|
||||
maxSize int
|
||||
samples []interface{}
|
||||
numInstances int64
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func (self *reservoirSampler) Len() int {
|
||||
self.lock.RLock()
|
||||
defer self.lock.RUnlock()
|
||||
return len(self.samples)
|
||||
}
|
||||
|
||||
func (self *reservoirSampler) Reset() {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
self.samples = make([]interface{}, 0, self.maxSize)
|
||||
self.numInstances = 0
|
||||
}
|
||||
|
||||
// Update samples according to http://en.wikipedia.org/wiki/Reservoir_sampling
|
||||
func (self *reservoirSampler) Update(d interface{}) {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
|
||||
self.numInstances++
|
||||
if len(self.samples) < self.maxSize {
|
||||
self.samples = append(self.samples, d)
|
||||
return
|
||||
}
|
||||
// Randomly generates a number between [0, numInstances).
|
||||
// Use this random number, j, as an index. If j is larger than the
|
||||
// reservoir size, we will ignore the current new data.
|
||||
// Otherwise replace the jth element in reservoir with the new data.
|
||||
j := rand.Int63n(self.numInstances)
|
||||
if j < int64(len(self.samples)) {
|
||||
self.samples[int(j)] = d
|
||||
}
|
||||
}
|
||||
|
||||
func (self *reservoirSampler) Map(f func(d interface{})) {
|
||||
self.lock.RLock()
|
||||
defer self.lock.RUnlock()
|
||||
|
||||
for _, d := range self.samples {
|
||||
f(d)
|
||||
}
|
||||
}
|
||||
|
||||
// Once an element is removed, the probability of sampling an observation will
|
||||
// be increased. Removing all elements in the sampler has the same effect as
|
||||
// calling Reset(). However, it will not guarantee the uniform probability of
|
||||
// all unfiltered samples.
|
||||
func (self *reservoirSampler) Filter(filter func(d interface{}) bool) {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
rmlist := make([]int, 0, len(self.samples))
|
||||
for i, d := range self.samples {
|
||||
if filter(d) {
|
||||
rmlist = append(rmlist, i)
|
||||
}
|
||||
}
|
||||
|
||||
for _, i := range rmlist {
|
||||
// slice trick: remove the ith element without preserving the order
|
||||
self.samples[i] = self.samples[len(self.samples)-1]
|
||||
self.samples = self.samples[:len(self.samples)-1]
|
||||
}
|
||||
self.numInstances -= int64(len(rmlist))
|
||||
}
|
||||
|
||||
func NewReservoirSampler(reservoirSize int) Sampler {
|
||||
return &reservoirSampler{
|
||||
maxSize: reservoirSize,
|
||||
}
|
||||
}
|
70
third_party/src/github.com/google/cadvisor/sampling/reservoir_test.go
vendored
Normal file
70
third_party/src/github.com/google/cadvisor/sampling/reservoir_test.go
vendored
Normal file
@ -0,0 +1,70 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package sampling
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func sampleStream(hist map[int]int, n int, s Sampler) {
|
||||
s.Reset()
|
||||
for i := 0; i < n; i++ {
|
||||
s.Update(i)
|
||||
}
|
||||
s.Map(func(d interface{}) {
|
||||
j := d.(int)
|
||||
if _, ok := hist[j]; !ok {
|
||||
hist[j] = 0
|
||||
}
|
||||
hist[j]++
|
||||
})
|
||||
}
|
||||
|
||||
func histMean(hist map[int]int) float64 {
|
||||
total := 0
|
||||
for _, v := range hist {
|
||||
total += v
|
||||
}
|
||||
return float64(total) / float64(len(hist))
|
||||
}
|
||||
|
||||
func histStddev(hist map[int]int) float64 {
|
||||
mean := histMean(hist)
|
||||
var totalDiff float64
|
||||
for _, v := range hist {
|
||||
diff := float64(v) - mean
|
||||
sq := diff * diff
|
||||
totalDiff += sq
|
||||
}
|
||||
return math.Sqrt(totalDiff / float64(len(hist)))
|
||||
}
|
||||
|
||||
// XXX(dengnan): This test may take more than 10 seconds.
|
||||
func TestReservoirSampler(t *testing.T) {
|
||||
reservoirSize := 10
|
||||
numSamples := 10 * reservoirSize
|
||||
numSampleRounds := 100 * numSamples
|
||||
|
||||
s := NewReservoirSampler(reservoirSize)
|
||||
hist := make(map[int]int, numSamples)
|
||||
for i := 0; i < numSampleRounds; i++ {
|
||||
sampleStream(hist, numSamples, s)
|
||||
}
|
||||
ratio := histStddev(hist) / histMean(hist)
|
||||
if ratio > 0.05 {
|
||||
t.Errorf("std dev: %v; mean: %v. Either we have a really bad PRNG, or a bad implementation", histStddev(hist), histMean(hist))
|
||||
}
|
||||
}
|
42
third_party/src/github.com/google/cadvisor/sampling/sampler.go
vendored
Normal file
42
third_party/src/github.com/google/cadvisor/sampling/sampler.go
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package sampling
|
||||
|
||||
import (
|
||||
crand "crypto/rand"
|
||||
"encoding/binary"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// NOTE(dengnan): Even if we picked a good random seed,
|
||||
// the random number from math/rand is still not cryptographically secure!
|
||||
var seed int64
|
||||
binary.Read(crand.Reader, binary.LittleEndian, &seed)
|
||||
rand.Seed(seed)
|
||||
}
|
||||
|
||||
type Sampler interface {
|
||||
Update(d interface{})
|
||||
Len() int
|
||||
Reset()
|
||||
Map(f func(interface{}))
|
||||
|
||||
// Filter() should update in place. Removing elements may or may not
|
||||
// affect the statistical behavior of the sampler, i.e. the probability
|
||||
// that an observation will be sampled after removing some elements is
|
||||
// implementation defined.
|
||||
Filter(filter func(interface{}) bool)
|
||||
}
|
224
third_party/src/github.com/google/cadvisor/storage/memory/memory.go
vendored
Normal file
224
third_party/src/github.com/google/cadvisor/storage/memory/memory.go
vendored
Normal file
@ -0,0 +1,224 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package memory
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/google/cadvisor/info"
|
||||
"github.com/google/cadvisor/sampling"
|
||||
"github.com/google/cadvisor/storage"
|
||||
)
|
||||
|
||||
// containerStorage is used to store per-container information
|
||||
type containerStorage struct {
|
||||
ref info.ContainerReference
|
||||
prevStats *info.ContainerStats
|
||||
sampler sampling.Sampler
|
||||
recentStats *list.List
|
||||
maxNumStats int
|
||||
maxMemUsage uint64
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func (self *containerStorage) updatePrevStats(stats *info.ContainerStats) {
|
||||
if stats == nil || stats.Cpu == nil || stats.Memory == nil {
|
||||
// discard incomplete stats
|
||||
self.prevStats = nil
|
||||
return
|
||||
}
|
||||
if self.prevStats == nil {
|
||||
self.prevStats = &info.ContainerStats{
|
||||
Cpu: &info.CpuStats{},
|
||||
Memory: &info.MemoryStats{},
|
||||
}
|
||||
}
|
||||
// make a deep copy.
|
||||
self.prevStats.Timestamp = stats.Timestamp
|
||||
*self.prevStats.Cpu = *stats.Cpu
|
||||
self.prevStats.Cpu.Usage.PerCpu = make([]uint64, len(stats.Cpu.Usage.PerCpu))
|
||||
for i, perCpu := range stats.Cpu.Usage.PerCpu {
|
||||
self.prevStats.Cpu.Usage.PerCpu[i] = perCpu
|
||||
}
|
||||
*self.prevStats.Memory = *stats.Memory
|
||||
}
|
||||
|
||||
func (self *containerStorage) AddStats(stats *info.ContainerStats) error {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
if self.prevStats != nil {
|
||||
sample, err := info.NewSample(self.prevStats, stats)
|
||||
if err != nil {
|
||||
return fmt.Errorf("wrong stats: %v", err)
|
||||
}
|
||||
if sample != nil {
|
||||
self.sampler.Update(sample)
|
||||
}
|
||||
}
|
||||
if stats.Memory != nil {
|
||||
if self.maxMemUsage < stats.Memory.Usage {
|
||||
self.maxMemUsage = stats.Memory.Usage
|
||||
}
|
||||
}
|
||||
if self.recentStats.Len() >= self.maxNumStats {
|
||||
self.recentStats.Remove(self.recentStats.Front())
|
||||
}
|
||||
self.recentStats.PushBack(stats)
|
||||
self.updatePrevStats(stats)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *containerStorage) RecentStats(numStats int) ([]*info.ContainerStats, error) {
|
||||
self.lock.RLock()
|
||||
defer self.lock.RUnlock()
|
||||
if self.recentStats.Len() < numStats || numStats < 0 {
|
||||
numStats = self.recentStats.Len()
|
||||
}
|
||||
ret := make([]*info.ContainerStats, 0, numStats)
|
||||
e := self.recentStats.Front()
|
||||
for i := 0; i < numStats; i++ {
|
||||
data, ok := e.Value.(*info.ContainerStats)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("The %vth element is not a ContainerStats", i)
|
||||
}
|
||||
ret = append(ret, data)
|
||||
e = e.Next()
|
||||
if e == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (self *containerStorage) Samples(numSamples int) ([]*info.ContainerStatsSample, error) {
|
||||
self.lock.RLock()
|
||||
defer self.lock.RUnlock()
|
||||
if self.sampler.Len() < numSamples || numSamples < 0 {
|
||||
numSamples = self.sampler.Len()
|
||||
}
|
||||
ret := make([]*info.ContainerStatsSample, 0, numSamples)
|
||||
|
||||
var err error
|
||||
self.sampler.Map(func(d interface{}) {
|
||||
if len(ret) >= numSamples || err != nil {
|
||||
return
|
||||
}
|
||||
sample, ok := d.(*info.ContainerStatsSample)
|
||||
if !ok {
|
||||
err = fmt.Errorf("An element in the sample is not a ContainerStatsSample")
|
||||
}
|
||||
ret = append(ret, sample)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (self *containerStorage) Percentiles(cpuPercentiles, memPercentiles []int) (*info.ContainerStatsPercentiles, error) {
|
||||
samples, err := self.Samples(-1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(samples) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
ret := info.NewPercentiles(samples, cpuPercentiles, memPercentiles)
|
||||
ret.MaxMemoryUsage = self.maxMemUsage
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func newContainerStore(ref info.ContainerReference, maxNumSamples, maxNumStats int) *containerStorage {
|
||||
s := sampling.NewReservoirSampler(maxNumSamples)
|
||||
return &containerStorage{
|
||||
ref: ref,
|
||||
recentStats: list.New(),
|
||||
sampler: s,
|
||||
maxNumStats: maxNumStats,
|
||||
}
|
||||
}
|
||||
|
||||
type InMemoryStorage struct {
|
||||
lock sync.RWMutex
|
||||
containerStorageMap map[string]*containerStorage
|
||||
maxNumSamples int
|
||||
maxNumStats int
|
||||
}
|
||||
|
||||
func (self *InMemoryStorage) AddStats(ref info.ContainerReference, stats *info.ContainerStats) error {
|
||||
var cstore *containerStorage
|
||||
var ok bool
|
||||
self.lock.Lock()
|
||||
if cstore, ok = self.containerStorageMap[ref.Name]; !ok {
|
||||
cstore = newContainerStore(ref, self.maxNumSamples, self.maxNumStats)
|
||||
self.containerStorageMap[ref.Name] = cstore
|
||||
}
|
||||
self.lock.Unlock()
|
||||
return cstore.AddStats(stats)
|
||||
}
|
||||
|
||||
func (self *InMemoryStorage) Samples(name string, numSamples int) ([]*info.ContainerStatsSample, error) {
|
||||
var cstore *containerStorage
|
||||
var ok bool
|
||||
self.lock.RLock()
|
||||
if cstore, ok = self.containerStorageMap[name]; !ok {
|
||||
return nil, fmt.Errorf("unable to find data for container %v", name)
|
||||
}
|
||||
self.lock.RUnlock()
|
||||
|
||||
return cstore.Samples(numSamples)
|
||||
}
|
||||
|
||||
func (self *InMemoryStorage) RecentStats(name string, numStats int) ([]*info.ContainerStats, error) {
|
||||
var cstore *containerStorage
|
||||
var ok bool
|
||||
self.lock.RLock()
|
||||
if cstore, ok = self.containerStorageMap[name]; !ok {
|
||||
return nil, fmt.Errorf("unable to find data for container %v", name)
|
||||
}
|
||||
self.lock.RUnlock()
|
||||
|
||||
return cstore.RecentStats(numStats)
|
||||
}
|
||||
|
||||
func (self *InMemoryStorage) Percentiles(name string, cpuPercentiles, memPercentiles []int) (*info.ContainerStatsPercentiles, error) {
|
||||
var cstore *containerStorage
|
||||
var ok bool
|
||||
self.lock.RLock()
|
||||
if cstore, ok = self.containerStorageMap[name]; !ok {
|
||||
return nil, fmt.Errorf("unable to find data for container %v", name)
|
||||
}
|
||||
self.lock.RUnlock()
|
||||
|
||||
return cstore.Percentiles(cpuPercentiles, memPercentiles)
|
||||
}
|
||||
|
||||
func (self *InMemoryStorage) Close() error {
|
||||
self.lock.Lock()
|
||||
self.containerStorageMap = make(map[string]*containerStorage, 32)
|
||||
self.lock.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func New(maxNumSamples, maxNumStats int) storage.StorageDriver {
|
||||
ret := &InMemoryStorage{
|
||||
containerStorageMap: make(map[string]*containerStorage, 32),
|
||||
maxNumSamples: maxNumSamples,
|
||||
maxNumStats: maxNumStats,
|
||||
}
|
||||
return ret
|
||||
}
|
49
third_party/src/github.com/google/cadvisor/storage/memory/memory_test.go
vendored
Normal file
49
third_party/src/github.com/google/cadvisor/storage/memory/memory_test.go
vendored
Normal file
@ -0,0 +1,49 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package memory
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/cadvisor/storage"
|
||||
"github.com/google/cadvisor/storage/test"
|
||||
)
|
||||
|
||||
func runStorageTest(f func(storage.StorageDriver, *testing.T), t *testing.T) {
|
||||
maxSize := 200
|
||||
|
||||
var driver storage.StorageDriver
|
||||
for N := 10; N < maxSize; N += 10 {
|
||||
driver = New(N, N)
|
||||
f(driver, t)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestMaxMemoryUsage(t *testing.T) {
|
||||
runStorageTest(test.StorageDriverTestMaxMemoryUsage, t)
|
||||
}
|
||||
|
||||
func TestSampleCpuUsage(t *testing.T) {
|
||||
runStorageTest(test.StorageDriverTestSampleCpuUsage, t)
|
||||
}
|
||||
|
||||
func TestSamplesWithoutSample(t *testing.T) {
|
||||
runStorageTest(test.StorageDriverTestSamplesWithoutSample, t)
|
||||
}
|
||||
|
||||
func TestPercentilessWithoutSample(t *testing.T) {
|
||||
runStorageTest(test.StorageDriverTestPercentilesWithoutSample, t)
|
||||
}
|
40
third_party/src/github.com/google/cadvisor/storage/storage.go
vendored
Normal file
40
third_party/src/github.com/google/cadvisor/storage/storage.go
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package storage
|
||||
|
||||
import "github.com/google/cadvisor/info"
|
||||
|
||||
type StorageDriver interface {
|
||||
AddStats(ref info.ContainerReference, stats *info.ContainerStats) error
|
||||
|
||||
// Read most recent stats. numStats indicates max number of stats
|
||||
// returned. The returned stats must be consecutive observed stats. If
|
||||
// numStats < 0, then return all stats stored in the storage.
|
||||
RecentStats(containerName string, numStats int) ([]*info.ContainerStats, error)
|
||||
|
||||
// Read the specified percentiles of CPU and memory usage of the container.
|
||||
// The implementation decides which time range to look at.
|
||||
Percentiles(containerName string, cpuUsagePercentiles []int, memUsagePercentiles []int) (*info.ContainerStatsPercentiles, error)
|
||||
|
||||
// Returns samples of the container stats. If numSamples < 0, then
|
||||
// the number of returned samples is implementation defined. Otherwise, the driver
|
||||
// should return at most numSamples samples.
|
||||
Samples(containername string, numSamples int) ([]*info.ContainerStatsSample, error)
|
||||
|
||||
// Close will clear the state of the storage driver. The elements
|
||||
// stored in the underlying storage may or may not be deleted depending
|
||||
// on the implementation of the storage driver.
|
||||
Close() error
|
||||
}
|
170
third_party/src/github.com/google/cadvisor/storage/test/storagetests.go
vendored
Normal file
170
third_party/src/github.com/google/cadvisor/storage/test/storagetests.go
vendored
Normal file
@ -0,0 +1,170 @@
|
||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package test
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/cadvisor/info"
|
||||
"github.com/google/cadvisor/storage"
|
||||
)
|
||||
|
||||
func buildTrace(cpu, mem []uint64, duration time.Duration) []*info.ContainerStats {
|
||||
if len(cpu) != len(mem) {
|
||||
panic("len(cpu) != len(mem)")
|
||||
}
|
||||
|
||||
ret := make([]*info.ContainerStats, len(cpu))
|
||||
currentTime := time.Now()
|
||||
|
||||
var cpuTotalUsage uint64 = 0
|
||||
for i, cpuUsage := range cpu {
|
||||
cpuTotalUsage += cpuUsage
|
||||
stats := new(info.ContainerStats)
|
||||
stats.Cpu = new(info.CpuStats)
|
||||
stats.Memory = new(info.MemoryStats)
|
||||
stats.Timestamp = currentTime
|
||||
currentTime = currentTime.Add(duration)
|
||||
|
||||
stats.Cpu.Usage.Total = cpuTotalUsage
|
||||
stats.Cpu.Usage.User = stats.Cpu.Usage.Total
|
||||
stats.Cpu.Usage.System = 0
|
||||
|
||||
stats.Memory.Usage = mem[i]
|
||||
|
||||
ret[i] = stats
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// The underlying driver must be able to hold more than 10 samples.
|
||||
func StorageDriverTestSampleCpuUsage(driver storage.StorageDriver, t *testing.T) {
|
||||
defer driver.Close()
|
||||
N := 10
|
||||
cpuTrace := make([]uint64, 0, N)
|
||||
memTrace := make([]uint64, 0, N)
|
||||
|
||||
// We need N+1 observations to get N samples
|
||||
for i := 0; i < N+1; i++ {
|
||||
cpuTrace = append(cpuTrace, uint64(rand.Intn(1000)))
|
||||
memTrace = append(memTrace, uint64(rand.Intn(1000)))
|
||||
}
|
||||
|
||||
samplePeriod := 1 * time.Second
|
||||
|
||||
ref := info.ContainerReference{
|
||||
Name: "container",
|
||||
}
|
||||
|
||||
trace := buildTrace(cpuTrace, memTrace, samplePeriod)
|
||||
|
||||
for _, stats := range trace {
|
||||
driver.AddStats(ref, stats)
|
||||
}
|
||||
|
||||
samples, err := driver.Samples(ref.Name, N)
|
||||
if err != nil {
|
||||
t.Errorf("unable to sample stats: %v", err)
|
||||
}
|
||||
for _, sample := range samples {
|
||||
if sample.Duration != samplePeriod {
|
||||
t.Errorf("sample duration is %v, not %v", sample.Duration, samplePeriod)
|
||||
}
|
||||
cpuUsage := sample.Cpu.Usage
|
||||
found := false
|
||||
for _, u := range cpuTrace {
|
||||
if u == cpuUsage {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("unable to find cpu usage %v", cpuUsage)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func StorageDriverTestMaxMemoryUsage(driver storage.StorageDriver, t *testing.T) {
|
||||
defer driver.Close()
|
||||
N := 100
|
||||
memTrace := make([]uint64, N)
|
||||
cpuTrace := make([]uint64, N)
|
||||
for i := 0; i < N; i++ {
|
||||
memTrace[i] = uint64(i + 1)
|
||||
cpuTrace[i] = uint64(1)
|
||||
}
|
||||
|
||||
ref := info.ContainerReference{
|
||||
Name: "container",
|
||||
}
|
||||
|
||||
trace := buildTrace(cpuTrace, memTrace, 1*time.Second)
|
||||
|
||||
for _, stats := range trace {
|
||||
driver.AddStats(ref, stats)
|
||||
}
|
||||
|
||||
percentiles, err := driver.Percentiles(ref.Name, []int{50}, []int{50})
|
||||
if err != nil {
|
||||
t.Errorf("unable to call Percentiles(): %v", err)
|
||||
}
|
||||
maxUsage := uint64(N)
|
||||
if percentiles.MaxMemoryUsage != maxUsage {
|
||||
t.Fatalf("Max memory usage should be %v; received %v", maxUsage, percentiles.MaxMemoryUsage)
|
||||
}
|
||||
}
|
||||
|
||||
func StorageDriverTestSamplesWithoutSample(driver storage.StorageDriver, t *testing.T) {
|
||||
defer driver.Close()
|
||||
trace := buildTrace(
|
||||
[]uint64{10},
|
||||
[]uint64{10},
|
||||
1*time.Second)
|
||||
ref := info.ContainerReference{
|
||||
Name: "container",
|
||||
}
|
||||
driver.AddStats(ref, trace[0])
|
||||
samples, err := driver.Samples(ref.Name, -1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(samples) != 0 {
|
||||
t.Errorf("There should be no sample")
|
||||
}
|
||||
}
|
||||
|
||||
func StorageDriverTestPercentilesWithoutSample(driver storage.StorageDriver, t *testing.T) {
|
||||
defer driver.Close()
|
||||
trace := buildTrace(
|
||||
[]uint64{10},
|
||||
[]uint64{10},
|
||||
1*time.Second)
|
||||
ref := info.ContainerReference{
|
||||
Name: "container",
|
||||
}
|
||||
driver.AddStats(ref, trace[0])
|
||||
percentiles, err := driver.Percentiles(
|
||||
ref.Name,
|
||||
[]int{50},
|
||||
[]int{50},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if percentiles != nil {
|
||||
t.Errorf("There should be no percentiles")
|
||||
}
|
||||
}
|
23
third_party/src/github.com/stretchr/objx/LICENSE.md
vendored
Normal file
23
third_party/src/github.com/stretchr/objx/LICENSE.md
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
objx - by Mat Ryer and Tyler Bunnell
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Stretchr, Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
3
third_party/src/github.com/stretchr/objx/README.md
vendored
Normal file
3
third_party/src/github.com/stretchr/objx/README.md
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# objx
|
||||
|
||||
* Jump into the [API Documentation](http://godoc.org/github.com/stretchr/objx)
|
179
third_party/src/github.com/stretchr/objx/accessors.go
vendored
Normal file
179
third_party/src/github.com/stretchr/objx/accessors.go
vendored
Normal file
@ -0,0 +1,179 @@
|
||||
package objx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// arrayAccesRegexString is the regex used to extract the array number
|
||||
// from the access path
|
||||
const arrayAccesRegexString = `^(.+)\[([0-9]+)\]$`
|
||||
|
||||
// arrayAccesRegex is the compiled arrayAccesRegexString
|
||||
var arrayAccesRegex = regexp.MustCompile(arrayAccesRegexString)
|
||||
|
||||
// Get gets the value using the specified selector and
|
||||
// returns it inside a new Obj object.
|
||||
//
|
||||
// If it cannot find the value, Get will return a nil
|
||||
// value inside an instance of Obj.
|
||||
//
|
||||
// Get can only operate directly on map[string]interface{} and []interface.
|
||||
//
|
||||
// Example
|
||||
//
|
||||
// To access the title of the third chapter of the second book, do:
|
||||
//
|
||||
// o.Get("books[1].chapters[2].title")
|
||||
func (m Map) Get(selector string) *Value {
|
||||
rawObj := access(m, selector, nil, false, false)
|
||||
return &Value{data: rawObj}
|
||||
}
|
||||
|
||||
// Set sets the value using the specified selector and
|
||||
// returns the object on which Set was called.
|
||||
//
|
||||
// Set can only operate directly on map[string]interface{} and []interface
|
||||
//
|
||||
// Example
|
||||
//
|
||||
// To set the title of the third chapter of the second book, do:
|
||||
//
|
||||
// o.Set("books[1].chapters[2].title","Time to Go")
|
||||
func (m Map) Set(selector string, value interface{}) Map {
|
||||
access(m, selector, value, true, false)
|
||||
return m
|
||||
}
|
||||
|
||||
// access accesses the object using the selector and performs the
|
||||
// appropriate action.
|
||||
func access(current, selector, value interface{}, isSet, panics bool) interface{} {
|
||||
|
||||
switch selector.(type) {
|
||||
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
|
||||
|
||||
if array, ok := current.([]interface{}); ok {
|
||||
index := intFromInterface(selector)
|
||||
|
||||
if index >= len(array) {
|
||||
if panics {
|
||||
panic(fmt.Sprintf("objx: Index %d is out of range. Slice only contains %d items.", index, len(array)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
return array[index]
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
case string:
|
||||
|
||||
selStr := selector.(string)
|
||||
selSegs := strings.SplitN(selStr, PathSeparator, 2)
|
||||
thisSel := selSegs[0]
|
||||
index := -1
|
||||
var err error
|
||||
|
||||
// https://github.com/stretchr/objx/issues/12
|
||||
if strings.Contains(thisSel, "[") {
|
||||
|
||||
arrayMatches := arrayAccesRegex.FindStringSubmatch(thisSel)
|
||||
|
||||
if len(arrayMatches) > 0 {
|
||||
|
||||
// Get the key into the map
|
||||
thisSel = arrayMatches[1]
|
||||
|
||||
// Get the index into the array at the key
|
||||
index, err = strconv.Atoi(arrayMatches[2])
|
||||
|
||||
if err != nil {
|
||||
// This should never happen. If it does, something has gone
|
||||
// seriously wrong. Panic.
|
||||
panic("objx: Array index is not an integer. Must use array[int].")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if curMap, ok := current.(Map); ok {
|
||||
current = map[string]interface{}(curMap)
|
||||
}
|
||||
|
||||
// get the object in question
|
||||
switch current.(type) {
|
||||
case map[string]interface{}:
|
||||
curMSI := current.(map[string]interface{})
|
||||
if len(selSegs) <= 1 && isSet {
|
||||
curMSI[thisSel] = value
|
||||
return nil
|
||||
} else {
|
||||
current = curMSI[thisSel]
|
||||
}
|
||||
default:
|
||||
current = nil
|
||||
}
|
||||
|
||||
if current == nil && panics {
|
||||
panic(fmt.Sprintf("objx: '%v' invalid on object.", selector))
|
||||
}
|
||||
|
||||
// do we need to access the item of an array?
|
||||
if index > -1 {
|
||||
if array, ok := current.([]interface{}); ok {
|
||||
if index < len(array) {
|
||||
current = array[index]
|
||||
} else {
|
||||
if panics {
|
||||
panic(fmt.Sprintf("objx: Index %d is out of range. Slice only contains %d items.", index, len(array)))
|
||||
}
|
||||
current = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(selSegs) > 1 {
|
||||
current = access(current, selSegs[1], value, isSet, panics)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return current
|
||||
|
||||
}
|
||||
|
||||
// intFromInterface converts an interface object to the largest
|
||||
// representation of an unsigned integer using a type switch and
|
||||
// assertions
|
||||
func intFromInterface(selector interface{}) int {
|
||||
var value int
|
||||
switch selector.(type) {
|
||||
case int:
|
||||
value = selector.(int)
|
||||
case int8:
|
||||
value = int(selector.(int8))
|
||||
case int16:
|
||||
value = int(selector.(int16))
|
||||
case int32:
|
||||
value = int(selector.(int32))
|
||||
case int64:
|
||||
value = int(selector.(int64))
|
||||
case uint:
|
||||
value = int(selector.(uint))
|
||||
case uint8:
|
||||
value = int(selector.(uint8))
|
||||
case uint16:
|
||||
value = int(selector.(uint16))
|
||||
case uint32:
|
||||
value = int(selector.(uint32))
|
||||
case uint64:
|
||||
value = int(selector.(uint64))
|
||||
default:
|
||||
panic("objx: array access argument is not an integer type (this should never happen)")
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
145
third_party/src/github.com/stretchr/objx/accessors_test.go
vendored
Normal file
145
third_party/src/github.com/stretchr/objx/accessors_test.go
vendored
Normal file
@ -0,0 +1,145 @@
|
||||
package objx
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAccessorsAccessGetSingleField(t *testing.T) {
|
||||
|
||||
current := map[string]interface{}{"name": "Tyler"}
|
||||
assert.Equal(t, "Tyler", access(current, "name", nil, false, true))
|
||||
|
||||
}
|
||||
func TestAccessorsAccessGetDeep(t *testing.T) {
|
||||
|
||||
current := map[string]interface{}{"name": map[string]interface{}{"first": "Tyler", "last": "Bunnell"}}
|
||||
assert.Equal(t, "Tyler", access(current, "name.first", nil, false, true))
|
||||
assert.Equal(t, "Bunnell", access(current, "name.last", nil, false, true))
|
||||
|
||||
}
|
||||
func TestAccessorsAccessGetDeepDeep(t *testing.T) {
|
||||
|
||||
current := map[string]interface{}{"one": map[string]interface{}{"two": map[string]interface{}{"three": map[string]interface{}{"four": 4}}}}
|
||||
assert.Equal(t, 4, access(current, "one.two.three.four", nil, false, true))
|
||||
|
||||
}
|
||||
func TestAccessorsAccessGetInsideArray(t *testing.T) {
|
||||
|
||||
current := map[string]interface{}{"names": []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}}}
|
||||
assert.Equal(t, "Tyler", access(current, "names[0].first", nil, false, true))
|
||||
assert.Equal(t, "Bunnell", access(current, "names[0].last", nil, false, true))
|
||||
assert.Equal(t, "Capitol", access(current, "names[1].first", nil, false, true))
|
||||
assert.Equal(t, "Bollocks", access(current, "names[1].last", nil, false, true))
|
||||
|
||||
assert.Panics(t, func() {
|
||||
access(current, "names[2]", nil, false, true)
|
||||
})
|
||||
assert.Nil(t, access(current, "names[2]", nil, false, false))
|
||||
|
||||
}
|
||||
|
||||
func TestAccessorsAccessGetFromArrayWithInt(t *testing.T) {
|
||||
|
||||
current := []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}}
|
||||
one := access(current, 0, nil, false, false)
|
||||
two := access(current, 1, nil, false, false)
|
||||
three := access(current, 2, nil, false, false)
|
||||
|
||||
assert.Equal(t, "Tyler", one.(map[string]interface{})["first"])
|
||||
assert.Equal(t, "Capitol", two.(map[string]interface{})["first"])
|
||||
assert.Nil(t, three)
|
||||
|
||||
}
|
||||
|
||||
func TestAccessorsGet(t *testing.T) {
|
||||
|
||||
current := New(map[string]interface{}{"name": "Tyler"})
|
||||
assert.Equal(t, "Tyler", current.Get("name").data)
|
||||
|
||||
}
|
||||
|
||||
func TestAccessorsAccessSetSingleField(t *testing.T) {
|
||||
|
||||
current := map[string]interface{}{"name": "Tyler"}
|
||||
access(current, "name", "Mat", true, false)
|
||||
assert.Equal(t, current["name"], "Mat")
|
||||
|
||||
access(current, "age", 29, true, true)
|
||||
assert.Equal(t, current["age"], 29)
|
||||
|
||||
}
|
||||
|
||||
func TestAccessorsAccessSetSingleFieldNotExisting(t *testing.T) {
|
||||
|
||||
current := map[string]interface{}{}
|
||||
access(current, "name", "Mat", true, false)
|
||||
assert.Equal(t, current["name"], "Mat")
|
||||
|
||||
}
|
||||
|
||||
func TestAccessorsAccessSetDeep(t *testing.T) {
|
||||
|
||||
current := map[string]interface{}{"name": map[string]interface{}{"first": "Tyler", "last": "Bunnell"}}
|
||||
|
||||
access(current, "name.first", "Mat", true, true)
|
||||
access(current, "name.last", "Ryer", true, true)
|
||||
|
||||
assert.Equal(t, "Mat", access(current, "name.first", nil, false, true))
|
||||
assert.Equal(t, "Ryer", access(current, "name.last", nil, false, true))
|
||||
|
||||
}
|
||||
func TestAccessorsAccessSetDeepDeep(t *testing.T) {
|
||||
|
||||
current := map[string]interface{}{"one": map[string]interface{}{"two": map[string]interface{}{"three": map[string]interface{}{"four": 4}}}}
|
||||
|
||||
access(current, "one.two.three.four", 5, true, true)
|
||||
|
||||
assert.Equal(t, 5, access(current, "one.two.three.four", nil, false, true))
|
||||
|
||||
}
|
||||
func TestAccessorsAccessSetArray(t *testing.T) {
|
||||
|
||||
current := map[string]interface{}{"names": []interface{}{"Tyler"}}
|
||||
|
||||
access(current, "names[0]", "Mat", true, true)
|
||||
|
||||
assert.Equal(t, "Mat", access(current, "names[0]", nil, false, true))
|
||||
|
||||
}
|
||||
func TestAccessorsAccessSetInsideArray(t *testing.T) {
|
||||
|
||||
current := map[string]interface{}{"names": []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}}}
|
||||
|
||||
access(current, "names[0].first", "Mat", true, true)
|
||||
access(current, "names[0].last", "Ryer", true, true)
|
||||
access(current, "names[1].first", "Captain", true, true)
|
||||
access(current, "names[1].last", "Underpants", true, true)
|
||||
|
||||
assert.Equal(t, "Mat", access(current, "names[0].first", nil, false, true))
|
||||
assert.Equal(t, "Ryer", access(current, "names[0].last", nil, false, true))
|
||||
assert.Equal(t, "Captain", access(current, "names[1].first", nil, false, true))
|
||||
assert.Equal(t, "Underpants", access(current, "names[1].last", nil, false, true))
|
||||
|
||||
}
|
||||
|
||||
func TestAccessorsAccessSetFromArrayWithInt(t *testing.T) {
|
||||
|
||||
current := []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}}
|
||||
one := access(current, 0, nil, false, false)
|
||||
two := access(current, 1, nil, false, false)
|
||||
three := access(current, 2, nil, false, false)
|
||||
|
||||
assert.Equal(t, "Tyler", one.(map[string]interface{})["first"])
|
||||
assert.Equal(t, "Capitol", two.(map[string]interface{})["first"])
|
||||
assert.Nil(t, three)
|
||||
|
||||
}
|
||||
|
||||
func TestAccessorsSet(t *testing.T) {
|
||||
|
||||
current := New(map[string]interface{}{"name": "Tyler"})
|
||||
current.Set("name", "Mat")
|
||||
assert.Equal(t, "Mat", current.Get("name").data)
|
||||
|
||||
}
|
14
third_party/src/github.com/stretchr/objx/codegen/array-access.txt
vendored
Normal file
14
third_party/src/github.com/stretchr/objx/codegen/array-access.txt
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
case []{1}:
|
||||
a := object.([]{1})
|
||||
if isSet {
|
||||
a[index] = value.({1})
|
||||
} else {
|
||||
if index >= len(a) {
|
||||
if panics {
|
||||
panic(fmt.Sprintf("objx: Index %d is out of range because the []{1} only contains %d items.", index, len(a)))
|
||||
}
|
||||
return nil
|
||||
} else {
|
||||
return a[index]
|
||||
}
|
||||
}
|
86
third_party/src/github.com/stretchr/objx/codegen/index.html
vendored
Normal file
86
third_party/src/github.com/stretchr/objx/codegen/index.html
vendored
Normal file
@ -0,0 +1,86 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>
|
||||
Codegen
|
||||
</title>
|
||||
<style>
|
||||
body {
|
||||
width: 800px;
|
||||
margin: auto;
|
||||
}
|
||||
textarea {
|
||||
width: 100%;
|
||||
min-height: 100px;
|
||||
font-family: Courier;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h2>
|
||||
Template
|
||||
</h2>
|
||||
<p>
|
||||
Use <code>{x}</code> as a placeholder for each argument.
|
||||
</p>
|
||||
<textarea id="template"></textarea>
|
||||
|
||||
<h2>
|
||||
Arguments (comma separated)
|
||||
</h2>
|
||||
<p>
|
||||
One block per line
|
||||
</p>
|
||||
<textarea id="args"></textarea>
|
||||
|
||||
<h2>
|
||||
Output
|
||||
</h2>
|
||||
<input id="go" type="button" value="Generate code" />
|
||||
|
||||
<textarea id="output"></textarea>
|
||||
|
||||
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
|
||||
<script>
|
||||
|
||||
$(function(){
|
||||
|
||||
$("#go").click(function(){
|
||||
|
||||
var output = ""
|
||||
var template = $("#template").val()
|
||||
var args = $("#args").val()
|
||||
|
||||
// collect the args
|
||||
var argLines = args.split("\n")
|
||||
for (var line in argLines) {
|
||||
|
||||
var argLine = argLines[line];
|
||||
var thisTemp = template
|
||||
|
||||
// get individual args
|
||||
var args = argLine.split(",")
|
||||
|
||||
for (var argI in args) {
|
||||
var argText = args[argI];
|
||||
var argPlaceholder = "{" + argI + "}";
|
||||
|
||||
while (thisTemp.indexOf(argPlaceholder) > -1) {
|
||||
thisTemp = thisTemp.replace(argPlaceholder, argText);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
output += thisTemp
|
||||
|
||||
}
|
||||
|
||||
$("#output").val(output);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
286
third_party/src/github.com/stretchr/objx/codegen/template.txt
vendored
Normal file
286
third_party/src/github.com/stretchr/objx/codegen/template.txt
vendored
Normal file
@ -0,0 +1,286 @@
|
||||
/*
|
||||
{4} ({1} and []{1})
|
||||
--------------------------------------------------
|
||||
*/
|
||||
|
||||
// {4} gets the value as a {1}, returns the optionalDefault
|
||||
// value or a system default object if the value is the wrong type.
|
||||
func (v *Value) {4}(optionalDefault ...{1}) {1} {
|
||||
if s, ok := v.data.({1}); ok {
|
||||
return s
|
||||
}
|
||||
if len(optionalDefault) == 1 {
|
||||
return optionalDefault[0]
|
||||
}
|
||||
return {3}
|
||||
}
|
||||
|
||||
// Must{4} gets the value as a {1}.
|
||||
//
|
||||
// Panics if the object is not a {1}.
|
||||
func (v *Value) Must{4}() {1} {
|
||||
return v.data.({1})
|
||||
}
|
||||
|
||||
// {4}Slice gets the value as a []{1}, returns the optionalDefault
|
||||
// value or nil if the value is not a []{1}.
|
||||
func (v *Value) {4}Slice(optionalDefault ...[]{1}) []{1} {
|
||||
if s, ok := v.data.([]{1}); ok {
|
||||
return s
|
||||
}
|
||||
if len(optionalDefault) == 1 {
|
||||
return optionalDefault[0]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Must{4}Slice gets the value as a []{1}.
|
||||
//
|
||||
// Panics if the object is not a []{1}.
|
||||
func (v *Value) Must{4}Slice() []{1} {
|
||||
return v.data.([]{1})
|
||||
}
|
||||
|
||||
// Is{4} gets whether the object contained is a {1} or not.
|
||||
func (v *Value) Is{4}() bool {
|
||||
_, ok := v.data.({1})
|
||||
return ok
|
||||
}
|
||||
|
||||
// Is{4}Slice gets whether the object contained is a []{1} or not.
|
||||
func (v *Value) Is{4}Slice() bool {
|
||||
_, ok := v.data.([]{1})
|
||||
return ok
|
||||
}
|
||||
|
||||
// Each{4} calls the specified callback for each object
|
||||
// in the []{1}.
|
||||
//
|
||||
// Panics if the object is the wrong type.
|
||||
func (v *Value) Each{4}(callback func(int, {1}) bool) *Value {
|
||||
|
||||
for index, val := range v.Must{4}Slice() {
|
||||
carryon := callback(index, val)
|
||||
if carryon == false {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return v
|
||||
|
||||
}
|
||||
|
||||
// Where{4} uses the specified decider function to select items
|
||||
// from the []{1}. The object contained in the result will contain
|
||||
// only the selected items.
|
||||
func (v *Value) Where{4}(decider func(int, {1}) bool) *Value {
|
||||
|
||||
var selected []{1}
|
||||
|
||||
v.Each{4}(func(index int, val {1}) bool {
|
||||
shouldSelect := decider(index, val)
|
||||
if shouldSelect == false {
|
||||
selected = append(selected, val)
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
return &Value{data:selected}
|
||||
|
||||
}
|
||||
|
||||
// Group{4} uses the specified grouper function to group the items
|
||||
// keyed by the return of the grouper. The object contained in the
|
||||
// result will contain a map[string][]{1}.
|
||||
func (v *Value) Group{4}(grouper func(int, {1}) string) *Value {
|
||||
|
||||
groups := make(map[string][]{1})
|
||||
|
||||
v.Each{4}(func(index int, val {1}) bool {
|
||||
group := grouper(index, val)
|
||||
if _, ok := groups[group]; !ok {
|
||||
groups[group] = make([]{1}, 0)
|
||||
}
|
||||
groups[group] = append(groups[group], val)
|
||||
return true
|
||||
})
|
||||
|
||||
return &Value{data:groups}
|
||||
|
||||
}
|
||||
|
||||
// Replace{4} uses the specified function to replace each {1}s
|
||||
// by iterating each item. The data in the returned result will be a
|
||||
// []{1} containing the replaced items.
|
||||
func (v *Value) Replace{4}(replacer func(int, {1}) {1}) *Value {
|
||||
|
||||
arr := v.Must{4}Slice()
|
||||
replaced := make([]{1}, len(arr))
|
||||
|
||||
v.Each{4}(func(index int, val {1}) bool {
|
||||
replaced[index] = replacer(index, val)
|
||||
return true
|
||||
})
|
||||
|
||||
return &Value{data:replaced}
|
||||
|
||||
}
|
||||
|
||||
// Collect{4} uses the specified collector function to collect a value
|
||||
// for each of the {1}s in the slice. The data returned will be a
|
||||
// []interface{}.
|
||||
func (v *Value) Collect{4}(collector func(int, {1}) interface{}) *Value {
|
||||
|
||||
arr := v.Must{4}Slice()
|
||||
collected := make([]interface{}, len(arr))
|
||||
|
||||
v.Each{4}(func(index int, val {1}) bool {
|
||||
collected[index] = collector(index, val)
|
||||
return true
|
||||
})
|
||||
|
||||
return &Value{data:collected}
|
||||
}
|
||||
|
||||
// ************************************************************
|
||||
// TESTS
|
||||
// ************************************************************
|
||||
|
||||
func Test{4}(t *testing.T) {
|
||||
|
||||
val := {1}( {2} )
|
||||
m := map[string]interface{}{"value": val, "nothing": nil}
|
||||
assert.Equal(t, val, New(m).Get("value").{4}())
|
||||
assert.Equal(t, val, New(m).Get("value").Must{4}())
|
||||
assert.Equal(t, {1}({3}), New(m).Get("nothing").{4}())
|
||||
assert.Equal(t, val, New(m).Get("nothing").{4}({2}))
|
||||
|
||||
assert.Panics(t, func() {
|
||||
New(m).Get("age").Must{4}()
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func Test{4}Slice(t *testing.T) {
|
||||
|
||||
val := {1}( {2} )
|
||||
m := map[string]interface{}{"value": []{1}{ val }, "nothing": nil}
|
||||
assert.Equal(t, val, New(m).Get("value").{4}Slice()[0])
|
||||
assert.Equal(t, val, New(m).Get("value").Must{4}Slice()[0])
|
||||
assert.Equal(t, []{1}(nil), New(m).Get("nothing").{4}Slice())
|
||||
assert.Equal(t, val, New(m).Get("nothing").{4}Slice( []{1}{ {1}({2}) } )[0])
|
||||
|
||||
assert.Panics(t, func() {
|
||||
New(m).Get("nothing").Must{4}Slice()
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestIs{4}(t *testing.T) {
|
||||
|
||||
var v *Value
|
||||
|
||||
v = &Value{data: {1}({2})}
|
||||
assert.True(t, v.Is{4}())
|
||||
|
||||
v = &Value{data: []{1}{ {1}({2}) }}
|
||||
assert.True(t, v.Is{4}Slice())
|
||||
|
||||
}
|
||||
|
||||
func TestEach{4}(t *testing.T) {
|
||||
|
||||
v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }}
|
||||
count := 0
|
||||
replacedVals := make([]{1}, 0)
|
||||
assert.Equal(t, v, v.Each{4}(func(i int, val {1}) bool {
|
||||
|
||||
count++
|
||||
replacedVals = append(replacedVals, val)
|
||||
|
||||
// abort early
|
||||
if i == 2 {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
}))
|
||||
|
||||
assert.Equal(t, count, 3)
|
||||
assert.Equal(t, replacedVals[0], v.Must{4}Slice()[0])
|
||||
assert.Equal(t, replacedVals[1], v.Must{4}Slice()[1])
|
||||
assert.Equal(t, replacedVals[2], v.Must{4}Slice()[2])
|
||||
|
||||
}
|
||||
|
||||
func TestWhere{4}(t *testing.T) {
|
||||
|
||||
v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }}
|
||||
|
||||
selected := v.Where{4}(func(i int, val {1}) bool {
|
||||
return i%2==0
|
||||
}).Must{4}Slice()
|
||||
|
||||
assert.Equal(t, 3, len(selected))
|
||||
|
||||
}
|
||||
|
||||
func TestGroup{4}(t *testing.T) {
|
||||
|
||||
v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }}
|
||||
|
||||
grouped := v.Group{4}(func(i int, val {1}) string {
|
||||
return fmt.Sprintf("%v", i%2==0)
|
||||
}).data.(map[string][]{1})
|
||||
|
||||
assert.Equal(t, 2, len(grouped))
|
||||
assert.Equal(t, 3, len(grouped["true"]))
|
||||
assert.Equal(t, 3, len(grouped["false"]))
|
||||
|
||||
}
|
||||
|
||||
func TestReplace{4}(t *testing.T) {
|
||||
|
||||
v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }}
|
||||
|
||||
rawArr := v.Must{4}Slice()
|
||||
|
||||
replaced := v.Replace{4}(func(index int, val {1}) {1} {
|
||||
if index < len(rawArr)-1 {
|
||||
return rawArr[index+1]
|
||||
}
|
||||
return rawArr[0]
|
||||
})
|
||||
|
||||
replacedArr := replaced.Must{4}Slice()
|
||||
if assert.Equal(t, 6, len(replacedArr)) {
|
||||
assert.Equal(t, replacedArr[0], rawArr[1])
|
||||
assert.Equal(t, replacedArr[1], rawArr[2])
|
||||
assert.Equal(t, replacedArr[2], rawArr[3])
|
||||
assert.Equal(t, replacedArr[3], rawArr[4])
|
||||
assert.Equal(t, replacedArr[4], rawArr[5])
|
||||
assert.Equal(t, replacedArr[5], rawArr[0])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestCollect{4}(t *testing.T) {
|
||||
|
||||
v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }}
|
||||
|
||||
collected := v.Collect{4}(func(index int, val {1}) interface{} {
|
||||
return index
|
||||
})
|
||||
|
||||
collectedArr := collected.MustInterSlice()
|
||||
if assert.Equal(t, 6, len(collectedArr)) {
|
||||
assert.Equal(t, collectedArr[0], 0)
|
||||
assert.Equal(t, collectedArr[1], 1)
|
||||
assert.Equal(t, collectedArr[2], 2)
|
||||
assert.Equal(t, collectedArr[3], 3)
|
||||
assert.Equal(t, collectedArr[4], 4)
|
||||
assert.Equal(t, collectedArr[5], 5)
|
||||
}
|
||||
|
||||
}
|
20
third_party/src/github.com/stretchr/objx/codegen/types_list.txt
vendored
Normal file
20
third_party/src/github.com/stretchr/objx/codegen/types_list.txt
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
Interface,interface{},"something",nil,Inter
|
||||
Map,map[string]interface{},map[string]interface{}{"name":"Tyler"},nil,MSI
|
||||
ObjxMap,(Map),New(1),New(nil),ObjxMap
|
||||
Bool,bool,true,false,Bool
|
||||
String,string,"hello","",Str
|
||||
Int,int,1,0,Int
|
||||
Int8,int8,1,0,Int8
|
||||
Int16,int16,1,0,Int16
|
||||
Int32,int32,1,0,Int32
|
||||
Int64,int64,1,0,Int64
|
||||
Uint,uint,1,0,Uint
|
||||
Uint8,uint8,1,0,Uint8
|
||||
Uint16,uint16,1,0,Uint16
|
||||
Uint32,uint32,1,0,Uint32
|
||||
Uint64,uint64,1,0,Uint64
|
||||
Uintptr,uintptr,1,0,Uintptr
|
||||
Float32,float32,1,0,Float32
|
||||
Float64,float64,1,0,Float64
|
||||
Complex64,complex64,1,0,Complex64
|
||||
Complex128,complex128,1,0,Complex128
|
13
third_party/src/github.com/stretchr/objx/constants.go
vendored
Normal file
13
third_party/src/github.com/stretchr/objx/constants.go
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
package objx
|
||||
|
||||
const (
|
||||
// PathSeparator is the character used to separate the elements
|
||||
// of the keypath.
|
||||
//
|
||||
// For example, `location.address.city`
|
||||
PathSeparator string = "."
|
||||
|
||||
// SignatureSeparator is the character that is used to
|
||||
// separate the Base64 string from the security signature.
|
||||
SignatureSeparator = "_"
|
||||
)
|
117
third_party/src/github.com/stretchr/objx/conversions.go
vendored
Normal file
117
third_party/src/github.com/stretchr/objx/conversions.go
vendored
Normal file
@ -0,0 +1,117 @@
|
||||
package objx
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// JSON converts the contained object to a JSON string
|
||||
// representation
|
||||
func (m Map) JSON() (string, error) {
|
||||
|
||||
result, err := json.Marshal(m)
|
||||
|
||||
if err != nil {
|
||||
err = errors.New("objx: JSON encode failed with: " + err.Error())
|
||||
}
|
||||
|
||||
return string(result), err
|
||||
|
||||
}
|
||||
|
||||
// MustJSON converts the contained object to a JSON string
|
||||
// representation and panics if there is an error
|
||||
func (m Map) MustJSON() string {
|
||||
result, err := m.JSON()
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Base64 converts the contained object to a Base64 string
|
||||
// representation of the JSON string representation
|
||||
func (m Map) Base64() (string, error) {
|
||||
|
||||
var buf bytes.Buffer
|
||||
|
||||
jsonData, err := m.JSON()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
encoder := base64.NewEncoder(base64.StdEncoding, &buf)
|
||||
encoder.Write([]byte(jsonData))
|
||||
encoder.Close()
|
||||
|
||||
return buf.String(), nil
|
||||
|
||||
}
|
||||
|
||||
// MustBase64 converts the contained object to a Base64 string
|
||||
// representation of the JSON string representation and panics
|
||||
// if there is an error
|
||||
func (m Map) MustBase64() string {
|
||||
result, err := m.Base64()
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// SignedBase64 converts the contained object to a Base64 string
|
||||
// representation of the JSON string representation and signs it
|
||||
// using the provided key.
|
||||
func (m Map) SignedBase64(key string) (string, error) {
|
||||
|
||||
base64, err := m.Base64()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
sig := HashWithKey(base64, key)
|
||||
|
||||
return base64 + SignatureSeparator + sig, nil
|
||||
|
||||
}
|
||||
|
||||
// MustSignedBase64 converts the contained object to a Base64 string
|
||||
// representation of the JSON string representation and signs it
|
||||
// using the provided key and panics if there is an error
|
||||
func (m Map) MustSignedBase64(key string) string {
|
||||
result, err := m.SignedBase64(key)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/*
|
||||
URL Query
|
||||
------------------------------------------------
|
||||
*/
|
||||
|
||||
// URLValues creates a url.Values object from an Obj. This
|
||||
// function requires that the wrapped object be a map[string]interface{}
|
||||
func (m Map) URLValues() url.Values {
|
||||
|
||||
vals := make(url.Values)
|
||||
|
||||
for k, v := range m {
|
||||
//TODO: can this be done without sprintf?
|
||||
vals.Set(k, fmt.Sprintf("%v", v))
|
||||
}
|
||||
|
||||
return vals
|
||||
}
|
||||
|
||||
// URLQuery gets an encoded URL query representing the given
|
||||
// Obj. This function requires that the wrapped object be a
|
||||
// map[string]interface{}
|
||||
func (m Map) URLQuery() (string, error) {
|
||||
return m.URLValues().Encode(), nil
|
||||
}
|
94
third_party/src/github.com/stretchr/objx/conversions_test.go
vendored
Normal file
94
third_party/src/github.com/stretchr/objx/conversions_test.go
vendored
Normal file
@ -0,0 +1,94 @@
|
||||
package objx
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConversionJSON(t *testing.T) {
|
||||
|
||||
jsonString := `{"name":"Mat"}`
|
||||
o := MustFromJSON(jsonString)
|
||||
|
||||
result, err := o.JSON()
|
||||
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, jsonString, result)
|
||||
}
|
||||
|
||||
assert.Equal(t, jsonString, o.MustJSON())
|
||||
|
||||
}
|
||||
|
||||
func TestConversionJSONWithError(t *testing.T) {
|
||||
|
||||
o := MSI()
|
||||
o["test"] = func() {}
|
||||
|
||||
assert.Panics(t, func() {
|
||||
o.MustJSON()
|
||||
})
|
||||
|
||||
_, err := o.JSON()
|
||||
|
||||
assert.Error(t, err)
|
||||
|
||||
}
|
||||
|
||||
func TestConversionBase64(t *testing.T) {
|
||||
|
||||
o := New(map[string]interface{}{"name": "Mat"})
|
||||
|
||||
result, err := o.Base64()
|
||||
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, "eyJuYW1lIjoiTWF0In0=", result)
|
||||
}
|
||||
|
||||
assert.Equal(t, "eyJuYW1lIjoiTWF0In0=", o.MustBase64())
|
||||
|
||||
}
|
||||
|
||||
func TestConversionBase64WithError(t *testing.T) {
|
||||
|
||||
o := MSI()
|
||||
o["test"] = func() {}
|
||||
|
||||
assert.Panics(t, func() {
|
||||
o.MustBase64()
|
||||
})
|
||||
|
||||
_, err := o.Base64()
|
||||
|
||||
assert.Error(t, err)
|
||||
|
||||
}
|
||||
|
||||
func TestConversionSignedBase64(t *testing.T) {
|
||||
|
||||
o := New(map[string]interface{}{"name": "Mat"})
|
||||
|
||||
result, err := o.SignedBase64("key")
|
||||
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, "eyJuYW1lIjoiTWF0In0=_67ee82916f90b2c0d68c903266e8998c9ef0c3d6", result)
|
||||
}
|
||||
|
||||
assert.Equal(t, "eyJuYW1lIjoiTWF0In0=_67ee82916f90b2c0d68c903266e8998c9ef0c3d6", o.MustSignedBase64("key"))
|
||||
|
||||
}
|
||||
|
||||
func TestConversionSignedBase64WithError(t *testing.T) {
|
||||
|
||||
o := MSI()
|
||||
o["test"] = func() {}
|
||||
|
||||
assert.Panics(t, func() {
|
||||
o.MustSignedBase64("key")
|
||||
})
|
||||
|
||||
_, err := o.SignedBase64("key")
|
||||
|
||||
assert.Error(t, err)
|
||||
|
||||
}
|
72
third_party/src/github.com/stretchr/objx/doc.go
vendored
Normal file
72
third_party/src/github.com/stretchr/objx/doc.go
vendored
Normal file
@ -0,0 +1,72 @@
|
||||
// objx - Go package for dealing with maps, slices, JSON and other data.
|
||||
//
|
||||
// Overview
|
||||
//
|
||||
// Objx provides the `objx.Map` type, which is a `map[string]interface{}` that exposes
|
||||
// a powerful `Get` method (among others) that allows you to easily and quickly get
|
||||
// access to data within the map, without having to worry too much about type assertions,
|
||||
// missing data, default values etc.
|
||||
//
|
||||
// Pattern
|
||||
//
|
||||
// Objx uses a preditable pattern to make access data from within `map[string]interface{}'s
|
||||
// easy.
|
||||
//
|
||||
// Call one of the `objx.` functions to create your `objx.Map` to get going:
|
||||
//
|
||||
// m, err := objx.FromJSON(json)
|
||||
//
|
||||
// NOTE: Any methods or functions with the `Must` prefix will panic if something goes wrong,
|
||||
// the rest will be optimistic and try to figure things out without panicking.
|
||||
//
|
||||
// Use `Get` to access the value you're interested in. You can use dot and array
|
||||
// notation too:
|
||||
//
|
||||
// m.Get("places[0].latlng")
|
||||
//
|
||||
// Once you have saught the `Value` you're interested in, you can use the `Is*` methods
|
||||
// to determine its type.
|
||||
//
|
||||
// if m.Get("code").IsStr() { /* ... */ }
|
||||
//
|
||||
// Or you can just assume the type, and use one of the strong type methods to
|
||||
// extract the real value:
|
||||
//
|
||||
// m.Get("code").Int()
|
||||
//
|
||||
// If there's no value there (or if it's the wrong type) then a default value
|
||||
// will be returned, or you can be explicit about the default value.
|
||||
//
|
||||
// Get("code").Int(-1)
|
||||
//
|
||||
// If you're dealing with a slice of data as a value, Objx provides many useful
|
||||
// methods for iterating, manipulating and selecting that data. You can find out more
|
||||
// by exploring the index below.
|
||||
//
|
||||
// Reading data
|
||||
//
|
||||
// A simple example of how to use Objx:
|
||||
//
|
||||
// // use MustFromJSON to make an objx.Map from some JSON
|
||||
// m := objx.MustFromJSON(`{"name": "Mat", "age": 30}`)
|
||||
//
|
||||
// // get the details
|
||||
// name := m.Get("name").Str()
|
||||
// age := m.Get("age").Int()
|
||||
//
|
||||
// // get their nickname (or use their name if they
|
||||
// // don't have one)
|
||||
// nickname := m.Get("nickname").Str(name)
|
||||
//
|
||||
// Ranging
|
||||
//
|
||||
// Since `objx.Map` is a `map[string]interface{}` you can treat it as such. For
|
||||
// example, to `range` the data, do what you would expect:
|
||||
//
|
||||
// m := objx.MustFromJSON(json)
|
||||
// for key, value := range m {
|
||||
//
|
||||
// /* ... do your magic ... */
|
||||
//
|
||||
// }
|
||||
package objx
|
98
third_party/src/github.com/stretchr/objx/fixture_test.go
vendored
Normal file
98
third_party/src/github.com/stretchr/objx/fixture_test.go
vendored
Normal file
@ -0,0 +1,98 @@
|
||||
package objx
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var fixtures = []struct {
|
||||
// name is the name of the fixture (used for reporting
|
||||
// failures)
|
||||
name string
|
||||
// data is the JSON data to be worked on
|
||||
data string
|
||||
// get is the argument(s) to pass to Get
|
||||
get interface{}
|
||||
// output is the expected output
|
||||
output interface{}
|
||||
}{
|
||||
{
|
||||
name: "Simple get",
|
||||
data: `{"name": "Mat"}`,
|
||||
get: "name",
|
||||
output: "Mat",
|
||||
},
|
||||
{
|
||||
name: "Get with dot notation",
|
||||
data: `{"address": {"city": "Boulder"}}`,
|
||||
get: "address.city",
|
||||
output: "Boulder",
|
||||
},
|
||||
{
|
||||
name: "Deep get with dot notation",
|
||||
data: `{"one": {"two": {"three": {"four": "hello"}}}}`,
|
||||
get: "one.two.three.four",
|
||||
output: "hello",
|
||||
},
|
||||
{
|
||||
name: "Get missing with dot notation",
|
||||
data: `{"one": {"two": {"three": {"four": "hello"}}}}`,
|
||||
get: "one.ten",
|
||||
output: nil,
|
||||
},
|
||||
{
|
||||
name: "Get with array notation",
|
||||
data: `{"tags": ["one", "two", "three"]}`,
|
||||
get: "tags[1]",
|
||||
output: "two",
|
||||
},
|
||||
{
|
||||
name: "Get with array and dot notation",
|
||||
data: `{"types": { "tags": ["one", "two", "three"]}}`,
|
||||
get: "types.tags[1]",
|
||||
output: "two",
|
||||
},
|
||||
{
|
||||
name: "Get with array and dot notation - field after array",
|
||||
data: `{"tags": [{"name":"one"}, {"name":"two"}, {"name":"three"}]}`,
|
||||
get: "tags[1].name",
|
||||
output: "two",
|
||||
},
|
||||
{
|
||||
name: "Complex get with array and dot notation",
|
||||
data: `{"tags": [{"list": [{"one":"pizza"}]}]}`,
|
||||
get: "tags[0].list[0].one",
|
||||
output: "pizza",
|
||||
},
|
||||
{
|
||||
name: "Get field from within string should be nil",
|
||||
data: `{"name":"Tyler"}`,
|
||||
get: "name.something",
|
||||
output: nil,
|
||||
},
|
||||
{
|
||||
name: "Get field from within string (using array accessor) should be nil",
|
||||
data: `{"numbers":["one", "two", "three"]}`,
|
||||
get: "numbers[0].nope",
|
||||
output: nil,
|
||||
},
|
||||
}
|
||||
|
||||
func TestFixtures(t *testing.T) {
|
||||
|
||||
for _, fixture := range fixtures {
|
||||
|
||||
m := MustFromJSON(fixture.data)
|
||||
|
||||
// get the value
|
||||
t.Logf("Running get fixture: \"%s\" (%v)", fixture.name, fixture)
|
||||
value := m.Get(fixture.get.(string))
|
||||
|
||||
// make sure it matches
|
||||
assert.Equal(t, fixture.output, value.data,
|
||||
"Get fixture \"%s\" failed: %v", fixture.name, fixture,
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
}
|
222
third_party/src/github.com/stretchr/objx/map.go
vendored
Normal file
222
third_party/src/github.com/stretchr/objx/map.go
vendored
Normal file
@ -0,0 +1,222 @@
|
||||
package objx
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// MSIConvertable is an interface that defines methods for converting your
|
||||
// custom types to a map[string]interface{} representation.
|
||||
type MSIConvertable interface {
|
||||
// MSI gets a map[string]interface{} (msi) representing the
|
||||
// object.
|
||||
MSI() map[string]interface{}
|
||||
}
|
||||
|
||||
// Map provides extended functionality for working with
|
||||
// untyped data, in particular map[string]interface (msi).
|
||||
type Map map[string]interface{}
|
||||
|
||||
// Value returns the internal value instance
|
||||
func (m Map) Value() *Value {
|
||||
return &Value{data: m}
|
||||
}
|
||||
|
||||
// Nil represents a nil Map.
|
||||
var Nil Map = New(nil)
|
||||
|
||||
// New creates a new Map containing the map[string]interface{} in the data argument.
|
||||
// If the data argument is not a map[string]interface, New attempts to call the
|
||||
// MSI() method on the MSIConvertable interface to create one.
|
||||
func New(data interface{}) Map {
|
||||
if _, ok := data.(map[string]interface{}); !ok {
|
||||
if converter, ok := data.(MSIConvertable); ok {
|
||||
data = converter.MSI()
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return Map(data.(map[string]interface{}))
|
||||
}
|
||||
|
||||
// MSI creates a map[string]interface{} and puts it inside a new Map.
|
||||
//
|
||||
// The arguments follow a key, value pattern.
|
||||
//
|
||||
// Panics
|
||||
//
|
||||
// Panics if any key arugment is non-string or if there are an odd number of arguments.
|
||||
//
|
||||
// Example
|
||||
//
|
||||
// To easily create Maps:
|
||||
//
|
||||
// m := objx.MSI("name", "Mat", "age", 29, "subobj", objx.MSI("active", true))
|
||||
//
|
||||
// // creates an Map equivalent to
|
||||
// m := objx.New(map[string]interface{}{"name": "Mat", "age": 29, "subobj": map[string]interface{}{"active": true}})
|
||||
func MSI(keyAndValuePairs ...interface{}) Map {
|
||||
|
||||
newMap := make(map[string]interface{})
|
||||
keyAndValuePairsLen := len(keyAndValuePairs)
|
||||
|
||||
if keyAndValuePairsLen%2 != 0 {
|
||||
panic("objx: MSI must have an even number of arguments following the 'key, value' pattern.")
|
||||
}
|
||||
|
||||
for i := 0; i < keyAndValuePairsLen; i = i + 2 {
|
||||
|
||||
key := keyAndValuePairs[i]
|
||||
value := keyAndValuePairs[i+1]
|
||||
|
||||
// make sure the key is a string
|
||||
keyString, keyStringOK := key.(string)
|
||||
if !keyStringOK {
|
||||
panic("objx: MSI must follow 'string, interface{}' pattern. " + keyString + " is not a valid key.")
|
||||
}
|
||||
|
||||
newMap[keyString] = value
|
||||
|
||||
}
|
||||
|
||||
return New(newMap)
|
||||
}
|
||||
|
||||
// ****** Conversion Constructors
|
||||
|
||||
// MustFromJSON creates a new Map containing the data specified in the
|
||||
// jsonString.
|
||||
//
|
||||
// Panics if the JSON is invalid.
|
||||
func MustFromJSON(jsonString string) Map {
|
||||
o, err := FromJSON(jsonString)
|
||||
|
||||
if err != nil {
|
||||
panic("objx: MustFromJSON failed with error: " + err.Error())
|
||||
}
|
||||
|
||||
return o
|
||||
}
|
||||
|
||||
// FromJSON creates a new Map containing the data specified in the
|
||||
// jsonString.
|
||||
//
|
||||
// Returns an error if the JSON is invalid.
|
||||
func FromJSON(jsonString string) (Map, error) {
|
||||
|
||||
var data interface{}
|
||||
err := json.Unmarshal([]byte(jsonString), &data)
|
||||
|
||||
if err != nil {
|
||||
return Nil, err
|
||||
}
|
||||
|
||||
return New(data), nil
|
||||
|
||||
}
|
||||
|
||||
// FromBase64 creates a new Obj containing the data specified
|
||||
// in the Base64 string.
|
||||
//
|
||||
// The string is an encoded JSON string returned by Base64
|
||||
func FromBase64(base64String string) (Map, error) {
|
||||
|
||||
decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(base64String))
|
||||
|
||||
decoded, err := ioutil.ReadAll(decoder)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return FromJSON(string(decoded))
|
||||
}
|
||||
|
||||
// MustFromBase64 creates a new Obj containing the data specified
|
||||
// in the Base64 string and panics if there is an error.
|
||||
//
|
||||
// The string is an encoded JSON string returned by Base64
|
||||
func MustFromBase64(base64String string) Map {
|
||||
|
||||
result, err := FromBase64(base64String)
|
||||
|
||||
if err != nil {
|
||||
panic("objx: MustFromBase64 failed with error: " + err.Error())
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// FromSignedBase64 creates a new Obj containing the data specified
|
||||
// in the Base64 string.
|
||||
//
|
||||
// The string is an encoded JSON string returned by SignedBase64
|
||||
func FromSignedBase64(base64String, key string) (Map, error) {
|
||||
parts := strings.Split(base64String, SignatureSeparator)
|
||||
if len(parts) != 2 {
|
||||
return nil, errors.New("objx: Signed base64 string is malformed.")
|
||||
}
|
||||
|
||||
sig := HashWithKey(parts[0], key)
|
||||
if parts[1] != sig {
|
||||
return nil, errors.New("objx: Signature for base64 data does not match.")
|
||||
}
|
||||
|
||||
return FromBase64(parts[0])
|
||||
}
|
||||
|
||||
// MustFromSignedBase64 creates a new Obj containing the data specified
|
||||
// in the Base64 string and panics if there is an error.
|
||||
//
|
||||
// The string is an encoded JSON string returned by Base64
|
||||
func MustFromSignedBase64(base64String, key string) Map {
|
||||
|
||||
result, err := FromSignedBase64(base64String, key)
|
||||
|
||||
if err != nil {
|
||||
panic("objx: MustFromSignedBase64 failed with error: " + err.Error())
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// FromURLQuery generates a new Obj by parsing the specified
|
||||
// query.
|
||||
//
|
||||
// For queries with multiple values, the first value is selected.
|
||||
func FromURLQuery(query string) (Map, error) {
|
||||
|
||||
vals, err := url.ParseQuery(query)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m := make(map[string]interface{})
|
||||
for k, vals := range vals {
|
||||
m[k] = vals[0]
|
||||
}
|
||||
|
||||
return New(m), nil
|
||||
}
|
||||
|
||||
// MustFromURLQuery generates a new Obj by parsing the specified
|
||||
// query.
|
||||
//
|
||||
// For queries with multiple values, the first value is selected.
|
||||
//
|
||||
// Panics if it encounters an error
|
||||
func MustFromURLQuery(query string) Map {
|
||||
|
||||
o, err := FromURLQuery(query)
|
||||
|
||||
if err != nil {
|
||||
panic("objx: MustFromURLQuery failed with error: " + err.Error())
|
||||
}
|
||||
|
||||
return o
|
||||
|
||||
}
|
10
third_party/src/github.com/stretchr/objx/map_for_test.go
vendored
Normal file
10
third_party/src/github.com/stretchr/objx/map_for_test.go
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
package objx
|
||||
|
||||
var TestMap map[string]interface{} = map[string]interface{}{
|
||||
"name": "Tyler",
|
||||
"address": map[string]interface{}{
|
||||
"city": "Salt Lake City",
|
||||
"state": "UT",
|
||||
},
|
||||
"numbers": []interface{}{"one", "two", "three", "four", "five"},
|
||||
}
|
147
third_party/src/github.com/stretchr/objx/map_test.go
vendored
Normal file
147
third_party/src/github.com/stretchr/objx/map_test.go
vendored
Normal file
@ -0,0 +1,147 @@
|
||||
package objx
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type Convertable struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func (c *Convertable) MSI() map[string]interface{} {
|
||||
return map[string]interface{}{"name": c.name}
|
||||
}
|
||||
|
||||
type Unconvertable struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func TestMapCreation(t *testing.T) {
|
||||
|
||||
o := New(nil)
|
||||
assert.Nil(t, o)
|
||||
|
||||
o = New("Tyler")
|
||||
assert.Nil(t, o)
|
||||
|
||||
unconvertable := &Unconvertable{name: "Tyler"}
|
||||
o = New(unconvertable)
|
||||
assert.Nil(t, o)
|
||||
|
||||
convertable := &Convertable{name: "Tyler"}
|
||||
o = New(convertable)
|
||||
if assert.NotNil(t, convertable) {
|
||||
assert.Equal(t, "Tyler", o["name"], "Tyler")
|
||||
}
|
||||
|
||||
o = MSI()
|
||||
if assert.NotNil(t, o) {
|
||||
assert.NotNil(t, o)
|
||||
}
|
||||
|
||||
o = MSI("name", "Tyler")
|
||||
if assert.NotNil(t, o) {
|
||||
if assert.NotNil(t, o) {
|
||||
assert.Equal(t, o["name"], "Tyler")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestMapMustFromJSONWithError(t *testing.T) {
|
||||
|
||||
_, err := FromJSON(`"name":"Mat"}`)
|
||||
assert.Error(t, err)
|
||||
|
||||
}
|
||||
|
||||
func TestMapFromJSON(t *testing.T) {
|
||||
|
||||
o := MustFromJSON(`{"name":"Mat"}`)
|
||||
|
||||
if assert.NotNil(t, o) {
|
||||
if assert.NotNil(t, o) {
|
||||
assert.Equal(t, "Mat", o["name"])
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestMapFromJSONWithError(t *testing.T) {
|
||||
|
||||
var m Map
|
||||
|
||||
assert.Panics(t, func() {
|
||||
m = MustFromJSON(`"name":"Mat"}`)
|
||||
})
|
||||
|
||||
assert.Nil(t, m)
|
||||
|
||||
}
|
||||
|
||||
func TestMapFromBase64String(t *testing.T) {
|
||||
|
||||
base64String := "eyJuYW1lIjoiTWF0In0="
|
||||
|
||||
o, err := FromBase64(base64String)
|
||||
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, o.Get("name").Str(), "Mat")
|
||||
}
|
||||
|
||||
assert.Equal(t, MustFromBase64(base64String).Get("name").Str(), "Mat")
|
||||
|
||||
}
|
||||
|
||||
func TestMapFromBase64StringWithError(t *testing.T) {
|
||||
|
||||
base64String := "eyJuYW1lIjoiTWFasd0In0="
|
||||
|
||||
_, err := FromBase64(base64String)
|
||||
|
||||
assert.Error(t, err)
|
||||
|
||||
assert.Panics(t, func() {
|
||||
MustFromBase64(base64String)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestMapFromSignedBase64String(t *testing.T) {
|
||||
|
||||
base64String := "eyJuYW1lIjoiTWF0In0=_67ee82916f90b2c0d68c903266e8998c9ef0c3d6"
|
||||
|
||||
o, err := FromSignedBase64(base64String, "key")
|
||||
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, o.Get("name").Str(), "Mat")
|
||||
}
|
||||
|
||||
assert.Equal(t, MustFromSignedBase64(base64String, "key").Get("name").Str(), "Mat")
|
||||
|
||||
}
|
||||
|
||||
func TestMapFromSignedBase64StringWithError(t *testing.T) {
|
||||
|
||||
base64String := "eyJuYW1lasdIjoiTWF0In0=_67ee82916f90b2c0d68c903266e8998c9ef0c3d6"
|
||||
|
||||
_, err := FromSignedBase64(base64String, "key")
|
||||
|
||||
assert.Error(t, err)
|
||||
|
||||
assert.Panics(t, func() {
|
||||
MustFromSignedBase64(base64String, "key")
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestMapFromURLQuery(t *testing.T) {
|
||||
|
||||
m, err := FromURLQuery("name=tyler&state=UT")
|
||||
if assert.NoError(t, err) && assert.NotNil(t, m) {
|
||||
assert.Equal(t, "tyler", m.Get("name").Str())
|
||||
assert.Equal(t, "UT", m.Get("state").Str())
|
||||
}
|
||||
|
||||
}
|
81
third_party/src/github.com/stretchr/objx/mutations.go
vendored
Normal file
81
third_party/src/github.com/stretchr/objx/mutations.go
vendored
Normal file
@ -0,0 +1,81 @@
|
||||
package objx
|
||||
|
||||
// Exclude returns a new Map with the keys in the specified []string
|
||||
// excluded.
|
||||
func (d Map) Exclude(exclude []string) Map {
|
||||
|
||||
excluded := make(Map)
|
||||
for k, v := range d {
|
||||
var shouldInclude bool = true
|
||||
for _, toExclude := range exclude {
|
||||
if k == toExclude {
|
||||
shouldInclude = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if shouldInclude {
|
||||
excluded[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
return excluded
|
||||
}
|
||||
|
||||
// Copy creates a shallow copy of the Obj.
|
||||
func (m Map) Copy() Map {
|
||||
copied := make(map[string]interface{})
|
||||
for k, v := range m {
|
||||
copied[k] = v
|
||||
}
|
||||
return New(copied)
|
||||
}
|
||||
|
||||
// Merge blends the specified map with a copy of this map and returns the result.
|
||||
//
|
||||
// Keys that appear in both will be selected from the specified map.
|
||||
// This method requires that the wrapped object be a map[string]interface{}
|
||||
func (m Map) Merge(merge Map) Map {
|
||||
return m.Copy().MergeHere(merge)
|
||||
}
|
||||
|
||||
// Merge blends the specified map with this map and returns the current map.
|
||||
//
|
||||
// Keys that appear in both will be selected from the specified map. The original map
|
||||
// will be modified. This method requires that
|
||||
// the wrapped object be a map[string]interface{}
|
||||
func (m Map) MergeHere(merge Map) Map {
|
||||
|
||||
for k, v := range merge {
|
||||
m[k] = v
|
||||
}
|
||||
|
||||
return m
|
||||
|
||||
}
|
||||
|
||||
// Transform builds a new Obj giving the transformer a chance
|
||||
// to change the keys and values as it goes. This method requires that
|
||||
// the wrapped object be a map[string]interface{}
|
||||
func (m Map) Transform(transformer func(key string, value interface{}) (string, interface{})) Map {
|
||||
newMap := make(map[string]interface{})
|
||||
for k, v := range m {
|
||||
modifiedKey, modifiedVal := transformer(k, v)
|
||||
newMap[modifiedKey] = modifiedVal
|
||||
}
|
||||
return New(newMap)
|
||||
}
|
||||
|
||||
// TransformKeys builds a new map using the specified key mapping.
|
||||
//
|
||||
// Unspecified keys will be unaltered.
|
||||
// This method requires that the wrapped object be a map[string]interface{}
|
||||
func (m Map) TransformKeys(mapping map[string]string) Map {
|
||||
return m.Transform(func(key string, value interface{}) (string, interface{}) {
|
||||
|
||||
if newKey, ok := mapping[key]; ok {
|
||||
return newKey, value
|
||||
}
|
||||
|
||||
return key, value
|
||||
})
|
||||
}
|
77
third_party/src/github.com/stretchr/objx/mutations_test.go
vendored
Normal file
77
third_party/src/github.com/stretchr/objx/mutations_test.go
vendored
Normal file
@ -0,0 +1,77 @@
|
||||
package objx
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExclude(t *testing.T) {
|
||||
|
||||
d := make(Map)
|
||||
d["name"] = "Mat"
|
||||
d["age"] = 29
|
||||
d["secret"] = "ABC"
|
||||
|
||||
excluded := d.Exclude([]string{"secret"})
|
||||
|
||||
assert.Equal(t, d["name"], excluded["name"])
|
||||
assert.Equal(t, d["age"], excluded["age"])
|
||||
assert.False(t, excluded.Has("secret"), "secret should be excluded")
|
||||
|
||||
}
|
||||
|
||||
func TestCopy(t *testing.T) {
|
||||
|
||||
d1 := make(map[string]interface{})
|
||||
d1["name"] = "Tyler"
|
||||
d1["location"] = "UT"
|
||||
|
||||
d1Obj := New(d1)
|
||||
d2Obj := d1Obj.Copy()
|
||||
|
||||
d2Obj["name"] = "Mat"
|
||||
|
||||
assert.Equal(t, d1Obj.Get("name").Str(), "Tyler")
|
||||
assert.Equal(t, d2Obj.Get("name").Str(), "Mat")
|
||||
|
||||
}
|
||||
|
||||
func TestMerge(t *testing.T) {
|
||||
|
||||
d := make(map[string]interface{})
|
||||
d["name"] = "Mat"
|
||||
|
||||
d1 := make(map[string]interface{})
|
||||
d1["name"] = "Tyler"
|
||||
d1["location"] = "UT"
|
||||
|
||||
dObj := New(d)
|
||||
d1Obj := New(d1)
|
||||
|
||||
merged := dObj.Merge(d1Obj)
|
||||
|
||||
assert.Equal(t, merged.Get("name").Str(), d1Obj.Get("name").Str())
|
||||
assert.Equal(t, merged.Get("location").Str(), d1Obj.Get("location").Str())
|
||||
assert.Empty(t, dObj.Get("location").Str())
|
||||
|
||||
}
|
||||
|
||||
func TestMergeHere(t *testing.T) {
|
||||
|
||||
d := make(map[string]interface{})
|
||||
d["name"] = "Mat"
|
||||
|
||||
d1 := make(map[string]interface{})
|
||||
d1["name"] = "Tyler"
|
||||
d1["location"] = "UT"
|
||||
|
||||
dObj := New(d)
|
||||
d1Obj := New(d1)
|
||||
|
||||
merged := dObj.MergeHere(d1Obj)
|
||||
|
||||
assert.Equal(t, dObj, merged, "With MergeHere, it should return the first modified map")
|
||||
assert.Equal(t, merged.Get("name").Str(), d1Obj.Get("name").Str())
|
||||
assert.Equal(t, merged.Get("location").Str(), d1Obj.Get("location").Str())
|
||||
assert.Equal(t, merged.Get("location").Str(), dObj.Get("location").Str())
|
||||
}
|
14
third_party/src/github.com/stretchr/objx/security.go
vendored
Normal file
14
third_party/src/github.com/stretchr/objx/security.go
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
package objx
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
// HashWithKey hashes the specified string using the security
|
||||
// key.
|
||||
func HashWithKey(data, key string) string {
|
||||
hash := sha1.New()
|
||||
hash.Write([]byte(data + ":" + key))
|
||||
return hex.EncodeToString(hash.Sum(nil))
|
||||
}
|
12
third_party/src/github.com/stretchr/objx/security_test.go
vendored
Normal file
12
third_party/src/github.com/stretchr/objx/security_test.go
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
package objx
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHashWithKey(t *testing.T) {
|
||||
|
||||
assert.Equal(t, "0ce84d8d01f2c7b6e0882b784429c54d280ea2d9", HashWithKey("abc", "def"))
|
||||
|
||||
}
|
41
third_party/src/github.com/stretchr/objx/simple_example_test.go
vendored
Normal file
41
third_party/src/github.com/stretchr/objx/simple_example_test.go
vendored
Normal file
@ -0,0 +1,41 @@
|
||||
package objx
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSimpleExample(t *testing.T) {
|
||||
|
||||
// build a map from a JSON object
|
||||
o := MustFromJSON(`{"name":"Mat","foods":["indian","chinese"], "location":{"county":"hobbiton","city":"the shire"}}`)
|
||||
|
||||
// Map can be used as a straight map[string]interface{}
|
||||
assert.Equal(t, o["name"], "Mat")
|
||||
|
||||
// Get an Value object
|
||||
v := o.Get("name")
|
||||
assert.Equal(t, v, &Value{data: "Mat"})
|
||||
|
||||
// Test the contained value
|
||||
assert.False(t, v.IsInt())
|
||||
assert.False(t, v.IsBool())
|
||||
assert.True(t, v.IsStr())
|
||||
|
||||
// Get the contained value
|
||||
assert.Equal(t, v.Str(), "Mat")
|
||||
|
||||
// Get a default value if the contained value is not of the expected type or does not exist
|
||||
assert.Equal(t, 1, v.Int(1))
|
||||
|
||||
// Get a value by using array notation
|
||||
assert.Equal(t, "indian", o.Get("foods[0]").Data())
|
||||
|
||||
// Set a value by using array notation
|
||||
o.Set("foods[0]", "italian")
|
||||
assert.Equal(t, "italian", o.Get("foods[0]").Str())
|
||||
|
||||
// Get a value by using dot notation
|
||||
assert.Equal(t, "hobbiton", o.Get("location.county").Str())
|
||||
|
||||
}
|
17
third_party/src/github.com/stretchr/objx/tests.go
vendored
Normal file
17
third_party/src/github.com/stretchr/objx/tests.go
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
package objx
|
||||
|
||||
// Has gets whether there is something at the specified selector
|
||||
// or not.
|
||||
//
|
||||
// If m is nil, Has will always return false.
|
||||
func (m Map) Has(selector string) bool {
|
||||
if m == nil {
|
||||
return false
|
||||
}
|
||||
return !m.Get(selector).IsNil()
|
||||
}
|
||||
|
||||
// IsNil gets whether the data is nil or not.
|
||||
func (v *Value) IsNil() bool {
|
||||
return v == nil || v.data == nil
|
||||
}
|
24
third_party/src/github.com/stretchr/objx/tests_test.go
vendored
Normal file
24
third_party/src/github.com/stretchr/objx/tests_test.go
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
package objx
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHas(t *testing.T) {
|
||||
|
||||
m := New(TestMap)
|
||||
|
||||
assert.True(t, m.Has("name"))
|
||||
assert.True(t, m.Has("address.state"))
|
||||
assert.True(t, m.Has("numbers[4]"))
|
||||
|
||||
assert.False(t, m.Has("address.state.nope"))
|
||||
assert.False(t, m.Has("address.nope"))
|
||||
assert.False(t, m.Has("nope"))
|
||||
assert.False(t, m.Has("numbers[5]"))
|
||||
|
||||
m = nil
|
||||
assert.False(t, m.Has("nothing"))
|
||||
|
||||
}
|
2881
third_party/src/github.com/stretchr/objx/type_specific_codegen.go
vendored
Normal file
2881
third_party/src/github.com/stretchr/objx/type_specific_codegen.go
vendored
Normal file
File diff suppressed because it is too large
Load Diff
2867
third_party/src/github.com/stretchr/objx/type_specific_codegen_test.go
vendored
Normal file
2867
third_party/src/github.com/stretchr/objx/type_specific_codegen_test.go
vendored
Normal file
File diff suppressed because it is too large
Load Diff
13
third_party/src/github.com/stretchr/objx/value.go
vendored
Normal file
13
third_party/src/github.com/stretchr/objx/value.go
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
package objx
|
||||
|
||||
// Value provides methods for extracting interface{} data in various
|
||||
// types.
|
||||
type Value struct {
|
||||
// data contains the raw data being managed by this Value
|
||||
data interface{}
|
||||
}
|
||||
|
||||
// Data returns the raw data contained by this Value
|
||||
func (v *Value) Data() interface{} {
|
||||
return v.data
|
||||
}
|
1
third_party/src/github.com/stretchr/objx/value_test.go
vendored
Normal file
1
third_party/src/github.com/stretchr/objx/value_test.go
vendored
Normal file
@ -0,0 +1 @@
|
||||
package objx
|
9
third_party/src/github.com/stretchr/testify/LICENCE.txt
vendored
Normal file
9
third_party/src/github.com/stretchr/testify/LICENCE.txt
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
Copyright (c) 2012 - 2013 Mat Ryer and Tyler Bunnell
|
||||
|
||||
Please consider promoting this project if you find it useful.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
230
third_party/src/github.com/stretchr/testify/README.md
vendored
Normal file
230
third_party/src/github.com/stretchr/testify/README.md
vendored
Normal file
@ -0,0 +1,230 @@
|
||||
Testify - Thou Shalt Write Tests
|
||||
================================
|
||||
|
||||
Go code (golang) set of packages that provide many tools for testifying that your code will behave as you intend.
|
||||
|
||||
Features include:
|
||||
|
||||
* [Easy assertions](#assert-package)
|
||||
* [Mocking](#mock-package)
|
||||
* [HTTP response trapping](#http-package)
|
||||
* [Testing suite interfaces and functions](#suite-package)
|
||||
|
||||
Get started:
|
||||
|
||||
* Install testify with [one line of code](#installation), or [update it with another](#staying-up-to-date)
|
||||
* For an introduction to writing test code in Go, see our [blog post article](http://blog.stretchr.com/2014/03/05/test-driven-development-specifically-in-golang/) or check out http://golang.org/doc/code.html#Testing
|
||||
* Check out the API Documentation http://godoc.org/github.com/stretchr/testify
|
||||
* To make your testing life easier, check out our other project, [gorc](http://github.com/stretchr/gorc)
|
||||
* A little about [Test-Driven Development (TDD)](http://en.wikipedia.org/wiki/Test-driven_development)
|
||||
|
||||
|
||||
|
||||
`assert` package
|
||||
----------------
|
||||
|
||||
The `assert` package provides some helpful methods that allow you to write better test code in Go. Check out the [API documentation for the `assert` package](http://godoc.org/github.com/stretchr/testify/assert).
|
||||
|
||||
* Prints friendly, easy to read failure descriptions
|
||||
* Allows for very readable code
|
||||
* Optionally annotate each assertion with a message
|
||||
|
||||
See it in action:
|
||||
|
||||
func TestSomething(t *testing.T) {
|
||||
|
||||
// assert equality
|
||||
assert.Equal(t, 123, 123, "they should be equal")
|
||||
|
||||
// assert inequality
|
||||
assert.NotEqual(t, 123, 456, "they should not be equal")
|
||||
|
||||
// assert for nil (good for errors)
|
||||
assert.Nil(t, object)
|
||||
|
||||
// assert for not nil (good when you expect something)
|
||||
if assert.NotNil(t, object) {
|
||||
|
||||
// now we know that object isn't nil, we are safe to make
|
||||
// further assertions without causing any errors
|
||||
assert.Equal(t, "Something", object.Value)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
* Every assert func takes the `testing.T` object as the first argument. This is how it writes the errors out through the normal `go test` capabilities.
|
||||
* Every assert func returns a bool indicating whether the assertion was successful or not, this is useful for if you want to go on making further assertions under certain conditions.
|
||||
|
||||
`http` package
|
||||
--------------
|
||||
|
||||
The `http` package contains test objects useful for testing code that relies on the `net/http` package. Check out the [API documentation for the `http` package](http://godoc.org/github.com/stretchr/testify/http).
|
||||
|
||||
`mock` package
|
||||
--------------
|
||||
|
||||
The `mock` package provides a mechanism for easily writing mock objects that can be used in place of real objects when writing test code.
|
||||
|
||||
An example test function that tests a piece of code that relies on an external object `testObj`, can setup expectations (testify) and assert that they indeed happened:
|
||||
|
||||
package yours
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
/*
|
||||
Test objects
|
||||
*/
|
||||
|
||||
// MyMockedObject is a mocked object that implements an interface
|
||||
// that describes an object that the code I am testing relies on.
|
||||
type MyMockedObject struct{
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// DoSomething is a method on MyMockedObject that implements some interface
|
||||
// and just records the activity, and returns what the Mock object tells it to.
|
||||
//
|
||||
// In the real object, this method would do something useful, but since this
|
||||
// is a mocked object - we're just going to stub it out.
|
||||
//
|
||||
// NOTE: This method is not being tested here, code that uses this object is.
|
||||
func (m *MyMockedObject) DoSomething(number int) (bool, error) {
|
||||
|
||||
args := m.Mock.Called(number)
|
||||
return args.Bool(0), args.Error(1)
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
Actual test functions
|
||||
*/
|
||||
|
||||
// TestSomething is an example of how to use our test object to
|
||||
// make assertions about some target code we are testing.
|
||||
func TestSomething(t *testing.T) {
|
||||
|
||||
// create an instance of our test object
|
||||
testObj := new(MyMockedObject)
|
||||
|
||||
// setup expectations
|
||||
testObj.On("DoSomething", 123).Return(true, nil)
|
||||
|
||||
// call the code we are testing
|
||||
targetFuncThatDoesSomethingWithObj(testObj)
|
||||
|
||||
// assert that the expectations were met
|
||||
testObj.Mock.AssertExpectations(t)
|
||||
|
||||
}
|
||||
|
||||
For more information on how to write mock code, check out the [API documentation for the `mock` package](http://godoc.org/github.com/stretchr/testify/mock).
|
||||
|
||||
`suite` package
|
||||
---------------
|
||||
|
||||
The `suite` package provides functionality that you might be used to from more common object oriented languages. With it, you can build a testing suite as a struct, build setup/teardown methods and testing methods on your struct, and run them with 'go test' as per normal.
|
||||
|
||||
An example suite is shown below:
|
||||
|
||||
// Basic imports
|
||||
import (
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
// Define the suite, and absorb the built-in basic suite
|
||||
// functionality from testify - including a T() method which
|
||||
// returns the current testing context
|
||||
type ExampleTestSuite struct {
|
||||
suite.Suite
|
||||
VariableThatShouldStartAtFive int
|
||||
}
|
||||
|
||||
// Make sure that VariableThatShouldStartAtFive is set to five
|
||||
// before each test
|
||||
func (suite *ExampleTestSuite) SetupTest() {
|
||||
suite.VariableThatShouldStartAtFive = 5
|
||||
}
|
||||
|
||||
// All methods that begin with "Test" are run as tests within a
|
||||
// suite.
|
||||
func (suite *ExampleTestSuite) TestExample() {
|
||||
assert.Equal(suite.T(), suite.VariableThatShouldStartAtFive, 5)
|
||||
}
|
||||
|
||||
// In order for 'go test' to run this suite, we need to create
|
||||
// a normal test function and pass our suite to suite.Run
|
||||
func TestExampleTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(ExampleTestSuite))
|
||||
}
|
||||
|
||||
For a more complete example, using all of the functionality provided by the suite package, look at our [example testing suite](https://github.com/stretchr/testify/blob/master/suite/suite_test.go)
|
||||
|
||||
For more information on writing suites, check out the [API documentation for the `suite` package](http://godoc.org/github.com/stretchr/testify/suite).
|
||||
|
||||
------
|
||||
|
||||
Installation
|
||||
============
|
||||
|
||||
To install Testify, use `go get`:
|
||||
|
||||
go get github.com/stretchr/testify
|
||||
|
||||
This will then make the following packages available to you:
|
||||
|
||||
github.com/stretchr/testify/assert
|
||||
github.com/stretchr/testify/mock
|
||||
github.com/stretchr/testify/http
|
||||
|
||||
Import the `testify/assert` package into your code using this template:
|
||||
|
||||
package yours
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSomething(t *testing.T) {
|
||||
|
||||
assert.True(t, true, "True is true!")
|
||||
|
||||
}
|
||||
|
||||
------
|
||||
|
||||
Staying up to date
|
||||
==================
|
||||
|
||||
To update Testify, use `go get -u`:
|
||||
|
||||
go get -u github.com/stretchr/testify
|
||||
|
||||
------
|
||||
|
||||
Contributing
|
||||
============
|
||||
|
||||
Please feel free to submit issues, fork the repository and send pull requests!
|
||||
|
||||
When submitting an issue, we ask that you please include a complete test function that demonstrates the issue. Extra credit for those using Testify to write the test code that demonstrates it.
|
||||
|
||||
------
|
||||
|
||||
Licence
|
||||
=======
|
||||
Copyright (c) 2012 - 2013 Mat Ryer and Tyler Bunnell
|
||||
|
||||
Please consider promoting this project if you find it useful.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
537
third_party/src/github.com/stretchr/testify/assert/assertions.go
vendored
Normal file
537
third_party/src/github.com/stretchr/testify/assert/assertions.go
vendored
Normal file
@ -0,0 +1,537 @@
|
||||
package assert
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestingT is an interface wrapper around *testing.T
|
||||
type TestingT interface {
|
||||
Errorf(format string, args ...interface{})
|
||||
}
|
||||
|
||||
// Comparison a custom function that returns true on success and false on failure
|
||||
type Comparison func() (success bool)
|
||||
|
||||
/*
|
||||
Helper functions
|
||||
*/
|
||||
|
||||
// ObjectsAreEqual determines if two objects are considered equal.
|
||||
//
|
||||
// This function does no assertion of any kind.
|
||||
func ObjectsAreEqual(expected, actual interface{}) bool {
|
||||
|
||||
if expected == nil || actual == nil {
|
||||
return expected == actual
|
||||
}
|
||||
|
||||
if reflect.DeepEqual(expected, actual) {
|
||||
return true
|
||||
}
|
||||
|
||||
expectedValue := reflect.ValueOf(expected)
|
||||
actualValue := reflect.ValueOf(actual)
|
||||
if expectedValue == actualValue {
|
||||
return true
|
||||
}
|
||||
|
||||
// Attempt comparison after type conversion
|
||||
if actualValue.Type().ConvertibleTo(expectedValue.Type()) && expectedValue == actualValue.Convert(expectedValue.Type()) {
|
||||
return true
|
||||
}
|
||||
|
||||
// Last ditch effort
|
||||
if fmt.Sprintf("%#v", expected) == fmt.Sprintf("%#v", actual) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
|
||||
}
|
||||
|
||||
/* CallerInfo is necessary because the assert functions use the testing object
|
||||
internally, causing it to print the file:line of the assert method, rather than where
|
||||
the problem actually occured in calling code.*/
|
||||
|
||||
// CallerInfo returns a string containing the file and line number of the assert call
|
||||
// that failed.
|
||||
func CallerInfo() string {
|
||||
|
||||
file := ""
|
||||
line := 0
|
||||
ok := false
|
||||
|
||||
for i := 0; ; i++ {
|
||||
_, file, line, ok = runtime.Caller(i)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
parts := strings.Split(file, "/")
|
||||
dir := parts[len(parts)-2]
|
||||
file = parts[len(parts)-1]
|
||||
if (dir != "assert" && dir != "mock") || file == "mock_test.go" {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s:%d", file, line)
|
||||
}
|
||||
|
||||
// getWhitespaceString returns a string that is long enough to overwrite the default
|
||||
// output from the go testing framework.
|
||||
func getWhitespaceString() string {
|
||||
|
||||
_, file, line, ok := runtime.Caller(1)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
parts := strings.Split(file, "/")
|
||||
file = parts[len(parts)-1]
|
||||
|
||||
return strings.Repeat(" ", len(fmt.Sprintf("%s:%d: ", file, line)))
|
||||
|
||||
}
|
||||
|
||||
func messageFromMsgAndArgs(msgAndArgs ...interface{}) string {
|
||||
if len(msgAndArgs) == 0 || msgAndArgs == nil {
|
||||
return ""
|
||||
}
|
||||
if len(msgAndArgs) == 1 {
|
||||
return msgAndArgs[0].(string)
|
||||
}
|
||||
if len(msgAndArgs) > 1 {
|
||||
return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Fail reports a failure through
|
||||
func Fail(t TestingT, failureMessage string, msgAndArgs ...interface{}) bool {
|
||||
|
||||
message := messageFromMsgAndArgs(msgAndArgs...)
|
||||
|
||||
if len(message) > 0 {
|
||||
t.Errorf("\r%s\r\tLocation:\t%s\n\r\tError:\t\t%s\n\r\tMessages:\t%s\n\r", getWhitespaceString(), CallerInfo(), failureMessage, message)
|
||||
} else {
|
||||
t.Errorf("\r%s\r\tLocation:\t%s\n\r\tError:\t\t%s\n\r", getWhitespaceString(), CallerInfo(), failureMessage)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Implements asserts that an object is implemented by the specified interface.
|
||||
//
|
||||
// assert.Implements(t, (*MyInterface)(nil), new(MyObject), "MyObject")
|
||||
func Implements(t TestingT, interfaceObject interface{}, object interface{}, msgAndArgs ...interface{}) bool {
|
||||
|
||||
interfaceType := reflect.TypeOf(interfaceObject).Elem()
|
||||
|
||||
if !reflect.TypeOf(object).Implements(interfaceType) {
|
||||
return Fail(t, fmt.Sprintf("Object must implement %v", interfaceType), msgAndArgs...)
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
// IsType asserts that the specified objects are of the same type.
|
||||
func IsType(t TestingT, expectedType interface{}, object interface{}, msgAndArgs ...interface{}) bool {
|
||||
|
||||
if !ObjectsAreEqual(reflect.TypeOf(object), reflect.TypeOf(expectedType)) {
|
||||
return Fail(t, fmt.Sprintf("Object expected to be of type %v, but was %v", reflect.TypeOf(expectedType), reflect.TypeOf(object)), msgAndArgs...)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Equal asserts that two objects are equal.
|
||||
//
|
||||
// assert.Equal(t, 123, 123, "123 and 123 should be equal")
|
||||
//
|
||||
// Returns whether the assertion was successful (true) or not (false).
|
||||
func Equal(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
|
||||
|
||||
if !ObjectsAreEqual(expected, actual) {
|
||||
return Fail(t, fmt.Sprintf("Not equal: %#v != %#v", expected, actual), msgAndArgs...)
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
// Exactly asserts that two objects are equal is value and type.
|
||||
//
|
||||
// assert.Exactly(t, int32(123), int64(123), "123 and 123 should NOT be equal")
|
||||
//
|
||||
// Returns whether the assertion was successful (true) or not (false).
|
||||
func Exactly(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
|
||||
|
||||
aType := reflect.TypeOf(expected)
|
||||
bType := reflect.TypeOf(actual)
|
||||
|
||||
if aType != bType {
|
||||
return Fail(t, "Types expected to match exactly", "%v != %v", aType, bType)
|
||||
}
|
||||
|
||||
return Equal(t, expected, actual, msgAndArgs...)
|
||||
|
||||
}
|
||||
|
||||
// NotNil asserts that the specified object is not nil.
|
||||
//
|
||||
// assert.NotNil(t, err, "err should be something")
|
||||
//
|
||||
// Returns whether the assertion was successful (true) or not (false).
|
||||
func NotNil(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
|
||||
|
||||
success := true
|
||||
|
||||
if object == nil {
|
||||
success = false
|
||||
} else {
|
||||
value := reflect.ValueOf(object)
|
||||
kind := value.Kind()
|
||||
if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() {
|
||||
success = false
|
||||
}
|
||||
}
|
||||
|
||||
if !success {
|
||||
Fail(t, "Expected not to be nil.", msgAndArgs...)
|
||||
}
|
||||
|
||||
return success
|
||||
}
|
||||
|
||||
// isNil checks if a specified object is nil or not, without Failing.
|
||||
func isNil(object interface{}) bool {
|
||||
if object == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
value := reflect.ValueOf(object)
|
||||
kind := value.Kind()
|
||||
if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Nil asserts that the specified object is nil.
|
||||
//
|
||||
// assert.Nil(t, err, "err should be nothing")
|
||||
//
|
||||
// Returns whether the assertion was successful (true) or not (false).
|
||||
func Nil(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
|
||||
if isNil(object) {
|
||||
return true
|
||||
}
|
||||
return Fail(t, fmt.Sprintf("Expected nil, but got: %#v", object), msgAndArgs...)
|
||||
}
|
||||
|
||||
var zeros = []interface{}{
|
||||
int(0),
|
||||
int8(0),
|
||||
int16(0),
|
||||
int32(0),
|
||||
int64(0),
|
||||
uint(0),
|
||||
uint8(0),
|
||||
uint16(0),
|
||||
uint32(0),
|
||||
uint64(0),
|
||||
float32(0),
|
||||
float64(0),
|
||||
}
|
||||
|
||||
// isEmpty gets whether the specified object is considered empty or not.
|
||||
func isEmpty(object interface{}) bool {
|
||||
|
||||
if object == nil {
|
||||
return true
|
||||
} else if object == "" {
|
||||
return true
|
||||
} else if object == false {
|
||||
return true
|
||||
}
|
||||
|
||||
for _, v := range zeros {
|
||||
if object == v {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
objValue := reflect.ValueOf(object)
|
||||
|
||||
switch objValue.Kind() {
|
||||
case reflect.Map:
|
||||
fallthrough
|
||||
case reflect.Slice, reflect.Chan:
|
||||
{
|
||||
return (objValue.Len() == 0)
|
||||
}
|
||||
case reflect.Ptr:
|
||||
{
|
||||
switch object.(type) {
|
||||
case *time.Time:
|
||||
return object.(*time.Time).IsZero()
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Empty asserts that the specified object is empty. I.e. nil, "", false, 0 or either
|
||||
// a slice or a channel with len == 0.
|
||||
//
|
||||
// assert.Empty(t, obj)
|
||||
//
|
||||
// Returns whether the assertion was successful (true) or not (false).
|
||||
func Empty(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
|
||||
|
||||
pass := isEmpty(object)
|
||||
if !pass {
|
||||
Fail(t, fmt.Sprintf("Should be empty, but was %v", object), msgAndArgs...)
|
||||
}
|
||||
|
||||
return pass
|
||||
|
||||
}
|
||||
|
||||
// NotEmpty asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either
|
||||
// a slice or a channel with len == 0.
|
||||
//
|
||||
// if assert.NotEmpty(t, obj) {
|
||||
// assert.Equal(t, "two", obj[1])
|
||||
// }
|
||||
//
|
||||
// Returns whether the assertion was successful (true) or not (false).
|
||||
func NotEmpty(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
|
||||
|
||||
pass := !isEmpty(object)
|
||||
if !pass {
|
||||
Fail(t, fmt.Sprintf("Should NOT be empty, but was %v", object), msgAndArgs...)
|
||||
}
|
||||
|
||||
return pass
|
||||
|
||||
}
|
||||
|
||||
// True asserts that the specified value is true.
|
||||
//
|
||||
// assert.True(t, myBool, "myBool should be true")
|
||||
//
|
||||
// Returns whether the assertion was successful (true) or not (false).
|
||||
func True(t TestingT, value bool, msgAndArgs ...interface{}) bool {
|
||||
|
||||
if value != true {
|
||||
return Fail(t, "Should be true", msgAndArgs...)
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
// False asserts that the specified value is true.
|
||||
//
|
||||
// assert.False(t, myBool, "myBool should be false")
|
||||
//
|
||||
// Returns whether the assertion was successful (true) or not (false).
|
||||
func False(t TestingT, value bool, msgAndArgs ...interface{}) bool {
|
||||
|
||||
if value != false {
|
||||
return Fail(t, "Should be false", msgAndArgs...)
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
// NotEqual asserts that the specified values are NOT equal.
|
||||
//
|
||||
// assert.NotEqual(t, obj1, obj2, "two objects shouldn't be equal")
|
||||
//
|
||||
// Returns whether the assertion was successful (true) or not (false).
|
||||
func NotEqual(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
|
||||
|
||||
if ObjectsAreEqual(expected, actual) {
|
||||
return Fail(t, "Should not be equal", msgAndArgs...)
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
// Contains asserts that the specified string contains the specified substring.
|
||||
//
|
||||
// assert.Contains(t, "Hello World", "World", "But 'Hello World' does contain 'World'")
|
||||
//
|
||||
// Returns whether the assertion was successful (true) or not (false).
|
||||
func Contains(t TestingT, s, contains string, msgAndArgs ...interface{}) bool {
|
||||
|
||||
if !strings.Contains(s, contains) {
|
||||
return Fail(t, fmt.Sprintf("\"%s\" does not contain \"%s\"", s, contains), msgAndArgs...)
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
// NotContains asserts that the specified string does NOT contain the specified substring.
|
||||
//
|
||||
// assert.NotContains(t, "Hello World", "Earth", "But 'Hello World' does NOT contain 'Earth'")
|
||||
//
|
||||
// Returns whether the assertion was successful (true) or not (false).
|
||||
func NotContains(t TestingT, s, contains string, msgAndArgs ...interface{}) bool {
|
||||
|
||||
if strings.Contains(s, contains) {
|
||||
return Fail(t, fmt.Sprintf("\"%s\" should not contain \"%s\"", s, contains), msgAndArgs...)
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
// Condition uses a Comparison to assert a complex condition.
|
||||
func Condition(t TestingT, comp Comparison, msgAndArgs ...interface{}) bool {
|
||||
result := comp()
|
||||
if !result {
|
||||
Fail(t, "Condition failed!", msgAndArgs...)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// PanicTestFunc defines a func that should be passed to the assert.Panics and assert.NotPanics
|
||||
// methods, and represents a simple func that takes no arguments, and returns nothing.
|
||||
type PanicTestFunc func()
|
||||
|
||||
// didPanic returns true if the function passed to it panics. Otherwise, it returns false.
|
||||
func didPanic(f PanicTestFunc) (bool, interface{}) {
|
||||
|
||||
didPanic := false
|
||||
var message interface{}
|
||||
func() {
|
||||
|
||||
defer func() {
|
||||
if message = recover(); message != nil {
|
||||
didPanic = true
|
||||
}
|
||||
}()
|
||||
|
||||
// call the target function
|
||||
f()
|
||||
|
||||
}()
|
||||
|
||||
return didPanic, message
|
||||
|
||||
}
|
||||
|
||||
// Panics asserts that the code inside the specified PanicTestFunc panics.
|
||||
//
|
||||
// assert.Panics(t, func(){
|
||||
// GoCrazy()
|
||||
// }, "Calling GoCrazy() should panic")
|
||||
//
|
||||
// Returns whether the assertion was successful (true) or not (false).
|
||||
func Panics(t TestingT, f PanicTestFunc, msgAndArgs ...interface{}) bool {
|
||||
|
||||
if funcDidPanic, panicValue := didPanic(f); !funcDidPanic {
|
||||
return Fail(t, fmt.Sprintf("func %#v should panic\n\r\tPanic value:\t%v", f, panicValue), msgAndArgs...)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// NotPanics asserts that the code inside the specified PanicTestFunc does NOT panic.
|
||||
//
|
||||
// assert.NotPanics(t, func(){
|
||||
// RemainCalm()
|
||||
// }, "Calling RemainCalm() should NOT panic")
|
||||
//
|
||||
// Returns whether the assertion was successful (true) or not (false).
|
||||
func NotPanics(t TestingT, f PanicTestFunc, msgAndArgs ...interface{}) bool {
|
||||
|
||||
if funcDidPanic, panicValue := didPanic(f); funcDidPanic {
|
||||
return Fail(t, fmt.Sprintf("func %#v should not panic\n\r\tPanic value:\t%v", f, panicValue), msgAndArgs...)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// WithinDuration asserts that the two times are within duration delta of each other.
|
||||
//
|
||||
// assert.WithinDuration(t, time.Now(), time.Now(), 10*time.Second, "The difference should not be more than 10s")
|
||||
//
|
||||
// Returns whether the assertion was successful (true) or not (false).
|
||||
func WithinDuration(t TestingT, expected, actual time.Time, delta time.Duration, msgAndArgs ...interface{}) bool {
|
||||
|
||||
dt := expected.Sub(actual)
|
||||
if dt < -delta || dt > delta {
|
||||
return Fail(t, fmt.Sprintf("Max difference between %v and %v allowed is %v, but difference was %v", expected, actual, dt, delta), msgAndArgs...)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
/*
|
||||
Errors
|
||||
*/
|
||||
|
||||
// NoError asserts that a function returned no error (i.e. `nil`).
|
||||
//
|
||||
// actualObj, err := SomeFunction()
|
||||
// if assert.NoError(t, err) {
|
||||
// assert.Equal(t, actualObj, expectedObj)
|
||||
// }
|
||||
//
|
||||
// Returns whether the assertion was successful (true) or not (false).
|
||||
func NoError(t TestingT, err error, msgAndArgs ...interface{}) bool {
|
||||
if isNil(err) {
|
||||
return true
|
||||
}
|
||||
|
||||
return Fail(t, fmt.Sprintf("No error is expected but got %v", err), msgAndArgs...)
|
||||
}
|
||||
|
||||
// Error asserts that a function returned an error (i.e. not `nil`).
|
||||
//
|
||||
// actualObj, err := SomeFunction()
|
||||
// if assert.Error(t, err, "An error was expected") {
|
||||
// assert.Equal(t, err, expectedError)
|
||||
// }
|
||||
//
|
||||
// Returns whether the assertion was successful (true) or not (false).
|
||||
func Error(t TestingT, err error, msgAndArgs ...interface{}) bool {
|
||||
|
||||
message := messageFromMsgAndArgs(msgAndArgs...)
|
||||
return NotNil(t, err, "An error is expected but got nil. %s", message)
|
||||
|
||||
}
|
||||
|
||||
// EqualError asserts that a function returned an error (i.e. not `nil`)
|
||||
// and that it is equal to the provided error.
|
||||
//
|
||||
// actualObj, err := SomeFunction()
|
||||
// if assert.Error(t, err, "An error was expected") {
|
||||
// assert.Equal(t, err, expectedError)
|
||||
// }
|
||||
//
|
||||
// Returns whether the assertion was successful (true) or not (false).
|
||||
func EqualError(t TestingT, theError error, errString string, msgAndArgs ...interface{}) bool {
|
||||
|
||||
message := messageFromMsgAndArgs(msgAndArgs...)
|
||||
if !NotNil(t, theError, "An error is expected but got nil. %s", message) {
|
||||
return false
|
||||
}
|
||||
s := "An error with value \"%s\" is expected but got \"%s\". %s"
|
||||
return Equal(t, theError.Error(), errString,
|
||||
s, errString, theError.Error(), message)
|
||||
}
|
420
third_party/src/github.com/stretchr/testify/assert/assertions_test.go
vendored
Normal file
420
third_party/src/github.com/stretchr/testify/assert/assertions_test.go
vendored
Normal file
@ -0,0 +1,420 @@
|
||||
package assert
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// AssertionTesterInterface defines an interface to be used for testing assertion methods
|
||||
type AssertionTesterInterface interface {
|
||||
TestMethod()
|
||||
}
|
||||
|
||||
// AssertionTesterConformingObject is an object that conforms to the AssertionTesterInterface interface
|
||||
type AssertionTesterConformingObject struct {
|
||||
}
|
||||
|
||||
func (a *AssertionTesterConformingObject) TestMethod() {
|
||||
}
|
||||
|
||||
// AssertionTesterNonConformingObject is an object that does not conform to the AssertionTesterInterface interface
|
||||
type AssertionTesterNonConformingObject struct {
|
||||
}
|
||||
|
||||
func TestObjectsAreEqual(t *testing.T) {
|
||||
|
||||
if !ObjectsAreEqual("Hello World", "Hello World") {
|
||||
t.Error("objectsAreEqual should return true")
|
||||
}
|
||||
if !ObjectsAreEqual(123, 123) {
|
||||
t.Error("objectsAreEqual should return true")
|
||||
}
|
||||
if !ObjectsAreEqual(123.5, 123.5) {
|
||||
t.Error("objectsAreEqual should return true")
|
||||
}
|
||||
if !ObjectsAreEqual([]byte("Hello World"), []byte("Hello World")) {
|
||||
t.Error("objectsAreEqual should return true")
|
||||
}
|
||||
if !ObjectsAreEqual(nil, nil) {
|
||||
t.Error("objectsAreEqual should return true")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestImplements(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
|
||||
if !Implements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject)) {
|
||||
t.Error("Implements method should return true: AssertionTesterConformingObject implements AssertionTesterInterface")
|
||||
}
|
||||
if Implements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject)) {
|
||||
t.Error("Implements method should return false: AssertionTesterNonConformingObject does not implements AssertionTesterInterface")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestIsType(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
|
||||
if !IsType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterConformingObject)) {
|
||||
t.Error("IsType should return true: AssertionTesterConformingObject is the same type as AssertionTesterConformingObject")
|
||||
}
|
||||
if IsType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject)) {
|
||||
t.Error("IsType should return false: AssertionTesterConformingObject is not the same type as AssertionTesterNonConformingObject")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestEqual(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
|
||||
if !Equal(mockT, "Hello World", "Hello World") {
|
||||
t.Error("Equal should return true")
|
||||
}
|
||||
if !Equal(mockT, 123, 123) {
|
||||
t.Error("Equal should return true")
|
||||
}
|
||||
if !Equal(mockT, 123.5, 123.5) {
|
||||
t.Error("Equal should return true")
|
||||
}
|
||||
if !Equal(mockT, []byte("Hello World"), []byte("Hello World")) {
|
||||
t.Error("Equal should return true")
|
||||
}
|
||||
if !Equal(mockT, nil, nil) {
|
||||
t.Error("Equal should return true")
|
||||
}
|
||||
if !Equal(mockT, int32(123), int64(123)) {
|
||||
t.Error("Equal should return true")
|
||||
}
|
||||
if !Equal(mockT, int64(123), uint64(123)) {
|
||||
t.Error("Equal should return true")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestNotNil(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
|
||||
if !NotNil(mockT, new(AssertionTesterConformingObject)) {
|
||||
t.Error("NotNil should return true: object is not nil")
|
||||
}
|
||||
if NotNil(mockT, nil) {
|
||||
t.Error("NotNil should return false: object is nil")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestNil(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
|
||||
if !Nil(mockT, nil) {
|
||||
t.Error("Nil should return true: object is nil")
|
||||
}
|
||||
if Nil(mockT, new(AssertionTesterConformingObject)) {
|
||||
t.Error("Nil should return false: object is not nil")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestTrue(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
|
||||
if !True(mockT, true) {
|
||||
t.Error("True should return true")
|
||||
}
|
||||
if True(mockT, false) {
|
||||
t.Error("True should return false")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestFalse(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
|
||||
if !False(mockT, false) {
|
||||
t.Error("False should return true")
|
||||
}
|
||||
if False(mockT, true) {
|
||||
t.Error("False should return false")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestExactly(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
|
||||
a := float32(1)
|
||||
b := float64(1)
|
||||
c := float32(1)
|
||||
d := float32(2)
|
||||
|
||||
if Exactly(mockT, a, b) {
|
||||
t.Error("Exactly should return false")
|
||||
}
|
||||
if Exactly(mockT, a, d) {
|
||||
t.Error("Exactly should return false")
|
||||
}
|
||||
if !Exactly(mockT, a, c) {
|
||||
t.Error("Exactly should return true")
|
||||
}
|
||||
|
||||
if Exactly(mockT, nil, a) {
|
||||
t.Error("Exactly should return false")
|
||||
}
|
||||
if Exactly(mockT, a, nil) {
|
||||
t.Error("Exactly should return false")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestNotEqual(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
|
||||
if !NotEqual(mockT, "Hello World", "Hello World!") {
|
||||
t.Error("NotEqual should return true")
|
||||
}
|
||||
if !NotEqual(mockT, 123, 1234) {
|
||||
t.Error("NotEqual should return true")
|
||||
}
|
||||
if !NotEqual(mockT, 123.5, 123.55) {
|
||||
t.Error("NotEqual should return true")
|
||||
}
|
||||
if !NotEqual(mockT, []byte("Hello World"), []byte("Hello World!")) {
|
||||
t.Error("NotEqual should return true")
|
||||
}
|
||||
if !NotEqual(mockT, nil, new(AssertionTesterConformingObject)) {
|
||||
t.Error("NotEqual should return true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestContains(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
|
||||
if !Contains(mockT, "Hello World", "Hello") {
|
||||
t.Error("Contains should return true: \"Hello World\" contains \"Hello\"")
|
||||
}
|
||||
if Contains(mockT, "Hello World", "Salut") {
|
||||
t.Error("Contains should return false: \"Hello World\" does not contain \"Salut\"")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestNotContains(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
|
||||
if !NotContains(mockT, "Hello World", "Hello!") {
|
||||
t.Error("NotContains should return true: \"Hello World\" does not contain \"Hello!\"")
|
||||
}
|
||||
if NotContains(mockT, "Hello World", "Hello") {
|
||||
t.Error("NotContains should return false: \"Hello World\" contains \"Hello\"")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestDidPanic(t *testing.T) {
|
||||
|
||||
if funcDidPanic, _ := didPanic(func() {
|
||||
panic("Panic!")
|
||||
}); !funcDidPanic {
|
||||
t.Error("didPanic should return true")
|
||||
}
|
||||
|
||||
if funcDidPanic, _ := didPanic(func() {
|
||||
}); funcDidPanic {
|
||||
t.Error("didPanic should return false")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestPanics(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
|
||||
if !Panics(mockT, func() {
|
||||
panic("Panic!")
|
||||
}) {
|
||||
t.Error("Panics should return true")
|
||||
}
|
||||
|
||||
if Panics(mockT, func() {
|
||||
}) {
|
||||
t.Error("Panics should return false")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestNotPanics(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
|
||||
if !NotPanics(mockT, func() {
|
||||
}) {
|
||||
t.Error("NotPanics should return true")
|
||||
}
|
||||
|
||||
if NotPanics(mockT, func() {
|
||||
panic("Panic!")
|
||||
}) {
|
||||
t.Error("NotPanics should return false")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestEqual_Funcs(t *testing.T) {
|
||||
|
||||
type f func() int
|
||||
f1 := func() int { return 1 }
|
||||
f2 := func() int { return 2 }
|
||||
|
||||
f1Copy := f1
|
||||
|
||||
Equal(t, f1Copy, f1, "Funcs are the same and should be considered equal")
|
||||
NotEqual(t, f1, f2, "f1 and f2 are different")
|
||||
|
||||
}
|
||||
|
||||
func TestNoError(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
|
||||
// start with a nil error
|
||||
var err error
|
||||
|
||||
True(t, NoError(mockT, err), "NoError should return True for nil arg")
|
||||
|
||||
// now set an error
|
||||
err = errors.New("some error")
|
||||
|
||||
False(t, NoError(mockT, err), "NoError with error should return False")
|
||||
|
||||
}
|
||||
|
||||
func TestError(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
|
||||
// start with a nil error
|
||||
var err error
|
||||
|
||||
False(t, Error(mockT, err), "Error should return False for nil arg")
|
||||
|
||||
// now set an error
|
||||
err = errors.New("some error")
|
||||
|
||||
True(t, Error(mockT, err), "Error with error should return True")
|
||||
|
||||
}
|
||||
|
||||
func TestEqualError(t *testing.T) {
|
||||
mockT := new(testing.T)
|
||||
|
||||
// start with a nil error
|
||||
var err error
|
||||
False(t, EqualError(mockT, err, ""),
|
||||
"EqualError should return false for nil arg")
|
||||
|
||||
// now set an error
|
||||
err = errors.New("some error")
|
||||
False(t, EqualError(mockT, err, "Not some error"),
|
||||
"EqualError should return false for different error string")
|
||||
True(t, EqualError(mockT, err, "some error"),
|
||||
"EqualError should return true")
|
||||
}
|
||||
|
||||
func Test_isEmpty(t *testing.T) {
|
||||
|
||||
chWithValue := make(chan struct{}, 1)
|
||||
chWithValue <- struct{}{}
|
||||
|
||||
True(t, isEmpty(""))
|
||||
True(t, isEmpty(nil))
|
||||
True(t, isEmpty([]string{}))
|
||||
True(t, isEmpty(0))
|
||||
True(t, isEmpty(int32(0)))
|
||||
True(t, isEmpty(int64(0)))
|
||||
True(t, isEmpty(false))
|
||||
True(t, isEmpty(map[string]string{}))
|
||||
True(t, isEmpty(new(time.Time)))
|
||||
True(t, isEmpty(make(chan struct{})))
|
||||
False(t, isEmpty("something"))
|
||||
False(t, isEmpty(errors.New("something")))
|
||||
False(t, isEmpty([]string{"something"}))
|
||||
False(t, isEmpty(1))
|
||||
False(t, isEmpty(true))
|
||||
False(t, isEmpty(map[string]string{"Hello": "World"}))
|
||||
False(t, isEmpty(chWithValue))
|
||||
|
||||
}
|
||||
|
||||
func TestEmpty(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
chWithValue := make(chan struct{}, 1)
|
||||
chWithValue <- struct{}{}
|
||||
|
||||
True(t, Empty(mockT, ""), "Empty string is empty")
|
||||
True(t, Empty(mockT, nil), "Nil is empty")
|
||||
True(t, Empty(mockT, []string{}), "Empty string array is empty")
|
||||
True(t, Empty(mockT, 0), "Zero int value is empty")
|
||||
True(t, Empty(mockT, false), "False value is empty")
|
||||
True(t, Empty(mockT, make(chan struct{})), "Channel without values is empty")
|
||||
|
||||
False(t, Empty(mockT, "something"), "Non Empty string is not empty")
|
||||
False(t, Empty(mockT, errors.New("something")), "Non nil object is not empty")
|
||||
False(t, Empty(mockT, []string{"something"}), "Non empty string array is not empty")
|
||||
False(t, Empty(mockT, 1), "Non-zero int value is not empty")
|
||||
False(t, Empty(mockT, true), "True value is not empty")
|
||||
False(t, Empty(mockT, chWithValue), "Channel with values is not empty")
|
||||
}
|
||||
|
||||
func TestNotEmpty(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
chWithValue := make(chan struct{}, 1)
|
||||
chWithValue <- struct{}{}
|
||||
|
||||
False(t, NotEmpty(mockT, ""), "Empty string is empty")
|
||||
False(t, NotEmpty(mockT, nil), "Nil is empty")
|
||||
False(t, NotEmpty(mockT, []string{}), "Empty string array is empty")
|
||||
False(t, NotEmpty(mockT, 0), "Zero int value is empty")
|
||||
False(t, NotEmpty(mockT, false), "False value is empty")
|
||||
False(t, NotEmpty(mockT, make(chan struct{})), "Channel without values is empty")
|
||||
|
||||
True(t, NotEmpty(mockT, "something"), "Non Empty string is not empty")
|
||||
True(t, NotEmpty(mockT, errors.New("something")), "Non nil object is not empty")
|
||||
True(t, NotEmpty(mockT, []string{"something"}), "Non empty string array is not empty")
|
||||
True(t, NotEmpty(mockT, 1), "Non-zero int value is not empty")
|
||||
True(t, NotEmpty(mockT, true), "True value is not empty")
|
||||
True(t, NotEmpty(mockT, chWithValue), "Channel with values is not empty")
|
||||
}
|
||||
|
||||
func TestWithinDuration(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
a := time.Now()
|
||||
b := a.Add(10 * time.Second)
|
||||
|
||||
True(t, WithinDuration(mockT, a, b, 10*time.Second), "A 10s difference is within a 10s time difference")
|
||||
True(t, WithinDuration(mockT, b, a, 10*time.Second), "A 10s difference is within a 10s time difference")
|
||||
|
||||
False(t, WithinDuration(mockT, a, b, 9*time.Second), "A 10s difference is not within a 9s time difference")
|
||||
False(t, WithinDuration(mockT, b, a, 9*time.Second), "A 10s difference is not within a 9s time difference")
|
||||
|
||||
False(t, WithinDuration(mockT, a, b, -9*time.Second), "A 10s difference is not within a 9s time difference")
|
||||
False(t, WithinDuration(mockT, b, a, -9*time.Second), "A 10s difference is not within a 9s time difference")
|
||||
|
||||
False(t, WithinDuration(mockT, a, b, -11*time.Second), "A 10s difference is not within a 9s time difference")
|
||||
False(t, WithinDuration(mockT, b, a, -11*time.Second), "A 10s difference is not within a 9s time difference")
|
||||
}
|
74
third_party/src/github.com/stretchr/testify/assert/doc.go
vendored
Normal file
74
third_party/src/github.com/stretchr/testify/assert/doc.go
vendored
Normal file
@ -0,0 +1,74 @@
|
||||
// A set of comprehensive testing tools for use with the normal Go testing system.
|
||||
//
|
||||
// Example Usage
|
||||
//
|
||||
// The following is a complete example using assert in a standard test function:
|
||||
// import (
|
||||
// "testing"
|
||||
// "github.com/stretchr/testify/assert"
|
||||
// )
|
||||
//
|
||||
// func TestSomething(t *testing.T) {
|
||||
//
|
||||
// var a string = "Hello"
|
||||
// var b string = "Hello"
|
||||
//
|
||||
// assert.Equal(t, a, b, "The two words should be the same.")
|
||||
//
|
||||
// }
|
||||
//
|
||||
// Assertions
|
||||
//
|
||||
// Assertions allow you to easily write test code, and are global funcs in the `assert` package.
|
||||
// All assertion functions take, as the first argument, the `*testing.T` object provided by the
|
||||
// testing framework. This allows the assertion funcs to write the failings and other details to
|
||||
// the correct place.
|
||||
//
|
||||
// Every assertion function also takes an optional string message as the final argument,
|
||||
// allowing custom error messages to be appended to the message the assertion method outputs.
|
||||
//
|
||||
// Here is an overview of the assert functions:
|
||||
//
|
||||
// assert.Equal(t, expected, actual [, message [, format-args])
|
||||
//
|
||||
// assert.NotEqual(t, notExpected, actual [, message [, format-args]])
|
||||
//
|
||||
// assert.True(t, actualBool [, message [, format-args]])
|
||||
//
|
||||
// assert.False(t, actualBool [, message [, format-args]])
|
||||
//
|
||||
// assert.Nil(t, actualObject [, message [, format-args]])
|
||||
//
|
||||
// assert.NotNil(t, actualObject [, message [, format-args]])
|
||||
//
|
||||
// assert.Empty(t, actualObject [, message [, format-args]])
|
||||
//
|
||||
// assert.NotEmpty(t, actualObject [, message [, format-args]])
|
||||
//
|
||||
// assert.Error(t, errorObject [, message [, format-args]])
|
||||
//
|
||||
// assert.NoError(t, errorObject [, message [, format-args]])
|
||||
//
|
||||
// assert.Implements(t, (*MyInterface)(nil), new(MyObject) [,message [, format-args]])
|
||||
//
|
||||
// assert.IsType(t, expectedObject, actualObject [, message [, format-args]])
|
||||
//
|
||||
// assert.Contains(t, string, substring [, message [, format-args]])
|
||||
//
|
||||
// assert.NotContains(t, string, substring [, message [, format-args]])
|
||||
//
|
||||
// assert.Panics(t, func(){
|
||||
//
|
||||
// // call code that should panic
|
||||
//
|
||||
// } [, message [, format-args]])
|
||||
//
|
||||
// assert.NotPanics(t, func(){
|
||||
//
|
||||
// // call code that should not panic
|
||||
//
|
||||
// } [, message [, format-args]])
|
||||
//
|
||||
// assert.WithinDuration(t, timeA, timeB, deltaTime, [, message [, format-args]])
|
||||
|
||||
package assert
|
10
third_party/src/github.com/stretchr/testify/assert/errors.go
vendored
Normal file
10
third_party/src/github.com/stretchr/testify/assert/errors.go
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
package assert
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// AnError is an error instance useful for testing. If the code does not care
|
||||
// about error specifics, and only needs to return the error for example, this
|
||||
// error should be used to make the test code more readable.
|
||||
var AnError = errors.New("assert.AnError general error for testing")
|
18
third_party/src/github.com/stretchr/testify/doc.go
vendored
Normal file
18
third_party/src/github.com/stretchr/testify/doc.go
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
// A set of packages that provide many tools for testifying that your code will behave as you intend.
|
||||
//
|
||||
// testify contains the following packages:
|
||||
//
|
||||
// The assert package provides a comprehensive set of assertion functions that tie in to the Go testing system.
|
||||
//
|
||||
// The http package contains tools to make it easier to test http activity using the Go testing system.
|
||||
//
|
||||
// The mock package provides a system by which it is possible to mock your objects and verify calls are happening as expected.
|
||||
//
|
||||
// The suite package provides a basic structure for using structs as testing suites, and methods on those structs as tests. It includes setup/teardown functionality in the way of interfaces.
|
||||
package testify
|
||||
|
||||
import (
|
||||
_ "github.com/stretchr/testify/assert"
|
||||
_ "github.com/stretchr/testify/http"
|
||||
_ "github.com/stretchr/testify/mock"
|
||||
)
|
2
third_party/src/github.com/stretchr/testify/http/doc.go
vendored
Normal file
2
third_party/src/github.com/stretchr/testify/http/doc.go
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
// A set of tools to make testing http activity using the Go testing system easier.
|
||||
package http
|
52
third_party/src/github.com/stretchr/testify/http/test_response_writer.go
vendored
Normal file
52
third_party/src/github.com/stretchr/testify/http/test_response_writer.go
vendored
Normal file
@ -0,0 +1,52 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// TestResponseWriter is a http.ResponseWriter object that keeps track of all activity
|
||||
// allowing you to make assertions about how it was used.
|
||||
//
|
||||
// DEPRECATED: We recommend you use http://golang.org/pkg/net/http/httptest instead.
|
||||
type TestResponseWriter struct {
|
||||
|
||||
// StatusCode is the last int written by the call to WriteHeader(int)
|
||||
StatusCode int
|
||||
|
||||
// Output is a string containing the written bytes using the Write([]byte) func.
|
||||
Output string
|
||||
|
||||
// header is the internal storage of the http.Header object
|
||||
header http.Header
|
||||
}
|
||||
|
||||
// Header gets the http.Header describing the headers that were set in this response.
|
||||
func (rw *TestResponseWriter) Header() http.Header {
|
||||
|
||||
if rw.header == nil {
|
||||
rw.header = make(http.Header)
|
||||
}
|
||||
|
||||
return rw.header
|
||||
}
|
||||
|
||||
// Write writes the specified bytes to Output.
|
||||
func (rw *TestResponseWriter) Write(bytes []byte) (int, error) {
|
||||
|
||||
// assume 200 success if no header has been set
|
||||
if rw.StatusCode == 0 {
|
||||
rw.WriteHeader(200)
|
||||
}
|
||||
|
||||
// add these bytes to the output string
|
||||
rw.Output = rw.Output + string(bytes)
|
||||
|
||||
// return normal values
|
||||
return 0, nil
|
||||
|
||||
}
|
||||
|
||||
// WriteHeader stores the HTTP status code in the StatusCode.
|
||||
func (rw *TestResponseWriter) WriteHeader(i int) {
|
||||
rw.StatusCode = i
|
||||
}
|
15
third_party/src/github.com/stretchr/testify/http/test_round_tripper.go
vendored
Normal file
15
third_party/src/github.com/stretchr/testify/http/test_round_tripper.go
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/mock"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type TestRoundTripper struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (t *TestRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
args := t.Called(req)
|
||||
return args.Get(0).(*http.Response), args.Error(1)
|
||||
}
|
43
third_party/src/github.com/stretchr/testify/mock/doc.go
vendored
Normal file
43
third_party/src/github.com/stretchr/testify/mock/doc.go
vendored
Normal file
@ -0,0 +1,43 @@
|
||||
// Provides a system by which it is possible to mock your objects and verify calls are happening as expected.
|
||||
//
|
||||
// Example Usage
|
||||
//
|
||||
// The mock package provides an object, Mock, that tracks activity on another object. It is usually
|
||||
// embedded into a test object as shown below:
|
||||
//
|
||||
// type MyTestObject struct {
|
||||
// // add a Mock object instance
|
||||
// mock.Mock
|
||||
//
|
||||
// // other fields go here as normal
|
||||
// }
|
||||
//
|
||||
// When implementing the methods of an interface, you wire your functions up
|
||||
// to call the Mock.Called(args...) method, and return the appropriate values.
|
||||
//
|
||||
// For example, to mock a method that saves the name and age of a person and returns
|
||||
// the year of their birth or an error, you might write this:
|
||||
//
|
||||
// func (o *MyTestObject) SavePersonDetails(firstname, lastname string, age int) (int, error) {
|
||||
// args := o.Mock.Called(firstname, lastname, age)
|
||||
// return args.Int(0), args.Error(1)
|
||||
// }
|
||||
//
|
||||
// The Int, Error and Bool methods are examples of strongly typed getters that take the argument
|
||||
// index position. Given this argument list:
|
||||
//
|
||||
// (12, true, "Something")
|
||||
//
|
||||
// You could read them out strongly typed like this:
|
||||
//
|
||||
// args.Int(0)
|
||||
// args.Bool(1)
|
||||
// args.String(2)
|
||||
//
|
||||
// For objects of your own type, use the generic Arguments.Get(index) method and make a type assertion:
|
||||
//
|
||||
// return args.Get(0).(*MyObject), args.Get(1).(*AnotherObjectOfMine)
|
||||
//
|
||||
// This may cause a panic if the object you are getting is nil (the type assertion will fail), in those
|
||||
// cases you should check for nil first.
|
||||
package mock
|
505
third_party/src/github.com/stretchr/testify/mock/mock.go
vendored
Normal file
505
third_party/src/github.com/stretchr/testify/mock/mock.go
vendored
Normal file
@ -0,0 +1,505 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/stretchr/objx"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// TestingT is an interface wrapper around *testing.T
|
||||
type TestingT interface {
|
||||
Logf(format string, args ...interface{})
|
||||
Errorf(format string, args ...interface{})
|
||||
}
|
||||
|
||||
/*
|
||||
Call
|
||||
*/
|
||||
|
||||
// Call represents a method call and is used for setting expectations,
|
||||
// as well as recording activity.
|
||||
type Call struct {
|
||||
|
||||
// The name of the method that was or will be called.
|
||||
Method string
|
||||
|
||||
// Holds the arguments of the method.
|
||||
Arguments Arguments
|
||||
|
||||
// Holds the arguments that should be returned when
|
||||
// this method is called.
|
||||
ReturnArguments Arguments
|
||||
|
||||
// The number of times to return the return arguments when setting
|
||||
// expectations. 0 means to always return the value.
|
||||
Repeatability int
|
||||
}
|
||||
|
||||
// Mock is the workhorse used to track activity on another object.
|
||||
// For an example of its usage, refer to the "Example Usage" section at the top of this document.
|
||||
type Mock struct {
|
||||
|
||||
// The method name that is currently
|
||||
// being referred to by the On method.
|
||||
onMethodName string
|
||||
|
||||
// An array of the arguments that are
|
||||
// currently being referred to by the On method.
|
||||
onMethodArguments Arguments
|
||||
|
||||
// Represents the calls that are expected of
|
||||
// an object.
|
||||
ExpectedCalls []Call
|
||||
|
||||
// Holds the calls that were made to this mocked object.
|
||||
Calls []Call
|
||||
|
||||
// TestData holds any data that might be useful for testing. Testify ignores
|
||||
// this data completely allowing you to do whatever you like with it.
|
||||
testData objx.Map
|
||||
}
|
||||
|
||||
// TestData holds any data that might be useful for testing. Testify ignores
|
||||
// this data completely allowing you to do whatever you like with it.
|
||||
func (m *Mock) TestData() objx.Map {
|
||||
|
||||
if m.testData == nil {
|
||||
m.testData = make(objx.Map)
|
||||
}
|
||||
|
||||
return m.testData
|
||||
}
|
||||
|
||||
/*
|
||||
Setting expectations
|
||||
*/
|
||||
|
||||
// On starts a description of an expectation of the specified method
|
||||
// being called.
|
||||
//
|
||||
// Mock.On("MyMethod", arg1, arg2)
|
||||
func (m *Mock) On(methodName string, arguments ...interface{}) *Mock {
|
||||
m.onMethodName = methodName
|
||||
m.onMethodArguments = arguments
|
||||
return m
|
||||
}
|
||||
|
||||
// Return finishes a description of an expectation of the method (and arguments)
|
||||
// specified in the most recent On method call.
|
||||
//
|
||||
// Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2)
|
||||
func (m *Mock) Return(returnArguments ...interface{}) *Mock {
|
||||
m.ExpectedCalls = append(m.ExpectedCalls, Call{m.onMethodName, m.onMethodArguments, returnArguments, 0})
|
||||
return m
|
||||
}
|
||||
|
||||
// Once indicates that that the mock should only return the value once.
|
||||
//
|
||||
// Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Once()
|
||||
func (m *Mock) Once() {
|
||||
m.ExpectedCalls[len(m.ExpectedCalls)-1].Repeatability = 1
|
||||
}
|
||||
|
||||
// Twice indicates that that the mock should only return the value twice.
|
||||
//
|
||||
// Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Twice()
|
||||
func (m *Mock) Twice() {
|
||||
m.ExpectedCalls[len(m.ExpectedCalls)-1].Repeatability = 2
|
||||
}
|
||||
|
||||
// Times indicates that that the mock should only return the indicated number
|
||||
// of times.
|
||||
//
|
||||
// Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Times(5)
|
||||
func (m *Mock) Times(i int) {
|
||||
m.ExpectedCalls[len(m.ExpectedCalls)-1].Repeatability = i
|
||||
}
|
||||
|
||||
/*
|
||||
Recording and responding to activity
|
||||
*/
|
||||
|
||||
func (m *Mock) findExpectedCall(method string, arguments ...interface{}) (int, *Call) {
|
||||
for i, call := range m.ExpectedCalls {
|
||||
if call.Method == method && call.Repeatability > -1 {
|
||||
|
||||
_, diffCount := call.Arguments.Diff(arguments)
|
||||
if diffCount == 0 {
|
||||
return i, &call
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
func (m *Mock) findClosestCall(method string, arguments ...interface{}) (bool, *Call) {
|
||||
|
||||
diffCount := 0
|
||||
var closestCall *Call = nil
|
||||
|
||||
for _, call := range m.ExpectedCalls {
|
||||
if call.Method == method {
|
||||
|
||||
_, tempDiffCount := call.Arguments.Diff(arguments)
|
||||
if tempDiffCount < diffCount || diffCount == 0 {
|
||||
diffCount = tempDiffCount
|
||||
closestCall = &call
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if closestCall == nil {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return true, closestCall
|
||||
}
|
||||
|
||||
func callString(method string, arguments Arguments, includeArgumentValues bool) string {
|
||||
|
||||
var argValsString string = ""
|
||||
if includeArgumentValues {
|
||||
var argVals []string
|
||||
for argIndex, arg := range arguments {
|
||||
argVals = append(argVals, fmt.Sprintf("%d: %v", argIndex, arg))
|
||||
}
|
||||
argValsString = fmt.Sprintf("\n\t\t%s", strings.Join(argVals, "\n\t\t"))
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s(%s)%s", method, arguments.String(), argValsString)
|
||||
}
|
||||
|
||||
// Called tells the mock object that a method has been called, and gets an array
|
||||
// of arguments to return. Panics if the call is unexpected (i.e. not preceeded by
|
||||
// appropriate .On .Return() calls)
|
||||
func (m *Mock) Called(arguments ...interface{}) Arguments {
|
||||
|
||||
// get the calling function's name
|
||||
pc, _, _, ok := runtime.Caller(1)
|
||||
if !ok {
|
||||
panic("Couldn't get the caller information")
|
||||
}
|
||||
functionPath := runtime.FuncForPC(pc).Name()
|
||||
parts := strings.Split(functionPath, ".")
|
||||
functionName := parts[len(parts)-1]
|
||||
|
||||
found, call := m.findExpectedCall(functionName, arguments...)
|
||||
|
||||
switch {
|
||||
case found < 0:
|
||||
// we have to fail here - because we don't know what to do
|
||||
// as the return arguments. This is because:
|
||||
//
|
||||
// a) this is a totally unexpected call to this method,
|
||||
// b) the arguments are not what was expected, or
|
||||
// c) the developer has forgotten to add an accompanying On...Return pair.
|
||||
|
||||
closestFound, closestCall := m.findClosestCall(functionName, arguments...)
|
||||
|
||||
if closestFound {
|
||||
panic(fmt.Sprintf("\n\nmock: Unexpected Method Call\n-----------------------------\n\n%s\n\nThe closest call I have is: \n\n%s\n", callString(functionName, arguments, true), callString(functionName, closestCall.Arguments, true)))
|
||||
} else {
|
||||
panic(fmt.Sprintf("\nassert: mock: I don't know what to return because the method call was unexpected.\n\tEither do Mock.On(\"%s\").Return(...) first, or remove the %s() call.\n\tThis method was unexpected:\n\t\t%s\n\tat: %s", functionName, functionName, callString(functionName, arguments, true), assert.CallerInfo()))
|
||||
}
|
||||
case call.Repeatability == 1:
|
||||
call.Repeatability = -1
|
||||
m.ExpectedCalls[found] = *call
|
||||
case call.Repeatability > 1:
|
||||
call.Repeatability -= 1
|
||||
m.ExpectedCalls[found] = *call
|
||||
}
|
||||
|
||||
// add the call
|
||||
m.Calls = append(m.Calls, Call{functionName, arguments, make([]interface{}, 0), 0})
|
||||
|
||||
return call.ReturnArguments
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
Assertions
|
||||
*/
|
||||
|
||||
// AssertExpectationsForObjects asserts that everything specified with On and Return
|
||||
// of the specified objects was in fact called as expected.
|
||||
//
|
||||
// Calls may have occurred in any order.
|
||||
func AssertExpectationsForObjects(t TestingT, testObjects ...interface{}) bool {
|
||||
var success bool = true
|
||||
for _, obj := range testObjects {
|
||||
mockObj := obj.(Mock)
|
||||
success = success && mockObj.AssertExpectations(t)
|
||||
}
|
||||
return success
|
||||
}
|
||||
|
||||
// AssertExpectations asserts that everything specified with On and Return was
|
||||
// in fact called as expected. Calls may have occurred in any order.
|
||||
func (m *Mock) AssertExpectations(t TestingT) bool {
|
||||
|
||||
var somethingMissing bool = false
|
||||
var failedExpectations int = 0
|
||||
|
||||
// iterate through each expectation
|
||||
for _, expectedCall := range m.ExpectedCalls {
|
||||
switch {
|
||||
case !m.methodWasCalled(expectedCall.Method, expectedCall.Arguments):
|
||||
somethingMissing = true
|
||||
failedExpectations++
|
||||
t.Logf("\u274C\t%s(%s)", expectedCall.Method, expectedCall.Arguments.String())
|
||||
case expectedCall.Repeatability > 0:
|
||||
somethingMissing = true
|
||||
failedExpectations++
|
||||
default:
|
||||
t.Logf("\u2705\t%s(%s)", expectedCall.Method, expectedCall.Arguments.String())
|
||||
}
|
||||
}
|
||||
|
||||
if somethingMissing {
|
||||
t.Errorf("FAIL: %d out of %d expectation(s) were met.\n\tThe code you are testing needs to make %d more call(s).\n\tat: %s", len(m.ExpectedCalls)-failedExpectations, len(m.ExpectedCalls), failedExpectations, assert.CallerInfo())
|
||||
}
|
||||
|
||||
return !somethingMissing
|
||||
}
|
||||
|
||||
// AssertNumberOfCalls asserts that the method was called expectedCalls times.
|
||||
func (m *Mock) AssertNumberOfCalls(t TestingT, methodName string, expectedCalls int) bool {
|
||||
var actualCalls int = 0
|
||||
for _, call := range m.Calls {
|
||||
if call.Method == methodName {
|
||||
actualCalls++
|
||||
}
|
||||
}
|
||||
return assert.Equal(t, actualCalls, expectedCalls, fmt.Sprintf("Expected number of calls (%d) does not match the actual number of calls (%d).", expectedCalls, actualCalls))
|
||||
}
|
||||
|
||||
// AssertCalled asserts that the method was called.
|
||||
func (m *Mock) AssertCalled(t TestingT, methodName string, arguments ...interface{}) bool {
|
||||
if !assert.True(t, m.methodWasCalled(methodName, arguments), fmt.Sprintf("The \"%s\" method should have been called with %d argument(s), but was not.", methodName, len(arguments))) {
|
||||
t.Logf("%s", m.ExpectedCalls)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// AssertNotCalled asserts that the method was not called.
|
||||
func (m *Mock) AssertNotCalled(t TestingT, methodName string, arguments ...interface{}) bool {
|
||||
if !assert.False(t, m.methodWasCalled(methodName, arguments), fmt.Sprintf("The \"%s\" method was called with %d argument(s), but should NOT have been.", methodName, len(arguments))) {
|
||||
t.Logf("%s", m.ExpectedCalls)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (m *Mock) methodWasCalled(methodName string, expected []interface{}) bool {
|
||||
for _, call := range m.Calls {
|
||||
if call.Method == methodName {
|
||||
|
||||
_, differences := Arguments(expected).Diff(call.Arguments)
|
||||
|
||||
if differences == 0 {
|
||||
// found the expected call
|
||||
return true
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
// we didn't find the expected call
|
||||
return false
|
||||
}
|
||||
|
||||
/*
|
||||
Arguments
|
||||
*/
|
||||
|
||||
// Arguments holds an array of method arguments or return values.
|
||||
type Arguments []interface{}
|
||||
|
||||
const (
|
||||
// The "any" argument. Used in Diff and Assert when
|
||||
// the argument being tested shouldn't be taken into consideration.
|
||||
Anything string = "mock.Anything"
|
||||
)
|
||||
|
||||
// AnythingOfTypeArgument is a string that contains the type of an argument
|
||||
// for use when type checking. Used in Diff and Assert.
|
||||
type AnythingOfTypeArgument string
|
||||
|
||||
// AnythingOfType returns an AnythingOfTypeArgument object containing the
|
||||
// name of the type to check for. Used in Diff and Assert.
|
||||
//
|
||||
// For example:
|
||||
// Assert(t, AnythingOfType("string"), AnythingOfType("int"))
|
||||
func AnythingOfType(t string) AnythingOfTypeArgument {
|
||||
return AnythingOfTypeArgument(t)
|
||||
}
|
||||
|
||||
// Get Returns the argument at the specified index.
|
||||
func (args Arguments) Get(index int) interface{} {
|
||||
if index+1 > len(args) {
|
||||
panic(fmt.Sprintf("assert: arguments: Cannot call Get(%d) because there are %d argument(s).", index, len(args)))
|
||||
}
|
||||
return args[index]
|
||||
}
|
||||
|
||||
// Is gets whether the objects match the arguments specified.
|
||||
func (args Arguments) Is(objects ...interface{}) bool {
|
||||
for i, obj := range args {
|
||||
if obj != objects[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Diff gets a string describing the differences between the arguments
|
||||
// and the specified objects.
|
||||
//
|
||||
// Returns the diff string and number of differences found.
|
||||
func (args Arguments) Diff(objects []interface{}) (string, int) {
|
||||
|
||||
var output string = "\n"
|
||||
var differences int
|
||||
|
||||
var maxArgCount int = len(args)
|
||||
if len(objects) > maxArgCount {
|
||||
maxArgCount = len(objects)
|
||||
}
|
||||
|
||||
for i := 0; i < maxArgCount; i++ {
|
||||
var actual, expected interface{}
|
||||
|
||||
if len(objects) <= i {
|
||||
actual = "(Missing)"
|
||||
} else {
|
||||
actual = objects[i]
|
||||
}
|
||||
|
||||
if len(args) <= i {
|
||||
expected = "(Missing)"
|
||||
} else {
|
||||
expected = args[i]
|
||||
}
|
||||
|
||||
if reflect.TypeOf(expected) == reflect.TypeOf((*AnythingOfTypeArgument)(nil)).Elem() {
|
||||
|
||||
// type checking
|
||||
if reflect.TypeOf(actual).Name() != string(expected.(AnythingOfTypeArgument)) && reflect.TypeOf(actual).String() != string(expected.(AnythingOfTypeArgument)) {
|
||||
// not match
|
||||
differences++
|
||||
output = fmt.Sprintf("%s\t%d: \u274C type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actual)
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
// normal checking
|
||||
|
||||
if assert.ObjectsAreEqual(expected, Anything) || assert.ObjectsAreEqual(actual, Anything) || assert.ObjectsAreEqual(actual, expected) {
|
||||
// match
|
||||
output = fmt.Sprintf("%s\t%d: \u2705 %s == %s\n", output, i, actual, expected)
|
||||
} else {
|
||||
// not match
|
||||
differences++
|
||||
output = fmt.Sprintf("%s\t%d: \u274C %s != %s\n", output, i, actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if differences == 0 {
|
||||
return "No differences.", differences
|
||||
}
|
||||
|
||||
return output, differences
|
||||
|
||||
}
|
||||
|
||||
// Assert compares the arguments with the specified objects and fails if
|
||||
// they do not exactly match.
|
||||
func (args Arguments) Assert(t TestingT, objects ...interface{}) bool {
|
||||
|
||||
// get the differences
|
||||
diff, diffCount := args.Diff(objects)
|
||||
|
||||
if diffCount == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
// there are differences... report them...
|
||||
t.Logf(diff)
|
||||
t.Errorf("%sArguments do not match.", assert.CallerInfo())
|
||||
|
||||
return false
|
||||
|
||||
}
|
||||
|
||||
// String gets the argument at the specified index. Panics if there is no argument, or
|
||||
// if the argument is of the wrong type.
|
||||
//
|
||||
// If no index is provided, String() returns a complete string representation
|
||||
// of the arguments.
|
||||
func (args Arguments) String(indexOrNil ...int) string {
|
||||
|
||||
if len(indexOrNil) == 0 {
|
||||
// normal String() method - return a string representation of the args
|
||||
var argsStr []string
|
||||
for _, arg := range args {
|
||||
argsStr = append(argsStr, fmt.Sprintf("%s", reflect.TypeOf(arg)))
|
||||
}
|
||||
return strings.Join(argsStr, ",")
|
||||
} else if len(indexOrNil) == 1 {
|
||||
// Index has been specified - get the argument at that index
|
||||
var index int = indexOrNil[0]
|
||||
var s string
|
||||
var ok bool
|
||||
if s, ok = args.Get(index).(string); !ok {
|
||||
panic(fmt.Sprintf("assert: arguments: String(%d) failed because object wasn't correct type: %s", index, args.Get(index)))
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("assert: arguments: Wrong number of arguments passed to String. Must be 0 or 1, not %d", len(indexOrNil)))
|
||||
|
||||
}
|
||||
|
||||
// Int gets the argument at the specified index. Panics if there is no argument, or
|
||||
// if the argument is of the wrong type.
|
||||
func (args Arguments) Int(index int) int {
|
||||
var s int
|
||||
var ok bool
|
||||
if s, ok = args.Get(index).(int); !ok {
|
||||
panic(fmt.Sprintf("assert: arguments: Int(%d) failed because object wasn't correct type: %s", index, args.Get(index)))
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Error gets the argument at the specified index. Panics if there is no argument, or
|
||||
// if the argument is of the wrong type.
|
||||
func (args Arguments) Error(index int) error {
|
||||
obj := args.Get(index)
|
||||
var s error
|
||||
var ok bool
|
||||
if obj == nil {
|
||||
return nil
|
||||
}
|
||||
if s, ok = obj.(error); !ok {
|
||||
panic(fmt.Sprintf("assert: arguments: Error(%d) failed because object wasn't correct type: %s", index, args.Get(index)))
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Bool gets the argument at the specified index. Panics if there is no argument, or
|
||||
// if the argument is of the wrong type.
|
||||
func (args Arguments) Bool(index int) bool {
|
||||
var s bool
|
||||
var ok bool
|
||||
if s, ok = args.Get(index).(bool); !ok {
|
||||
panic(fmt.Sprintf("assert: arguments: Bool(%d) failed because object wasn't correct type: %s", index, args.Get(index)))
|
||||
}
|
||||
return s
|
||||
}
|
669
third_party/src/github.com/stretchr/testify/mock/mock_test.go
vendored
Normal file
669
third_party/src/github.com/stretchr/testify/mock/mock_test.go
vendored
Normal file
@ -0,0 +1,669 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
/*
|
||||
Test objects
|
||||
*/
|
||||
|
||||
// ExampleInterface represents an example interface.
|
||||
type ExampleInterface interface {
|
||||
TheExampleMethod(a, b, c int) (int, error)
|
||||
}
|
||||
|
||||
// TestExampleImplementation is a test implementation of ExampleInterface
|
||||
type TestExampleImplementation struct {
|
||||
Mock
|
||||
}
|
||||
|
||||
func (i *TestExampleImplementation) TheExampleMethod(a, b, c int) (int, error) {
|
||||
args := i.Mock.Called(a, b, c)
|
||||
return args.Int(0), errors.New("Whoops")
|
||||
}
|
||||
|
||||
func (i *TestExampleImplementation) TheExampleMethod2(yesorno bool) {
|
||||
i.Mock.Called(yesorno)
|
||||
}
|
||||
|
||||
type ExampleType struct{}
|
||||
|
||||
func (i *TestExampleImplementation) TheExampleMethod3(et *ExampleType) error {
|
||||
args := i.Mock.Called(et)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
/*
|
||||
Mock
|
||||
*/
|
||||
|
||||
func Test_Mock_TestData(t *testing.T) {
|
||||
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
if assert.NotNil(t, mockedService.TestData()) {
|
||||
|
||||
mockedService.TestData().Set("something", 123)
|
||||
assert.Equal(t, 123, mockedService.TestData().Get("something").Data())
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_On(t *testing.T) {
|
||||
|
||||
// make a test impl object
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
assert.Equal(t, mockedService.Mock.On("TheExampleMethod"), &mockedService.Mock)
|
||||
assert.Equal(t, "TheExampleMethod", mockedService.Mock.onMethodName)
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_On_WithArgs(t *testing.T) {
|
||||
|
||||
// make a test impl object
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
assert.Equal(t, mockedService.Mock.On("TheExampleMethod", 1, 2, 3), &mockedService.Mock)
|
||||
assert.Equal(t, "TheExampleMethod", mockedService.Mock.onMethodName)
|
||||
assert.Equal(t, 1, mockedService.Mock.onMethodArguments[0])
|
||||
assert.Equal(t, 2, mockedService.Mock.onMethodArguments[1])
|
||||
assert.Equal(t, 3, mockedService.Mock.onMethodArguments[2])
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_Return(t *testing.T) {
|
||||
|
||||
// make a test impl object
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
assert.Equal(t, mockedService.Mock.On("TheExampleMethod", "A", "B", true).Return(1, "two", true), &mockedService.Mock)
|
||||
|
||||
// ensure the call was created
|
||||
if assert.Equal(t, 1, len(mockedService.Mock.ExpectedCalls)) {
|
||||
call := mockedService.Mock.ExpectedCalls[0]
|
||||
|
||||
assert.Equal(t, "TheExampleMethod", call.Method)
|
||||
assert.Equal(t, "A", call.Arguments[0])
|
||||
assert.Equal(t, "B", call.Arguments[1])
|
||||
assert.Equal(t, true, call.Arguments[2])
|
||||
assert.Equal(t, 1, call.ReturnArguments[0])
|
||||
assert.Equal(t, "two", call.ReturnArguments[1])
|
||||
assert.Equal(t, true, call.ReturnArguments[2])
|
||||
assert.Equal(t, 0, call.Repeatability)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_Return_Once(t *testing.T) {
|
||||
|
||||
// make a test impl object
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
mockedService.Mock.On("TheExampleMethod", "A", "B", true).Return(1, "two", true).Once()
|
||||
|
||||
// ensure the call was created
|
||||
if assert.Equal(t, 1, len(mockedService.Mock.ExpectedCalls)) {
|
||||
call := mockedService.Mock.ExpectedCalls[0]
|
||||
|
||||
assert.Equal(t, "TheExampleMethod", call.Method)
|
||||
assert.Equal(t, "A", call.Arguments[0])
|
||||
assert.Equal(t, "B", call.Arguments[1])
|
||||
assert.Equal(t, true, call.Arguments[2])
|
||||
assert.Equal(t, 1, call.ReturnArguments[0])
|
||||
assert.Equal(t, "two", call.ReturnArguments[1])
|
||||
assert.Equal(t, true, call.ReturnArguments[2])
|
||||
assert.Equal(t, 1, call.Repeatability)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_Return_Twice(t *testing.T) {
|
||||
|
||||
// make a test impl object
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
mockedService.Mock.On("TheExampleMethod", "A", "B", true).Return(1, "two", true).Twice()
|
||||
|
||||
// ensure the call was created
|
||||
if assert.Equal(t, 1, len(mockedService.Mock.ExpectedCalls)) {
|
||||
call := mockedService.Mock.ExpectedCalls[0]
|
||||
|
||||
assert.Equal(t, "TheExampleMethod", call.Method)
|
||||
assert.Equal(t, "A", call.Arguments[0])
|
||||
assert.Equal(t, "B", call.Arguments[1])
|
||||
assert.Equal(t, true, call.Arguments[2])
|
||||
assert.Equal(t, 1, call.ReturnArguments[0])
|
||||
assert.Equal(t, "two", call.ReturnArguments[1])
|
||||
assert.Equal(t, true, call.ReturnArguments[2])
|
||||
assert.Equal(t, 2, call.Repeatability)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_Return_Times(t *testing.T) {
|
||||
|
||||
// make a test impl object
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
mockedService.Mock.On("TheExampleMethod", "A", "B", true).Return(1, "two", true).Times(5)
|
||||
|
||||
// ensure the call was created
|
||||
if assert.Equal(t, 1, len(mockedService.Mock.ExpectedCalls)) {
|
||||
call := mockedService.Mock.ExpectedCalls[0]
|
||||
|
||||
assert.Equal(t, "TheExampleMethod", call.Method)
|
||||
assert.Equal(t, "A", call.Arguments[0])
|
||||
assert.Equal(t, "B", call.Arguments[1])
|
||||
assert.Equal(t, true, call.Arguments[2])
|
||||
assert.Equal(t, 1, call.ReturnArguments[0])
|
||||
assert.Equal(t, "two", call.ReturnArguments[1])
|
||||
assert.Equal(t, true, call.ReturnArguments[2])
|
||||
assert.Equal(t, 5, call.Repeatability)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_Return_Nothing(t *testing.T) {
|
||||
|
||||
// make a test impl object
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
assert.Equal(t, mockedService.Mock.On("TheExampleMethod", "A", "B", true).Return(), &mockedService.Mock)
|
||||
|
||||
// ensure the call was created
|
||||
if assert.Equal(t, 1, len(mockedService.Mock.ExpectedCalls)) {
|
||||
call := mockedService.Mock.ExpectedCalls[0]
|
||||
|
||||
assert.Equal(t, "TheExampleMethod", call.Method)
|
||||
assert.Equal(t, "A", call.Arguments[0])
|
||||
assert.Equal(t, "B", call.Arguments[1])
|
||||
assert.Equal(t, true, call.Arguments[2])
|
||||
assert.Equal(t, 0, len(call.ReturnArguments))
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_findExpectedCall(t *testing.T) {
|
||||
|
||||
m := new(Mock)
|
||||
m.On("One", 1).Return("one")
|
||||
m.On("Two", 2).Return("two")
|
||||
m.On("Two", 3).Return("three")
|
||||
|
||||
f, c := m.findExpectedCall("Two", 3)
|
||||
|
||||
if assert.Equal(t, 2, f) {
|
||||
if assert.NotNil(t, c) {
|
||||
assert.Equal(t, "Two", c.Method)
|
||||
assert.Equal(t, 3, c.Arguments[0])
|
||||
assert.Equal(t, "three", c.ReturnArguments[0])
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_findExpectedCall_For_Unknown_Method(t *testing.T) {
|
||||
|
||||
m := new(Mock)
|
||||
m.On("One", 1).Return("one")
|
||||
m.On("Two", 2).Return("two")
|
||||
m.On("Two", 3).Return("three")
|
||||
|
||||
f, _ := m.findExpectedCall("Two")
|
||||
|
||||
assert.Equal(t, -1, f)
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_findExpectedCall_Respects_Repeatability(t *testing.T) {
|
||||
|
||||
m := new(Mock)
|
||||
m.On("One", 1).Return("one")
|
||||
m.On("Two", 2).Return("two").Once()
|
||||
m.On("Two", 3).Return("three").Twice()
|
||||
m.On("Two", 3).Return("three").Times(8)
|
||||
|
||||
f, c := m.findExpectedCall("Two", 3)
|
||||
|
||||
if assert.Equal(t, 2, f) {
|
||||
if assert.NotNil(t, c) {
|
||||
assert.Equal(t, "Two", c.Method)
|
||||
assert.Equal(t, 3, c.Arguments[0])
|
||||
assert.Equal(t, "three", c.ReturnArguments[0])
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_callString(t *testing.T) {
|
||||
|
||||
assert.Equal(t, `Method(int,bool,string)`, callString("Method", []interface{}{1, true, "something"}, false))
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_Called(t *testing.T) {
|
||||
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
mockedService.Mock.On("Test_Mock_Called", 1, 2, 3).Return(5, "6", true)
|
||||
|
||||
returnArguments := mockedService.Mock.Called(1, 2, 3)
|
||||
|
||||
if assert.Equal(t, 1, len(mockedService.Mock.Calls)) {
|
||||
assert.Equal(t, "Test_Mock_Called", mockedService.Mock.Calls[0].Method)
|
||||
assert.Equal(t, 1, mockedService.Mock.Calls[0].Arguments[0])
|
||||
assert.Equal(t, 2, mockedService.Mock.Calls[0].Arguments[1])
|
||||
assert.Equal(t, 3, mockedService.Mock.Calls[0].Arguments[2])
|
||||
}
|
||||
|
||||
if assert.Equal(t, 3, len(returnArguments)) {
|
||||
assert.Equal(t, 5, returnArguments[0])
|
||||
assert.Equal(t, "6", returnArguments[1])
|
||||
assert.Equal(t, true, returnArguments[2])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_Called_For_Bounded_Repeatability(t *testing.T) {
|
||||
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
mockedService.Mock.On("Test_Mock_Called_For_Bounded_Repeatability", 1, 2, 3).Return(5, "6", true).Once()
|
||||
mockedService.Mock.On("Test_Mock_Called_For_Bounded_Repeatability", 1, 2, 3).Return(-1, "hi", false)
|
||||
|
||||
returnArguments1 := mockedService.Mock.Called(1, 2, 3)
|
||||
returnArguments2 := mockedService.Mock.Called(1, 2, 3)
|
||||
|
||||
if assert.Equal(t, 2, len(mockedService.Mock.Calls)) {
|
||||
assert.Equal(t, "Test_Mock_Called_For_Bounded_Repeatability", mockedService.Mock.Calls[0].Method)
|
||||
assert.Equal(t, 1, mockedService.Mock.Calls[0].Arguments[0])
|
||||
assert.Equal(t, 2, mockedService.Mock.Calls[0].Arguments[1])
|
||||
assert.Equal(t, 3, mockedService.Mock.Calls[0].Arguments[2])
|
||||
|
||||
assert.Equal(t, "Test_Mock_Called_For_Bounded_Repeatability", mockedService.Mock.Calls[1].Method)
|
||||
assert.Equal(t, 1, mockedService.Mock.Calls[1].Arguments[0])
|
||||
assert.Equal(t, 2, mockedService.Mock.Calls[1].Arguments[1])
|
||||
assert.Equal(t, 3, mockedService.Mock.Calls[1].Arguments[2])
|
||||
}
|
||||
|
||||
if assert.Equal(t, 3, len(returnArguments1)) {
|
||||
assert.Equal(t, 5, returnArguments1[0])
|
||||
assert.Equal(t, "6", returnArguments1[1])
|
||||
assert.Equal(t, true, returnArguments1[2])
|
||||
}
|
||||
|
||||
if assert.Equal(t, 3, len(returnArguments2)) {
|
||||
assert.Equal(t, -1, returnArguments2[0])
|
||||
assert.Equal(t, "hi", returnArguments2[1])
|
||||
assert.Equal(t, false, returnArguments2[2])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_Called_For_SetTime_Expectation(t *testing.T) {
|
||||
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
mockedService.Mock.On("TheExampleMethod", 1, 2, 3).Return(5, "6", true).Times(4)
|
||||
|
||||
mockedService.TheExampleMethod(1, 2, 3)
|
||||
mockedService.TheExampleMethod(1, 2, 3)
|
||||
mockedService.TheExampleMethod(1, 2, 3)
|
||||
mockedService.TheExampleMethod(1, 2, 3)
|
||||
assert.Panics(t, func() {
|
||||
mockedService.TheExampleMethod(1, 2, 3)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_Called_Unexpected(t *testing.T) {
|
||||
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
// make sure it panics if no expectation was made
|
||||
assert.Panics(t, func() {
|
||||
mockedService.Mock.Called(1, 2, 3)
|
||||
}, "Calling unexpected method should panic")
|
||||
|
||||
}
|
||||
|
||||
func Test_AssertExpectationsForObjects_Helper(t *testing.T) {
|
||||
|
||||
var mockedService1 *TestExampleImplementation = new(TestExampleImplementation)
|
||||
var mockedService2 *TestExampleImplementation = new(TestExampleImplementation)
|
||||
var mockedService3 *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
mockedService1.Mock.On("Test_AssertExpectationsForObjects_Helper", 1).Return()
|
||||
mockedService2.Mock.On("Test_AssertExpectationsForObjects_Helper", 2).Return()
|
||||
mockedService3.Mock.On("Test_AssertExpectationsForObjects_Helper", 3).Return()
|
||||
|
||||
mockedService1.Called(1)
|
||||
mockedService2.Called(2)
|
||||
mockedService3.Called(3)
|
||||
|
||||
assert.True(t, AssertExpectationsForObjects(t, mockedService1.Mock, mockedService2.Mock, mockedService3.Mock))
|
||||
|
||||
}
|
||||
|
||||
func Test_AssertExpectationsForObjects_Helper_Failed(t *testing.T) {
|
||||
|
||||
var mockedService1 *TestExampleImplementation = new(TestExampleImplementation)
|
||||
var mockedService2 *TestExampleImplementation = new(TestExampleImplementation)
|
||||
var mockedService3 *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
mockedService1.Mock.On("Test_AssertExpectationsForObjects_Helper_Failed", 1).Return()
|
||||
mockedService2.Mock.On("Test_AssertExpectationsForObjects_Helper_Failed", 2).Return()
|
||||
mockedService3.Mock.On("Test_AssertExpectationsForObjects_Helper_Failed", 3).Return()
|
||||
|
||||
mockedService1.Called(1)
|
||||
mockedService3.Called(3)
|
||||
|
||||
tt := new(testing.T)
|
||||
assert.False(t, AssertExpectationsForObjects(tt, mockedService1.Mock, mockedService2.Mock, mockedService3.Mock))
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_AssertExpectations(t *testing.T) {
|
||||
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
mockedService.Mock.On("Test_Mock_AssertExpectations", 1, 2, 3).Return(5, 6, 7)
|
||||
|
||||
tt := new(testing.T)
|
||||
assert.False(t, mockedService.AssertExpectations(tt))
|
||||
|
||||
// make the call now
|
||||
mockedService.Mock.Called(1, 2, 3)
|
||||
|
||||
// now assert expectations
|
||||
assert.True(t, mockedService.AssertExpectations(tt))
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_AssertExpectationsCustomType(t *testing.T) {
|
||||
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
mockedService.Mock.On("TheExampleMethod3", AnythingOfType("*mock.ExampleType")).Return(nil).Once()
|
||||
|
||||
tt := new(testing.T)
|
||||
assert.False(t, mockedService.AssertExpectations(tt))
|
||||
|
||||
// make the call now
|
||||
mockedService.TheExampleMethod3(&ExampleType{})
|
||||
|
||||
// now assert expectations
|
||||
assert.True(t, mockedService.AssertExpectations(tt))
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_AssertExpectations_With_Repeatability(t *testing.T) {
|
||||
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
mockedService.Mock.On("Test_Mock_AssertExpectations_With_Repeatability", 1, 2, 3).Return(5, 6, 7).Twice()
|
||||
|
||||
tt := new(testing.T)
|
||||
assert.False(t, mockedService.AssertExpectations(tt))
|
||||
|
||||
// make the call now
|
||||
mockedService.Mock.Called(1, 2, 3)
|
||||
|
||||
assert.False(t, mockedService.AssertExpectations(tt))
|
||||
|
||||
mockedService.Mock.Called(1, 2, 3)
|
||||
|
||||
// now assert expectations
|
||||
assert.True(t, mockedService.AssertExpectations(tt))
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_TwoCallsWithDifferentArguments(t *testing.T) {
|
||||
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
mockedService.Mock.On("Test_Mock_TwoCallsWithDifferentArguments", 1, 2, 3).Return(5, 6, 7)
|
||||
mockedService.Mock.On("Test_Mock_TwoCallsWithDifferentArguments", 4, 5, 6).Return(5, 6, 7)
|
||||
|
||||
args1 := mockedService.Mock.Called(1, 2, 3)
|
||||
assert.Equal(t, 5, args1.Int(0))
|
||||
assert.Equal(t, 6, args1.Int(1))
|
||||
assert.Equal(t, 7, args1.Int(2))
|
||||
|
||||
args2 := mockedService.Mock.Called(4, 5, 6)
|
||||
assert.Equal(t, 5, args2.Int(0))
|
||||
assert.Equal(t, 6, args2.Int(1))
|
||||
assert.Equal(t, 7, args2.Int(2))
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_AssertNumberOfCalls(t *testing.T) {
|
||||
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
mockedService.Mock.On("Test_Mock_AssertNumberOfCalls", 1, 2, 3).Return(5, 6, 7)
|
||||
|
||||
mockedService.Mock.Called(1, 2, 3)
|
||||
assert.True(t, mockedService.AssertNumberOfCalls(t, "Test_Mock_AssertNumberOfCalls", 1))
|
||||
|
||||
mockedService.Mock.Called(1, 2, 3)
|
||||
assert.True(t, mockedService.AssertNumberOfCalls(t, "Test_Mock_AssertNumberOfCalls", 2))
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_AssertCalled(t *testing.T) {
|
||||
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
mockedService.Mock.On("Test_Mock_AssertCalled", 1, 2, 3).Return(5, 6, 7)
|
||||
|
||||
mockedService.Mock.Called(1, 2, 3)
|
||||
|
||||
assert.True(t, mockedService.AssertCalled(t, "Test_Mock_AssertCalled", 1, 2, 3))
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_AssertCalled_WithAnythingOfTypeArgument(t *testing.T) {
|
||||
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
mockedService.Mock.On("Test_Mock_AssertCalled_WithAnythingOfTypeArgument", Anything, Anything, Anything).Return()
|
||||
|
||||
mockedService.Mock.Called(1, "two", []uint8("three"))
|
||||
|
||||
assert.True(t, mockedService.AssertCalled(t, "Test_Mock_AssertCalled_WithAnythingOfTypeArgument", AnythingOfType("int"), AnythingOfType("string"), AnythingOfType("[]uint8")))
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_AssertCalled_WithArguments(t *testing.T) {
|
||||
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
mockedService.Mock.On("Test_Mock_AssertCalled_WithArguments", 1, 2, 3).Return(5, 6, 7)
|
||||
|
||||
mockedService.Mock.Called(1, 2, 3)
|
||||
|
||||
tt := new(testing.T)
|
||||
assert.True(t, mockedService.AssertCalled(tt, "Test_Mock_AssertCalled_WithArguments", 1, 2, 3))
|
||||
assert.False(t, mockedService.AssertCalled(tt, "Test_Mock_AssertCalled_WithArguments", 2, 3, 4))
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_AssertCalled_WithArguments_With_Repeatability(t *testing.T) {
|
||||
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
mockedService.Mock.On("Test_Mock_AssertCalled_WithArguments_With_Repeatability", 1, 2, 3).Return(5, 6, 7).Once()
|
||||
mockedService.Mock.On("Test_Mock_AssertCalled_WithArguments_With_Repeatability", 2, 3, 4).Return(5, 6, 7).Once()
|
||||
|
||||
mockedService.Mock.Called(1, 2, 3)
|
||||
mockedService.Mock.Called(2, 3, 4)
|
||||
|
||||
tt := new(testing.T)
|
||||
assert.True(t, mockedService.AssertCalled(tt, "Test_Mock_AssertCalled_WithArguments_With_Repeatability", 1, 2, 3))
|
||||
assert.True(t, mockedService.AssertCalled(tt, "Test_Mock_AssertCalled_WithArguments_With_Repeatability", 2, 3, 4))
|
||||
assert.False(t, mockedService.AssertCalled(tt, "Test_Mock_AssertCalled_WithArguments_With_Repeatability", 3, 4, 5))
|
||||
|
||||
}
|
||||
|
||||
func Test_Mock_AssertNotCalled(t *testing.T) {
|
||||
|
||||
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
|
||||
|
||||
mockedService.Mock.On("Test_Mock_AssertNotCalled", 1, 2, 3).Return(5, 6, 7)
|
||||
|
||||
mockedService.Mock.Called(1, 2, 3)
|
||||
|
||||
assert.True(t, mockedService.AssertNotCalled(t, "Test_Mock_NotCalled"))
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
Arguments helper methods
|
||||
*/
|
||||
func Test_Arguments_Get(t *testing.T) {
|
||||
|
||||
var args Arguments = []interface{}{"string", 123, true}
|
||||
|
||||
assert.Equal(t, "string", args.Get(0).(string))
|
||||
assert.Equal(t, 123, args.Get(1).(int))
|
||||
assert.Equal(t, true, args.Get(2).(bool))
|
||||
|
||||
}
|
||||
|
||||
func Test_Arguments_Is(t *testing.T) {
|
||||
|
||||
var args Arguments = []interface{}{"string", 123, true}
|
||||
|
||||
assert.True(t, args.Is("string", 123, true))
|
||||
assert.False(t, args.Is("wrong", 456, false))
|
||||
|
||||
}
|
||||
|
||||
func Test_Arguments_Diff(t *testing.T) {
|
||||
|
||||
var args Arguments = []interface{}{"Hello World", 123, true}
|
||||
var diff string
|
||||
var count int
|
||||
diff, count = args.Diff([]interface{}{"Hello World", 456, "false"})
|
||||
|
||||
assert.Equal(t, 2, count)
|
||||
assert.Contains(t, diff, `%!s(int=456) != %!s(int=123)`)
|
||||
assert.Contains(t, diff, `false != %!s(bool=true)`)
|
||||
|
||||
}
|
||||
|
||||
func Test_Arguments_Diff_DifferentNumberOfArgs(t *testing.T) {
|
||||
|
||||
var args Arguments = []interface{}{"string", 123, true}
|
||||
var diff string
|
||||
var count int
|
||||
diff, count = args.Diff([]interface{}{"string", 456, "false", "extra"})
|
||||
|
||||
assert.Equal(t, 3, count)
|
||||
assert.Contains(t, diff, `extra != (Missing)`)
|
||||
|
||||
}
|
||||
|
||||
func Test_Arguments_Diff_WithAnythingArgument(t *testing.T) {
|
||||
|
||||
var args Arguments = []interface{}{"string", 123, true}
|
||||
var count int
|
||||
_, count = args.Diff([]interface{}{"string", Anything, true})
|
||||
|
||||
assert.Equal(t, 0, count)
|
||||
|
||||
}
|
||||
|
||||
func Test_Arguments_Diff_WithAnythingArgument_InActualToo(t *testing.T) {
|
||||
|
||||
var args Arguments = []interface{}{"string", Anything, true}
|
||||
var count int
|
||||
_, count = args.Diff([]interface{}{"string", 123, true})
|
||||
|
||||
assert.Equal(t, 0, count)
|
||||
|
||||
}
|
||||
|
||||
func Test_Arguments_Diff_WithAnythingOfTypeArgument(t *testing.T) {
|
||||
|
||||
var args Arguments = []interface{}{"string", AnythingOfType("int"), true}
|
||||
var count int
|
||||
_, count = args.Diff([]interface{}{"string", 123, true})
|
||||
|
||||
assert.Equal(t, 0, count)
|
||||
|
||||
}
|
||||
|
||||
func Test_Arguments_Diff_WithAnythingOfTypeArgument_Failing(t *testing.T) {
|
||||
|
||||
var args Arguments = []interface{}{"string", AnythingOfType("string"), true}
|
||||
var count int
|
||||
var diff string
|
||||
diff, count = args.Diff([]interface{}{"string", 123, true})
|
||||
|
||||
assert.Equal(t, 1, count)
|
||||
assert.Contains(t, diff, `string != type int - %!s(int=123)`)
|
||||
|
||||
}
|
||||
|
||||
func Test_Arguments_Assert(t *testing.T) {
|
||||
|
||||
var args Arguments = []interface{}{"string", 123, true}
|
||||
|
||||
assert.True(t, args.Assert(t, "string", 123, true))
|
||||
|
||||
}
|
||||
|
||||
func Test_Arguments_String_Representation(t *testing.T) {
|
||||
|
||||
var args Arguments = []interface{}{"string", 123, true}
|
||||
assert.Equal(t, `string,int,bool`, args.String())
|
||||
|
||||
}
|
||||
|
||||
func Test_Arguments_String(t *testing.T) {
|
||||
|
||||
var args Arguments = []interface{}{"string", 123, true}
|
||||
assert.Equal(t, "string", args.String(0))
|
||||
|
||||
}
|
||||
|
||||
func Test_Arguments_Error(t *testing.T) {
|
||||
|
||||
var err error = errors.New("An Error")
|
||||
var args Arguments = []interface{}{"string", 123, true, err}
|
||||
assert.Equal(t, err, args.Error(3))
|
||||
|
||||
}
|
||||
|
||||
func Test_Arguments_Error_Nil(t *testing.T) {
|
||||
|
||||
var args Arguments = []interface{}{"string", 123, true, nil}
|
||||
assert.Equal(t, nil, args.Error(3))
|
||||
|
||||
}
|
||||
|
||||
func Test_Arguments_Int(t *testing.T) {
|
||||
|
||||
var args Arguments = []interface{}{"string", 123, true}
|
||||
assert.Equal(t, 123, args.Int(1))
|
||||
|
||||
}
|
||||
|
||||
func Test_Arguments_Bool(t *testing.T) {
|
||||
|
||||
var args Arguments = []interface{}{"string", 123, true}
|
||||
assert.Equal(t, true, args.Bool(2))
|
||||
|
||||
}
|
12
third_party/src/github.com/stretchr/testify/package_test.go
vendored
Normal file
12
third_party/src/github.com/stretchr/testify/package_test.go
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
package testify
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestImports(t *testing.T) {
|
||||
if assert.Equal(t, 1, 1) != true {
|
||||
t.Error("Something is wrong.")
|
||||
}
|
||||
}
|
63
third_party/src/github.com/stretchr/testify/suite/doc.go
vendored
Normal file
63
third_party/src/github.com/stretchr/testify/suite/doc.go
vendored
Normal file
@ -0,0 +1,63 @@
|
||||
// The suite package contains logic for creating testing suite structs
|
||||
// and running the methods on those structs as tests. The most useful
|
||||
// piece of this package is that you can create setup/teardown methods
|
||||
// on your testing suites, which will run before/after the whole suite
|
||||
// or individual tests (depending on which interface(s) you
|
||||
// implement).
|
||||
//
|
||||
// A testing suite is usually built by first extending the built-in
|
||||
// suite functionality from suite.Suite in testify. Alternatively,
|
||||
// you could reproduce that logic on your own if you wanted (you
|
||||
// just need to implement the TestingSuite interface from
|
||||
// suite/interfaces.go).
|
||||
//
|
||||
// After that, you can implement any of the interfaces in
|
||||
// suite/interfaces.go to add setup/teardown functionality to your
|
||||
// suite, and add any methods that start with "Test" to add tests.
|
||||
// Methods that do not match any suite interfaces and do not begin
|
||||
// with "Test" will not be run by testify, and can safely be used as
|
||||
// helper methods.
|
||||
//
|
||||
// Once you've built your testing suite, you need to run the suite
|
||||
// (using suite.Run from testify) inside any function that matches the
|
||||
// identity that "go test" is already looking for (i.e.
|
||||
// func(*testing.T)).
|
||||
//
|
||||
// Regular expression to select test suites specified command-line
|
||||
// argument "-run". Regular expression to select the methods
|
||||
// of test suites specified command-line argument "-m".
|
||||
//
|
||||
// A crude example:
|
||||
// // Basic imports
|
||||
// import (
|
||||
// "testing"
|
||||
// "github.com/stretchr/testify/assert"
|
||||
// "github.com/stretchr/testify/suite"
|
||||
// )
|
||||
//
|
||||
// // Define the suite, and absorb the built-in basic suite
|
||||
// // functionality from testify - including a T() method which
|
||||
// // returns the current testing context
|
||||
// type ExampleTestSuite struct {
|
||||
// suite.Suite
|
||||
// VariableThatShouldStartAtFive int
|
||||
// }
|
||||
//
|
||||
// // Make sure that VariableThatShouldStartAtFive is set to five
|
||||
// // before each test
|
||||
// func (suite *ExampleTestSuite) SetupTest() {
|
||||
// suite.VariableThatShouldStartAtFive = 5
|
||||
// }
|
||||
//
|
||||
// // All methods that begin with "Test" are run as tests within a
|
||||
// // suite.
|
||||
// func (suite *ExampleTestSuite) TestExample() {
|
||||
// assert.Equal(suite.T(), suite.VariableThatShouldStartAtFive, 5)
|
||||
// }
|
||||
//
|
||||
// // In order for 'go test' to run this suite, we need to create
|
||||
// // a normal test function and pass our suite to suite.Run
|
||||
// func TestExampleTestSuite(t *testing.T) {
|
||||
// suite.Run(t, new(ExampleTestSuite))
|
||||
// }
|
||||
package suite
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user