From 28126897fb2890d23ff9d9f4790ab699268644c5 Mon Sep 17 00:00:00 2001 From: nakabonne Date: Thu, 12 Aug 2021 15:13:43 +0900 Subject: [PATCH 1/6] Add text regex support for Event watcher --- pkg/app/piped/eventwatcher/BUILD.bazel | 1 + pkg/app/piped/eventwatcher/eventwatcher.go | 37 +++++++++- .../piped/eventwatcher/eventwatcher_test.go | 67 +++++++++++++++++++ .../piped/eventwatcher/testdata/invalid.yaml | 32 +++++++++ pkg/config/event_watcher.go | 16 ++++- 5 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 pkg/app/piped/eventwatcher/testdata/invalid.yaml diff --git a/pkg/app/piped/eventwatcher/BUILD.bazel b/pkg/app/piped/eventwatcher/BUILD.bazel index 15506e2ebb..203af22517 100644 --- a/pkg/app/piped/eventwatcher/BUILD.bazel +++ b/pkg/app/piped/eventwatcher/BUILD.bazel @@ -9,6 +9,7 @@ go_library( "//pkg/config:go_default_library", "//pkg/git:go_default_library", "//pkg/model:go_default_library", + "//pkg/regexpool:go_default_library", "//pkg/yamlprocessor:go_default_library", "@org_uber_go_zap//:go_default_library", ], diff --git a/pkg/app/piped/eventwatcher/eventwatcher.go b/pkg/app/piped/eventwatcher/eventwatcher.go index b8c2fc4201..13b260105f 100644 --- a/pkg/app/piped/eventwatcher/eventwatcher.go +++ b/pkg/app/piped/eventwatcher/eventwatcher.go @@ -33,6 +33,7 @@ import ( "github.com/pipe-cd/pipe/pkg/config" "github.com/pipe-cd/pipe/pkg/git" "github.com/pipe-cd/pipe/pkg/model" + "github.com/pipe-cd/pipe/pkg/regexpool" "github.com/pipe-cd/pipe/pkg/yamlprocessor" ) @@ -239,6 +240,8 @@ func (w *watcher) commitFiles(ctx context.Context, eventCfg config.EventWatcherE // TODO: Empower Event watcher to parse JSON format case r.HCLField != "": // TODO: Empower Event watcher to parse HCL format + case r.TextField.Filled(): + newContent, upToDate, err = modifyText(path, r.TextField, latestEvent.Data) } if err != nil { return err @@ -269,7 +272,7 @@ func (w *watcher) commitFiles(ctx context.Context, eventCfg config.EventWatcherE // modifyYAML returns a new YAML content as a first returned value if the value of given // field was outdated. True as a second returned value means it's already up-to-date. func modifyYAML(path, field, newValue string) ([]byte, bool, error) { - yml, err := ioutil.ReadFile(path) + yml, err := os.ReadFile(path) if err != nil { return nil, false, fmt.Errorf("failed to read file: %w", err) } @@ -320,3 +323,35 @@ func convertStr(value interface{}) (out string, err error) { } return } + +// FIXME: Remove: +// e.g.) +// textField.Regex = "(image: gcr.io/foo/bar):(.+)" +// textField.Template = "$1:%s\n" +func modifyText(path string, textField config.EventWatcherReplacementTextField, newValue string) ([]byte, bool, error) { + text, err := os.ReadFile(path) + if err != nil { + return nil, false, fmt.Errorf("failed to read file: %w", err) + } + + pool := regexpool.DefaultPool() + lineRegex, err := pool.Get(textField.LineRegex) + if err != nil { + return nil, false, fmt.Errorf("failed to compile line regex: %w", err) + } + replaceRegex, err := pool.Get(textField.ReplaceRegex) + if err != nil { + return nil, false, fmt.Errorf("failed to compile replace regex: %w", err) + } + + touched := false + newText := lineRegex.ReplaceAllFunc(text, func(match []byte) []byte { + touched = true + return replaceRegex.ReplaceAll(match, []byte(newValue)) + }) + if !touched { + return nil, true, nil + } + + return newText, false, nil +} diff --git a/pkg/app/piped/eventwatcher/eventwatcher_test.go b/pkg/app/piped/eventwatcher/eventwatcher_test.go index 207c1594a8..04a8f8e8b8 100644 --- a/pkg/app/piped/eventwatcher/eventwatcher_test.go +++ b/pkg/app/piped/eventwatcher/eventwatcher_test.go @@ -18,6 +18,8 @@ import ( "testing" "github.com/stretchr/testify/assert" + + "github.com/pipe-cd/pipe/pkg/config" ) func TestConvertStr(t *testing.T) { @@ -117,3 +119,68 @@ func TestModifyYAML(t *testing.T) { }) } } + +func TestModifyText(t *testing.T) { + testcases := []struct { + name string + path string + textField config.EventWatcherReplacementTextField + newValue string + want []byte + wantUpToDate bool + wantErr bool + }{ + { + name: "different between defined one and given one", + path: "testdata/invalid.yaml", + textField: config.EventWatcherReplacementTextField{ + LineRegex: "image: gcr.io/pipecd/foo:(.+)", + ReplaceRegex: "v[0-9].[0-9].[0-9]", + }, + newValue: "v0.2.0", + want: []byte(`apiVersion: apps/v1 +kind: Deployment +metadata: + name: foo +spec: + template: + spec: + containers: + - name: foo + image: gcr.io/pipecd/foo:v0.2.0 + ports: + - containerPort: 9085 + env: + - name: FOO + value: {{ .encryptedSecrets.foo }} + +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: bar +spec: + template: + spec: + containers: + - name: bar + image: gcr.io/pipecd/bar:v0.1.0 + ports: + - containerPort: 9085 + env: + - name: BAR + value: {{ .encryptedSecrets.bar }} +`), + wantUpToDate: false, + wantErr: false, + }, + } + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + got, gotUpToDate, err := modifyText(tc.path, tc.textField, tc.newValue) + assert.Equal(t, tc.wantErr, err != nil) + assert.Equal(t, tc.want, got) + assert.Equal(t, tc.wantUpToDate, gotUpToDate) + }) + } +} diff --git a/pkg/app/piped/eventwatcher/testdata/invalid.yaml b/pkg/app/piped/eventwatcher/testdata/invalid.yaml new file mode 100644 index 0000000000..16f24dd176 --- /dev/null +++ b/pkg/app/piped/eventwatcher/testdata/invalid.yaml @@ -0,0 +1,32 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: foo +spec: + template: + spec: + containers: + - name: foo + image: gcr.io/pipecd/foo:v0.1.0 + ports: + - containerPort: 9085 + env: + - name: FOO + value: {{ .encryptedSecrets.foo }} + +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: bar +spec: + template: + spec: + containers: + - name: bar + image: gcr.io/pipecd/bar:v0.1.0 + ports: + - containerPort: 9085 + env: + - name: BAR + value: {{ .encryptedSecrets.bar }} diff --git a/pkg/config/event_watcher.go b/pkg/config/event_watcher.go index aadfd2a37f..90de4e7a66 100644 --- a/pkg/config/event_watcher.go +++ b/pkg/config/event_watcher.go @@ -49,7 +49,18 @@ type EventWatcherReplacement struct { // The JSON path to the field to be updated. JSONField string `json:"jsonField"` // The HCL path to the field to be updated. - HCLField string `json:"HCLField"` + HCLField string `json:"HCLField"` + TextField EventWatcherReplacementTextField `json:"textField"` +} + +type EventWatcherReplacementTextField struct { + LineRegex string `json:"lineRegex"` + // TODO: Support the "line" field that specifies tha line number staticaly + ReplaceRegex string `json:"replaceRegex"` +} + +func (e *EventWatcherReplacementTextField) Filled() bool { + return e.LineRegex != "" && e.ReplaceRegex != "" } // LoadEventWatcher gives back parsed EventWatcher config after merging config files placed under @@ -172,6 +183,9 @@ func (e *EventWatcherEvent) Validate() error { if r.HCLField != "" { count++ } + if r.TextField.Filled() { + count++ + } if count == 0 { return fmt.Errorf("event %q has a replacement with no field", e.Name) } From b7becc5309f989894c6cc1962d27c1093f28496d Mon Sep 17 00:00:00 2001 From: nakabonne Date: Thu, 12 Aug 2021 15:17:04 +0900 Subject: [PATCH 2/6] Add field comment --- pkg/app/piped/eventwatcher/eventwatcher.go | 4 ---- pkg/config/event_watcher.go | 3 +++ 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/app/piped/eventwatcher/eventwatcher.go b/pkg/app/piped/eventwatcher/eventwatcher.go index 13b260105f..788136bce3 100644 --- a/pkg/app/piped/eventwatcher/eventwatcher.go +++ b/pkg/app/piped/eventwatcher/eventwatcher.go @@ -324,10 +324,6 @@ func convertStr(value interface{}) (out string, err error) { return } -// FIXME: Remove: -// e.g.) -// textField.Regex = "(image: gcr.io/foo/bar):(.+)" -// textField.Template = "$1:%s\n" func modifyText(path string, textField config.EventWatcherReplacementTextField, newValue string) ([]byte, bool, error) { text, err := os.ReadFile(path) if err != nil { diff --git a/pkg/config/event_watcher.go b/pkg/config/event_watcher.go index 90de4e7a66..ebb755284c 100644 --- a/pkg/config/event_watcher.go +++ b/pkg/config/event_watcher.go @@ -54,8 +54,11 @@ type EventWatcherReplacement struct { } type EventWatcherReplacementTextField struct { + // The pattern to detect lines that should be updated by Event watcher. LineRegex string `json:"lineRegex"` // TODO: Support the "line" field that specifies tha line number staticaly + + // The pattern to decide which part in the line should be updated. ReplaceRegex string `json:"replaceRegex"` } From 07dd0b16171dbf9972048e1c50a422698b56a630 Mon Sep 17 00:00:00 2001 From: nakabonne Date: Mon, 16 Aug 2021 16:58:35 +0900 Subject: [PATCH 3/6] Use capturing group --- .../user-guide/configuration-reference.md | 4 +- pkg/app/piped/eventwatcher/eventwatcher.go | 43 +++++++++---- .../piped/eventwatcher/eventwatcher_test.go | 60 +++++++++++++++---- pkg/config/event_watcher.go | 22 ++----- 4 files changed, 91 insertions(+), 38 deletions(-) diff --git a/docs/content/en/docs/user-guide/configuration-reference.md b/docs/content/en/docs/user-guide/configuration-reference.md index 11c447adaf..399f989b3c 100644 --- a/docs/content/en/docs/user-guide/configuration-reference.md +++ b/docs/content/en/docs/user-guide/configuration-reference.md @@ -148,11 +148,13 @@ spec: | replacements | [][EventWatcherReplacement](/docs/user-guide/configuration-reference/#eventwatcherreplacement) | List of places where will be replaced when the new event matches. | Yes | ## EventWatcherReplacement +One of `yamlField` or `regex` is required. | Field | Type | Description | Required | |-|-|-|-| | file | string | The path to the file to be updated. | Yes | -| yamlField | string | The yaml path to the field to be updated. It requires to start with `$` which represents the root element. e.g. `$.foo.bar[0].baz`. | Yes | +| yamlField | string | The yaml path to the field to be updated. It requires to start with `$` which represents the root element. e.g. `$.foo.bar[0].baz`. | No | +| regex | string | The regex string that specify what should be replaced. The only first capturing group enclosed by `()` will be replaced with the new value. e.g. `host.xz/foo/bar:(v[0-9].[0-9].[0-9])` | No | ## CommitMatcher diff --git a/pkg/app/piped/eventwatcher/eventwatcher.go b/pkg/app/piped/eventwatcher/eventwatcher.go index 788136bce3..971c7b7b8d 100644 --- a/pkg/app/piped/eventwatcher/eventwatcher.go +++ b/pkg/app/piped/eventwatcher/eventwatcher.go @@ -24,6 +24,7 @@ import ( "io/ioutil" "os" "path/filepath" + "regexp/syntax" "strconv" "sync" "time" @@ -240,8 +241,8 @@ func (w *watcher) commitFiles(ctx context.Context, eventCfg config.EventWatcherE // TODO: Empower Event watcher to parse JSON format case r.HCLField != "": // TODO: Empower Event watcher to parse HCL format - case r.TextField.Filled(): - newContent, upToDate, err = modifyText(path, r.TextField, latestEvent.Data) + case r.Regex != "": + newContent, upToDate, err = modifyText(path, r.Regex, latestEvent.Data) } if err != nil { return err @@ -324,28 +325,48 @@ func convertStr(value interface{}) (out string, err error) { return } -func modifyText(path string, textField config.EventWatcherReplacementTextField, newValue string) ([]byte, bool, error) { - text, err := os.ReadFile(path) +func modifyText(path, regexText, newValue string) ([]byte, bool, error) { + content, err := os.ReadFile(path) if err != nil { return nil, false, fmt.Errorf("failed to read file: %w", err) } pool := regexpool.DefaultPool() - lineRegex, err := pool.Get(textField.LineRegex) + regex, err := pool.Get(regexText) if err != nil { - return nil, false, fmt.Errorf("failed to compile line regex: %w", err) + return nil, false, fmt.Errorf("failed to compile regex text (%s): %w", regexText, err) } - replaceRegex, err := pool.Get(textField.ReplaceRegex) + + // Extract the first capturing group. + firstGroup := "" + re, err := syntax.Parse(regexText, syntax.Perl) if err != nil { - return nil, false, fmt.Errorf("failed to compile replace regex: %w", err) + return nil, false, fmt.Errorf("failed to parse the first capturing group regex") + } + for _, s := range re.Sub { + if s.Op == syntax.OpCapture { + firstGroup = s.String() + break + } + } + if firstGroup == "" { + return nil, false, fmt.Errorf("capturing group not found in the given regex") + } + subRegex, err := pool.Get(firstGroup) + if err != nil { + return nil, false, fmt.Errorf("failed to compile the first capturing group") } - touched := false - newText := lineRegex.ReplaceAllFunc(text, func(match []byte) []byte { + var touched, outDated bool + newText := regex.ReplaceAllFunc(content, func(match []byte) []byte { touched = true - return replaceRegex.ReplaceAll(match, []byte(newValue)) + outDated = string(subRegex.Find(match)) != newValue + return subRegex.ReplaceAll(match, []byte(newValue)) }) if !touched { + return nil, false, fmt.Errorf("the content of %s doesn't match %s", path, regexText) + } + if !outDated { return nil, true, nil } diff --git a/pkg/app/piped/eventwatcher/eventwatcher_test.go b/pkg/app/piped/eventwatcher/eventwatcher_test.go index 04a8f8e8b8..6b26888d88 100644 --- a/pkg/app/piped/eventwatcher/eventwatcher_test.go +++ b/pkg/app/piped/eventwatcher/eventwatcher_test.go @@ -18,8 +18,6 @@ import ( "testing" "github.com/stretchr/testify/assert" - - "github.com/pipe-cd/pipe/pkg/config" ) func TestConvertStr(t *testing.T) { @@ -124,19 +122,61 @@ func TestModifyText(t *testing.T) { testcases := []struct { name string path string - textField config.EventWatcherReplacementTextField + regex string newValue string want []byte wantUpToDate bool wantErr bool }{ { - name: "different between defined one and given one", - path: "testdata/invalid.yaml", - textField: config.EventWatcherReplacementTextField{ - LineRegex: "image: gcr.io/pipecd/foo:(.+)", - ReplaceRegex: "v[0-9].[0-9].[0-9]", - }, + name: "invalid regex given", + path: "testdata/invalid.yaml", + regex: "[", + newValue: "v0.2.0", + want: nil, + wantUpToDate: false, + wantErr: true, + }, + { + name: "no capturing group given", + path: "testdata/invalid.yaml", + regex: "image: gcr.io/pipecd/foo:v[0-9].[0-9].[0-9]", + newValue: "v0.2.0", + want: nil, + wantUpToDate: false, + wantErr: true, + }, + { + name: "invalid capturing group given", + path: "testdata/invalid.yaml", + regex: "image: gcr.io/pipecd/foo:([)", + newValue: "v0.2.0", + want: nil, + wantUpToDate: false, + wantErr: true, + }, + { + name: "the file doesn't match regex", + path: "testdata/invalid.yaml", + regex: "abcdefg", + newValue: "v0.1.0", + want: nil, + wantUpToDate: false, + wantErr: true, + }, + { + name: "the file is up-to-date", + path: "testdata/invalid.yaml", + regex: "image: gcr.io/pipecd/foo:(v[0-9].[0-9].[0-9])", + newValue: "v0.1.0", + want: nil, + wantUpToDate: true, + wantErr: false, + }, + { + name: "different between defined one and given one", + path: "testdata/invalid.yaml", + regex: "image: gcr.io/pipecd/foo:(v[0-9].[0-9].[0-9])", newValue: "v0.2.0", want: []byte(`apiVersion: apps/v1 kind: Deployment @@ -177,7 +217,7 @@ spec: } for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { - got, gotUpToDate, err := modifyText(tc.path, tc.textField, tc.newValue) + got, gotUpToDate, err := modifyText(tc.path, tc.regex, tc.newValue) assert.Equal(t, tc.wantErr, err != nil) assert.Equal(t, tc.want, got) assert.Equal(t, tc.wantUpToDate, gotUpToDate) diff --git a/pkg/config/event_watcher.go b/pkg/config/event_watcher.go index ebb755284c..ce2da85923 100644 --- a/pkg/config/event_watcher.go +++ b/pkg/config/event_watcher.go @@ -49,21 +49,11 @@ type EventWatcherReplacement struct { // The JSON path to the field to be updated. JSONField string `json:"jsonField"` // The HCL path to the field to be updated. - HCLField string `json:"HCLField"` - TextField EventWatcherReplacementTextField `json:"textField"` -} - -type EventWatcherReplacementTextField struct { - // The pattern to detect lines that should be updated by Event watcher. - LineRegex string `json:"lineRegex"` - // TODO: Support the "line" field that specifies tha line number staticaly - - // The pattern to decide which part in the line should be updated. - ReplaceRegex string `json:"replaceRegex"` -} - -func (e *EventWatcherReplacementTextField) Filled() bool { - return e.LineRegex != "" && e.ReplaceRegex != "" + HCLField string `json:"HCLField"` + // The regex string that specify what should be replaced. + // The only first capturing group enclosed by `()` will be replaced with the new value. + // e.g. "host.xz/foo/bar:(v[0-9].[0-9].[0-9])" + Regex string `json:"regex"` } // LoadEventWatcher gives back parsed EventWatcher config after merging config files placed under @@ -186,7 +176,7 @@ func (e *EventWatcherEvent) Validate() error { if r.HCLField != "" { count++ } - if r.TextField.Filled() { + if r.Regex != "" { count++ } if count == 0 { From c6213a82890d6f0e640cdb44cbe44c9fc5cbe924 Mon Sep 17 00:00:00 2001 From: nakabonne Date: Mon, 16 Aug 2021 18:29:04 +0900 Subject: [PATCH 4/6] Wrap errors --- pkg/app/piped/eventwatcher/eventwatcher.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/app/piped/eventwatcher/eventwatcher.go b/pkg/app/piped/eventwatcher/eventwatcher.go index 971c7b7b8d..6cbfb4be16 100644 --- a/pkg/app/piped/eventwatcher/eventwatcher.go +++ b/pkg/app/piped/eventwatcher/eventwatcher.go @@ -341,7 +341,7 @@ func modifyText(path, regexText, newValue string) ([]byte, bool, error) { firstGroup := "" re, err := syntax.Parse(regexText, syntax.Perl) if err != nil { - return nil, false, fmt.Errorf("failed to parse the first capturing group regex") + return nil, false, fmt.Errorf("failed to parse the first capturing group regex: %w", err) } for _, s := range re.Sub { if s.Op == syntax.OpCapture { @@ -354,7 +354,7 @@ func modifyText(path, regexText, newValue string) ([]byte, bool, error) { } subRegex, err := pool.Get(firstGroup) if err != nil { - return nil, false, fmt.Errorf("failed to compile the first capturing group") + return nil, false, fmt.Errorf("failed to compile the first capturing group: %w", err) } var touched, outDated bool From 18318f5e3d00affa74967d32c4b6eba71983b591 Mon Sep 17 00:00:00 2001 From: Ryo Nakao Date: Tue, 17 Aug 2021 10:15:56 +0900 Subject: [PATCH 5/6] Update pkg/config/event_watcher.go Co-authored-by: Khanh Tran <32532742+khanhtc1202@users.noreply.github.com> --- pkg/config/event_watcher.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/config/event_watcher.go b/pkg/config/event_watcher.go index ce2da85923..5a73167deb 100644 --- a/pkg/config/event_watcher.go +++ b/pkg/config/event_watcher.go @@ -50,8 +50,8 @@ type EventWatcherReplacement struct { JSONField string `json:"jsonField"` // The HCL path to the field to be updated. HCLField string `json:"HCLField"` - // The regex string that specify what should be replaced. - // The only first capturing group enclosed by `()` will be replaced with the new value. + // The regex string specifying what should be replaced. + // Only the first capturing group enclosed by `()` will be replaced with the new value. // e.g. "host.xz/foo/bar:(v[0-9].[0-9].[0-9])" Regex string `json:"regex"` } From 7441e9b0f434292a9aacf255ea8dd04bb0e3f3db Mon Sep 17 00:00:00 2001 From: nakabonne Date: Tue, 17 Aug 2021 10:45:15 +0900 Subject: [PATCH 6/6] Fix --- pkg/app/piped/eventwatcher/eventwatcher.go | 4 +++ .../piped/eventwatcher/eventwatcher_test.go | 26 ++++++++++++++----- .../eventwatcher/testdata/kustomization.yaml | 3 +++ .../{invalid.yaml => with-template.yaml} | 0 4 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 pkg/app/piped/eventwatcher/testdata/kustomization.yaml rename pkg/app/piped/eventwatcher/testdata/{invalid.yaml => with-template.yaml} (100%) diff --git a/pkg/app/piped/eventwatcher/eventwatcher.go b/pkg/app/piped/eventwatcher/eventwatcher.go index 6cbfb4be16..4481d03dd9 100644 --- a/pkg/app/piped/eventwatcher/eventwatcher.go +++ b/pkg/app/piped/eventwatcher/eventwatcher.go @@ -325,6 +325,9 @@ func convertStr(value interface{}) (out string, err error) { return } +// modifyText returns a new text replacing all matches of the given regex with the newValue. +// The only first capturing group enclosed by `()` will be replaced. +// True as a second returned value means it's already up-to-date. func modifyText(path, regexText, newValue string) ([]byte, bool, error) { content, err := os.ReadFile(path) if err != nil { @@ -361,6 +364,7 @@ func modifyText(path, regexText, newValue string) ([]byte, bool, error) { newText := regex.ReplaceAllFunc(content, func(match []byte) []byte { touched = true outDated = string(subRegex.Find(match)) != newValue + // Return text replacing the only first capturing group with the newValue. return subRegex.ReplaceAll(match, []byte(newValue)) }) if !touched { diff --git a/pkg/app/piped/eventwatcher/eventwatcher_test.go b/pkg/app/piped/eventwatcher/eventwatcher_test.go index 6b26888d88..47d887cc19 100644 --- a/pkg/app/piped/eventwatcher/eventwatcher_test.go +++ b/pkg/app/piped/eventwatcher/eventwatcher_test.go @@ -130,7 +130,7 @@ func TestModifyText(t *testing.T) { }{ { name: "invalid regex given", - path: "testdata/invalid.yaml", + path: "testdata/with-template.yaml", regex: "[", newValue: "v0.2.0", want: nil, @@ -139,7 +139,7 @@ func TestModifyText(t *testing.T) { }, { name: "no capturing group given", - path: "testdata/invalid.yaml", + path: "testdata/with-template.yaml", regex: "image: gcr.io/pipecd/foo:v[0-9].[0-9].[0-9]", newValue: "v0.2.0", want: nil, @@ -148,7 +148,7 @@ func TestModifyText(t *testing.T) { }, { name: "invalid capturing group given", - path: "testdata/invalid.yaml", + path: "testdata/with-template.yaml", regex: "image: gcr.io/pipecd/foo:([)", newValue: "v0.2.0", want: nil, @@ -157,7 +157,7 @@ func TestModifyText(t *testing.T) { }, { name: "the file doesn't match regex", - path: "testdata/invalid.yaml", + path: "testdata/with-template.yaml", regex: "abcdefg", newValue: "v0.1.0", want: nil, @@ -166,7 +166,7 @@ func TestModifyText(t *testing.T) { }, { name: "the file is up-to-date", - path: "testdata/invalid.yaml", + path: "testdata/with-template.yaml", regex: "image: gcr.io/pipecd/foo:(v[0-9].[0-9].[0-9])", newValue: "v0.1.0", want: nil, @@ -174,8 +174,8 @@ func TestModifyText(t *testing.T) { wantErr: false, }, { - name: "different between defined one and given one", - path: "testdata/invalid.yaml", + name: "replace a part of text", + path: "testdata/with-template.yaml", regex: "image: gcr.io/pipecd/foo:(v[0-9].[0-9].[0-9])", newValue: "v0.2.0", want: []byte(`apiVersion: apps/v1 @@ -210,6 +210,18 @@ spec: env: - name: BAR value: {{ .encryptedSecrets.bar }} +`), + wantUpToDate: false, + wantErr: false, + }, + { + name: "replace text", + path: "testdata/kustomization.yaml", + regex: "newTag: (v[0-9].[0-9].[0-9])", + newValue: "v0.2.0", + want: []byte(`images: +- name: gcr.io/pipecd/foo + newTag: v0.2.0 `), wantUpToDate: false, wantErr: false, diff --git a/pkg/app/piped/eventwatcher/testdata/kustomization.yaml b/pkg/app/piped/eventwatcher/testdata/kustomization.yaml new file mode 100644 index 0000000000..301d0b985f --- /dev/null +++ b/pkg/app/piped/eventwatcher/testdata/kustomization.yaml @@ -0,0 +1,3 @@ +images: +- name: gcr.io/pipecd/foo + newTag: v0.1.0 diff --git a/pkg/app/piped/eventwatcher/testdata/invalid.yaml b/pkg/app/piped/eventwatcher/testdata/with-template.yaml similarity index 100% rename from pkg/app/piped/eventwatcher/testdata/invalid.yaml rename to pkg/app/piped/eventwatcher/testdata/with-template.yaml