Skip to content

Commit

Permalink
Extract out the filtering function and add unit tests for the same
Browse files Browse the repository at this point in the history
  • Loading branch information
neogopher committed Feb 22, 2024
1 parent 10213db commit 335934e
Show file tree
Hide file tree
Showing 16 changed files with 1,865 additions and 6 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/unit-tests.yaml
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
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 ./...
18 changes: 12 additions & 6 deletions cmd/hostpaths/hostpaths.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,12 +443,7 @@ func getPhysicalPodMap(ctx context.Context, options *context2.VirtualClusterOpti
}

// Limit Pods
pods = make([]corev1.Pod, 0, len(podList.Items))
for _, pod := range podList.Items {
if _, ok := vclusterNamespaces[pod.Namespace]; ok {
pods = append(pods, pod)
}
}
pods = filter(ctx, podList.Items, vclusterNamespaces)
} else {
pods = podList.Items
}
Expand Down Expand Up @@ -476,6 +471,17 @@ func getPhysicalPodMap(ctx context.Context, options *context2.VirtualClusterOpti
return podMappings, nil
}

func filter(ctx context.Context, podList []corev1.Pod, vclusterNamespaces map[string]struct{}) []corev1.Pod {
pods := make([]corev1.Pod, 0, len(podList))
for _, pod := range podList {
if _, ok := vclusterNamespaces[pod.Namespace]; ok {
pods = append(pods, pod)
}
}

return pods
}

func cleanupOldContainerPaths(ctx context.Context, existingVPodsWithNS map[string]bool) error {
options := ctx.Value(optionsKey).(*context2.VirtualClusterOptions)

Expand Down
83 changes: 83 additions & 0 deletions cmd/hostpaths/hostpaths_test.go
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,
)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/loft-sh/vcluster v0.15.2
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.7.0
gotest.tools v2.2.0+incompatible
k8s.io/api v0.27.4
k8s.io/apimachinery v0.27.4
k8s.io/client-go v0.27.4
Expand Down
13 changes: 13 additions & 0 deletions vendor/gotest.tools/LICENSE
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.
Loading

0 comments on commit 335934e

Please sign in to comment.