This repository has been archived by the owner on Nov 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moving the logic to the resource package and adding unit tests for th…
…e Istio labels enrurer
- Loading branch information
Showing
5 changed files
with
168 additions
and
76 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
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
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,81 @@ | ||
package resources | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
type labeledResourcesForTest struct { | ||
labels map[string]string | ||
name string | ||
} | ||
|
||
func newLabeledResourcesForTest(labels map[string]string, name string) *labeledResourcesForTest { | ||
return &labeledResourcesForTest{labels: labels, name: name} | ||
} | ||
|
||
func (l *labeledResourcesForTest) GetLabels() map[string]string { | ||
return l.labels | ||
} | ||
|
||
func (l *labeledResourcesForTest) SetLabels(labels map[string]string) { | ||
l.labels = labels | ||
} | ||
|
||
func (l *labeledResourcesForTest) GetName() string { | ||
return l.name | ||
} | ||
|
||
func TestIstioLabelsEnsurer(t *testing.T) { | ||
|
||
withoutAnyIstioLabel := map[string]string{} | ||
labeledResourceWithoutAnyIstioLabel := newLabeledResourcesForTest(withoutAnyIstioLabel, "my-service") | ||
expectedLabels := map[string]string{ | ||
"app": "my-service", | ||
"version": "baseline", | ||
} | ||
|
||
expectedBool := ensureIstioLabelsForResource(labeledResourceWithoutAnyIstioLabel) | ||
require.True(t, expectedBool) | ||
require.Equal(t, expectedLabels, labeledResourceWithoutAnyIstioLabel.GetLabels()) | ||
|
||
withK8sAppLabel := map[string]string{ | ||
"app.kubernetes.io/name": "my-service-using-k8s-app", | ||
} | ||
labeledResourceWithK8sAppLabel := newLabeledResourcesForTest(withK8sAppLabel, "my-service") | ||
expectedLabels = map[string]string{ | ||
"app.kubernetes.io/name": "my-service-using-k8s-app", | ||
"app": "my-service-using-k8s-app", | ||
"version": "baseline", | ||
} | ||
expectedBool = ensureIstioLabelsForResource(labeledResourceWithK8sAppLabel) | ||
require.True(t, expectedBool) | ||
require.Equal(t, expectedLabels, labeledResourceWithK8sAppLabel.GetLabels()) | ||
|
||
withVersionLabel := map[string]string{ | ||
"version": "v0.2.1", | ||
} | ||
labeledResourceWithVersionLabel := newLabeledResourcesForTest(withVersionLabel, "my-versioned-service") | ||
expectedLabels = map[string]string{ | ||
"app": "my-versioned-service", | ||
"version": "v0.2.1", | ||
} | ||
|
||
expectedBool = ensureIstioLabelsForResource(labeledResourceWithVersionLabel) | ||
require.True(t, expectedBool) | ||
require.Equal(t, expectedLabels, labeledResourceWithVersionLabel.GetLabels()) | ||
|
||
withVersionAndAppLabel := map[string]string{ | ||
"app": "my-app", | ||
"version": "v0.2.1", | ||
} | ||
labeledResourceWithVersionAndAppLabel := newLabeledResourcesForTest(withVersionAndAppLabel, "my-app-service") | ||
expectedLabels = map[string]string{ | ||
"app": "my-app", | ||
"version": "v0.2.1", | ||
} | ||
|
||
expectedBool = ensureIstioLabelsForResource(labeledResourceWithVersionAndAppLabel) | ||
require.False(t, expectedBool) | ||
require.Equal(t, expectedLabels, labeledResourceWithVersionAndAppLabel.GetLabels()) | ||
} |