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

Pjtester fix flags #2684

Merged
merged 3 commits into from Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 10 additions & 12 deletions development/tools/cmd/pjtester/main.go
@@ -1,24 +1,18 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/kyma-project/test-infra/development/tools/pkg/pjtester"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
prowflagutil "k8s.io/test-infra/prow/flagutil"
)

var (
log = logrus.New()
rootCmd = &cobra.Command{
Use: "pjtester",
Short: "pjtester generate new prowjob spec and schedule it",
Long: "pjtester generate new prowjob spec from provided path. It reuse PR refs.",
Run: func(cmd *cobra.Command, args []string) {
pjtester.SchedulePJ()
},
}
log = logrus.New()
ghOptions prowflagutil.GitHubOptions
)

func main() {
Expand All @@ -27,7 +21,11 @@ func main() {
if _, present := os.LookupEnv("IMAGE_COMMIT"); present {
fmt.Printf("IMAGE_COMMIT: %s\n", os.Getenv("IMAGE_COMMIT"))
}
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
ghOptions.AddFlagsWithoutDefaultGitHubTokenPath(fs)
_ = fs.Parse(os.Args[1:])
if err := ghOptions.Validate(false); err != nil {
logrus.WithError(err).Fatalf("github options validation failed")
}
pjtester.SchedulePJ(ghOptions)
}
9 changes: 5 additions & 4 deletions development/tools/pkg/pjtester/pjtester.go
Expand Up @@ -165,9 +165,10 @@ func getPjCfg(pjCfg pjCfg, o options) options {

// gatherOptions is building common options for all tests.
// Options are build from PR env variables and prowjob config read from pjtester.yaml file.
func gatherOptions(configPath string) options {
func gatherOptions(configPath string, ghOptions prowflagutil.GitHubOptions) options {
var o options
var err error
o.github = ghOptions
// configPath is a location of prow config file to test. It was read from pjtester.yaml file or set to default.
o.configPath = fmt.Sprintf("%s/%s", os.Getenv("KYMA_PROJECT_DIR"), configPath)
// baseRef is a base branch name for github pull request under test.
Expand Down Expand Up @@ -359,7 +360,7 @@ func newTestPJ(pjCfg pjCfg, opt options) prowapi.ProwJob {
// Building prowjob based on generated job specifications.
pj := pjutil.NewProwJob(pjs, job.Labels, job.Annotations)
// Add prefix to prowjob to test name.
pj.Spec.Job = fmt.Sprintf("test_of_prowjob_%s", pj.Spec.Job)
pj.Spec.Job = fmt.Sprintf("%s_test_of_prowjob_%s", opt.pullAuthor, pj.Spec.Job)
// Make sure prowjob to test will run on untrusted-workload cluster.
pj.Spec.Cluster = "untrusted-workload"
if pjCfg.Report {
Expand All @@ -371,15 +372,15 @@ func newTestPJ(pjCfg pjCfg, opt options) prowapi.ProwJob {
}

// SchedulePJ will generate prowjob for testing and schedule it on prow for execution.
func SchedulePJ() {
func SchedulePJ(ghOptions prowflagutil.GitHubOptions) {
log.SetOutput(os.Stdout)
log.SetLevel(logrus.InfoLevel)
var err error
if err := checkEnvVars(envVarsList); err != nil {
logrus.WithError(err).Fatalf("Required environment variable not set.")
}
testCfg := readTestCfg(testCfgFile)
o := gatherOptions(testCfg.ConfigPath).withGithubClientOptions()
o := gatherOptions(testCfg.ConfigPath, ghOptions)
prowClient := newProwK8sClientset()
pjsClient := prowClient.ProwV1()
var secretAgent *secret.Agent
Expand Down
8 changes: 5 additions & 3 deletions development/tools/pkg/pjtester/pjtester_test.go
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/stretchr/testify/assert"
prowapi "k8s.io/test-infra/prow/apis/prowjobs/v1"
prowflagutil "k8s.io/test-infra/prow/flagutil"
"k8s.io/test-infra/prow/github"
"k8s.io/test-infra/prow/github/fakegithub"
)
Expand All @@ -21,6 +22,7 @@ var (
otherPrSHA string
otherPrOrg string
otherPrRepo string
ghOptions *prowflagutil.GitHubOptions
)

func TestMain(m *testing.M) {
Expand All @@ -44,7 +46,7 @@ func TestMain(m *testing.M) {
os.Setenv("JOB_SPEC", fmt.Sprintf("{\"type\":\"presubmit\",\"job\":\"job-name\",\"buildid\":\"0\",\"prowjobid\":\"uuid\",\"refs\":{\"org\":\"org-name\",\"repo\":\"repo-name\",\"base_ref\":\"base-ref\",\"base_sha\":\"base-sha\",\"pulls\":[{\"number\":1,\"author\":\"%s\",\"sha\":\"pull-sha\"}]}}", prAuthor))
prNumber, _ = strconv.Atoi(os.Getenv("PULL_NUMBER"))
testCfgFile = fmt.Sprintf("%s/test-infra/development/tools/pkg/pjtester/test_artifacts/pjtester.yaml", os.Getenv("KYMA_PROJECT_DIR"))

ghOptions = prowflagutil.NewGitHubOptions()
os.Exit(m.Run())
}

Expand All @@ -69,7 +71,7 @@ func TestReadTestCfg(t *testing.T) {

func TestNewTestPJ(t *testing.T) {
testCfg := readTestCfg(testCfgFile)
o := gatherOptions(testCfg.ConfigPath)
o := gatherOptions(testCfg.ConfigPath, *ghOptions)
fakeGitHubClient := &fakegithub.FakeClient{}
fakeGitHubClient.PullRequests = map[int]*github.PullRequest{otherPrNumber: {
User: github.User{Login: otherPrAuthor},
Expand All @@ -84,7 +86,7 @@ func TestNewTestPJ(t *testing.T) {
fmt.Printf("Testing with values\n\tPjName: %s\n\tPjPath: %s\n", pjCfg.PjName, pjCfg.PjPath)
pj := newTestPJ(pjCfg, o)
assert.Equalf(t, "untrusted-workload", pj.Spec.Cluster, "Prowjob cluster name is not : trusted-workload")
assert.Regexpf(t, "^test_of_prowjob_.*", pj.Spec.Job, "Prowjob name doesn't start with : test_of_prowjob_")
assert.Regexpf(t, "^testAuthor_test_of_prowjob_.*", pj.Spec.Job, "Prowjob name doesn't start with : test_of_prowjob_")
if pj.Spec.Type == "periodic" {
assert.Containsf(t, pj.Spec.ExtraRefs, prowapi.Refs{
Org: os.Getenv("REPO_OWNER"),
Expand Down