Skip to content

Commit

Permalink
test: fix tests contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
plaffitt committed Nov 29, 2024
1 parent 5a09b13 commit 375976e
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 20 deletions.
8 changes: 4 additions & 4 deletions api/core/v1/pod_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ var podStub = corev1.Pod{
func TestRewriteImages(t *testing.T) {
podStub := *podStub.DeepCopy()

g := NewWithT(t)
t.Run("Rewrite image", func(t *testing.T) {
g := NewWithT(t)
ir := ImageRewriter{
ProxyPort: 4242,
}
Expand Down Expand Up @@ -78,8 +78,8 @@ func TestRewriteImages(t *testing.T) {
func TestRewriteImagesWithIgnore(t *testing.T) {
podStub := *podStub.DeepCopy()

g := NewWithT(t)
t.Run("Rewrite image", func(t *testing.T) {
g := NewWithT(t)
ir := ImageRewriter{
ProxyPort: 4242,
IgnoreImages: []*regexp.Regexp{
Expand Down Expand Up @@ -118,8 +118,8 @@ func TestRewriteImagesWithIgnore(t *testing.T) {
func TestRewriteImagesWithAccept(t *testing.T) {
podStub := *podStub.DeepCopy()

g := NewWithT(t)
t.Run("Rewrite image", func(t *testing.T) {
g := NewWithT(t)
ir := ImageRewriter{
ProxyPort: 4242,
AcceptImages: []*regexp.Regexp{
Expand Down Expand Up @@ -233,9 +233,9 @@ func Test_isImageRewritable(t *testing.T) {
},
}

g := NewWithT(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
imageRewriter := ImageRewriter{
IgnoreImages: tt.regexps,
IgnorePullPolicyAlways: tt.ignorePullPolicyAlways,
Expand Down
2 changes: 1 addition & 1 deletion api/kuik/v1alpha1/cachedimage_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
func TestDefault(t *testing.T) {
cachedImageStub := CachedImage{}

g := NewWithT(t)
tests := []struct {
name string
sourceImage string
Expand All @@ -37,6 +36,7 @@ func TestDefault(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
cachedImage := cachedImageStub.DeepCopy()
cachedImage.Spec.SourceImage = tt.sourceImage

Expand Down
4 changes: 2 additions & 2 deletions internal/controller/core/pod_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ func TestDesiredCachedImages(t *testing.T) {
},
}

g := NewWithT(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
cachedImages := DesiredCachedImages(context.Background(), &tt.pod)
g.Expect(cachedImages).To(HaveLen(len(tt.cachedImages)))
for i, cachedImage := range cachedImages {
Expand Down Expand Up @@ -115,9 +115,9 @@ func Test_cachedImageFromSourceImage(t *testing.T) {
},
}

g := NewWithT(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
cachedImage, err := cachedImageFromSourceImage(tt.sourceImage)
g.Expect(err).ToNot(HaveOccurred())

Expand Down
9 changes: 6 additions & 3 deletions internal/proxy/bearer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func Test_parseWwwAuthenticate(t *testing.T) {
},
}

g := NewWithT(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
wwwAuthenticateHeader := fmt.Sprintf("Bearer realm=\"%s\",service=\"%s\",scope=\"%s\"", tt.realm, tt.service, tt.scope)
wwwAuthenticate := parseWwwAuthenticate(wwwAuthenticateHeader)
g.Expect(wwwAuthenticate).To(Not(BeNil()))
Expand Down Expand Up @@ -93,9 +93,9 @@ func TestNewBearer(t *testing.T) {
},
}

g := NewWithT(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
gh := ghttp.NewGHTTPWithGomega(g)
server := ghttp.NewServer()
defer server.Close()
Expand Down Expand Up @@ -167,26 +167,29 @@ func TestNewBearer(t *testing.T) {
}

t.Run("Fail on first request", func(t *testing.T) {
g := NewWithT(t)
_, err := NewBearer("http://localhost:100000", "/")
g.Expect(err).To(HaveOccurred())
g.Expect(err.Error()).To(ContainSubstring("invalid port"))
})
}

func TestGetToken(t *testing.T) {
g := NewWithT(t)
bearer := &Bearer{AccessToken: "my-access-token"}

t.Run("Token set", func(t *testing.T) {
g := NewWithT(t)
g.Expect(bearer.GetToken()).To(Equal(bearer.AccessToken))
})

t.Run("Token and AccessToken set", func(t *testing.T) {
g := NewWithT(t)
bearer.Token = "my-token"
g.Expect(bearer.GetToken()).To(Equal(bearer.Token))
})

t.Run("AccessToken set", func(t *testing.T) {
g := NewWithT(t)
bearer.AccessToken = ""
g.Expect(bearer.GetToken()).To(Equal(bearer.Token))
})
Expand Down
2 changes: 1 addition & 1 deletion internal/proxy/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ func Test_handleOriginRegistryPort(t *testing.T) {
},
}

g := NewWithT(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
originRegistry := handleOriginRegistryPort(tt.originRegistry)
g.Expect(originRegistry).To(Equal(tt.expectedOutput))
})
Expand Down
6 changes: 4 additions & 2 deletions internal/registry/keychain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ func TestGetKeychains(t *testing.T) {
},
}

g := NewWithT(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
if tt.repositoryName == "" {
tt.repositoryName = "alpine"
}
Expand All @@ -254,12 +254,14 @@ func TestGetKeychains(t *testing.T) {
}

t.Run("Not from Amazon ECR", func(t *testing.T) {
g := NewWithT(t)
keychains, err := GetKeychains("alpine", []corev1.Secret{})
g.Expect(err).To(BeNil())
g.Expect(keychains).ToNot(ContainElement(authn.NewKeychainFromHelper(ecrLogin.NewECRHelper())))
})

t.Run("From Amazon ECR", func(t *testing.T) {
g := NewWithT(t)
keychains, err := GetKeychains("000000000000.dkr.ecr.eu-west-1.amazonaws.com/some-image", []corev1.Secret{})
g.Expect(err).To(BeNil())
g.Expect(keychains).To(ContainElement(authn.NewKeychainFromHelper(ecrLogin.NewECRHelper())))
Expand Down Expand Up @@ -308,9 +310,9 @@ func TestGetPullSecrets(t *testing.T) {
},
}

g := NewWithT(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
namespace := make([]byte, 10)
rand.Read(namespace)

Expand Down
14 changes: 7 additions & 7 deletions internal/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ func Test_parseLocalReference(t *testing.T) {
},
}

g := NewWithT(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
reference, err := parseLocalReference(tt.image)

if tt.wantErr != "" {
Expand Down Expand Up @@ -122,9 +122,9 @@ func Test_ImageIsCached(t *testing.T) {
},
}

g := NewWithT(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
gh := ghttp.NewGHTTPWithGomega(g)
server := ghttp.NewServer()
defer server.Close()
Expand Down Expand Up @@ -193,9 +193,9 @@ func Test_DeleteImage(t *testing.T) {
},
}

g := NewWithT(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
gh := ghttp.NewGHTTPWithGomega(g)
server := ghttp.NewServer()
defer server.Close()
Expand Down Expand Up @@ -250,9 +250,9 @@ func Test_CacheImage(t *testing.T) {
},
}

g := NewWithT(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
gh := ghttp.NewGHTTPWithGomega(g)

digestSha := "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
Expand Down Expand Up @@ -369,9 +369,9 @@ func TestSanitizeName(t *testing.T) {
},
}

g := NewWithT(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
label := SanitizeName(tt.image)
g.Expect(label).To(Equal(tt.expectedSanitizedImage))
})
Expand Down Expand Up @@ -406,9 +406,9 @@ func TestRepositoryLabel(t *testing.T) {
},
}

g := NewWithT(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
label := RepositoryLabel(tt.repositoryName)
g.Expect(label).To(Equal(tt.expectedLabel))
})
Expand Down Expand Up @@ -462,9 +462,9 @@ func TestContainerAnnotationKey(t *testing.T) {
},
}

g := NewWithT(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
annotationKey := ContainerAnnotationKey(tt.containerName, tt.initContainer)
g.Expect(annotationKey).To(Equal(tt.expectedAnnotationKey))
})
Expand Down

0 comments on commit 375976e

Please sign in to comment.