Skip to content

Commit a994d89

Browse files
Nick Lanejustaugustus
authored andcommitted
Incorporated E2E test changes made in CAPA (#264)
1 parent 7bf6590 commit a994d89

File tree

3 files changed

+337
-179
lines changed

3 files changed

+337
-179
lines changed

test/e2e/azure_test.go

Lines changed: 164 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ limitations under the License.
1717
package e2e_test
1818

1919
import (
20-
"bytes"
21-
"flag"
20+
"context"
2221
"fmt"
23-
"io/ioutil"
2422
"time"
2523

2624
. "github.com/onsi/ginkgo"
@@ -29,205 +27,221 @@ import (
2927
"github.com/onsi/gomega/types"
3028

3129
corev1 "k8s.io/api/core/v1"
30+
apierrors "k8s.io/apimachinery/pkg/api/errors"
3231
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33-
"k8s.io/apimachinery/pkg/runtime"
34-
"k8s.io/apimachinery/pkg/runtime/serializer"
32+
apimachinerytypes "k8s.io/apimachinery/pkg/types"
3533
"k8s.io/client-go/kubernetes"
36-
capz "sigs.k8s.io/cluster-api-provider-azure/pkg/apis/azureprovider/v1alpha1"
37-
"sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/actuators"
38-
"sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/actuators/machine"
39-
"sigs.k8s.io/cluster-api-provider-azure/pkg/cloudtest"
40-
"sigs.k8s.io/cluster-api-provider-azure/test/e2e/util/kind"
4134
capi "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
42-
clientset "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset"
43-
)
35+
"sigs.k8s.io/cluster-api/pkg/util"
36+
crclient "sigs.k8s.io/controller-runtime/pkg/client"
4437

45-
const (
46-
kindTimeout = 5 * 60
47-
namespace = "capz-test"
48-
clusterName = "capz-test-cluster"
49-
controlPlaneName = "capz-test-control-plane"
50-
51-
// TODO: Remove defaults once no longer required.
52-
defaultResourceGroup = "capz-test-cluster"
53-
defaultLocation = "eastus2"
38+
capz "sigs.k8s.io/cluster-api-provider-azure/pkg/apis/azureprovider/v1alpha1"
39+
//capa "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1alpha1"
5440
)
5541

56-
var (
57-
//credFile = flag.String("credFile", "", "path to an Azure credentials file")
58-
clusterYAML = flag.String("clusterYAML", "", "path to the YAML for the cluster we're creating")
59-
machineYAML = flag.String("machineYAML", "", "path to the YAML describing the control plane we're creating")
60-
regionFile = flag.String("regionFile", "", "The path to a text file containing the Azure region")
61-
region string
42+
const (
43+
workerClusterK8sVersion = "v1.15.1"
6244
)
6345

64-
func initRegion() error {
65-
data, err := ioutil.ReadFile(*regionFile)
66-
if err != nil {
67-
return fmt.Errorf("error reading Azure region file: %v", err)
68-
}
69-
region = string(bytes.TrimSpace(data))
70-
return nil
71-
}
72-
73-
var _ = Describe("Azure", func() {
46+
var _ = Describe("functional tests", func() {
7447
var (
75-
cluster kind.Cluster
76-
client *clientset.Clientset
48+
namespace string
49+
clusterName string
7750
)
7851
BeforeEach(func() {
79-
fmt.Fprintf(GinkgoWriter, "running in Azure region: %s\n", region)
80-
cluster.Setup()
81-
cfg := cluster.RestConfig()
82-
var err error
83-
client, err = clientset.NewForConfig(cfg)
84-
Expect(err).To(BeNil())
85-
}, kindTimeout)
86-
87-
AfterEach(func() {
88-
cluster.Teardown()
52+
namespace = "test-" + util.RandomString(6)
53+
fmt.Fprintf(GinkgoWriter, "creating namespace %q\n", namespace)
54+
createNamespace(kindCluster.KubeClient(), namespace)
55+
clusterName = "test-" + util.RandomString(6)
8956
})
9057

91-
Describe("control plane node", func() {
92-
It("should be running", func() {
58+
Describe("workload cluster lifecycle", func() {
59+
It("It should be creatable and deletable", func() {
60+
By("Creating a Cluster resource")
61+
fmt.Fprintf(GinkgoWriter, "Creating Cluster named %q\n", clusterName)
62+
Expect(kindClient.Create(context.TODO(), makeCluster(clusterName))).To(BeNil())
9363

94-
//sess := getSession()
95-
//accountID := getAccountID(sess)
64+
fmt.Fprintf(GinkgoWriter, "Ensuring cluster infrastructure is ready\n")
65+
Eventually(
66+
func() (map[string]string, error) {
67+
cluster := &capi.Cluster{}
68+
if err := kindClient.Get(context.TODO(), apimachinerytypes.NamespacedName{Namespace: namespace, Name: clusterName}, cluster); err != nil {
69+
return nil, err
70+
}
71+
return cluster.Annotations, nil
72+
},
73+
10*time.Minute, 15*time.Second,
74+
).Should(HaveKeyWithValue(capz.AnnotationClusterInfrastructureReady, capz.ValueReady))
9675

97-
//createKeyPair(sess)
98-
//createIAMRoles(sess, accountID)
76+
By("Creating the first control plane Machine resource")
77+
machineName := "cp-1"
78+
fmt.Fprintf(GinkgoWriter, "Creating Machine named %q for Cluster %q\n", machineName, clusterName)
79+
Expect(kindClient.Create(context.TODO(), makeMachine(machineName, clusterName, "controlplane", workerClusterK8sVersion))).To(BeNil())
9980

100-
createNamespace(cluster.KubeClient())
81+
fmt.Fprintf(GinkgoWriter, "Ensuring first control plane Machine is ready\n")
82+
Eventually(
83+
func() (*capa.AWSMachineProviderStatus, error) {
84+
machine := &capi.Machine{}
85+
if err := kindClient.Get(context.TODO(), apimachinerytypes.NamespacedName{Namespace: namespace, Name: machineName}, machine); err != nil {
86+
return nil, err
87+
}
88+
if machine.Status.ProviderStatus == nil {
89+
return &capa.AWSMachineProviderStatus{
90+
InstanceState: &capa.InstanceStatePending,
91+
}, nil
92+
}
93+
return capa.MachineStatusFromProviderStatus(machine.Status.ProviderStatus)
94+
},
95+
10*time.Minute, 15*time.Second,
96+
).Should(beHealthy())
10197

102-
By("Creating a cluster")
103-
clusterapi := client.ClusterV1alpha1().Clusters(namespace)
104-
_, err := clusterapi.Create(makeCluster())
105-
Expect(err).To(BeNil())
98+
fmt.Fprintf(GinkgoWriter, "Ensuring first control plane Machine NodeRef is set\n")
99+
Eventually(
100+
func() (*corev1.ObjectReference, error) {
101+
machine := &capi.Machine{}
102+
if err := kindClient.Get(context.TODO(), apimachinerytypes.NamespacedName{Namespace: namespace, Name: machineName}, machine); err != nil {
103+
return nil, err
104+
}
105+
return machine.Status.NodeRef, nil
106106

107-
By("Creating a machine")
108-
machineapi := client.ClusterV1alpha1().Machines(namespace)
109-
_, err = machineapi.Create(makeMachine())
110-
Expect(err).To(BeNil())
107+
},
108+
10*time.Minute, 15*time.Second,
109+
).ShouldNot(BeNil())
111110

111+
fmt.Fprintf(GinkgoWriter, "Ensuring Cluster reports the Control Plane is ready\n")
112112
Eventually(
113-
func() (*capz.AzureMachineProviderStatus, error) {
114-
machine, err := machineapi.Get(controlPlaneName, metav1.GetOptions{})
115-
if err != nil {
113+
func() (map[string]string, error) {
114+
cluster := &capi.Cluster{}
115+
if err := kindClient.Get(context.TODO(), apimachinerytypes.NamespacedName{Namespace: namespace, Name: clusterName}, cluster); err != nil {
116116
return nil, err
117117
}
118-
return capz.MachineStatusFromProviderStatus(machine.Status.ProviderStatus)
118+
return cluster.Annotations, nil
119119
},
120120
10*time.Minute, 15*time.Second,
121-
).Should(beHealthy())
121+
).Should(HaveKeyWithValue(capa.AnnotationControlPlaneReady, capa.ValueReady))
122+
123+
// TODO: Retrieve Cluster kubeconfig
124+
// TODO: Deploy Addons
125+
// TODO: Validate Node Ready
126+
// TODO: Deploy additional Control Plane Nodes
127+
// TODO: Deploy a MachineDeployment
128+
// TODO: Scale MachineDeployment up
129+
// TODO: Scale MachineDeployment down
130+
131+
By("Deleting cluster")
132+
fmt.Fprintf(GinkgoWriter, "Deleting Cluster named %q\n", clusterName)
133+
Expect(kindClient.Delete(context.TODO(), &capi.Cluster{
134+
ObjectMeta: metav1.ObjectMeta{
135+
Namespace: namespace,
136+
Name: clusterName,
137+
},
138+
}, noOptionsDelete())).To(BeNil())
139+
140+
Eventually(
141+
func() *capi.Cluster {
142+
cluster := &capi.Cluster{}
143+
if err := kindClient.Get(context.TODO(), apimachinerytypes.NamespacedName{Namespace: namespace, Name: clusterName}, cluster); err != nil {
144+
if apierrors.IsNotFound(err) {
145+
return nil
146+
}
147+
return &capi.Cluster{}
148+
}
149+
return cluster
150+
},
151+
20*time.Minute, 15*time.Second,
152+
).Should(BeNil())
122153
})
123154
})
124155
})
125156

157+
func noOptionsDelete() crclient.DeleteOptionFunc {
158+
return func(opts *crclient.DeleteOptions) {}
159+
}
160+
126161
func beHealthy() types.GomegaMatcher {
127162
return PointTo(
128163
MatchFields(IgnoreExtras, Fields{
129-
"VMState": PointTo(Equal(capz.VMStateSucceeded)),
164+
"InstanceState": PointTo(Equal(capz.InstanceStateRunning)),
130165
}),
131166
)
132167
}
133168

134-
func makeCluster() *capi.Cluster {
135-
yaml, err := ioutil.ReadFile(*clusterYAML)
136-
Expect(err).To(BeNil())
137-
138-
deserializer := serializer.NewCodecFactory(getScheme()).UniversalDeserializer()
139-
cluster := &capi.Cluster{}
140-
obj, _, err := deserializer.Decode(yaml, nil, cluster)
169+
func makeCluster(name string) *capi.Cluster {
170+
providerSpecValue, err := capz.EncodeClusterSpec(&capz.AzureClusterProviderSpec{
171+
SSHKeyName: keyPairName,
172+
Region: region,
173+
})
141174
Expect(err).To(BeNil())
142-
cluster, ok := obj.(*capi.Cluster)
143-
Expect(ok).To(BeTrue(), "Wanted cluster, got %T", obj)
144175

145-
cluster.ObjectMeta.Name = clusterName
146-
147-
clusterSpec, err := capz.ClusterConfigFromProviderSpec(cluster.Spec.ProviderSpec)
148-
Expect(err).To(BeNil())
149-
clusterSpec.ResourceGroup = defaultResourceGroup
150-
clusterSpec.Location = defaultLocation
151-
cluster.Spec.ProviderSpec.Value, err = capz.EncodeClusterSpec(clusterSpec)
152-
Expect(err).To(BeNil())
176+
cluster := &capi.Cluster{
177+
ObjectMeta: metav1.ObjectMeta{
178+
Name: name,
179+
},
180+
Spec: capi.ClusterSpec{
181+
ClusterNetwork: capi.ClusterNetworkingConfig{
182+
Services: capi.NetworkRanges{
183+
CIDRBlocks: []string{"10.96.0.0/12"},
184+
},
185+
Pods: capi.NetworkRanges{
186+
CIDRBlocks: []string{"192.168.0.0/16"},
187+
},
188+
ServiceDomain: "cluster.local",
189+
},
190+
ProviderSpec: capi.ProviderSpec{
191+
Value: providerSpecValue,
192+
},
193+
},
194+
}
153195

154196
return cluster
155197
}
156198

157-
func makeMachine() *capi.Machine {
158-
yaml, err := ioutil.ReadFile(*machineYAML)
159-
Expect(err).To(BeNil())
160-
161-
deserializer := serializer.NewCodecFactory(getScheme()).UniversalDeserializer()
162-
obj, _, err := deserializer.Decode(yaml, nil, &capi.MachineList{})
163-
Expect(err).To(BeNil())
164-
machineList, ok := obj.(*capi.MachineList)
165-
Expect(ok).To(BeTrue(), "Wanted machine, got %T", obj)
166-
167-
machines := machine.GetControlPlaneMachines(machineList)
168-
Expect(machines).NotTo(BeEmpty())
199+
func makeMachine(name, clusterName, role, k8sVersion string) *capi.Machine {
200+
var instanceRole string
201+
machineVersionInfo := capi.MachineVersionInfo{
202+
Kubelet: k8sVersion,
203+
}
169204

170-
machine := machines[0]
171-
machine.ObjectMeta.Name = controlPlaneName
172-
machine.ObjectMeta.Labels[capi.MachineClusterLabelName] = clusterName
205+
switch role {
206+
case "controlplane":
207+
instanceRole = "control-plane.cluster-api-provider-azure.sigs.k8s.io"
208+
machineVersionInfo.ControlPlane = k8sVersion
209+
case "node":
210+
instanceRole = "nodes.cluster-api-provider-aws.sigs.k8s.io"
211+
}
212+
Expect(instanceRole).ToNot(BeEmpty())
173213

174-
machineSpec, err := actuators.MachineConfigFromProviderSpec(nil, machine.Spec.ProviderSpec, &cloudtest.Log{})
175-
Expect(err).To(BeNil())
176-
machineSpec.Location = defaultLocation
177-
machineSpec.SSHPublicKey = ""
178-
machine.Spec.ProviderSpec.Value, err = capz.EncodeMachineSpec(machineSpec)
214+
providerSpecValue, err := capz.EncodeMachineSpec(&capz.AzureMachineProviderSpec{
215+
// KeyName: keyPairName,
216+
// IAMInstanceProfile: instanceRole,
217+
VMSize: "m5.large", //LOOK UP ACTUAL AZURE VM SIZES
218+
})
179219
Expect(err).To(BeNil())
180220

181-
return machine
182-
}
221+
machine := &capi.Machine{
222+
ObjectMeta: metav1.ObjectMeta{
223+
Name: name,
224+
Labels: map[string]string{
225+
capi.MachineClusterLabelName: clusterName,
226+
"set": role,
227+
},
228+
},
229+
Spec: capi.MachineSpec{
230+
Versions: machineVersionInfo,
231+
ProviderSpec: capi.ProviderSpec{
232+
Value: providerSpecValue,
233+
},
234+
},
235+
}
183236

184-
func getScheme() *runtime.Scheme {
185-
s := runtime.NewScheme()
186-
capi.SchemeBuilder.AddToScheme(s)
187-
capz.SchemeBuilder.AddToScheme(s)
188-
return s
237+
return machine
189238
}
190239

191-
func createNamespace(client kubernetes.Interface) {
240+
func createNamespace(client kubernetes.Interface, namespace string) {
192241
_, err := client.CoreV1().Namespaces().Create(&corev1.Namespace{
193242
ObjectMeta: metav1.ObjectMeta{
194243
Name: namespace,
195244
},
196245
})
197246
Expect(err).To(BeNil())
198247
}
199-
200-
/*
201-
func getAccountID(prov client.ConfigProvider) string {
202-
stsSvc := sts.NewService(azurests.New(prov))
203-
accountID, err := stsSvc.AccountID()
204-
Expect(err).To(BeNil())
205-
return accountID
206-
}
207-
208-
func createIAMRoles(prov client.ConfigProvider, accountID string) {
209-
cfnSvc := cloudformation.NewService(cfn.New(prov))
210-
Expect(
211-
cfnSvc.ReconcileBootstrapStack(stackName, accountID),
212-
).To(Succeed())
213-
}
214-
215-
func createKeyPair(prov client.ConfigProvider) {
216-
ec2c := ec2.New(prov)
217-
_, err := ec2c.CreateKeyPair(&ec2.CreateKeyPairInput{KeyName: azure.String(keyPairName)})
218-
if code, _ := azureerrors.Code(err); code != "InvalidKeyPair.Duplicate" {
219-
Expect(err).To(BeNil())
220-
}
221-
}
222-
*/
223-
224-
/*
225-
func getSession() client.ConfigProvider {
226-
creds := credentials.NewCredentials(&credentials.SharedCredentialsProvider{
227-
Filename: *credFile,
228-
})
229-
sess, err := session.NewSession(azure.NewConfig().WithCredentials(creds).WithRegion(region))
230-
Expect(err).To(BeNil())
231-
return sess
232-
}
233-
*/

0 commit comments

Comments
 (0)