Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit add565c

Browse files
Morven Caoistio-testing
Morven Cao
authored andcommitted
add e2e test for profile dump. (#230)
1 parent 5763321 commit add565c

11 files changed

+2104
-70
lines changed

cmd/mesh/manifest-generate_test.go

+1-44
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,14 @@
1515
package mesh
1616

1717
import (
18-
"bytes"
19-
"io/ioutil"
20-
"os"
21-
"os/exec"
2218
"path/filepath"
23-
"strings"
2419
"testing"
2520

2621
"istio.io/operator/pkg/object"
2722
)
2823

29-
var (
30-
repoRootDir string
31-
testDataDir string
32-
)
33-
34-
func init() {
35-
wd, err := os.Getwd()
36-
if err != nil {
37-
panic(err)
38-
}
39-
repoRootDir = filepath.Join(wd, "../..")
40-
testDataDir = filepath.Join(wd, "testdata/manifest-generate")
41-
42-
if err := syncCharts(); err != nil {
43-
panic(err)
44-
}
45-
}
46-
47-
func syncCharts() error {
48-
cmd := exec.Command(filepath.Join(repoRootDir, "scripts/run_update_charts.sh"))
49-
return cmd.Run()
50-
}
51-
5224
func TestManifestGenerate(t *testing.T) {
25+
testDataDir = filepath.Join(repoRootDir, "cmd/mesh/testdata/manifest-generate")
5326
tests := []struct {
5427
desc string
5528
diffSelect string
@@ -107,22 +80,6 @@ func TestManifestGenerate(t *testing.T) {
10780
}
10881
}
10982

110-
func runCommand(command string) (string, error) {
111-
var out bytes.Buffer
112-
rootCmd := GetRootCmd(strings.Split(command, " "))
113-
rootCmd.SetOutput(&out)
114-
115-
if err := rootCmd.Execute(); err != nil {
116-
return "", err
117-
}
118-
return out.String(), nil
119-
}
120-
12183
func runManifestGenerate(path string) (string, error) {
12284
return runCommand("manifest generate -f " + path)
12385
}
124-
125-
func readFile(path string) (string, error) {
126-
b, err := ioutil.ReadFile(path)
127-
return string(b), err
128-
}

cmd/mesh/manifest_shared_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2019 Istio Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package mesh
16+
17+
import (
18+
"bytes"
19+
"io/ioutil"
20+
"os"
21+
"os/exec"
22+
"path/filepath"
23+
"strings"
24+
)
25+
26+
var (
27+
repoRootDir string
28+
testDataDir string
29+
)
30+
31+
func init() {
32+
wd, err := os.Getwd()
33+
if err != nil {
34+
panic(err)
35+
}
36+
repoRootDir = filepath.Join(wd, "../..")
37+
38+
if err := syncCharts(); err != nil {
39+
panic(err)
40+
}
41+
}
42+
43+
func runCommand(command string) (string, error) {
44+
var out bytes.Buffer
45+
rootCmd := GetRootCmd(strings.Split(command, " "))
46+
rootCmd.SetOutput(&out)
47+
48+
if err := rootCmd.Execute(); err != nil {
49+
return "", err
50+
}
51+
return out.String(), nil
52+
}
53+
54+
func syncCharts() error {
55+
cmd := exec.Command(filepath.Join(repoRootDir, "scripts/run_update_charts.sh"))
56+
return cmd.Run()
57+
}
58+
59+
func readFile(path string) (string, error) {
60+
b, err := ioutil.ReadFile(path)
61+
return string(b), err
62+
}

cmd/mesh/profile-dump.go

+1-13
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,6 @@ func profileDump(args []string, rootArgs *rootArgs, pdArgs *profileDumpArgs, l *
6969
l.logAndFatal("Cannot specify both profile name and filename flag.")
7070
}
7171

72-
writer, err := getWriter("")
73-
if err != nil {
74-
l.logAndFatal(err.Error())
75-
}
76-
defer func() {
77-
if err := writer.Close(); err != nil {
78-
l.logAndFatal("Did not close output successfully: ", err)
79-
}
80-
}()
81-
8272
profile := ""
8373
if len(args) == 1 {
8474
profile = args[0]
@@ -88,9 +78,7 @@ func profileDump(args []string, rootArgs *rootArgs, pdArgs *profileDumpArgs, l *
8878
l.logAndFatal(err.Error())
8979
}
9080

91-
if _, err := writer.WriteString(y); err != nil {
92-
l.logAndFatal("Could not write values; ", err)
93-
}
81+
l.print(y + "\n")
9482
}
9583

9684
func genProfile(helmValues bool, inFilename, profile, setOverlayYAML, configPath string) (string, error) {

cmd/mesh/profile-dump_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2019 Istio Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package mesh
16+
17+
import (
18+
"path/filepath"
19+
"testing"
20+
21+
"istio.io/operator/pkg/util"
22+
)
23+
24+
func TestProfileDump(t *testing.T) {
25+
testDataDir = filepath.Join(repoRootDir, "cmd/mesh/testdata/profile-dump")
26+
tests := []struct {
27+
desc string
28+
}{
29+
{
30+
desc: "all_off",
31+
},
32+
{
33+
desc: "demo-auth_certmanager_on",
34+
},
35+
{
36+
desc: "sds_policy_off",
37+
},
38+
}
39+
for _, tt := range tests {
40+
t.Run(tt.desc, func(t *testing.T) {
41+
inPath := filepath.Join(testDataDir, "input", tt.desc+".yaml")
42+
outPath := filepath.Join(testDataDir, "output", tt.desc+".yaml")
43+
44+
got, err := runProfileDump(inPath)
45+
if err != nil {
46+
t.Fatal(err)
47+
}
48+
want, err := readFile(outPath)
49+
if err != nil {
50+
t.Fatal(err)
51+
}
52+
if !util.IsYAMLEqual(got, want) {
53+
t.Errorf("profile-dump command(%s): got:\n%s\n\nwant:\n%s\nDiff:\n%s\n", tt.desc, got, want, util.YAMLDiff(got, want))
54+
}
55+
})
56+
}
57+
}
58+
59+
func runProfileDump(path string) (string, error) {
60+
return runCommand("profile dump -f " + path)
61+
}

cmd/mesh/shared.go

-13
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,6 @@ func configLogs(args *rootArgs) error {
4747
return log.Configure(opt)
4848
}
4949

50-
func getWriter(outFilename string) (*os.File, error) {
51-
writer := os.Stdout
52-
if outFilename != "" {
53-
file, err := os.Create(outFilename)
54-
if err != nil {
55-
return nil, err
56-
}
57-
58-
writer = file
59-
}
60-
return writer, nil
61-
}
62-
6350
type logger struct {
6451
logToStdErr bool
6552
stdOut io.Writer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: install.istio.io/v1alpha2
2+
kind: IstioControlPlane
3+
spec:
4+
profile: default
5+
trafficManagement:
6+
enabled: false
7+
policy:
8+
enabled: false
9+
telemetry:
10+
enabled: false
11+
security:
12+
enabled: false
13+
configManagement:
14+
enabled: false
15+
autoInjection:
16+
enabled: false
17+
gateways:
18+
enabled: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: install.istio.io/v1alpha2
2+
kind: IstioControlPlane
3+
spec:
4+
profile: demo-auth
5+
security:
6+
components:
7+
citadel:
8+
enabled: false
9+
certManager:
10+
enabled: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: install.istio.io/v1alpha2
2+
kind: IstioControlPlane
3+
spec:
4+
profile: sds
5+
policy:
6+
enabled: false

0 commit comments

Comments
 (0)