-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract out the filtering function and add unit tests for the same
- Loading branch information
Showing
16 changed files
with
1,865 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Unit tests | ||
|
||
on: | ||
workflow_dispatch: | ||
pull_request: | ||
branches: | ||
- main | ||
paths: | ||
- "**.go" | ||
- ".github/workflows/unit-tests.yaml" | ||
|
||
jobs: | ||
unit-test: | ||
name: Execute all tests | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Check out code into the Go module directory | ||
uses: actions/checkout@v4 | ||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version-file: "go.mod" | ||
cache: false | ||
- name: Execute unit tests | ||
run: go test -v ./... |
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,83 @@ | ||
package hostpaths | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"gotest.tools/assert" | ||
"gotest.tools/assert/cmp" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func Test_filter(t *testing.T) { | ||
testPodList := []corev1.Pod{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-pod1", | ||
Namespace: "test-ns1", | ||
}, | ||
Spec: corev1.PodSpec{}, | ||
}, | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-pod2", | ||
Namespace: "test-ns2", | ||
}, | ||
Spec: corev1.PodSpec{}, | ||
}, | ||
} | ||
|
||
testCases := []struct { | ||
name string | ||
podList []corev1.Pod | ||
vclusterNamespaces map[string]struct{} | ||
expected []corev1.Pod | ||
}{ | ||
{ | ||
name: "None of the pods belong to namespace(s) managed by the current vCluster", | ||
podList: testPodList, | ||
vclusterNamespaces: map[string]struct{}{ | ||
"test-ns3": {}, | ||
"test-ns4": {}, | ||
}, | ||
expected: []corev1.Pod{}, | ||
}, | ||
{ | ||
name: "Some of the pods belong to namespace(s) managed by the current vCluster", | ||
podList: testPodList, | ||
vclusterNamespaces: map[string]struct{}{ | ||
"test-ns1": {}, | ||
"test-ns4": {}, | ||
}, | ||
expected: []corev1.Pod{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-pod1", | ||
Namespace: "test-ns1", | ||
}, | ||
Spec: corev1.PodSpec{}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "All of the pods belong to namespace(s) managed by the current vCluster", | ||
podList: testPodList, | ||
vclusterNamespaces: map[string]struct{}{ | ||
"test-ns1": {}, | ||
"test-ns2": {}, | ||
}, | ||
expected: testPodList, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
actual := filter(context.Background(), testCase.podList, testCase.vclusterNamespaces) | ||
|
||
assert.Assert(t, | ||
cmp.DeepEqual(actual, testCase.expected), | ||
"Unexpected result in test case %s", | ||
testCase.name, | ||
) | ||
} | ||
} |
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,13 @@ | ||
Copyright 2018 gotest.tools authors | ||
|
||
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. |
Oops, something went wrong.