-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
tests: ensure that pre-submits get additional reviews #33958
Open
pohly
wants to merge
2
commits into
kubernetes:master
Choose a base branch
from
pohly:pre-submit-checks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# See the OWNERS docs at https://go.k8s.io/owners | ||
|
||
options: | ||
no_parent_owners: true | ||
|
||
reviewers: | ||
- test-infra-oncall # oncall | ||
- BenTheElder # lead | ||
- aojea # lead | ||
approvers: | ||
- test-infra-oncall # oncall | ||
- BenTheElder # lead | ||
- aojea # lead | ||
|
||
labels: | ||
- sig/testing | ||
- sig/infra |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
The project has certain guidelines around jobs which are meant to ensure that | ||
there's a balance between test coverage and costs for running the CI. For | ||
example, non-blocking jobs that get trigger automatically for PRs should be | ||
used judiciously. | ||
|
||
Because SIG leads are not necessarily familiar with those policies, SIG Testing | ||
and SIG Infra need to be involved before merging jobs that fall into those | ||
sensitive areas. This is achieved with tests and additional files in this | ||
directory and a separate OWNERS file. | ||
|
||
To check whether jobs are okay, run the Go tests in this directory. | ||
If tests fail, re-run with the `UPDATE_FIXTURE_DATA=true` env variable | ||
and include the modified files in the PR which updates the jobs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
|
||
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 policy | ||
|
||
// This file validates Kubernetes's jobs configs against policies. | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"slices" | ||
"sort" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
yaml "sigs.k8s.io/yaml/goyaml.v3" | ||
|
||
cfg "sigs.k8s.io/prow/pkg/config" | ||
) | ||
|
||
var configPath = flag.String("config", "../../../../config/prow/config.yaml", "Path to prow config") | ||
var jobConfigPath = flag.String("job-config", "../../../jobs", "Path to prow job config") | ||
|
||
// Loaded at TestMain. | ||
var c *cfg.Config | ||
|
||
func TestMain(m *testing.M) { | ||
flag.Parse() | ||
if *configPath == "" { | ||
fmt.Println("--config must set") | ||
os.Exit(1) | ||
} | ||
|
||
conf, err := cfg.Load(*configPath, *jobConfigPath, nil, "") | ||
if err != nil { | ||
fmt.Printf("Could not load config: %v", err) | ||
os.Exit(1) | ||
} | ||
c = conf | ||
|
||
os.Exit(m.Run()) | ||
} | ||
|
||
func TestKubernetesPresubmitJobs(t *testing.T) { | ||
jobs := c.AllStaticPresubmits([]string{"kubernetes/kubernetes"}) | ||
var expected presubmitJobs | ||
|
||
for _, job := range jobs { | ||
if !job.AlwaysRun && job.RunIfChanged == "" { | ||
// Manually triggered, no additional review needed. | ||
continue | ||
} | ||
|
||
// Mirror those attributes of the job which must trigger additional reviews | ||
// or are needed to identify the job. | ||
j := presubmitJob{ | ||
Name: job.Name, | ||
SkipBranches: job.SkipBranches, | ||
Branches: job.Branches, | ||
|
||
RunIfChanged: job.RunIfChanged, | ||
SkipIfOnlyChanged: job.SkipIfOnlyChanged, | ||
} | ||
|
||
// This uses separate top-level fields instead of job attributes to | ||
// make it more obvious when run_if_changed is used. | ||
if job.AlwaysRun { | ||
expected.AlwaysRun = append(expected.AlwaysRun, j) | ||
} else { | ||
expected.RunIfChanged = append(expected.RunIfChanged, j) | ||
|
||
if !job.Optional { | ||
// Absolute path is more user-friendly than ../../config/... | ||
t.Errorf("Policy violation: %s in %s should use `optional: true` or `alwaysRun: true`.", job.Name, maybeAbsPath(job.SourcePath)) | ||
} | ||
} | ||
|
||
} | ||
expected.Normalize() | ||
|
||
// Encode the expected content. | ||
var expectedData bytes.Buffer | ||
if _, err := expectedData.Write([]byte(`# AUTOGENERATED by "UPDATE_FIXTURE_DATA=true go test ./config/tests/jobs". DO NOT EDIT! | ||
|
||
`)); err != nil { | ||
t.Fatalf("unexpected error writing into buffer: %v", err) | ||
} | ||
|
||
encoder := yaml.NewEncoder(&expectedData) | ||
encoder.SetIndent(4) | ||
if err := encoder.Encode(expected); err != nil { | ||
t.Fatalf("unexpected error encoding %s: %v", presubmitsFile, err) | ||
} | ||
|
||
// Compare. This proceeds on read or decoding errors because | ||
// the file might get re-generated below. | ||
var actual presubmitJobs | ||
actualData, err := os.ReadFile(presubmitsFile) | ||
if err != nil && !os.IsNotExist(err) { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
if err := yaml.Unmarshal(actualData, &actual); err != nil { | ||
t.Errorf("unexpected error decoding %s: %v", presubmitsFile, err) | ||
} | ||
|
||
// First check the in-memory structs. The diff is nicer for them (more context). | ||
diff := cmp.Diff(actual, expected) | ||
if diff == "" { | ||
// Next check the encoded data. This should only be different on test updates. | ||
diff = cmp.Diff(string(actualData), expectedData.String(), cmpopts.AcyclicTransformer("SplitLines", func(s string) []string { | ||
return strings.Split(s, "\n") | ||
})) | ||
} | ||
|
||
if diff != "" { | ||
if value, _ := os.LookupEnv("UPDATE_FIXTURE_DATA"); value == "true" { | ||
if err := os.WriteFile(presubmitsFile, expectedData.Bytes(), 0644); err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
t.Logf(` | ||
%s was out-dated. Updated as requested with the following changes (- actual, + expected): | ||
%s | ||
`, maybeAbsPath(presubmitsFile), diff) | ||
} else { | ||
t.Errorf(` | ||
%s is out-dated. Detected differences (- actual, + expected): | ||
%s | ||
|
||
Blocking pre-submit jobs must be for stable, important features. | ||
Non-blocking pre-submit jobs should only be run automatically if they meet | ||
the criteria outlined in https://github.com/kubernetes/community/pull/8196. | ||
|
||
To ensure that this is considered when defining pre-submit jobs, they | ||
need to be listed in %s. If the pre-submit job is really needed, | ||
re-run the test with UPDATE_FIXTURE_DATA=true and include the modified | ||
file. The following command can be used: | ||
|
||
make update-config-fixture | ||
`, presubmitsFile, diff, presubmitsFile) | ||
} | ||
} | ||
} | ||
|
||
// presubmitsFile contains the following struct. | ||
const presubmitsFile = "presubmit-jobs.yaml" | ||
|
||
type presubmitJobs struct { | ||
AlwaysRun []presubmitJob `yaml:"always_run"` | ||
RunIfChanged []presubmitJob `yaml:"run_if_changed"` | ||
} | ||
type presubmitJob struct { | ||
Name string `yaml:"name"` | ||
SkipBranches []string `yaml:"skip_branches,omitempty"` | ||
Branches []string `yaml:"branches,omitempty"` | ||
RunIfChanged string `yaml:"run_if_changed,omitempty"` | ||
SkipIfOnlyChanged string `yaml:"skip_if_only_changed,omitempty"` | ||
} | ||
|
||
func (p *presubmitJobs) Normalize() { | ||
sortJobs(&p.AlwaysRun) | ||
sortJobs(&p.RunIfChanged) | ||
} | ||
|
||
func sortJobs(jobs *[]presubmitJob) { | ||
for _, job := range *jobs { | ||
sort.Strings(job.SkipBranches) | ||
sort.Strings(job.Branches) | ||
} | ||
sort.Slice(*jobs, func(i, j int) bool { | ||
switch strings.Compare((*jobs)[i].Name, (*jobs)[j].Name) { | ||
case -1: | ||
return true | ||
case 1: | ||
return false | ||
} | ||
switch slices.Compare((*jobs)[i].SkipBranches, (*jobs)[j].SkipBranches) { | ||
case -1: | ||
return true | ||
case 1: | ||
return false | ||
} | ||
switch slices.Compare((*jobs)[i].Branches, (*jobs)[j].Branches) { | ||
case -1: | ||
return true | ||
case 1: | ||
return false | ||
} | ||
return false | ||
}) | ||
|
||
// If a job has the same settings regardless of the branch, then | ||
// we can reduce to a single entry without the branch info. | ||
shorterJobs := make([]presubmitJob, 0, len(*jobs)) | ||
for i := 0; i < len(*jobs); { | ||
job := (*jobs)[i] | ||
job.Branches = nil | ||
job.SkipBranches = nil | ||
|
||
if sameSettings(*jobs, job) { | ||
shorterJobs = append(shorterJobs, job) | ||
// Fast-forward to next job. | ||
for i < len(*jobs) && (*jobs)[i].Name == job.Name { | ||
i++ | ||
} | ||
} else { | ||
// Keep all of the different entries. | ||
for i < len(*jobs) && (*jobs)[i].Name == job.Name { | ||
shorterJobs = append(shorterJobs, (*jobs)[i]) | ||
} | ||
} | ||
} | ||
*jobs = shorterJobs | ||
} | ||
|
||
func sameSettings(jobs []presubmitJob, ref presubmitJob) bool { | ||
for _, job := range jobs { | ||
if job.Name != ref.Name { | ||
continue | ||
} | ||
if job.RunIfChanged != ref.RunIfChanged || | ||
job.SkipIfOnlyChanged != ref.SkipIfOnlyChanged { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// maybeAbsPath tries to make a path absolute. This is useful because | ||
// relative paths in test output tend to be confusing when the user | ||
// invoked the test outside of the test's directory. | ||
func maybeAbsPath(path string) string { | ||
if path, err := filepath.Abs(path); err == nil { | ||
return path | ||
} | ||
return path | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should probably have a convenient make target since people will be doing this often, normally we have verify scripts spit out the command people need to run when there's generated data to update
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added
make update-config-fixtures
plus some other make targets likeupdate-unit
for symmetry withmake unit
which happens to run the Go test. The Go test itself now points tomake update-config-fixtures
because that is faster when the goal is to update the file.