Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for AdminNetworkPolicy #388

Merged
merged 43 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
0d03b9a
Added some ANP tests from policy-assistant.
tanyaveksler Jul 23, 2024
82364f6
fixing lint errors
tanyaveksler Jul 23, 2024
b9230c4
Fixing lint error
tanyaveksler Jul 28, 2024
a38ec50
Reorganized testing infrastructure from for tests fro parsed resource…
tanyaveksler Jul 28, 2024
53259a5
Merge branch 'support_admin_netpolicy' into tests_for_admin_netpolicy
tanyaveksler Jul 28, 2024
45a7181
fixing lint errors
tanyaveksler Jul 28, 2024
1805549
return error if ANPs are without name or not unique names
shireenf-ibm Jul 30, 2024
6e77b96
Revert "return error if ANPs are without name or not unique names"
shireenf-ibm Jul 30, 2024
dd0a3b4
Added ANP/BANP names in tests.
tanyaveksler Jul 30, 2024
0861c19
:Merge branch 'tests_for_admin_netpolicy' of github.com:np-guard/netp…
tanyaveksler Jul 30, 2024
87a0ba6
Fixed lint errors.
tanyaveksler Jul 30, 2024
a060dd8
Fixed lint errors
tanyaveksler Jul 30, 2024
dedbf4b
Added eval parsed resources tests (along with connlist tests).
tanyaveksler Aug 4, 2024
2dabef1
fixing lint errors
tanyaveksler Aug 4, 2024
1f97afc
fixing lint errors
tanyaveksler Aug 4, 2024
1f1a67c
Added testing of CheckIfAllowed and CheckIfAllowedNew
tanyaveksler Aug 6, 2024
28f746d
fixing lint errors
tanyaveksler Aug 6, 2024
8ad121a
making linter happy
tanyaveksler Aug 6, 2024
63aa720
Reorganized eval ANP tests, to not depend on connlist.
tanyaveksler Aug 13, 2024
e9efb41
Small fixes.
tanyaveksler Aug 13, 2024
735eb73
Merge branch 'support_admin_netpolicy' into tests_for_admin_netpolicy
tanyaveksler Aug 13, 2024
e4c3277
small fixes
tanyaveksler Aug 13, 2024
5081036
Changed expected results to not use "all but" expressions.
tanyaveksler Aug 13, 2024
82248ba
making linter happy
tanyaveksler Aug 13, 2024
821f67e
making linter happy
tanyaveksler Aug 13, 2024
6c20baa
making lint happy
tanyaveksler Aug 13, 2024
9f2c3ac
making linter happy
tanyaveksler Aug 13, 2024
1eb9520
make linter happy
tanyaveksler Aug 13, 2024
476d4de
Creating k8sObjects during a test run, rather then in a test creation.
tanyaveksler Aug 13, 2024
9bf8e68
making lint happy
tanyaveksler Aug 13, 2024
08c713a
make lint happy
tanyaveksler Aug 13, 2024
d4cebea
linter
tanyaveksler Aug 13, 2024
c37634c
shutting up linter
tanyaveksler Aug 13, 2024
63190a7
Moved to parsed_resources_tests some functions used only there.
tanyaveksler Aug 20, 2024
58f973c
Added fake pod status IP fields
tanyaveksler Aug 20, 2024
d6b85d8
Avoiding unnecessary exports;
tanyaveksler Aug 20, 2024
73fabd3
Merge branch 'support_admin_netpolicy' into tests_for_admin_netpolicy
tanyaveksler Aug 20, 2024
b922829
Merge branch 'support_admin_netpolicy' into tests_for_admin_netpolicy
tanyaveksler Aug 25, 2024
cece146
Making linter happy
tanyaveksler Aug 25, 2024
7f90b7b
Update pkg/internal/testutils/parsed_resources_tests.go
tanyaveksler Sep 3, 2024
a3d0a43
Update pkg/internal/testutils/parsed_resources_tests.go
tanyaveksler Sep 3, 2024
9bc0047
Fixed typos;
tanyaveksler Sep 3, 2024
55a6680
Merge branch 'support_admin_netpolicy' into tests_for_admin_netpolicy
tanyaveksler Sep 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions pkg/internal/testutils/parsed_resources_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
func newDefaultPod(namespace, name string, ports []int, protocols []v1.Protocol) *v1.Pod {
podObj := v1.Pod{}
podObj.TypeMeta.APIVersion = "v1"
podObj.TypeMeta.Kind = "Pod"

Check failure on line 36 in pkg/internal/testutils/parsed_resources_tests.go

View workflow job for this annotation

GitHub Actions / golangci-lint

string `Pod` has 2 occurrences, make it a constant (goconst)
podObj.ObjectMeta.Name = name
podObj.ObjectMeta.Namespace = namespace
podObj.Labels = map[string]string{"pod": name}
Expand Down Expand Up @@ -135,24 +135,52 @@
res := []parser.K8sObject{}
test.TestInfo = fmt.Sprintf("test: %q, output format: %q", test.Name, test.OutputFormat)
for _, ns := range test.Resources.NsList {
res = append(res, parser.CreateNamespaceK8sObject(ns))
res = append(res, CreateNamespaceK8sObject(ns))
}
for _, pod := range test.Resources.PodList {
res = append(res, parser.CreatePodK8sObject(pod))
res = append(res, CreatePodK8sObject(pod))
}
for _, np := range test.NpList {
res = append(res, parser.CreateNetwordPolicyK8sObject(np))
res = append(res, CreateNetwordPolicyK8sObject(np))
}
for _, anp := range test.AnpList {
res = append(res, parser.CreateAdminNetwordPolicyK8sObject(anp))
res = append(res, CreateAdminNetwordPolicyK8sObject(anp))
}
// Tanya: uncomment the code below when BaselineAdminNetworkPolicy is implemented
// if test.Banp != nil {
// res = append(res, parser.CreateBaselineAdminNetwordPolicyK8sObject(test.Banp))
// res = append(res, CreateBaselineAdminNetwordPolicyK8sObject(test.Banp))
// }
return res
}

