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' + fix: CI & syntax #147

Merged
merged 15 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@ on:
pull_request:
jobs:
build:
name: make all
name: build-test-coverage
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v4
- uses: actions/setup-go@v4
with:
go-version: ^1.19
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v4
- name: Run 'all' make target
run: make all
- name: Codecov
uses: codecov/codecov-action@v3
- uses: actions/checkout@v4
- run: make build
- uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
file: ./coverage.txt # optional
flags: unittests # optional
name: codecov-umbrella # optional
fail_ci_if_error: true # optional (default = false)
verbose: true
9 changes: 4 additions & 5 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: golangci-lint
name: CI
on:
push:
tags:
Expand All @@ -9,16 +9,15 @@ on:
permissions:
contents: read
jobs:
golangci:
name: lint
build:
name: golangci-lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v4
with:
go-version: 1.19
- uses: actions/checkout@v4
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
- uses: golangci/golangci-lint-action@v3
with:
version: v1.55.2
args: --timeout 3m --verbose
19 changes: 19 additions & 0 deletions .github/workflows/syntax.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: CI
on:
push:
tags:
- v*
branches:
- master
pull_request:
jobs:
build:
name: syntax
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v4
with:
go-version: ^1.19
id: go
- uses: actions/checkout@v4
- run: make check-syntax
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ BINARY := kubedog
COVER_FILE := coverage.txt


all: generate check-dirty-repo build
all: check-syntax build

check-syntax: generate check-dirty-repo

generate: download
go generate kubedog.go
Expand Down
7 changes: 7 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
coverage:
range: "70...90"
round: down
precision: 2

ignore:
- "examples"
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
danielhelfand marked this conversation as resolved.
Show resolved Hide resolved
- `<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 @@
"strconv"
"strings"

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

Expand Down Expand Up @@ -48,18 +49,37 @@
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 @@
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)

Check warning on line 167 in generate/syntax/main.go

View check run for this annotation

Codecov / codecov/patch

generate/syntax/main.go#L166-L167

Added lines #L166 - L167 were not covered by tests
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)

Check warning on line 53 in generate/syntax/replace/replace.go

View check run for this annotation

Codecov / codecov/patch

generate/syntax/replace/replace.go#L53

Added line #L53 was not covered by tests
}
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