Skip to content

Commit

Permalink
Move test-only files to test-only packages
Browse files Browse the repository at this point in the history
  • Loading branch information
tallclair committed Mar 1, 2016
1 parent 69f4ac3 commit 7b6d843
Show file tree
Hide file tree
Showing 35 changed files with 240 additions and 192 deletions.
10 changes: 5 additions & 5 deletions cmd/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ import (
endpointcontroller "k8s.io/kubernetes/pkg/controller/endpoint"
nodecontroller "k8s.io/kubernetes/pkg/controller/node"
replicationcontroller "k8s.io/kubernetes/pkg/controller/replication"
"k8s.io/kubernetes/pkg/kubelet/cadvisor"
cadvisortest "k8s.io/kubernetes/pkg/kubelet/cadvisor/testing"
"k8s.io/kubernetes/pkg/kubelet/cm"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
"k8s.io/kubernetes/pkg/kubelet/dockertools"
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
"k8s.io/kubernetes/pkg/labels"
Expand Down Expand Up @@ -203,7 +203,7 @@ func startComponents(firstManifestURL, secondManifestURL string) (string, string
nodeController := nodecontroller.NewNodeController(nil, clientset, 5*time.Minute, util.NewFakeAlwaysRateLimiter(), util.NewFakeAlwaysRateLimiter(),
40*time.Second, 60*time.Second, 5*time.Second, nil, false)
nodeController.Run(5 * time.Second)
cadvisorInterface := new(cadvisor.Fake)
cadvisorInterface := new(cadvisortest.Fake)

// Kubelet (localhost)
testRootDir := integration.MakeTempDirOrDie("kubelet_integ_1.", "")
Expand All @@ -225,7 +225,7 @@ func startComponents(firstManifestURL, secondManifestURL string) (string, string
cadvisorInterface,
configFilePath,
nil,
kubecontainer.FakeOS{},
containertest.FakeOS{},
1*time.Second, /* FileCheckFrequency */
1*time.Second, /* HTTPCheckFrequency */
10*time.Second, /* MinimumGCAge */
Expand Down Expand Up @@ -257,7 +257,7 @@ func startComponents(firstManifestURL, secondManifestURL string) (string, string
cadvisorInterface,
"",
nil,
kubecontainer.FakeOS{},
containertest.FakeOS{},
1*time.Second, /* FileCheckFrequency */
1*time.Second, /* HTTPCheckFrequency */
10*time.Second, /* MinimumGCAge */
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubemark/hollow-node.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"k8s.io/kubernetes/pkg/client/record"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
"k8s.io/kubernetes/pkg/kubelet/cadvisor"
cadvisortest "k8s.io/kubernetes/pkg/kubelet/cadvisor/testing"
"k8s.io/kubernetes/pkg/kubelet/cm"
"k8s.io/kubernetes/pkg/kubelet/dockertools"
"k8s.io/kubernetes/pkg/kubemark"
Expand Down Expand Up @@ -95,7 +95,7 @@ func main() {
}

if config.Morph == "kubelet" {
cadvisorInterface := new(cadvisor.Fake)
cadvisorInterface := new(cadvisortest.Fake)
containerManager := cm.NewStubContainerManager()

fakeDockerClient := dockertools.NewFakeDockerClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package cadvisor
package testing

import (
"github.com/google/cadvisor/events"
cadvisorapi "github.com/google/cadvisor/info/v1"
cadvisorapiv2 "github.com/google/cadvisor/info/v2"
"k8s.io/kubernetes/pkg/kubelet/cadvisor"
)

// Fake cAdvisor implementation.
type Fake struct {
}

var _ Interface = new(Fake)
var _ cadvisor.Interface = new(Fake)

func (c *Fake) Start() error {
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package cadvisor
package testing

import (
"github.com/google/cadvisor/events"
cadvisorapi "github.com/google/cadvisor/info/v1"
cadvisorapiv2 "github.com/google/cadvisor/info/v2"
"github.com/stretchr/testify/mock"
"k8s.io/kubernetes/pkg/kubelet/cadvisor"
)

type Mock struct {
mock.Mock
}

var _ Interface = new(Mock)
var _ cadvisor.Interface = new(Mock)

func (c *Mock) Start() error {
args := c.Called()
Expand Down
40 changes: 40 additions & 0 deletions pkg/kubelet/container/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2015 The Kubernetes Authors 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

// TestRunTimeCache embeds runtimeCache with some additional methods for
// testing.
type TestRuntimeCache struct {
runtimeCache
}

func (r *TestRuntimeCache) UpdateCacheWithLock() error {
r.Lock()
defer r.Unlock()
return r.updateCache()
}

func (r *TestRuntimeCache) GetCachedPods() []*Pod {
r.Lock()
defer r.Unlock()
return r.pods
}

func NewTestRuntimeCache(getter podsGetter) *TestRuntimeCache {
c, _ := NewRuntimeCache(getter)
return &TestRuntimeCache{*c.(*runtimeCache)}
}
6 changes: 4 additions & 2 deletions pkg/kubelet/container/image_puller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package container
package container_test

import (
"errors"
Expand All @@ -24,6 +24,8 @@ import (
"github.com/stretchr/testify/assert"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/client/record"
. "k8s.io/kubernetes/pkg/kubelet/container"
ctest "k8s.io/kubernetes/pkg/kubelet/container/testing"
"k8s.io/kubernetes/pkg/util"
)

Expand Down Expand Up @@ -101,7 +103,7 @@ func TestPuller(t *testing.T) {
fakeClock := util.NewFakeClock(time.Now())
backOff.Clock = fakeClock

fakeRuntime := &FakeRuntime{}
fakeRuntime := &ctest.FakeRuntime{}
fakeRecorder := &record.FakeRecorder{}
puller := NewImagePuller(fakeRecorder, fakeRuntime, backOff)

Expand Down
14 changes: 0 additions & 14 deletions pkg/kubelet/container/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,3 @@ func (RealOS) Mkdir(path string, perm os.FileMode) error {
func (RealOS) Symlink(oldname string, newname string) error {
return os.Symlink(oldname, newname)
}

// FakeOS mocks out certain OS calls to avoid perturbing the filesystem
// on the test machine.
type FakeOS struct{}

// MkDir is a fake call that just returns nil.
func (FakeOS) Mkdir(path string, perm os.FileMode) error {
return nil
}

// Symlink is a fake call that just returns nil.
func (FakeOS) Symlink(oldname string, newname string) error {
return nil
}
42 changes: 11 additions & 31 deletions pkg/kubelet/container/runtime_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,22 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package container
package container_test

import (
"reflect"
"testing"
"time"
)

// testRunTimeCache embeds runtimeCache with some additional methods for
// testing.
type testRuntimeCache struct {
runtimeCache
}

func (r *testRuntimeCache) updateCacheWithLock() error {
r.Lock()
defer r.Unlock()
return r.updateCache()
}

func (r *testRuntimeCache) getCachedPods() []*Pod {
r.Lock()
defer r.Unlock()
return r.pods
}

func newTestRuntimeCache(getter podsGetter) *testRuntimeCache {
c, _ := NewRuntimeCache(getter)
return &testRuntimeCache{*c.(*runtimeCache)}
}
. "k8s.io/kubernetes/pkg/kubelet/container"
ctest "k8s.io/kubernetes/pkg/kubelet/container/testing"
)

func TestGetPods(t *testing.T) {
runtime := &FakeRuntime{}
runtime := &ctest.FakeRuntime{}
expected := []*Pod{{ID: "1111"}, {ID: "2222"}, {ID: "3333"}}
runtime.PodList = expected
cache := newTestRuntimeCache(runtime)
cache := NewTestRuntimeCache(runtime)
actual, err := cache.GetPods()
if err != nil {
t.Errorf("unexpected error %v", err)
Expand All @@ -60,28 +40,28 @@ func TestGetPods(t *testing.T) {
}

func TestForceUpdateIfOlder(t *testing.T) {
runtime := &FakeRuntime{}
cache := newTestRuntimeCache(runtime)
runtime := &ctest.FakeRuntime{}
cache := NewTestRuntimeCache(runtime)

// Cache old pods.
oldpods := []*Pod{{ID: "1111"}}
runtime.PodList = oldpods
cache.updateCacheWithLock()
cache.UpdateCacheWithLock()

// Update the runtime to new pods.
newpods := []*Pod{{ID: "1111"}, {ID: "2222"}, {ID: "3333"}}
runtime.PodList = newpods

// An older timestamp should not force an update.
cache.ForceUpdateIfOlder(time.Now().Add(-20 * time.Minute))
actual := cache.getCachedPods()
actual := cache.GetCachedPods()
if !reflect.DeepEqual(oldpods, actual) {
t.Errorf("expected %#v, got %#v", oldpods, actual)
}

// A newer timestamp should force an update.
cache.ForceUpdateIfOlder(time.Now().Add(20 * time.Second))
actual = cache.getCachedPods()
actual = cache.GetCachedPods()
if !reflect.DeepEqual(newpods, actual) {
t.Errorf("expected %#v, got %#v", newpods, actual)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/kubelet/container/serialized_image_puller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package container
package container_test

import (
"errors"
Expand All @@ -24,6 +24,8 @@ import (
"github.com/stretchr/testify/assert"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/client/record"
. "k8s.io/kubernetes/pkg/kubelet/container"
ctest "k8s.io/kubernetes/pkg/kubelet/container/testing"
"k8s.io/kubernetes/pkg/util"
)

Expand Down Expand Up @@ -101,7 +103,7 @@ func TestSerializedPuller(t *testing.T) {
fakeClock := util.NewFakeClock(time.Now())
backOff.Clock = fakeClock

fakeRuntime := &FakeRuntime{}
fakeRuntime := &ctest.FakeRuntime{}
fakeRecorder := &record.FakeRecorder{}
puller := NewSerializedImagePuller(fakeRecorder, fakeRuntime, backOff)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,32 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package container
package testing

import (
"time"

"k8s.io/kubernetes/pkg/kubelet/container"
"k8s.io/kubernetes/pkg/types"
)

type fakeCache struct {
runtime Runtime
runtime container.Runtime
}

func NewFakeCache(runtime Runtime) Cache {
func NewFakeCache(runtime container.Runtime) container.Cache {
return &fakeCache{runtime: runtime}
}

func (c *fakeCache) Get(id types.UID) (*PodStatus, error) {
func (c *fakeCache) Get(id types.UID) (*container.PodStatus, error) {
return c.runtime.GetPodStatus(id, "", "")
}

func (c *fakeCache) GetNewerThan(id types.UID, minTime time.Time) (*PodStatus, error) {
func (c *fakeCache) GetNewerThan(id types.UID, minTime time.Time) (*container.PodStatus, error) {
return c.Get(id)
}

func (c *fakeCache) Set(id types.UID, status *PodStatus, err error, timestamp time.Time) {
func (c *fakeCache) Set(id types.UID, status *container.PodStatus, err error, timestamp time.Time) {
}

func (c *fakeCache) Delete(id types.UID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package container
package testing

import (
"fmt"
Expand All @@ -24,6 +24,7 @@ import (
"time"

"k8s.io/kubernetes/pkg/api"
. "k8s.io/kubernetes/pkg/kubelet/container"
"k8s.io/kubernetes/pkg/types"
"k8s.io/kubernetes/pkg/util"
"k8s.io/kubernetes/pkg/volume"
Expand Down Expand Up @@ -70,6 +71,10 @@ func (fv *FakeVersion) Compare(other string) (int, error) {
return result, nil
}

type podsGetter interface {
GetPods(bool) ([]*Pod, error)
}

type FakeRuntimeCache struct {
getter podsGetter
}
Expand Down
Loading

0 comments on commit 7b6d843

Please sign in to comment.