-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
common.go
115 lines (93 loc) · 4.44 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Copyright 2020 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 e2e
import (
"context"
"fmt"
"path/filepath"
. "github.com/onsi/ginkgo"
"github.com/blang/semver"
"github.com/onsi/gomega/types"
corev1 "k8s.io/api/core/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
"sigs.k8s.io/cluster-api/test/framework"
"sigs.k8s.io/cluster-api/util"
)
// Test suite constants for e2e config variables
const (
KubernetesVersion = "KUBERNETES_VERSION"
CNIPath = "CNI"
CNIResources = "CNI_RESOURCES"
KubernetesVersionUpgradeFrom = "KUBERNETES_VERSION_UPGRADE_FROM"
KubernetesVersionUpgradeTo = "KUBERNETES_VERSION_UPGRADE_TO"
EtcdVersionUpgradeTo = "ETCD_VERSION_UPGRADE_TO"
CoreDNSVersionUpgradeTo = "COREDNS_VERSION_UPGRADE_TO"
)
func Byf(format string, a ...interface{}) {
By(fmt.Sprintf(format, a...))
}
func setupSpecNamespace(ctx context.Context, specName string, clusterProxy framework.ClusterProxy, artifactFolder string) (*corev1.Namespace, context.CancelFunc) {
Byf("Creating a namespace for hosting the %q test spec", specName)
namespace, cancelWatches := framework.CreateNamespaceAndWatchEvents(ctx, framework.CreateNamespaceAndWatchEventsInput{
Creator: clusterProxy.GetClient(),
ClientSet: clusterProxy.GetClientSet(),
Name: fmt.Sprintf("%s-%s", specName, util.RandomString(6)),
LogFolder: filepath.Join(artifactFolder, "clusters", clusterProxy.GetName()),
})
return namespace, cancelWatches
}
func dumpSpecResourcesAndCleanup(ctx context.Context, specName string, clusterProxy framework.ClusterProxy, artifactFolder string, namespace *corev1.Namespace, cancelWatches context.CancelFunc, cluster *clusterv1.Cluster, intervalsGetter func(spec, key string) []interface{}, skipCleanup bool) {
Byf("Dumping logs from the %q workload cluster", cluster.Name)
// Dump all the logs from the workload cluster before deleting them.
clusterProxy.CollectWorkloadClusterLogs(ctx, cluster.Namespace, cluster.Name, filepath.Join(artifactFolder, "clusters", cluster.Name, "machines"))
Byf("Dumping all the Cluster API resources in the %q namespace", namespace.Name)
// Dump all Cluster API related resources to artifacts before deleting them.
framework.DumpAllResources(ctx, framework.DumpAllResourcesInput{
Lister: clusterProxy.GetClient(),
Namespace: namespace.Name,
LogPath: filepath.Join(artifactFolder, "clusters", clusterProxy.GetName(), "resources"),
})
if !skipCleanup {
Byf("Deleting cluster %s/%s", cluster.Namespace, cluster.Name)
// While https://github.com/kubernetes-sigs/cluster-api/issues/2955 is addressed in future iterations, there is a chance
// that cluster variable is not set even if the cluster exists, so we are calling DeleteAllClustersAndWait
// instead of DeleteClusterAndWait
framework.DeleteAllClustersAndWait(ctx, framework.DeleteAllClustersAndWaitInput{
Client: clusterProxy.GetClient(),
Namespace: namespace.Name,
}, intervalsGetter(specName, "wait-delete-cluster")...)
Byf("Deleting namespace used for hosting the %q test spec", specName)
framework.DeleteNamespace(ctx, framework.DeleteNamespaceInput{
Deleter: clusterProxy.GetClient(),
Name: namespace.Name,
})
}
cancelWatches()
}
// HaveValidVersion succeeds if version is a valid semver version
func HaveValidVersion(version string) types.GomegaMatcher {
return &validVersionMatcher{version: version}
}
type validVersionMatcher struct{ version string }
func (m *validVersionMatcher) Match(actual interface{}) (success bool, err error) {
if _, err := semver.ParseTolerant(m.version); err != nil {
return false, err
}
return true, nil
}
func (m *validVersionMatcher) FailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("Expected\n%s\n%s", m.version, " to be a valid version ")
}
func (m *validVersionMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("Expected\n%s\n%s", m.version, " not to be a valid version ")
}