Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
test: add test for network metric
Browse files Browse the repository at this point in the history
add test for metric interface

Signed-off-by: Ace-Tang <[email protected]>
  • Loading branch information
Ace-Tang committed Aug 19, 2019
1 parent dc38ba7 commit 4a28b52
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions containerd-shim-v2/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2017 Intel Corporation
// Copyright (c) 2018 HyperHQ Inc.
//
// SPDX-License-Identifier: Apache-2.0
//

package containerdshim

import (
"context"
"testing"

"github.com/containerd/cgroups"
"github.com/containerd/containerd/namespaces"
vc "github.com/kata-containers/runtime/virtcontainers"
"github.com/stretchr/testify/assert"
)

func TestStatNetworkMetric(t *testing.T) {

assert := assert.New(t)
var err error

mockNetwork := []*vc.NetworkStats{
{
Name: "test-network",
RxBytes: 10,
TxBytes: 20,
},
}

expectedNetwork := []*cgroups.NetworkStat{
{
Name: "test-network",
RxBytes: 10,
TxBytes: 20,
},
}

testingImpl.StatsContainerFunc = func(ctx context.Context, sandboxID, containerID string) (vc.ContainerStats, error) {
return vc.ContainerStats{
NetworkStats: mockNetwork,
}, nil
}

defer func() {
testingImpl.StatsContainerFunc = nil
}()

ctx := namespaces.WithNamespace(context.Background(), "UnitTest")
resp, err := testingImpl.StatsContainer(ctx, testSandboxID, testContainerID)
assert.NoError(err)

metrics := statsToMetrics(&resp)
assert.Equal(expectedNetwork, metrics.Network)
}

0 comments on commit 4a28b52

Please sign in to comment.