func CreatePodK8sObject(pod *v1.Pod) parser.K8sObject {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should these funcs be exported?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They shouldn't :)

k8sObj := parser.K8sObject{}
k8sObj.Kind = "Pod"

Check failure on line 158 in pkg/internal/testutils/parsed_resources_tests.go

View workflow job for this annotation

GitHub Actions / golangci-lint

add-constant: string literal "Pod" appears, at least, 2 times, create a named constant for it (revive)
k8sObj.Pod = pod
return k8sObj
}

func CreateNamespaceK8sObject(ns *v1.Namespace) parser.K8sObject {
k8sObj := parser.K8sObject{}
k8sObj.Kind = "Namespace"
k8sObj.Namespace = ns
return k8sObj
}

func CreateNetwordPolicyK8sObject(np *netv1.NetworkPolicy) parser.K8sObject {
k8sObj := parser.K8sObject{}
k8sObj.Kind = "NetworkPolicy"
k8sObj.NetworkPolicy = np
return k8sObj
}

func CreateAdminNetwordPolicyK8sObject(anp *v1alpha1.AdminNetworkPolicy) parser.K8sObject {
k8sObj := parser.K8sObject{}
k8sObj.Kind = "AdminNetworkPolicy"
k8sObj.AdminNetworkPolicy = anp
return k8sObj
}

//////////////////////////////////// The following tests are taken from /////////////////////////////////////
// https://github.com/kubernetes-sigs/network-policy-api/blob/main/cmd/policy-assistant/test/integration/integration_test.go

Expand Down
35 changes: 0 additions & 35 deletions pkg/manifests/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ import (
"errors"
"fmt"

v1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/resource"
apisv1a "sigs.k8s.io/network-policy-api/apis/v1alpha1"

"github.com/np-guard/netpol-analyzer/pkg/internal/netpolerrors"
"github.com/np-guard/netpol-analyzer/pkg/logger"
Expand Down Expand Up @@ -95,38 +92,6 @@ func resourceInfoToK8sObject(info *resource.Info, l logger.Logger, muteErrsAndWa
return &resObject, nil
}

func CreatePodK8sObject(pod *v1.Pod) K8sObject {
k8sObj := K8sObject{}
k8sObj.Kind = "Pod"
k8sObj.Pod = pod
k8sObj.initDefaultNamespace()
return k8sObj
}

func CreateNamespaceK8sObject(ns *v1.Namespace) K8sObject {
k8sObj := K8sObject{}
k8sObj.Kind = "Namespace"
k8sObj.Namespace = ns
k8sObj.initDefaultNamespace()
return k8sObj
}

func CreateNetwordPolicyK8sObject(np *netv1.NetworkPolicy) K8sObject {
k8sObj := K8sObject{}
k8sObj.Kind = "NetworkPolicy"
k8sObj.NetworkPolicy = np
k8sObj.initDefaultNamespace()
return k8sObj
}

func CreateAdminNetwordPolicyK8sObject(anp *apisv1a.AdminNetworkPolicy) K8sObject {
k8sObj := K8sObject{}
k8sObj.Kind = "AdminNetworkPolicy"
k8sObj.AdminNetworkPolicy = anp
k8sObj.initDefaultNamespace()
return k8sObj
}

// error for resource with kind: , name: ,namespace: ,
func getResourceInfoStr(kind, name, namespace string) string {
res := ""
Expand Down
Loading