-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test/e2e: add tests for NodeFeatureRules
Add tests covering the basic functionality of NodeFeatureRule objects, covering different feature types ("flag features", "attribute features" and "instance features") as well as backreferencing (using the output of previously run rules) and templating. The test relies on the "fake" feature source and its default configuration.
- Loading branch information
Showing
5 changed files
with
334 additions
and
1 deletion.
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,76 @@ | ||
apiVersion: nfd.k8s-sigs.io/v1alpha1 | ||
kind: NodeFeatureRule | ||
metadata: | ||
name: e2e-test-1 | ||
spec: | ||
rules: | ||
# | ||
# Simple test rules for flag features | ||
# | ||
- name: "e2e-flag-test-1" | ||
labels: | ||
e2e-flag-test-1: "true" | ||
vars: | ||
e2e-flag-test-1.not: "false" | ||
matchFeatures: | ||
- feature: "fake.flag" | ||
matchExpressions: | ||
"flag_1": {op: Exists} | ||
|
||
# Negative test not supposed to create a label | ||
- name: "e2e-flag-test-neg-1" | ||
labels: | ||
e2e-flag-test-neg-1: "true" | ||
matchFeatures: | ||
- feature: "fake.flag" | ||
matchExpressions: | ||
"flag_1": {op: DoesNotExist} | ||
|
||
# | ||
# Simple test rules for attribute features | ||
# | ||
- name: "e2e-attribute-test-1" | ||
labels: | ||
e2e-attribute-test-1: "true" | ||
vars: | ||
e2e-attribute-test-1.not: "false" | ||
matchFeatures: | ||
- feature: "fake.attribute" | ||
matchExpressions: | ||
"attr_1": {op: IsTrue} | ||
"attr_2": {op: IsFalse} | ||
|
||
# Negative test not supposed to create a label | ||
- name: "e2e-attribute-test-neg-1" | ||
labels: | ||
e2e-attribute-test-neg-1: "true" | ||
matchFeatures: | ||
- feature: "fake.attribute" | ||
matchExpressions: | ||
"attr_1": {op: IsTrue} | ||
"attr_2": {op: IsTrue} | ||
|
||
# | ||
# Simple test rules for instnace features | ||
# | ||
- name: "e2e-instance-test-1" | ||
labels: | ||
e2e-instance-test-1: "true" | ||
vars: | ||
e2e-instance-test-1.not: "false" | ||
e2e-instance-test-1.123: "123" | ||
matchFeatures: | ||
- feature: "fake.instance" | ||
matchExpressions: | ||
"attr_1": {op: In, value: ["true"]} | ||
"attr_3": {op: Gt, value: ["10"]} | ||
|
||
# Negative test not supposed to create a label | ||
- name: "e2e-instance-test-neg-1" | ||
labels: | ||
e2e-instance-test-neg-1: "true" | ||
matchFeatures: | ||
- feature: "fake.instance" | ||
matchExpressions: | ||
"attr_1": {op: In, value: ["true"]} | ||
"attr_3": {op: Lt, value: ["10"]} |
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,41 @@ | ||
apiVersion: nfd.k8s-sigs.io/v1alpha1 | ||
kind: NodeFeatureRule | ||
metadata: | ||
name: e2e-test-2 | ||
spec: | ||
rules: | ||
# | ||
# More complex rule testing backreferencing and matchAny field | ||
# | ||
- name: "e2e-matchany-test-1" | ||
labels: | ||
e2e-matchany-test-1: "true" | ||
vars: | ||
e2e-instance-test-1.not: "false" | ||
matchFeatures: | ||
- feature: "rule.matched" | ||
matchExpressions: | ||
"e2e-attribute-test-1": {op: InRegexp, value: ["^tru"]} | ||
"e2e-instance-test-1.123": {op: In, value: ["1", "12", "123"]} | ||
matchAny: | ||
- matchFeatures: | ||
- feature: "fake.instance" | ||
matchExpressions: | ||
"attr_1": {op: In, value: ["nomatch"]} | ||
- matchFeatures: | ||
- feature: "fake.instance" | ||
matchExpressions: | ||
"attr_3": {op: In, value: ["100"]} | ||
|
||
# | ||
# Simple test for templating | ||
# | ||
- name: "e2e-template-test-1" | ||
labelsTemplate: | | ||
{{ range .fake.instance }}e2e-template-test-1-{{ .name }}=found | ||
{{ end }} | ||
matchFeatures: | ||
- feature: "fake.instance" | ||
matchExpressions: | ||
"attr_1": {op: In, value: ["true"]} | ||
|
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,110 @@ | ||
/* | ||
Copyright 2022 The Kubernetes 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. | ||
*/ | ||
|
||
package utils | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
|
||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
extclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
apiruntime "k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/pkg/apis/nfd/v1alpha1" | ||
nfdclientset "sigs.k8s.io/node-feature-discovery/pkg/generated/clientset/versioned" | ||
nfdscheme "sigs.k8s.io/node-feature-discovery/pkg/generated/clientset/versioned/scheme" | ||
) | ||
|
||
var packagePath string | ||
|
||
// CreateNodeFeatureRulesCRD creates the NodeFeatureRule CRD in the API server. | ||
func CreateNodeFeatureRulesCRD(cli extclient.Interface) (*apiextensionsv1.CustomResourceDefinition, error) { | ||
crd, err := crdFromFile(filepath.Join(packagePath, "..", "..", "..", "deployment", "base", "nfd-crds", "nodefeaturerule-crd.yaml")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Delete existing CRD (if any) with this we also get rid of stale objects | ||
err = cli.ApiextensionsV1().CustomResourceDefinitions().Delete(context.TODO(), crd.Name, metav1.DeleteOptions{}) | ||
if err != nil && !errors.IsNotFound(err) { | ||
return nil, fmt.Errorf("failed to delete NodeFeatureRule CRD: %w", err) | ||
} | ||
|
||
return cli.ApiextensionsV1().CustomResourceDefinitions().Create(context.TODO(), crd, metav1.CreateOptions{}) | ||
} | ||
|
||
// CreateNodeFeatureRuleFromFile creates a NodeFeatureRule object from a given file located under test data directory. | ||
func CreateNodeFeatureRuleFromFile(cli nfdclientset.Interface, filename string) error { | ||
obj, err := nodeFeatureRuleFromFile(filepath.Join(packagePath, "..", "data", filename)) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = cli.NfdV1alpha1().NodeFeatureRules().Create(context.TODO(), obj, metav1.CreateOptions{}) | ||
return err | ||
} | ||
|
||
func apiObjFromFile(path string, decoder apiruntime.Decoder) (apiruntime.Object, error) { | ||
data, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
obj, _, err := decoder.Decode(data, nil, nil) | ||
return obj, err | ||
} | ||
|
||
// crdFromFile creates a CustomResourceDefinition API object from a file. | ||
func crdFromFile(path string) (*apiextensionsv1.CustomResourceDefinition, error) { | ||
obj, err := apiObjFromFile(path, scheme.Codecs.UniversalDeserializer()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
crd, ok := obj.(*apiextensionsv1.CustomResourceDefinition) | ||
if !ok { | ||
return nil, fmt.Errorf("unexpected type %t when reading %q", obj, path) | ||
} | ||
|
||
return crd, nil | ||
} | ||
|
||
func nodeFeatureRuleFromFile(path string) (*nfdv1alpha1.NodeFeatureRule, error) { | ||
obj, err := apiObjFromFile(path, nfdscheme.Codecs.UniversalDeserializer()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
crd, ok := obj.(*nfdv1alpha1.NodeFeatureRule) | ||
if !ok { | ||
return nil, fmt.Errorf("unexpected type %t when reading %q", obj, path) | ||
} | ||
|
||
return crd, nil | ||
} | ||
|
||
func init() { | ||
_, thisFile, _, _ := runtime.Caller(0) | ||
packagePath = filepath.Dir(thisFile) | ||
|
||
// Register k8s scheme to be able to create CRDs | ||
_ = apiextensionsv1.AddToScheme(scheme.Scheme) | ||
} |
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