Skip to content

Commit

Permalink
test: test agent tolerations
Browse files Browse the repository at this point in the history
  • Loading branch information
rajiteh committed Jan 26, 2023
1 parent 90c721a commit e8a7f9b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
89 changes: 88 additions & 1 deletion pkg/agent/manifest_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package agent

import "testing"
import (
"testing"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

func TestImageResolve(t *testing.T) {
tests := []struct {
Expand All @@ -21,3 +29,82 @@ func TestImageResolve(t *testing.T) {
}
}
}

func getDeploymentFromManifests(namespace string, scope string, opts ManifestOptions) *appsv1.Deployment {
objects := Manifest(namespace, scope, opts)
for _, obj := range objects {
dep, ok := obj.(*appsv1.Deployment)
if ok {
return dep
}
}
return nil
}

func TestManifestAdditionalTolerations(t *testing.T) {
const namespace = "fleet-system"
const scope = "test-scope"
baseOpts := ManifestOptions{
AgentEnvVars: []corev1.EnvVar{},
AgentImage: "rancher/fleet:1.2.3",
AgentImagePullPolicy: "Always",
AgentTolerations: []corev1.Toleration{},
CheckinInterval: "1s",
Generation: "100",
PrivateRepoURL: "private.rancher.com:5000",
SystemDefaultRegistry: "default.rancher.com",
}

// these tolerations should exist regardless of what user sent
baseTolerations := []corev1.Toleration{
{Key: "cattle.io/os", Operator: "Equal", Value: "linux", Effect: "NoSchedule"},
{Key: "node.cloudprovider.kubernetes.io/uninitialized", Operator: "Equal", Value: "true", Effect: "NoSchedule"},
}

less := func(a, b corev1.Toleration) bool { return a.Key < b.Key }
cmpOpt := cmpopts.SortSlices(less)

for _, testCase := range []struct {
name string
getOpts func() ManifestOptions
expectedTolerations []corev1.Toleration
}{
{
name: "BaseOpts",
getOpts: func() ManifestOptions {
return baseOpts
},
expectedTolerations: baseTolerations,
},
{
name: "ExtraToleration",
getOpts: func() ManifestOptions {
withTolerationsOpts := baseOpts
withTolerationsOpts.AgentTolerations = []corev1.Toleration{
{
Key: "fleet-agent",
Operator: "Equals",
Value: "true",
Effect: "NoSchedule",
},
}
return withTolerationsOpts
},
expectedTolerations: append(baseTolerations,
corev1.Toleration{Key: "fleet-agent", Operator: "Equals", Value: "true", Effect: "NoSchedule"},
),
},
} {
t.Run(testCase.name, func(t *testing.T) {

agentDeployment := getDeploymentFromManifests(namespace, scope, testCase.getOpts())
if agentDeployment == nil {
t.Fatal("there were no deployments returned from the manifests")
}

if !cmp.Equal(agentDeployment.Spec.Template.Spec.Tolerations, testCase.expectedTolerations, cmpOpt) {
t.Fatalf("tolerations were not as expected: %v", agentDeployment.Spec.Template.Spec.Tolerations)
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/controllers/cluster/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,10 @@ func (i *importHandler) importCluster(cluster *fleet.Cluster, status fleet.Clust
},
ManifestOptions: agent.ManifestOptions{
AgentEnvVars: cluster.Spec.AgentEnvVars,
AgentTolerations: cluster.Spec.AgentTolerations,
CheckinInterval: cfg.AgentCheckinInterval.Duration.String(),
Generation: string(cluster.UID) + "-" + strconv.FormatInt(cluster.Generation, 10),
PrivateRepoURL: cluster.Spec.PrivateRepoURL,
AgentTolerations: cluster.Spec.AgentTolerations,
},
})
if err != nil {
Expand Down

0 comments on commit e8a7f9b

Please sign in to comment.