forked from cert-manager/webhook-example
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_test.go
99 lines (82 loc) · 2.34 KB
/
main_test.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
package main
import (
"encoding/json"
"math/rand"
"os"
"testing"
"time"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
"github.com/cert-manager/cert-manager/test/acme"
)
var (
domain = os.Getenv("TEST_DOMAIN_NAME")
apiKey = os.Getenv("TEST_SECRET")
configFile = "_test/data/config.json"
secretYamlFilePath = "_test/data/cert-manager-webhook-desec-http-secret.yml"
secretName = "cert-manager-webhook-desec-http-secret"
secretKeyName = "api-key"
)
type SecretYaml struct {
ApiVersion string `yaml:"apiVersion" json:"apiVersion"`
Kind string `yaml:"kind,omitempty" json:"kind,omitempty"`
SecretType string `yaml:"type" json:"type"`
Metadata struct {
Name string `yaml:"name"`
}
Data struct {
ApiKey string `yaml:"api-key"`
}
}
func TestRunsSuite(t *testing.T) {
logger := zap.Must(zap.NewDevelopment())
slogger := logger.Sugar()
secretYaml := SecretYaml{}
secretYaml.ApiVersion = "v1"
secretYaml.Kind = "Secret"
secretYaml.SecretType = "Opaque"
secretYaml.Metadata.Name = secretName
secretYaml.Data.ApiKey = apiKey
secretYamlFile, err := yaml.Marshal(&secretYaml)
if err != nil {
slogger.Error(err)
}
_ = os.WriteFile(secretYamlFilePath, secretYamlFile, 0644)
providerConfig := desechttpDNSProviderConfig{
"https://desec.io/api/v1",
domain,
secretName,
secretKeyName,
}
file, _ := json.MarshalIndent(providerConfig, "", " ")
_ = os.WriteFile(configFile, file, 0644)
// resolvedFQDN must end with a '.'
if domain[len(domain)-1:] != "." {
domain = domain + "."
}
pollTime, _ := time.ParseDuration("15s")
timeOut, _ := time.ParseDuration("5m")
fixture := dns.NewFixture(&desechttpDNSProviderSolver{},
dns.SetDNSName(domain),
dns.SetResolvedZone(domain),
dns.SetResolvedFQDN(GetRandomString(8)+"."+domain),
// Increase the poll interval to 15s
dns.SetPollInterval(pollTime),
// Increase the limit from 2 min to 5 min as we need more time for the propagation of the TXT Record
dns.SetPropagationLimit(timeOut),
dns.SetAllowAmbientCredentials(false),
dns.SetManifestPath("_test/data"),
dns.SetStrict(true),
)
fixture.RunConformance(t)
_ = os.Remove(configFile)
_ = os.Remove(secretYamlFilePath)
}
func GetRandomString(n int) string {
letters := []rune("abcdefghijklmnopqrstuvwxyz")
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}