-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgolden_test.go
More file actions
142 lines (122 loc) · 3.97 KB
/
golden_test.go
File metadata and controls
142 lines (122 loc) · 3.97 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
/*
Copyright 2026 OSS Container Tools
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 golden
import (
"bytes"
"flag"
"os"
"path/filepath"
"sort"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/osscontainertools/kaniko/cmd/executor/cmd"
testissuemz195 "github.com/osscontainertools/kaniko/golden/testdata/test_issue_mz195"
testissuemz333 "github.com/osscontainertools/kaniko/golden/testdata/test_issue_mz333"
testissuemz338 "github.com/osscontainertools/kaniko/golden/testdata/test_issue_mz338"
testissuemz480 "github.com/osscontainertools/kaniko/golden/testdata/test_issue_mz480"
testissuemz487 "github.com/osscontainertools/kaniko/golden/testdata/test_issue_mz487"
testunittests "github.com/osscontainertools/kaniko/golden/testdata/test_unittests"
"github.com/osscontainertools/kaniko/golden/types"
"github.com/osscontainertools/kaniko/pkg/config"
"github.com/osscontainertools/kaniko/pkg/executor"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func renderCommand(env map[string]string, args []string) string {
var parts []string
if len(env) > 0 {
keys := make([]string, 0, len(env))
for k := range env {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
parts = append(parts, k+"="+env[k])
}
}
parts = append(parts, strings.Join(args, " "))
return strings.Join(parts, " ")
}
var allTests = map[string][]types.GoldenTests{
"test_issue_mz195": {testissuemz195.Tests},
"test_issue_mz333": {testissuemz333.Tests},
"test_issue_mz338": {testissuemz338.Tests},
"test_issue_mz487": {testissuemz487.Tests},
"test_issue_mz480": {testissuemz480.Tests},
"test_unittests": testunittests.Tests,
}
var update bool
func TestMain(m *testing.M) {
flag.BoolVar(&update, "update", false, "Whether to update expected output instead of testing it")
flag.Parse()
exitCode := m.Run()
os.Exit(exitCode)
}
func TestRun(t *testing.T) {
logrus.SetLevel(logrus.WarnLevel)
for testName, testSuites := range allTests {
t.Run(testName, func(t *testing.T) {
testDir := filepath.Join("testdata", testName)
for _, testSuite := range testSuites {
t.Run(testSuite.Name, func(t *testing.T) {
dockerfilePath := filepath.Join(testDir, testSuite.Dockerfile)
for _, test := range testSuite.Tests {
t.Run(renderCommand(test.Env, test.Args), func(t *testing.T) {
for k, v := range test.Env {
t.Setenv(k, v)
}
opts := config.KanikoOptions{}
exec := &cobra.Command{
Use: "kaniko",
}
cmd.AddKanikoOptionsFlags(exec, &opts)
args := []string{
"--dryrun",
"--dockerfile=" + dockerfilePath,
}
err := exec.ParseFlags(append(args, test.Args...))
if err != nil {
t.Fatal(err)
}
cmd.ValidateFlags(&opts)
var buf bytes.Buffer
executor.Out = &buf
_, err = executor.DoBuild(&opts)
if err != nil {
t.Error(err)
}
planPath := filepath.Join(testDir, "plans", test.Plan)
if update {
err = os.WriteFile(planPath, buf.Bytes(), 0644)
if err != nil {
t.Fatal(err)
}
} else {
output := strings.Trim(buf.String(), "\n")
expectedPlan, err := os.ReadFile(planPath)
if err != nil {
t.Fatal(err)
}
expected := strings.Trim(string(expectedPlan), "\n")
if diff := cmp.Diff(expected, output); diff != "" {
t.Errorf("plan mismatch (-expected +got):\n%s", diff)
}
}
})
}
})
}
})
}
}