This repository has been archived by the owner on Dec 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_test.go
95 lines (71 loc) · 2.18 KB
/
app_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
package pk8s
import (
"fmt"
"io"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/alexferl/pk8s/k8s"
)
func TestNew(t *testing.T) {
app := New()
assert.Equal(t, DefaultConfig.OutputPath, app.config.OutputPath)
assert.Equal(t, DefaultConfig.OutputFileExtension, app.config.OutputFileExtension)
assert.Empty(t, app.stacks)
}
func TestNewWithConfig(t *testing.T) {
config := AppConfig{OutputPath: k8s.String("custom"), OutputFileExtension: k8s.String(".k8s.yaml")}
app := NewWithConfig(config)
assert.Equal(t, "custom", *app.config.OutputPath)
assert.Equal(t, ".k8s.yaml", *app.config.OutputFileExtension)
assert.Empty(t, app.stacks)
}
func TestNewWithConfig_Empty(t *testing.T) {
config := AppConfig{}
app := NewWithConfig(config)
assert.Empty(t, app.stacks)
}
var deploymentYAML = "---\napiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: test-chart\n"
func TestApp_Export(t *testing.T) {
tempDir := t.TempDir()
config := AppConfig{OutputPath: &tempDir}
app := NewWithConfig(config)
stack := NewStack(app, "test-stack")
chart := NewChart(stack, "test-chart", &ChartParams{DisableNameHashes: true})
filePath := fmt.Sprintf("%s/%s/%s.yaml", tempDir, stack.Name, chart.Name)
deployment := &k8s.DeploymentV1{}
chart.Append(deployment)
app.Export()
assert.DirExists(t, tempDir)
assert.FileExists(t, filePath)
data, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("failed reading file: %v", err)
}
assert.Equal(t, deploymentYAML, string(data))
}
func TestApp_Print(t *testing.T) {
app := New()
stack := NewStack(app, "test-stack")
chart := NewChart(stack, "test-chart", &ChartParams{DisableNameHashes: true})
deployment := &k8s.DeploymentV1{}
chart.Append(deployment)
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
app.Print()
w.Close()
os.Stdout = old
var buf strings.Builder
_, _ = io.Copy(&buf, r)
assert.Equal(t, deploymentYAML, buf.String())
}
func TestApp_String(t *testing.T) {
app := New()
stack := NewStack(app, "test-stack")
chart := NewChart(stack, "test-chart", &ChartParams{DisableNameHashes: true})
deployment := &k8s.DeploymentV1{}
chart.Append(deployment)
assert.Equal(t, deploymentYAML, app.String())
}