Skip to content

Commit

Permalink
use stub instead of mock for test objects
Browse files Browse the repository at this point in the history
  • Loading branch information
alphanota committed Jan 22, 2025
1 parent c052c83 commit 004c943
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions pkg/skaffold/docker/build_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,13 @@ func TestCreateBuildArgsFromArtifacts(t *testing.T) {
}{
{
description: "can resolve artifacts",
r: NewMockArtifactResolver(map[string]string{"img1": "tag1", "img2": "tag2", "img3": "tag3", "img4": "tag4"}),
r: NewStubArtifactResolver(map[string]string{"img1": "tag1", "img2": "tag2", "img3": "tag3", "img4": "tag4"}),
deps: []*latest.ArtifactDependency{{ImageName: "img3", Alias: "alias3"}, {ImageName: "img4", Alias: "alias4"}},
args: map[string]*string{"alias3": util.Ptr("tag3"), "alias4": util.Ptr("tag4")},
},
{
description: "cannot resolve artifacts",
r: NewMockArtifactResolver(map[string]string{}),
r: NewStubArtifactResolver(map[string]string{}),
args: map[string]*string{"alias3": nil, "alias4": nil},
deps: []*latest.ArtifactDependency{{ImageName: "img3", Alias: "alias3"}, {ImageName: "img4", Alias: "alias4"}},
},
Expand Down
42 changes: 21 additions & 21 deletions pkg/skaffold/docker/dockertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,70 +26,70 @@ import (
)

// MockArtifactResolver mocks docker.ArtifactResolver interface.
type mockArtifactResolver struct {
type stubArtifactResolver struct {
m map[string]string
}

// NewMockArtifactResolver returns a mock ArtifactResolver for testing.
func NewMockArtifactResolver(m map[string]string) *mockArtifactResolver {
return &mockArtifactResolver{m}
// NewStubArtifactResolver returns a mock ArtifactResolver for testing.
func NewStubArtifactResolver(m map[string]string) *stubArtifactResolver {
return &stubArtifactResolver{m}
}

func (r mockArtifactResolver) GetImageTag(imageName string) (string, bool) {
func (r stubArtifactResolver) GetImageTag(imageName string) (string, bool) {
val, found := r.m[imageName]
return val, found
}

// simpleMockArtifactResolver is an implementation of docker.ArtifactResolver
// simpleStubArtifactResolver is an implementation of docker.ArtifactResolver
// that returns the same value for any key
type simpleMockArtifactResolver struct{}
type simpleStubArtifactResolver struct{}

// GetImageTag is an implementation of docker.ArtifactResolver that
// always returns the same tag.
func (s *simpleMockArtifactResolver) GetImageTag(_ string) (string, bool) {
func (s *simpleStubArtifactResolver) GetImageTag(_ string) (string, bool) {
return "image:latest", true
}

func NewSimpleMockArtifactResolver() ArtifactResolver {
return &simpleMockArtifactResolver{}
func NewSimpleStubArtifactResolver() ArtifactResolver {
return &simpleStubArtifactResolver{}
}

// MockConfig is a mock implementation of the Config interface.
type MockConfig struct {
// configStub is a mock implementation of the Config interface.
type configStub struct {
runMode config.RunMode
prune bool
}

func (m MockConfig) GetKubeContext() string {
func (m configStub) GetKubeContext() string {
return ""
}

func (m MockConfig) GlobalConfig() string {
func (m configStub) GlobalConfig() string {
return ""
}

func (m MockConfig) MinikubeProfile() string {
func (m configStub) MinikubeProfile() string {
return ""
}

func (m MockConfig) GetInsecureRegistries() map[string]bool {
func (m configStub) GetInsecureRegistries() map[string]bool {
return map[string]bool{}
}

func (m MockConfig) Mode() config.RunMode {
func (m configStub) Mode() config.RunMode {
return m.runMode
}

func (m MockConfig) Prune() bool {
func (m configStub) Prune() bool {
return m.prune
}

func (m MockConfig) ContainerDebugging() bool {
func (m configStub) ContainerDebugging() bool {
return false
}

func NewMockConfig(mode config.RunMode, prune bool) Config {
return &MockConfig{runMode: mode, prune: prune}
func NewConfigStub(mode config.RunMode, prune bool) Config {
return &configStub{runMode: mode, prune: prune}
}

type fakeImageFetcher struct{}
Expand Down
6 changes: 3 additions & 3 deletions pkg/skaffold/graph/dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestSourceDependenciesCache(t *testing.T) {
return deps[a.ImageName], nil
})

r := NewSourceDependenciesCache(nil, docker.NewSimpleMockArtifactResolver(), g)
r := NewSourceDependenciesCache(nil, docker.NewSimpleStubArtifactResolver(), g)
d, err := r.TransitiveArtifactDependencies(context.Background(), g["img1"])
t.CheckNoError(err)
expectedDeps := []string{"file11", "file12", "file21", "file22", "file31", "file32", "file41", "file42", "file41", "file42"}
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestSourceDependenciesForArtifact(t *testing.T) {
"IMAGE_NAME": "{{.IMAGE_NAME}}",
"IMAGE_TAG": "{{.IMAGE_TAG}}",
},
dockerConfig: docker.NewMockConfig(config.RunModes.Build, false),
dockerConfig: docker.NewConfigStub(config.RunModes.Build, false),
dockerFileContents: `ARG IMAGE_REPO
ARG IMAGE_NAME
ARG IMAGE_TAG
Expand All @@ -129,7 +129,7 @@ COPY $IMAGE_TAG.go .

t.Override(&docker.RetrieveImage, docker.NewFakeImageFetcher())

d := docker.NewSimpleMockArtifactResolver()
d := docker.NewSimpleStubArtifactResolver()
tmpDir.Write("Dockerfile", test.dockerFileContents)
if test.dockerBuildArgs != nil {
args := map[string]*string{}
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/tag/input_digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type inputDigestTagger struct {
}

func NewInputDigestTagger(cfg docker.Config, ag graph.ArtifactGraph) (Tagger, error) {
return NewInputDigestTaggerWithSourceCache(cfg, graph.NewSourceDependenciesCache(cfg, docker.NewSimpleMockArtifactResolver(), ag))
return NewInputDigestTaggerWithSourceCache(cfg, graph.NewSourceDependenciesCache(cfg, docker.NewSimpleStubArtifactResolver(), ag))
}

func NewInputDigestTaggerWithSourceCache(cfg docker.Config, cache graph.SourceDependenciesCache) (Tagger, error) {
Expand Down

0 comments on commit 004c943

Please sign in to comment.