-
Notifications
You must be signed in to change notification settings - Fork 311
/
okteto.go
157 lines (140 loc) · 3.85 KB
/
okteto.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
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
// Copyright 2023 The Okteto 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 integration
import (
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/okteto/okteto/pkg/config"
"github.com/okteto/okteto/pkg/k8s/kubeconfig"
"github.com/okteto/okteto/pkg/model"
"github.com/okteto/okteto/pkg/okteto"
)
// GetOktetoPath returns the okteto path used to run tests
// set OKTETO_PATH to the bin you want to test otherwise it'll
// use the one you have in your path
func GetOktetoPath() (string, error) {
oktetoPath, ok := os.LookupEnv(model.OktetoPathEnvVar)
if !ok {
oktetoPath = "/usr/local/bin/okteto"
}
log.Printf("using %s", oktetoPath)
var err error
oktetoPath, err = filepath.Abs(oktetoPath)
if err != nil {
return "", err
}
output, err := RunOktetoVersion(oktetoPath)
if err != nil {
return "", fmt.Errorf("okteto version failed: %s - %w", output, err)
}
log.Println(output)
return oktetoPath, nil
}
// GetToken returns the token used to run tests
func GetToken() string {
var token string
if v := os.Getenv(model.OktetoTokenEnvVar); v != "" {
token = v
} else {
token = okteto.GetContext().Token
}
return token
}
// RunOktetoVersion runs okteto version given an oktetoPath
func RunOktetoVersion(oktetoPath string) (string, error) {
cmd := exec.Command(oktetoPath, "version")
cmd.Env = os.Environ()
o, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("okteto version failed: %s - %w", string(o), err)
}
return string(o), nil
}
// GetTestNamespace returns the name for a namespace
func GetTestNamespace(prefix, user string) string {
os := runtime.GOOS
if os == "windows" {
os = "win"
} else {
os = os[:3]
}
namespace := fmt.Sprintf("%s-%s-%d-%s", prefix, os, time.Now().Unix(), user)
return strings.ToLower(namespace)
}
// GetCurrentNamespace returns the current namespace of the kubeconfig path
func GetCurrentNamespace() string {
return kubeconfig.CurrentNamespace(config.GetKubeconfigPath())
}
// GetContentFromURL returns the content of the url
func GetContentFromURL(url string, timeout time.Duration) string {
ticker := time.NewTicker(1 * time.Second)
to := time.NewTicker(timeout)
retry := 0
for {
retry++
select {
case <-to.C:
log.Printf("endpoint %s didn't respond", url)
return ""
case <-ticker.C:
r, err := http.Get(url)
if err != nil {
if retry%10 == 0 {
log.Printf("called %s, got %s, retrying", url, err)
}
continue
}
defer r.Body.Close()
if r.StatusCode != http.StatusOK {
if retry%10 == 0 {
log.Printf("called %s, got status %d, retrying", url, r.StatusCode)
}
continue
}
body, err := io.ReadAll(r.Body)
if err != nil {
log.Printf("could not read body: %s", err)
return ""
}
content := strings.TrimSpace(string(body))
if content == "" {
if retry%10 == 0 {
log.Printf("called %s, got empty content", url)
}
continue
}
return content
}
}
}
// SkipIfWindows skips a tests if is on a windows environment
func SkipIfWindows(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping testing in windows CI environment")
}
}
// SkipIfNotOktetoCluster skips a tests if is not on an okteto cluster
func SkipIfNotOktetoCluster(t *testing.T) {
if !okteto.GetContext().IsOkteto {
t.Skip("Skipping because is not on an okteto cluster")
}
}