-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add testcases and refactor few methods to make it testable
- Loading branch information
1 parent
1be19da
commit 1505bb3
Showing
9 changed files
with
355 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
/vendor | ||
helm-images | ||
/dist | ||
cover.out | ||
cover.out | ||
cover.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
package pkg | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nikhilsbhat/helm-images/pkg/k8s" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestImages_filterImagesByRegistries(t *testing.T) { | ||
t.Run("should be able to return the filtered images by registries", func(t *testing.T) { | ||
imageKind := k8s.Image{ | ||
Kind: "Deployment", | ||
Name: "sample-deployment", | ||
Image: []string{ | ||
"quay.io/prometheus/node-exporter:v1.1.2", | ||
"k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.0.0", | ||
"quay.io/prometheus/alertmanager:v0.21.0", | ||
"prom/pushgateway:v1.3.1", | ||
"jimmidyson/configmap-reload:v0.5.0", | ||
}, | ||
} | ||
|
||
imageList := []*k8s.Image{&imageKind} | ||
|
||
imageClient := Images{ | ||
Registries: []string{"quay.io", "k8s.gcr.io"}, | ||
} | ||
imageClient.SetLogger("info") | ||
|
||
expectedImageKind := k8s.Image{ | ||
Kind: "Deployment", | ||
Name: "sample-deployment", | ||
Image: []string{ | ||
"quay.io/prometheus/node-exporter:v1.1.2", | ||
"k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.0.0", | ||
"quay.io/prometheus/alertmanager:v0.21.0", | ||
}, | ||
} | ||
|
||
expected := []*k8s.Image{&expectedImageKind} | ||
|
||
imagesFiltered := imageClient.filterImagesByRegistries(imageList) | ||
assert.ElementsMatch(t, expected[0].Image, imagesFiltered[0].Image) | ||
}) | ||
|
||
t.Run("should be able to return the filtered images by registries", func(t *testing.T) { | ||
imageKind := k8s.Image{ | ||
Kind: "Deployment", | ||
Name: "sample-deployment", | ||
Image: []string{ | ||
"quay.io/prometheus/node-exporter:v1.1.2", | ||
"k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.0.0", | ||
"quay.io/prometheus/alertmanager:v0.21.0", | ||
"prom/pushgateway:v1.3.1", | ||
"jimmidyson/configmap-reload:v0.5.0", | ||
}, | ||
} | ||
|
||
imageList := []*k8s.Image{&imageKind} | ||
|
||
imageClient := Images{ | ||
Registries: []string{"qquay.io"}, | ||
} | ||
|
||
imageClient.SetLogger("info") | ||
|
||
imagesFiltered := imageClient.filterImagesByRegistries(imageList) | ||
assert.Nil(t, imagesFiltered) | ||
}) | ||
|
||
t.Run("should be able to return the filtered unique images by registries", func(t *testing.T) { | ||
imageKind := k8s.Image{ | ||
Kind: "Deployment", | ||
Name: "sample-deployment", | ||
Image: []string{ | ||
"quay.io/prometheus/node-exporter:v1.1.2", | ||
"quay.io/prometheus/node-exporter:v1.1.2", | ||
"k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.0.0", | ||
"quay.io/prometheus/alertmanager:v0.21.0", | ||
"prom/pushgateway:v1.3.1", | ||
"jimmidyson/configmap-reload:v0.5.0", | ||
}, | ||
} | ||
|
||
imageList := []*k8s.Image{&imageKind} | ||
|
||
imageClient := Images{ | ||
Registries: []string{"quay.io"}, | ||
UniqueImages: true, | ||
} | ||
|
||
imageClient.SetLogger("info") | ||
|
||
expectedImageKind := k8s.Image{ | ||
Kind: "Deployment", | ||
Name: "sample-deployment", | ||
Image: []string{ | ||
"quay.io/prometheus/node-exporter:v1.1.2", | ||
"quay.io/prometheus/alertmanager:v0.21.0", | ||
}, | ||
} | ||
expected := []*k8s.Image{&expectedImageKind} | ||
|
||
imagesFiltered := imageClient.filterImagesByRegistries(imageList) | ||
assert.Equal(t, expected, imagesFiltered) | ||
}) | ||
|
||
t.Run("should be image list as it is as no registries matched", func(t *testing.T) { | ||
imageKind := k8s.Image{ | ||
Kind: "Deployment", | ||
Name: "sample-deployment", | ||
Image: []string{ | ||
"quay.io/prometheus/node-exporter:v1.1.2", | ||
"k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.0.0", | ||
"quay.io/prometheus/alertmanager:v0.21.0", | ||
"prom/pushgateway:v1.3.1", | ||
"jimmidyson/configmap-reload:v0.5.0", | ||
}, | ||
} | ||
|
||
imageList := []*k8s.Image{&imageKind} | ||
|
||
imageClient := Images{} | ||
|
||
imageClient.SetLogger("info") | ||
|
||
imagesFiltered := imageClient.filterImagesByRegistries(imageList) | ||
assert.ElementsMatch(t, imageList[0].Image, imagesFiltered[0].Image) | ||
}) | ||
|
||
t.Run("should be able to return the unique and not filtered images by registries", func(t *testing.T) { | ||
imageKind := k8s.Image{ | ||
Kind: "Deployment", | ||
Name: "sample-deployment", | ||
Image: []string{ | ||
"quay.io/prometheus/node-exporter:v1.1.2", | ||
"quay.io/prometheus/node-exporter:v1.1.2", | ||
"k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.0.0", | ||
"quay.io/prometheus/alertmanager:v0.21.0", | ||
"prom/pushgateway:v1.3.1", | ||
"jimmidyson/configmap-reload:v0.5.0", | ||
}, | ||
} | ||
|
||
imageList := []*k8s.Image{&imageKind} | ||
|
||
imageClient := Images{ | ||
UniqueImages: true, | ||
} | ||
imageClient.SetLogger("info") | ||
|
||
expectedImageKind := k8s.Image{ | ||
Kind: "Deployment", | ||
Name: "sample-deployment", | ||
Image: []string{ | ||
"quay.io/prometheus/node-exporter:v1.1.2", | ||
"k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.0.0", | ||
"quay.io/prometheus/alertmanager:v0.21.0", | ||
"prom/pushgateway:v1.3.1", | ||
"jimmidyson/configmap-reload:v0.5.0", | ||
}, | ||
} | ||
|
||
expected := []*k8s.Image{&expectedImageKind} | ||
|
||
imagesFiltered := imageClient.filterImagesByRegistries(imageList) | ||
assert.ElementsMatch(t, expected[0].Image, imagesFiltered[0].Image) | ||
}) | ||
} | ||
|
||
func Test_filteredImages(t *testing.T) { | ||
t.Run("should be able to filter the images by the list of registries", func(t *testing.T) { | ||
images := []string{ | ||
"quay.io/prometheus/node-exporter:v1.1.2", | ||
"k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.0.0", | ||
"quay.io/prometheus/alertmanager:v0.21.0", | ||
} | ||
registries := []string{"quay.io"} | ||
|
||
expected := []string{ | ||
"quay.io/prometheus/alertmanager:v0.21.0", | ||
"quay.io/prometheus/node-exporter:v1.1.2", | ||
} | ||
|
||
actual := filteredImages(images, registries) | ||
assert.ElementsMatch(t, actual, expected) | ||
}) | ||
|
||
t.Run("should be able to filter the images by the list of registries with matching names", func(t *testing.T) { | ||
images := []string{ | ||
"quay.io/prometheus/node-exporter:v1.1.2", | ||
"k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.0.0", | ||
"quay.io/prometheus/alertmanager:v0.21.0", | ||
} | ||
registries := []string{"quay"} | ||
|
||
expected := []string{ | ||
"quay.io/prometheus/alertmanager:v0.21.0", | ||
"quay.io/prometheus/node-exporter:v1.1.2", | ||
} | ||
|
||
actual := filteredImages(images, registries) | ||
assert.ElementsMatch(t, actual, expected) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.