Skip to content

Commit

Permalink
autodiscover: add unit tests (#893)
Browse files Browse the repository at this point in the history
* autodiscover: add unit tests

* Fix linter error
  • Loading branch information
jmontesi authored Mar 1, 2023
1 parent 7a04e81 commit 072c6f8
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 1 deletion.
7 changes: 6 additions & 1 deletion internal/clientsholder/clientsholder.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
ocpMachine "github.com/openshift/machine-config-operator/pkg/generated/clientset/versioned"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
rbacv1 "k8s.io/api/rbac/v1"
apiextv1fake "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
k8sFakeClient "k8s.io/client-go/kubernetes/fake"
Expand Down Expand Up @@ -75,7 +76,7 @@ func SetupFakeOlmClient(olmMockObjects []runtime.Object) {
// For other (OLM, )
// runtime mocking objects loading, use the proper clientset mocking function.
//
//nolint:funlen
//nolint:funlen,gocyclo
func GetTestClientsHolder(k8sMockObjects []runtime.Object, filenames ...string) *ClientsHolder {
// Build slices of different objects depending on what client
// is supposed to expect them.
Expand Down Expand Up @@ -104,12 +105,16 @@ func GetTestClientsHolder(k8sMockObjects []runtime.Object, filenames ...string)
k8sClientObjects = append(k8sClientObjects, v)
case *appsv1.Deployment:
k8sClientObjects = append(k8sClientObjects, v)
case *appsv1.StatefulSet:
k8sClientObjects = append(k8sClientObjects, v)
case *corev1.ResourceQuota:
k8sClientObjects = append(k8sClientObjects, v)
case *corev1.PersistentVolume:
k8sClientObjects = append(k8sClientObjects, v)
case *corev1.PersistentVolumeClaim:
k8sClientObjects = append(k8sClientObjects, v)
case *policyv1.PodDisruptionBudget:
k8sClientObjects = append(k8sClientObjects, v)

// K8s Extension Client Objects
case *apiextv1c.CustomResourceDefinition:
Expand Down
50 changes: 50 additions & 0 deletions pkg/autodiscover/autodiscover_pdbs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,53 @@
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

package autodiscover

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/test-network-function/cnf-certification-test/internal/clientsholder"
policyv1 "k8s.io/api/policy/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

func TestGetPodDisruptionBudgets(t *testing.T) {
generatePodDisruptionBudget := func(name, namespace string) *policyv1.PodDisruptionBudget {
return &policyv1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: policyv1.PodDisruptionBudgetSpec{},
}
}

testCases := []struct {
pdbName string
pdbNamespace string
expectedPDBs []policyv1.PodDisruptionBudget
}{
{
pdbName: "testPdb",
pdbNamespace: "tnf",
expectedPDBs: []policyv1.PodDisruptionBudget{
{
ObjectMeta: metav1.ObjectMeta{
Name: "testPdb",
Namespace: "tnf",
},
},
},
},
}

for _, tc := range testCases {
var testRuntimeObjects []runtime.Object
testRuntimeObjects = append(testRuntimeObjects, generatePodDisruptionBudget(tc.pdbName, tc.pdbNamespace))
oc := clientsholder.GetTestClientsHolder(testRuntimeObjects)
pdbs, err := getPodDisruptionBudgets(oc.K8sClient.PolicyV1(), []string{tc.pdbNamespace})
assert.Nil(t, err)
assert.Equal(t, tc.expectedPDBs, pdbs)
}
}
173 changes: 173 additions & 0 deletions pkg/autodiscover/autodiscover_podset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,176 @@
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
package autodiscover

import (
"testing"

"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

"github.com/test-network-function/cnf-certification-test/internal/clientsholder"
"github.com/test-network-function/cnf-certification-test/pkg/configuration"
)

func TestFindDeploymentByLabel(t *testing.T) {
generateDeployment := func(name, namespace, label string) *appsv1.Deployment {
return &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"testLabel": label,
},
},
},
},
}
}

testCases := []struct {
testNamespaces []string
expectedResults []appsv1.Deployment
testDeploymentName string
testDeploymentNamespace string
testDeploymentLabel string
queryLabel string
}{
{ // Test Case #1 - Happy path, labels found
testDeploymentName: "testName",
testDeploymentNamespace: "testNamespace",
testDeploymentLabel: "mylabel",
queryLabel: "mylabel",

expectedResults: []appsv1.Deployment{
{
ObjectMeta: metav1.ObjectMeta{
Name: "testName",
Namespace: "testNamespace",
},
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"testLabel": "mylabel",
},
},
},
},
},
},
},
{ // Test Case #2 - Invalid label
testDeploymentName: "testName",
testDeploymentNamespace: "testNamespace",
testDeploymentLabel: "testLabel",
queryLabel: "badlabel",

expectedResults: []appsv1.Deployment{},
},
}

for _, tc := range testCases {
testLabel := []configuration.Label{
{
Name: "testLabel",
Value: tc.testDeploymentLabel,
},
}
testNamespaces := []string{
tc.testDeploymentNamespace,
}
var testRuntimeObjects []runtime.Object
testRuntimeObjects = append(testRuntimeObjects, generateDeployment(tc.testDeploymentName, tc.testDeploymentNamespace, tc.queryLabel))
oc := clientsholder.GetTestClientsHolder(testRuntimeObjects)

deployments := findDeploymentByLabel(oc.K8sClient.AppsV1(), testLabel, testNamespaces)
assert.Equal(t, tc.expectedResults, deployments)
}
}

func TestFindStatefulSetByLabel(t *testing.T) {
generateStatefulSet := func(name, namespace, label string) *appsv1.StatefulSet {
return &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: appsv1.StatefulSetSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"testLabel": label,
},
},
},
},
}
}

testCases := []struct {
testNamespaces []string
expectedResults []appsv1.StatefulSet
testStatefulSetName string
testStatefulSetNamespace string
testStatefulSetLabel string
queryLabel string
}{
{ // Test Case #1 - Happy path, labels found
testStatefulSetName: "testName",
testStatefulSetNamespace: "testNamespace",
testStatefulSetLabel: "mylabel",
queryLabel: "mylabel",

expectedResults: []appsv1.StatefulSet{
{
ObjectMeta: metav1.ObjectMeta{
Name: "testName",
Namespace: "testNamespace",
},
Spec: appsv1.StatefulSetSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"testLabel": "mylabel",
},
},
},
},
},
},
},
{ // Test Case #2 - Invalid label
testStatefulSetName: "testName",
testStatefulSetNamespace: "testNamespace",
testStatefulSetLabel: "testLabel",
queryLabel: "badlabel",

expectedResults: []appsv1.StatefulSet{},
},
}

for _, tc := range testCases {
testLabel := []configuration.Label{
{
Name: "testLabel",
Value: tc.testStatefulSetLabel,
},
}
testNamespaces := []string{
tc.testStatefulSetNamespace,
}
var testRuntimeObjects []runtime.Object
testRuntimeObjects = append(testRuntimeObjects, generateStatefulSet(tc.testStatefulSetName, tc.testStatefulSetNamespace, tc.queryLabel))
oc := clientsholder.GetTestClientsHolder(testRuntimeObjects)

statefulSets := findStatefulSetByLabel(oc.K8sClient.AppsV1(), testLabel, testNamespaces)
assert.Equal(t, tc.expectedResults, statefulSets)
}
}

0 comments on commit 072c6f8

Please sign in to comment.