Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding step 'pods converge to selector' #146

Closed
wants to merge 9 commits into from
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ jobs:
- name: Run 'all' make target
run: make all
- name: Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
file: ./coverage.txt # optional
flags: unittests # optional
Expand Down
8 changes: 8 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
coverage:
range: "70...90"
round: down
precision: 2

ignore:
- "examples"
- "generate"
1 change: 1 addition & 0 deletions docs/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Below you will find the step syntax next to the name of the method it utilizes.
- `<GK> some pods in namespace <non-whitespace-characters> with selector <non-whitespace-characters> don't have "<any-characters-except-(")>" in logs since <any-characters-except-(")> time` kdt.KubeClientSet.SomePodsInNamespaceWithSelectorDontHaveStringInLogsSinceTime
- `<GK> [the] pods in namespace <non-whitespace-characters> with selector <non-whitespace-characters> have no errors in logs since <any-characters-except-(")> time` kdt.KubeClientSet.PodsInNamespaceWithSelectorHaveNoErrorsInLogsSinceTime
- `<GK> [the] pods in namespace <non-whitespace-characters> with selector <non-whitespace-characters> have some errors in logs since <any-characters-except-(")> time` kdt.KubeClientSet.PodsInNamespaceWithSelectorHaveSomeErrorsInLogsSinceTime
- `<GK> [all] [the] (pod|pods) in [the] namespace <non-whitespace-characters> with [the] label selector <non-whitespace-characters> [should] converge to [the] field selector <non-whitespace-characters>` kdt.KubeClientSet.PodsInNamespaceWithLabelSelectorConvergeToFieldSelector
- `<GK> [the] pods in namespace <non-whitespace-characters> with selector <non-whitespace-characters> should have labels <non-whitespace-characters>` kdt.KubeClientSet.PodsInNamespaceWithSelectorShouldHaveLabels
- `<GK> [the] pod <non-whitespace-characters> in namespace <non-whitespace-characters> should have labels <non-whitespace-characters>` kdt.KubeClientSet.PodInNamespaceShouldHaveLabels

Expand Down
49 changes: 34 additions & 15 deletions generate/syntax/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strconv"
"strings"

"github.com/keikoproj/kubedog/generate/syntax/replace"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -48,18 +49,37 @@ const (
destinationFileBeginning = "# Syntax" + newLine + "Below you will find the step syntax next to the name of the method it utilizes. Here GK stands for [Gherkin](https://cucumber.io/docs/gherkin/reference/#keywords) Keyword and words in brackets ([]) are optional:" + newLine
)

var replacers = []struct {
replacee string
replacer string
}{
{`(?:`, `[`},
{` )?`, `] `},
{`)?`, `]`},
{`(\d+)`, `<digits>`},
{`(\S+)`, `<non-whitespace-characters>`},
{`([^"]*)`, `<any-characters-except-(")>`},
{`\(`, `(`},
{`\)`, `)`},
var replacements = replace.Replacements{
{Replacee: `(\d+)`, Replacer: `<digits>`},
{Replacee: `(\S+)`, Replacer: `<non-whitespace-characters>`},
{Replacee: `([^"]*)`, Replacer: `<any-characters-except-(")>`},
}

var bracketsReplacements = replace.BracketsReplacements{
{
Opening: replace.Replacement{
Replacee: `(?:`, Replacer: `[`},
Closing: replace.Replacement{
Replacee: ` )?`, Replacer: `] `},
},
{
Opening: replace.Replacement{
Replacee: `(?:`, Replacer: `[`},
Closing: replace.Replacement{
Replacee: `)?`, Replacer: `]`},
},
{
Opening: replace.Replacement{
Replacee: `(?:`, Replacer: `(`},
Closing: replace.Replacement{
Replacee: `)`, Replacer: `)`},
},
{
Opening: replace.Replacement{
Replacee: `\(`, Replacer: `(`},
Closing: replace.Replacement{
Replacee: `\)`, Replacer: `)`},
},
}

func main() {
Expand Down Expand Up @@ -143,9 +163,8 @@ func processStep(rawStep string) string {
processedStep := rawStepSplit[1]
processedStep = strings.TrimPrefix(processedStep, stepPrefix)
processedStep = strings.TrimSuffix(processedStep, stepSuffix)
for _, r := range replacers {
processedStep = strings.ReplaceAll(processedStep, r.replacee, r.replacer)
}
processedStep = replacements.Replace(processedStep)
processedStep = bracketsReplacements.Replace(processedStep)
method := rawStepSplit[2]
method = strings.TrimPrefix(method, methodPrefix)
method = strings.TrimSuffix(method, methodSuffix)
Expand Down
89 changes: 89 additions & 0 deletions generate/syntax/replace/replace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
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 replace

import (
"bytes"
"log"
"regexp"
"strings"
)

const regExp_CharsWithinBrackets = "([^(]*)"

type Replacement struct {
Replacee string
Replacer string
}

func (r Replacement) Replace(src string) string {
return strings.ReplaceAll(src, r.Replacee, r.Replacer)
}

type Replacements []Replacement

func (rs Replacements) Replace(src string) string {
new := src
for _, r := range rs {
new = r.Replace(new)
}
return new
}

type BracketsReplacement struct {
Opening Replacement
Closing Replacement
}

func (br BracketsReplacement) Replace(src string) string {
re, err := regexp.Compile(br.getRegExp())
if err != nil {
log.Fatal(err)
}
new := re.ReplaceAllFunc([]byte(src), br.replaceSingle)
return string(new)
}

func (br BracketsReplacement) replaceSingle(src []byte) []byte {
s := string(src)
s = br.Opening.Replace(s)
s = br.Closing.Replace(s)
return []byte(s)
}

func (br BracketsReplacement) getRegExp() string {
return escapeEveryCharacter(br.Opening.Replacee) +
regExp_CharsWithinBrackets +
escapeEveryCharacter(br.Closing.Replacee)
}

func escapeEveryCharacter(s string) string {
var buffer bytes.Buffer
for _, c := range s {
buffer.WriteString(`\`)
buffer.WriteRune(c)
}
return buffer.String()
}

type BracketsReplacements []BracketsReplacement

func (brs BracketsReplacements) Replace(src string) string {
new := src
for _, br := range brs {
new = br.Replace(new)
}
return new
}
Loading
Loading