forked from openshift/osde2e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.go
More file actions
186 lines (154 loc) · 5.34 KB
/
setup.go
File metadata and controls
186 lines (154 loc) · 5.34 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package common
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/client-go/tools/clientcmd"
"github.com/openshift/osde2e/pkg/config"
"github.com/openshift/osde2e/pkg/events"
"github.com/openshift/osde2e/pkg/load"
"github.com/openshift/osde2e/pkg/metadata"
"github.com/openshift/osde2e/pkg/osd"
"github.com/openshift/osde2e/pkg/state"
)
func init() {
testing.Init()
// Load config and initial state
if err := load.IntoObject(config.Instance); err != nil {
panic(fmt.Errorf("error loading config: %v", err))
}
if err := load.IntoObject(state.Instance); err != nil {
panic(fmt.Errorf("error loading initial state: %v", err))
}
}
// Setup cluster before testing begins.
var _ = ginkgo.SynchronizedBeforeSuite(func() []byte {
defer ginkgo.GinkgoRecover()
cfg := config.Instance
state := state.Instance
err := setupCluster()
events.HandleErrorWithEvents(err, events.InstallSuccessful, events.InstallFailed).ShouldNot(HaveOccurred(), "failed to setup cluster for testing")
if len(cfg.Addons.IDs) > 0 {
err = installAddons()
events.HandleErrorWithEvents(err, events.InstallAddonsSuccessful, events.InstallAddonsFailed).ShouldNot(HaveOccurred(), "failed while installing addons")
}
if len(state.Kubeconfig.Contents) == 0 {
// Give the cluster some breathing room.
log.Println("OSD cluster installed. Sleeping for 600s.")
time.Sleep(600 * time.Second)
}
return []byte{}
}, func(data []byte) {
// only needs to run once
})
// Collect logs after each test
var _ = ginkgo.AfterSuite(func() {
log.Printf("Getting logs for cluster '%s'...", state.Instance.Cluster.ID)
getLogs()
})
var _ = ginkgo.JustAfterEach(getLogs)
func getLogs() {
defer ginkgo.GinkgoRecover()
state := state.Instance
if OSD == nil {
log.Println("OSD was not configured. Skipping log collection...")
} else if state.Cluster.ID == "" {
log.Println("CLUSTER_ID is not set, likely due to a setup failure. Skipping log collection...")
} else {
logs, err := OSD.FullLogs(state.Cluster.ID)
Expect(err).NotTo(HaveOccurred(), "failed to collect cluster logs")
writeLogs(logs)
}
}
// setupCluster brings up a cluster, waits for it to be ready, then returns it's name.
func setupCluster() (err error) {
cfg := config.Instance
state := state.Instance
// if TEST_KUBECONFIG has been set, skip configuring OCM
if len(state.Kubeconfig.Contents) > 0 || len(cfg.Kubeconfig.Path) > 0 {
return useKubeconfig()
}
// create a new cluster if no ID is specified
if state.Cluster.ID == "" {
if state.Cluster.Name == "" {
state.Cluster.Name = clusterName()
}
if state.Cluster.ID, err = OSD.LaunchCluster(); err != nil {
return fmt.Errorf("could not launch cluster: %v", err)
}
} else {
log.Printf("CLUSTER_ID of '%s' was provided, skipping cluster creation and using it instead", state.Cluster.ID)
if state.Cluster.Name == "" {
cluster, err := OSD.GetCluster(state.Cluster.ID)
if err != nil {
return fmt.Errorf("could not retrieve cluster information from OCM: %v", err)
}
if cluster.Name() == "" {
return fmt.Errorf("cluster name from OCM is empty, and this shouldn't be possible")
}
state.Cluster.Name = cluster.Name()
log.Printf("CLUSTER_NAME not provided, retrieved %s from OCM.", state.Cluster.Name)
}
}
metadata.Instance.SetClusterName(state.Cluster.Name)
metadata.Instance.SetClusterID(state.Cluster.ID)
if err = OSD.WaitForClusterReady(); err != nil {
return fmt.Errorf("failed waiting for cluster ready: %v", err)
}
if state.Kubeconfig.Contents, err = OSD.ClusterKubeconfig(state.Cluster.ID); err != nil {
return fmt.Errorf("could not get kubeconfig for cluster: %v", err)
}
return nil
}
// installAddons installs addons onto the cluster
func installAddons() (err error) {
num, err := OSD.InstallAddons(config.Instance.Addons.IDs)
if err != nil {
return fmt.Errorf("could not install addons: %s", err.Error())
}
if num > 0 {
if err = OSD.WaitForClusterReady(); err != nil {
return fmt.Errorf("failed waiting for cluster ready: %v", err)
}
}
return nil
}
// useKubeconfig reads the path provided for a TEST_KUBECONFIG and uses it for testing.
func useKubeconfig() (err error) {
cfg := config.Instance
state := state.Instance
_, err = clientcmd.RESTConfigFromKubeConfig(state.Kubeconfig.Contents)
if err != nil {
log.Println("Not an existing Kubeconfig, attempting to read file instead...")
} else {
log.Println("Existing valid kubeconfig!")
return nil
}
state.Kubeconfig.Contents, err = ioutil.ReadFile(cfg.Kubeconfig.Path)
if err != nil {
return fmt.Errorf("failed reading '%s' which has been set as the TEST_KUBECONFIG: %v", cfg.Kubeconfig.Path, err)
}
log.Printf("Using a set TEST_KUBECONFIG of '%s' for Origin API calls.", cfg.Kubeconfig.Path)
return nil
}
// cluster name format must be short enough to support all versions
func clusterName() string {
vers := strings.TrimPrefix(state.Instance.Cluster.Version, osd.VersionPrefix)
safeVersion := strings.Replace(vers, ".", "-", -1)
return "ci-cluster-" + safeVersion + "-" + config.Instance.Suffix
}
func writeLogs(m map[string][]byte) {
for k, v := range m {
name := k + "-log.txt"
filePath := filepath.Join(config.Instance.ReportDir, name)
err := ioutil.WriteFile(filePath, v, os.ModePerm)
Expect(err).NotTo(HaveOccurred(), "failed to write log '%s'", filePath)
}
}