-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from razo7/taint-ut
Validate utils Package and Taint Functionality
- Loading branch information
Showing
12 changed files
with
446 additions
and
35 deletions.
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
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
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
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,36 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
const ( | ||
dummyNode = "dummy-node" | ||
node01 = "worker-0" | ||
) | ||
|
||
var _ = Describe("Utils-nodes", func() { | ||
var node *corev1.Node | ||
Context("FAR CR and Node Names Validity test", func() { | ||
BeforeEach(func() { | ||
node = GetNode("", node01) | ||
Expect(k8sClient.Create(context.Background(), node)).To(Succeed()) | ||
DeferCleanup(k8sClient.Delete, context.Background(), node) | ||
}) | ||
When("FAR CR's name doesn't match to an existing node name", func() { | ||
It("should fail", func() { | ||
Expect(IsNodeNameValid(k8sClient, dummyNode)).To(BeFalse()) | ||
}) | ||
}) | ||
When("FAR's name does match to an existing node name", func() { | ||
It("should succeed", func() { | ||
Expect(IsNodeNameValid(k8sClient, node01)).To(BeTrue()) | ||
}) | ||
}) | ||
}) | ||
}) |
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,99 @@ | ||
/* | ||
Copyright 2023. | ||
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" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"go.uber.org/zap/zapcore" | ||
|
||
"k8s.io/client-go/kubernetes/scheme" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
|
||
"github.com/medik8s/fence-agents-remediation/api/v1alpha1" | ||
) | ||
|
||
// These tests use Ginkgo (BDD-style Go testing framework). Refer to | ||
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo. | ||
|
||
const defaultNamespace = "default" | ||
|
||
var k8sClient client.Client | ||
var k8sManager manager.Manager | ||
var testEnv *envtest.Environment | ||
var ctx context.Context | ||
var cancel context.CancelFunc | ||
|
||
func TestUtils(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Utils Suite") | ||
} | ||
|
||
var _ = BeforeSuite(func() { | ||
opts := zap.Options{ | ||
Development: true, | ||
TimeEncoder: zapcore.RFC3339NanoTimeEncoder, | ||
} | ||
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseFlagOptions(&opts))) | ||
|
||
By("bootstrapping test environment") | ||
testEnv = &envtest.Environment{ | ||
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, | ||
ErrorIfCRDPathMissing: true, | ||
} | ||
|
||
cfg, err := testEnv.Start() | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(cfg).NotTo(BeNil()) | ||
|
||
err = v1alpha1.AddToScheme(scheme.Scheme) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
//+kubebuilder:scaffold:scheme | ||
|
||
k8sManager, err = ctrl.NewManager(cfg, ctrl.Options{Scheme: scheme.Scheme}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(k8sClient).NotTo(BeNil()) | ||
|
||
os.Setenv("DEPLOYMENT_NAMESPACE", defaultNamespace) | ||
|
||
go func() { | ||
// https://github.com/kubernetes-sigs/controller-runtime/issues/1571 | ||
ctx, cancel = context.WithCancel(ctrl.SetupSignalHandler()) | ||
err := k8sManager.Start(ctx) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}() | ||
}) | ||
|
||
var _ = AfterSuite(func() { | ||
By("tearing down the test environment") | ||
cancel() | ||
err := testEnv.Stop() | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) |
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,58 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
|
||
medik8sLabels "github.com/medik8s/common/pkg/labels" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const node0 = "worker-0" | ||
|
||
var _ = Describe("Utils-taint", func() { | ||
nodeKey := client.ObjectKey{Name: node0} | ||
controlPlaneRoleTaint := getControlPlaneRoleTaint() | ||
farNoExecuteTaint := CreateFARNoExecuteTaint() | ||
Context("Taint functioninality test", func() { | ||
// Check functionaility with control-plane node which already has a taint | ||
BeforeEach(func() { | ||
node := GetNode("control-plane", node0) | ||
Expect(k8sClient.Create(context.Background(), node)).To(Succeed()) | ||
DeferCleanup(k8sClient.Delete, context.Background(), node) | ||
}) | ||
When("Control-plane node only has 1 the control-plane-role taint", func() { | ||
It("should add and delete medik8s NoSchedule taint and keep other existing taints", func() { | ||
By("having one control-plane-role taint") | ||
taintedNode := &corev1.Node{} | ||
Expect(k8sClient.Get(context.Background(), nodeKey, taintedNode)).To(Succeed()) | ||
// control-plane-role taint already exist by GetNode | ||
By("adding medik8s NoSchedule taint") | ||
Expect(AppendTaint(k8sClient, node0)).To(Succeed()) | ||
Expect(k8sClient.Get(context.Background(), nodeKey, taintedNode)).To(Succeed()) | ||
Expect(TaintExists(taintedNode.Spec.Taints, &controlPlaneRoleTaint)).To(BeTrue()) | ||
Expect(TaintExists(taintedNode.Spec.Taints, &farNoExecuteTaint)).To(BeTrue()) | ||
By("removing medik8s NoSchedule taint") | ||
// We want to see that RemoveTaint only remove the taint it receives | ||
Expect(RemoveTaint(k8sClient, node0)).To(Succeed()) | ||
Expect(k8sClient.Get(context.Background(), nodeKey, taintedNode)).To(Succeed()) | ||
Expect(TaintExists(taintedNode.Spec.Taints, &controlPlaneRoleTaint)).To(BeTrue()) | ||
Expect(TaintExists(taintedNode.Spec.Taints, &farNoExecuteTaint)).To(BeFalse()) | ||
|
||
// there is a not-ready taint now as well, so there will be 2 taints... skip count tests | ||
// Expect(len(taintedNode.Spec.Taints)).To(Equal(1)) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
// getControlPlaneRoleTaint returns a control-plane-role taint | ||
func getControlPlaneRoleTaint() corev1.Taint { | ||
return corev1.Taint{ | ||
Key: medik8sLabels.ControlPlaneRole, | ||
Effect: corev1.TaintEffectNoExecute, | ||
} | ||
} |
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
Oops, something went wrong.