-
Notifications
You must be signed in to change notification settings - Fork 20
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 #396 from Danil-Grigorev/test-capi-provider-operat…
…ions 🌱 Add tests for capi provider edit operations
- Loading branch information
Showing
16 changed files
with
294 additions
and
134 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
Copyright © 2023 - 2024 SUSE LLC | ||
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 controllers | ||
|
||
import ( | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
turtlesv1 "github.com/rancher-sandbox/rancher-turtles/api/v1alpha1" | ||
"github.com/rancher-sandbox/rancher-turtles/internal/sync" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
. "sigs.k8s.io/controller-runtime/pkg/envtest/komega" | ||
) | ||
|
||
func objectFromKey(key client.ObjectKey, obj client.Object) client.Object { | ||
obj.SetName(key.Name) | ||
obj.SetNamespace(key.Namespace) | ||
return obj | ||
} | ||
|
||
var _ = Describe("Reconcile CAPIProvider", func() { | ||
var ( | ||
ns *corev1.Namespace | ||
) | ||
|
||
BeforeEach(func() { | ||
var err error | ||
|
||
ns, err = testEnv.CreateNamespace(ctx, "capiprovider") | ||
Expect(err).ToNot(HaveOccurred()) | ||
_ = ns | ||
|
||
r := &CAPIProviderReconciler{ | ||
Client: testEnv.GetClient(), | ||
Scheme: testEnv.GetScheme(), | ||
} | ||
|
||
Expect(r.SetupWithManager(ctx, testEnv.Manager)).ToNot(HaveOccurred()) | ||
}) | ||
|
||
It("Should create infrastructure docker provider and secret", func() { | ||
provider := &turtlesv1.CAPIProvider{ObjectMeta: metav1.ObjectMeta{ | ||
Name: "docker", | ||
Namespace: ns.Name, | ||
}, Spec: turtlesv1.CAPIProviderSpec{ | ||
Type: turtlesv1.Infrastructure, | ||
}} | ||
Expect(cl.Create(ctx, provider)).ToNot(HaveOccurred()) | ||
|
||
dockerProvider := objectFromKey(client.ObjectKeyFromObject(provider), &operatorv1.InfrastructureProvider{}) | ||
dockerSecret := objectFromKey(client.ObjectKeyFromObject(provider), &corev1.Secret{}) | ||
Eventually(Object(dockerProvider)).ShouldNot(BeNil()) | ||
Eventually(Object(dockerSecret)).Should(HaveField("Data", Equal(map[string][]byte{ | ||
"CLUSTER_TOPOLOGY": []byte("true"), | ||
"EXP_CLUSTER_RESOURCE_SET": []byte("true"), | ||
"EXP_MACHINE_POOL": []byte("true"), | ||
}))) | ||
}) | ||
|
||
It("Should update infrastructure docker provider version and secret content from CAPI Provider change", func() { | ||
provider := &turtlesv1.CAPIProvider{ObjectMeta: metav1.ObjectMeta{ | ||
Name: "docker", | ||
Namespace: ns.Name, | ||
}, Spec: turtlesv1.CAPIProviderSpec{ | ||
Type: turtlesv1.Infrastructure, | ||
}} | ||
Expect(cl.Create(ctx, provider)).ToNot(HaveOccurred()) | ||
|
||
dockerProvider := objectFromKey(client.ObjectKeyFromObject(provider), &operatorv1.InfrastructureProvider{}) | ||
dockerSecret := objectFromKey(client.ObjectKeyFromObject(provider), &corev1.Secret{}) | ||
Eventually(Object(dockerProvider)).ShouldNot(BeNil()) | ||
Eventually(Object(dockerSecret)).ShouldNot(BeNil()) | ||
|
||
Eventually(Update(provider, func() { | ||
provider.Spec.Version = "v1.2.3" | ||
provider.Spec.Variables = map[string]string{ | ||
"other": "var", | ||
} | ||
})).Should(Succeed()) | ||
|
||
Eventually(Object(dockerProvider)).Should(HaveField("Spec.Version", Equal("v1.2.3"))) | ||
Eventually(Object(dockerSecret)).Should(HaveField("Data", Equal(map[string][]byte{ | ||
"other": []byte("var"), | ||
"CLUSTER_TOPOLOGY": []byte("true"), | ||
"EXP_CLUSTER_RESOURCE_SET": []byte("true"), | ||
"EXP_MACHINE_POOL": []byte("true"), | ||
}))) | ||
}) | ||
|
||
It("Should update infrastructure digitalocean provider features and convert rancher credentials secret on CAPI Provider change", func() { | ||
provider := &turtlesv1.CAPIProvider{ObjectMeta: metav1.ObjectMeta{ | ||
Name: "digitalocean", | ||
Namespace: ns.Name, | ||
}, Spec: turtlesv1.CAPIProviderSpec{ | ||
Type: turtlesv1.Infrastructure, | ||
}} | ||
Expect(cl.Create(ctx, provider)).ToNot(HaveOccurred()) | ||
|
||
doSecret := objectFromKey(client.ObjectKeyFromObject(provider), &corev1.Secret{}) | ||
Eventually(testEnv.GetAs(provider, &operatorv1.InfrastructureProvider{})).ShouldNot(BeNil()) | ||
Eventually(testEnv.GetAs(provider, doSecret)).ShouldNot(BeNil()) | ||
|
||
Expect(cl.Create(ctx, &corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: sync.RancherCredentialsNamespace, | ||
}, | ||
})).To(Succeed()) | ||
|
||
Expect(cl.Create(ctx, &corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "cc", | ||
GenerateName: "cc-", | ||
Namespace: sync.RancherCredentialsNamespace, | ||
Annotations: map[string]string{ | ||
sync.NameAnnotation: "test-rancher-secret", | ||
sync.DriverNameAnnotation: "digitalocean", | ||
}, | ||
}, | ||
StringData: map[string]string{ | ||
"digitaloceancredentialConfig-accessToken": "token", | ||
}, | ||
})).To(Succeed()) | ||
|
||
Eventually(Update(provider, func() { | ||
provider.Spec.Features = &turtlesv1.Features{MachinePool: true} | ||
provider.Spec.Credentials = &turtlesv1.Credentials{ | ||
RancherCloudCredential: "test-rancher-secret", | ||
} | ||
})).Should(Succeed()) | ||
|
||
Eventually(Object(doSecret), 10*time.Second).Should(HaveField("Data", Equal(map[string][]byte{ | ||
"EXP_MACHINE_POOL": []byte("true"), | ||
"CLUSTER_TOPOLOGY": []byte("false"), | ||
"EXP_CLUSTER_RESOURCE_SET": []byte("false"), | ||
"DIGITALOCEAN_ACCESS_TOKEN": []byte("token"), | ||
"DO_B64ENCODED_CREDENTIALS": []byte("dG9rZW4="), | ||
}))) | ||
|
||
}) | ||
}) |
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
Oops, something went wrong.