-
Notifications
You must be signed in to change notification settings - Fork 88
/
generate.go
161 lines (138 loc) · 4.47 KB
/
generate.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
158
159
160
161
package replicated
import (
"fmt"
"io/ioutil"
"os"
"path"
"github.com/mholt/archiver/v3"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/pull"
)
// GenerateTest will create a new replicated app fixture for integration tests
func GenerateTest(name string, rawArchivePath string) error {
namespace := "test_ns"
integrationLicenseData := fmt.Sprintf(`apiVersion: kots.io/v1beta1
kind: License
metadata:
name: integration
spec:
licenseID: %s
appSlug: %s
endpoint: http://localhost:3001
signature: IA==
`, name, name)
replicatedAppArchive, err := generateReplicatedAppArchive(rawArchivePath)
if err != nil {
return errors.Wrap(err, "failed to generate replicated app archive")
}
expectedFilesystem, err := generateExpectedFilesystem(namespace, rawArchivePath)
if err != nil {
return errors.Wrap(err, "failed to generate expected filesystem")
}
testRoot := path.Join("integration", "replicated", "tests", name)
if err := os.MkdirAll(testRoot, 0755); err != nil {
return errors.Wrap(err, "failed to create test root")
}
err = ioutil.WriteFile(path.Join(testRoot, "license.yaml"), []byte(integrationLicenseData), 0644)
if err != nil {
return errors.Wrap(err, "failed to write license")
}
err = ioutil.WriteFile(path.Join(testRoot, "archive.tar.gz"), replicatedAppArchive, 0644)
if err != nil {
return errors.Wrap(err, "failed to write archive")
}
expectedRoot := path.Join(testRoot, "expected")
if err := os.MkdirAll(expectedRoot, 0755); err != nil {
return errors.Wrap(err, "failed to create expected root")
}
tarGz := archiver.TarGz{
Tar: &archiver.Tar{
ImplicitTopLevelFolder: false,
},
}
tempExpectedFile, err := ioutil.TempDir("", "kotsintegration")
if err != nil {
return errors.Wrap(err, "failed to create temp file")
}
defer os.RemoveAll(tempExpectedFile)
err = ioutil.WriteFile(path.Join(tempExpectedFile, "archive.tar.gz"), expectedFilesystem, 0644)
if err != nil {
return errors.Wrap(err, "failed to write to temp file")
}
if err := tarGz.Unarchive(path.Join(tempExpectedFile, "archive.tar.gz"), expectedRoot); err != nil {
return errors.Wrap(err, "failed to unarchive expected")
}
return nil
}
func generateReplicatedAppArchive(rawArchivePath string) ([]byte, error) {
tarGz := archiver.TarGz{
Tar: &archiver.Tar{
ImplicitTopLevelFolder: true,
},
}
archiveDir, err := ioutil.TempDir("", "kots")
if err != nil {
return nil, errors.Wrap(err, "failed to create temp dir")
}
defer os.RemoveAll(archiveDir)
archiveFile := path.Join(archiveDir, "archive.tar.gz")
if err := tarGz.Archive([]string{rawArchivePath}, archiveFile); err != nil {
return nil, errors.Wrap(err, "failed to create archive")
}
b, err := ioutil.ReadFile(archiveFile)
if err != nil {
return nil, errors.Wrap(err, "failed to read archive file")
}
return b, nil
}
// generateExpectedFilesystem uses kots to simularae a pull from a local source
// and then creates a tar from what the output is. because of this, it's expected
// that kots is working as expected when creating a new test
func generateExpectedFilesystem(namespace, rawArchivePath string) ([]byte, error) {
tmpRootDir, err := ioutil.TempDir("", "kots")
if err != nil {
return nil, errors.Wrap(err, "failed to create temp dir")
}
defer os.RemoveAll(tmpRootDir)
pullOptions := pull.PullOptions{
RootDir: tmpRootDir,
LocalPath: rawArchivePath,
Namespace: namespace,
ExcludeKotsKinds: true,
ExcludeAdminConsole: true,
CreateAppDir: false,
Silent: true,
}
_, err = pull.Pull("replicated://integration", pullOptions)
if err != nil {
return nil, errors.Wrap(err, "failed to pull")
}
tarGz := archiver.TarGz{
Tar: &archiver.Tar{
ImplicitTopLevelFolder: true,
},
}
archiveDir, err := ioutil.TempDir("", "kots")
if err != nil {
return nil, errors.Wrap(err, "failed to create temp dir")
}
defer os.RemoveAll(archiveDir)
archiveFile := path.Join(archiveDir, "expected.tar.gz")
paths := []string{
path.Join(tmpRootDir, "upstream"),
path.Join(tmpRootDir, "base"),
path.Join(tmpRootDir, "overlays"),
}
skippedFilesPath := path.Join(tmpRootDir, "skippedFiles")
if _, err := os.Stat(skippedFilesPath); err == nil {
paths = append(paths, skippedFilesPath)
}
if err := tarGz.Archive(paths, archiveFile); err != nil {
return nil, errors.Wrap(err, "failed to create archive")
}
b, err := ioutil.ReadFile(archiveFile)
if err != nil {
return nil, errors.Wrap(err, "failed to read archive file")
}
return b, nil
}