Skip to content

Commit

Permalink
Migrate tpg-test to go
Browse files Browse the repository at this point in the history
  • Loading branch information
trodge committed Sep 19, 2023
1 parent 960ba10 commit 6662c93
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 20 deletions.
18 changes: 7 additions & 11 deletions .ci/gcb-generate-diffs-new.yml
Original file line number Diff line number Diff line change
Expand Up @@ -240,29 +240,25 @@ steps:
- "19" # Build step
- terraform-google-conversion

- name: 'gcr.io/graphite-docker-images/bash-plus'
- name: 'gcr.io/graphite-docker-images/go-plus'
id: tpgb-test
entrypoint: '/workspace/.ci/scripts/bash-plus/terraform-tester/test_terraform.sh'
entrypoint: '/workspace/.ci/scripts/go-plus/magician/exec.sh'
secretEnv: ["GITHUB_TOKEN"]
waitFor: ["tpgb-head", "tpgb-base"]
args:
- 'test-tpg'
env:
- VERSION=beta
- COMMIT_SHA=$COMMIT_SHA
- PR_NUMBER=$_PR_NUMBER

- name: 'gcr.io/graphite-docker-images/bash-plus'
- name: 'gcr.io/graphite-docker-images/go-plus'
id: tpg-test
entrypoint: '/workspace/.ci/scripts/bash-plus/terraform-tester/test_terraform.sh'
entrypoint: '/workspace/.ci/scripts/go-plus/magician/exec.sh'
secretEnv: ["GITHUB_TOKEN"]
waitFor: ["tpg-head", "tpg-base"]
args:
- 'ga' # remove after 07/2023
- $_PR_NUMBER # remove after 07/2023
- $COMMIT_SHA # remove after 07/2023
- $BUILD_ID # remove after 07/2023
- $PROJECT_ID # remove after 07/2023
- GoogleCloudPlatform/magic-modules # remove after 07/2023
- "21" # remove after 07/2023
- 'test-tpg'
env:
- VERSION=ga
- COMMIT_SHA=$COMMIT_SHA
Expand Down
5 changes: 5 additions & 0 deletions .ci/magician/cmd/mock_github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ func (m *mockGithub) PostBuildStatus(prNumber string, title string, state string
m.calledMethods["PostBuildStatus"] = true
return nil
}

func (m *mockGithub) CreateWorkflowDispatchEvent(workflowFileName string, inputs map[string]any) error {
m.calledMethods["CreateWorkflowDispatchEvent"] = true
return nil
}
63 changes: 63 additions & 0 deletions .ci/magician/cmd/test_tpg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cmd

import (
"fmt"
"magician/github"
"os"

"github.com/spf13/cobra"
)

type ttGithub interface {
CreateWorkflowDispatchEvent(string, map[string]any) error
}

var testTPGCmd = &cobra.Command{
Use: "test-tpg",
Short: "Run provider unit tests via workflow dispatch",
Long: `This command runs provider unit tests via workflow dispatch
The following PR details are expected as environment variables:
1. VERSION (beta or ga)
2. COMMIT_SHA
3. PR_NUMBER
`,
Run: func(cmd *cobra.Command, args []string) {
version := os.Getenv("VERSION")
commit := os.Getenv("COMMIT_SHA")
pr := os.Getenv("PR_NUMBER")

gh := github.NewGithubService()

execTestTPG(version, commit, pr, gh)
},
}

func execTestTPG(version, commit, pr string, gh ttGithub) {
var repo string
if version == "ga" {
repo = "terraform-provider-google"
} else if version == "beta" {
repo = "terraform-provider-google-beta"
} else {
fmt.Println("invalid version specified")
os.Exit(1)
}

branch := "auto-pr-" + pr

inputs := map[string]any{
"owner": "GoogleCloudPlatform",
"repo": repo,
"branch": branch,
"sha": commit,
}

if err := gh.CreateWorkflowDispatchEvent("test-tpg.yml", inputs); err != nil {
fmt.Printf("Error creating workflow dispatch event: %v\n", err)
}
}

func init() {
rootCmd.AddCommand(testTPGCmd)
}
17 changes: 17 additions & 0 deletions .ci/magician/cmd/test_tpg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cmd

import (
"testing"
)

func TestExecTestTPG(t *testing.T) {
gh := &mockGithub{
calledMethods: make(map[string]bool),
}

execTestTPG("beta", "sha1", "pr1", gh)

if !gh.calledMethods["CreateWorkflowDispatchEvent"] {
t.Fatal("workflow dispatch event not created")
}
}
1 change: 1 addition & 0 deletions .ci/magician/github/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type GithubService interface {
RequestPullRequestReviewer(prNumber, assignee string) error
AddLabel(prNumber, label string) error
RemoveLabel(prNumber, label string) error
CreateWorkflowDispatchEvent(workflowFileName string, inputs map[string]any) error
}

func NewGithubService() GithubService {
Expand Down
21 changes: 21 additions & 0 deletions .ci/magician/github/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,24 @@ func (gh *github) RemoveLabel(prNumber, label string) error {

return nil
}

func (gh *github) CreateWorkflowDispatchEvent(workflowFileName string, inputs map[string]any) error {
url := fmt.Sprintf("https://api.github.com/repos/GoogleCloudPlatform/magic-modules/workflows/%s/dispatches", workflowFileName)
fmt.Println(url)
resp, err := utils.RequestCall(url, "POST", gh.token, nil, map[string]any{
"ref": "main",
"inputs": inputs,
})

if resp != 200 {
return fmt.Errorf("server returned %d creating workflow dispatch event", resp)
}

if err != nil {
return fmt.Errorf("failed to create workflow dispatch event: %s", err)
}

fmt.Printf("Successfully created workflow dispatch event for %s with inputs %v", workflowFileName, inputs)

return nil
}
6 changes: 3 additions & 3 deletions .ci/magician/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ require (
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.7.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/oauth2 v0.6.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230303212802-e74f57abe488 // indirect
google.golang.org/grpc v1.53.0 // indirect
Expand Down
12 changes: 6 additions & 6 deletions .ci/magician/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.6.0 h1:Lh8GPgSKBfWSwFvtuWOfeI3aAAnbXTSutYxJiOJFgIw=
golang.org/x/oauth2 v0.6.0/go.mod h1:ycmewcwgD4Rpr3eZJLSB4Kyyljb3qDh40vJ8STE5HKw=
Expand All @@ -92,13 +92,13 @@ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
Expand Down
2 changes: 2 additions & 0 deletions .ci/scripts/go-plus/magician/exec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ GO_PROGRAM="$DIR/../../../magician/"

pushd $GO_PROGRAM

set -x
# Pass all arguments to the child command
go run . "$@"
set +x

0 comments on commit 6662c93

Please sign in to comment.