Skip to content

Commit c272d75

Browse files
authored
Merge pull request #121 from mattfenwick/default-label-tests
add test cases for default namespace labels
2 parents ec3aee4 + 1cbb688 commit c272d75

9 files changed

+54
-11
lines changed

hack/kind/run-cyclonus.sh

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@ CNI=${CNI:-calico}
88
CLUSTER_NAME="netpol-$CNI"
99
RUN_FROM_SOURCE=${RUN_FROM_SOURCE:-true}
1010
FROM_SOURCE_ARGS=${FROM_SOURCE_ARGS:-"generate --include conflict --job-timeout-seconds 2"}
11+
INSTALL_KIND=${INSTALL_KIND:-true}
1112

12-
# install kind if not found
13-
if ! command -v kind &> /dev/null
14-
then
13+
# see https://github.com/actions/virtual-environments/blob/main/images/linux/Ubuntu2004-README.md
14+
# github includes a kind version, but it may not be the version we want
15+
if [[ $INSTALL_KIND == true ]]; then
1516
curl -Lo ./kind https://github.com/kubernetes-sigs/kind/releases/download/"${KIND_VERSION}"/kind-$(uname)-amd64
1617
chmod +x ./kind
1718
sudo mv kind /usr/local/bin
1819
fi
1920

21+
kind version
22+
which -a kind
23+
2024
# create kind cluster
2125
pushd "$CNI"
2226
CLUSTER=$CLUSTER_NAME ./setup-kind.sh

pkg/cli/generate.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ import (
1313
"github.com/spf13/cobra"
1414
)
1515

16+
var (
17+
DefaultExcludeTags = []string{
18+
generator.TagMultiPeer,
19+
generator.TagUpstreamE2E,
20+
generator.TagExample,
21+
generator.TagEndPort,
22+
generator.TagNamespacesByDefaultLabel}
23+
)
24+
1625
type GenerateArgs struct {
1726
AllowDNS bool
1827
Noisy bool
@@ -67,7 +76,7 @@ func SetupGenerateCommand() *cobra.Command {
6776
command.Flags().IntVar(&args.JobTimeoutSeconds, "job-timeout-seconds", 10, "number of seconds to pass on to 'agnhost connect --timeout=%ds' flag")
6877

6978
command.Flags().StringSliceVar(&args.Include, "include", []string{}, "include tests with any of these tags; if empty, all tests will be included. Valid tags:\n"+strings.Join(generator.TagSlice, "\n"))
70-
command.Flags().StringSliceVar(&args.Exclude, "exclude", []string{generator.TagMultiPeer, generator.TagUpstreamE2E, generator.TagExample, generator.TagEndPort}, "exclude tests with any of these tags. See 'include' field for valid tags")
79+
command.Flags().StringSliceVar(&args.Exclude, "exclude", DefaultExcludeTags, "exclude tests with any of these tags. See 'include' field for valid tags")
7180

7281
command.Flags().BoolVar(&args.Mock, "mock", false, "if true, use a mock kube runner (i.e. don't actually run tests against kubernetes; instead, product fake results")
7382
command.Flags().BoolVar(&args.DryRun, "dry-run", false, "if true, don't actually do anything: just print out what would be done")

pkg/generator/constants.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package generator
22

33
import (
4+
"github.com/mattfenwick/cyclonus/pkg/kube"
45
v1 "k8s.io/api/core/v1"
56
. "k8s.io/api/networking/v1"
67
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -118,4 +119,5 @@ var (
118119
},
119120
},
120121
}
122+
nsZMatchDefaultLabelsSelector = &metav1.LabelSelector{MatchLabels: map[string]string{kube.DefaultNamespaceLabel: "z"}}
121123
)

pkg/generator/namespacecases.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package generator
2+
3+
import (
4+
networkingv1 "k8s.io/api/networking/v1"
5+
)
6+
7+
func (t *TestCaseGenerator) NamespaceTestCases() []*TestCase {
8+
peers := []networkingv1.NetworkPolicyPeer{{NamespaceSelector: nsZMatchDefaultLabelsSelector}}
9+
return []*TestCase{
10+
NewSingleStepTestCase("ingress: select namespace by default label",
11+
NewStringSet(TagNamespacesByDefaultLabel, TagIngress),
12+
ProbeAllAvailable,
13+
CreatePolicy(BuildPolicy(SetPeers(true, peers)).NetworkPolicy())),
14+
NewSingleStepTestCase("egress: select namespace by default label",
15+
NewStringSet(TagNamespacesByDefaultLabel, TagEgress),
16+
ProbeAllAvailable,
17+
CreatePolicy(BuildPolicy(SetPeers(false, peers)).NetworkPolicy())),
18+
}
19+
}

pkg/generator/tags.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ const (
5151
)
5252

5353
const (
54-
TagAllPods = "all-pods"
55-
TagPodsByLabel = "pods-by-label"
56-
TagAllNamespaces = "all-namespaces"
57-
TagNamespacesByLabel = "namespaces-by-label"
58-
TagPolicyNamespace = "policy-namespace"
54+
TagAllPods = "all-pods"
55+
TagPodsByLabel = "pods-by-label"
56+
TagAllNamespaces = "all-namespaces"
57+
TagNamespacesByLabel = "namespaces-by-label"
58+
TagPolicyNamespace = "policy-namespace"
59+
TagNamespacesByDefaultLabel = "namespaces-by-default-label"
5960
)
6061

6162
const (
@@ -118,6 +119,7 @@ var AllTags = map[string][]string{
118119
TagAllNamespaces,
119120
TagNamespacesByLabel,
120121
TagPolicyNamespace,
122+
TagNamespacesByDefaultLabel,
121123
},
122124
TagPeerIPBlock: {
123125
TagIPBlockNoExcept,

pkg/generator/testcasegenerator.go

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func (t *TestCaseGenerator) GenerateAllTestCases() []*TestCase {
7070
t.ExampleTestCases(),
7171
t.ActionTestCases(),
7272
t.ConflictTestCases(),
73+
t.NamespaceTestCases(),
7374
t.UpstreamE2ETestCases())
7475
}
7576

pkg/generator/testcasegenerator_tests.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ func RunTestCaseGeneratorTests() {
1818
Expect(len(gen.ExampleTestCases())).To(Equal(1))
1919
Expect(len(gen.PortProtocolTestCases())).To(Equal(70))
2020
Expect(len(gen.ConflictTestCases())).To(Equal(16))
21+
Expect(len(gen.NamespaceTestCases())).To(Equal(2))
2122

22-
Expect(len(gen.GenerateTestCases())).To(Equal(228))
23+
Expect(len(gen.GenerateTestCases())).To(Equal(230))
2324
})
2425
})
2526
}

pkg/kube/constants.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package kube
2+
3+
const (
4+
DefaultNamespaceLabel = "kubernetes.io/metadata.name"
5+
)

pkg/kube/ikubernetes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (m *MockKubernetes) GetNamespace(namespace string) (*v1.Namespace, error) {
117117
for k, v := range ns.NamespaceObject.Labels {
118118
labels[k] = v
119119
}
120-
labels["kubernetes.io/metadata.name"] = namespace
120+
labels[DefaultNamespaceLabel] = namespace
121121
return &v1.Namespace{
122122
ObjectMeta: metav1.ObjectMeta{
123123
Name: ns.NamespaceObject.Name,

0 commit comments

Comments
 (0)