Skip to content

Commit 831dae7

Browse files
authored
Merge pull request kubernetes#88185 from vinayakankugoyal/appendandreplace
append_or_replace_prefixed_line in /cluster/gce/gci/configure-helper.…
2 parents 9821d0e + 388ebfe commit 831dae7

File tree

3 files changed

+186
-1
lines changed

3 files changed

+186
-1
lines changed

cluster/gce/gci/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ go_test(
77
srcs = [
88
"apiserver_etcd_test.go",
99
"apiserver_kms_test.go",
10+
"append_or_replace_prefixed_line_test.go",
1011
"audit_policy_test.go",
1112
"configure_helper_test.go",
1213
],
@@ -26,6 +27,7 @@ go_test(
2627
"//staging/src/k8s.io/apiserver/pkg/audit/policy:go_default_library",
2728
"//staging/src/k8s.io/apiserver/pkg/authentication/user:go_default_library",
2829
"//staging/src/k8s.io/apiserver/pkg/authorization/authorizer:go_default_library",
30+
"//vendor/github.com/google/go-cmp/cmp:go_default_library",
2931
"//vendor/github.com/stretchr/testify/assert:go_default_library",
3032
"//vendor/github.com/stretchr/testify/require:go_default_library",
3133
],
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package gci
18+
19+
import (
20+
"fmt"
21+
"io/ioutil"
22+
"os"
23+
"os/exec"
24+
"testing"
25+
26+
"github.com/google/go-cmp/cmp"
27+
)
28+
29+
func TestAppendOrReplacePrefix(t *testing.T) {
30+
testCases := []struct {
31+
desc string
32+
prefix string
33+
suffix string
34+
initialFileContents string
35+
want string
36+
}{
37+
{
38+
desc: "simple string and empty file",
39+
prefix: "hello",
40+
suffix: "world",
41+
want: `helloworld
42+
`,
43+
},
44+
{
45+
desc: "simple string and non empty file",
46+
prefix: "hello",
47+
suffix: "world",
48+
initialFileContents: `jelloworld
49+
chelloworld
50+
`,
51+
want: `jelloworld
52+
chelloworld
53+
helloworld
54+
`,
55+
},
56+
{
57+
desc: "simple string and file already contains prefix",
58+
prefix: "hello",
59+
suffix: "world",
60+
initialFileContents: `helloworld
61+
helloworld
62+
jelloworld
63+
chelloworld
64+
`,
65+
want: `jelloworld
66+
chelloworld
67+
helloworld
68+
`,
69+
},
70+
{
71+
desc: "simple string and file already contains prefix with content between the prefix and suffix",
72+
prefix: "hello",
73+
suffix: "world",
74+
initialFileContents: `hellocontentsworld
75+
jelloworld
76+
chelloworld
77+
`,
78+
want: `jelloworld
79+
chelloworld
80+
helloworld
81+
`,
82+
},
83+
{
84+
desc: "simple string and file already contains prefix with prefix == suffix",
85+
prefix: "hello",
86+
suffix: "world",
87+
initialFileContents: `hellohello
88+
jelloworld
89+
chelloworld
90+
`,
91+
want: `jelloworld
92+
chelloworld
93+
helloworld
94+
`,
95+
},
96+
{
97+
desc: "string with quotes and = and empty file",
98+
prefix: `'"$argon2id$v=19"'`,
99+
suffix: "admin",
100+
want: `"$argon2id$v=19"admin
101+
`,
102+
},
103+
{
104+
desc: "string with quotes and = and non empty file",
105+
prefix: `'"$argon2id$v=19"'`,
106+
suffix: "admin",
107+
initialFileContents: `jelloworld
108+
chelloworld
109+
`,
110+
want: `jelloworld
111+
chelloworld
112+
"$argon2id$v=19"admin
113+
`,
114+
},
115+
{
116+
desc: "string with quotes and = and file already contains prefix",
117+
prefix: `'"$argon2id$v=19"'`,
118+
suffix: "admin",
119+
initialFileContents: `"$argon2id$v=19"admin
120+
"$argon2id$v=19"admin
121+
helloworld
122+
jelloworld
123+
`,
124+
want: `helloworld
125+
jelloworld
126+
"$argon2id$v=19"admin
127+
`,
128+
},
129+
{
130+
desc: "string with quotes and = and file already contains prefix with content between the prefix and suffix",
131+
prefix: `'"$argon2id$v=19"'`,
132+
suffix: "admin",
133+
initialFileContents: `"$argon2id$v=19"contentsadmin
134+
helloworld
135+
jelloworld
136+
`,
137+
want: `helloworld
138+
jelloworld
139+
"$argon2id$v=19"admin
140+
`,
141+
},
142+
{
143+
desc: "string with quotes and = and file already contains prefix with prefix == suffix",
144+
prefix: `'"$argon2id$v=19"'`,
145+
suffix: "admin",
146+
initialFileContents: `"$argon2id$v=19""$argon2id$v=19"
147+
helloworld
148+
jelloworld
149+
`,
150+
want: `helloworld
151+
jelloworld
152+
"$argon2id$v=19"admin
153+
`,
154+
},
155+
}
156+
for _, tc := range testCases {
157+
t.Run(tc.desc, func(t *testing.T) {
158+
f, err := ioutil.TempFile("", "append_or_replace_test")
159+
if err != nil {
160+
t.Fatalf("Failed to create temp file: %v", err)
161+
}
162+
defer os.Remove(f.Name())
163+
if _, err := f.WriteString(tc.initialFileContents); err != nil {
164+
t.Fatalf("Failed to write to file: %v", err)
165+
}
166+
f.Close()
167+
args := fmt.Sprintf("source configure-helper.sh; append_or_replace_prefixed_line %s %s %s", f.Name(), tc.prefix, tc.suffix)
168+
cmd := exec.Command("bash", "-c", args)
169+
stderr, err := cmd.CombinedOutput()
170+
if err != nil {
171+
t.Fatalf("Failed to run command: %v: %s", err, stderr)
172+
}
173+
got, err := ioutil.ReadFile(f.Name())
174+
if err != nil {
175+
t.Fatalf("Failed to read file contents: %v", err)
176+
}
177+
if diff := cmp.Diff(string(got), tc.want); diff != "" {
178+
t.Errorf("File contents: got=%s, want=%s, diff=%s", got, tc.want, diff)
179+
}
180+
})
181+
}
182+
183+
}

cluster/gce/gci/configure-helper.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ function append_or_replace_prefixed_line {
464464
local -r tmpfile="$(mktemp -t filtered.XXXX --tmpdir=${dirname})"
465465

466466
touch "${file}"
467-
awk "substr(\$0,0,length(\"${prefix}\")) != \"${prefix}\" { print }" "${file}" > "${tmpfile}"
467+
awk -v pfx="${prefix}" 'substr($0,1,length(pfx)) != pfx { print }' "${file}" > "${tmpfile}"
468468
echo "${prefix}${suffix}" >> "${tmpfile}"
469469
mv "${tmpfile}" "${file}"
470470
}

0 commit comments

Comments
 (0)