Skip to content

Commit

Permalink
fix: fix tests for github/gitlab providers
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasMrqes committed Nov 27, 2024
1 parent 5a0a692 commit fc2a0cb
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 64 deletions.
13 changes: 8 additions & 5 deletions internal/controllers/terraformpullrequest/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/padok-team/burrito/internal/controllers/terraformpullrequest/comment"
"github.com/padok-team/burrito/internal/controllers/terraformpullrequest/github"
"github.com/padok-team/burrito/internal/controllers/terraformpullrequest/gitlab"
"github.com/padok-team/burrito/internal/controllers/terraformpullrequest/mock"
datastore "github.com/padok-team/burrito/internal/datastore/client"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -83,10 +84,11 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
}
if _, ok := r.Providers[fmt.Sprintf("%s/%s", repository.Namespace, repository.Name)]; !ok {
log.Infof("initializing provider for repository %s/%s", repository.Namespace, repository.Name)
r.Providers[fmt.Sprintf("%s/%s", repository.Namespace, repository.Name)], err = r.initializeProvider(ctx, repository)
provider, err := r.initializeProvider(ctx, repository)
if err != nil {
log.Errorf("could not initialize provider for repository %s: %s", repository.Name, err)
return ctrl.Result{}, err
} else {
r.Providers[fmt.Sprintf("%s/%s", repository.Namespace, repository.Name)] = provider
}
}
state := r.GetState(ctx, pr)
Expand Down Expand Up @@ -153,8 +155,9 @@ func (r *Reconciler) initializeProvider(ctx context.Context, repository *configv
if repository.Spec.Repository.Url == "" {
return nil, fmt.Errorf("no repository URL found in TerraformRepository.spec.repository.url, %s", repository.Name)
}

if secret.Data["githubAppId"] != nil && secret.Data["githubAppInstallationId"] != nil && secret.Data["githubAppPrivateKey"] != nil {
if secret.Data["enableMock"] != nil && string(secret.Data["enableMock"]) == "true" {
provider = &mock.Mock{}
} else if secret.Data["githubAppId"] != nil && secret.Data["githubAppInstallationId"] != nil && secret.Data["githubAppPrivateKey"] != nil {
provider = &github.Github{
AppId: string(secret.Data["githubAppId"]),
AppInstallationId: string(secret.Data["githubAppInstallationId"]),
Expand All @@ -172,7 +175,7 @@ func (r *Reconciler) initializeProvider(ctx context.Context, repository *configv
Url: repository.Spec.Repository.Url,
}
} else {
return nil, fmt.Errorf("no valid provider credentials found in secret. %s Please provide at least one of the following: <githubAppId, githubAppInstallationId, githubAppPrivateKey>, <githubToken>, <gitlabToken> in the secret referenced in TerraformRepository.spec.repository.secretName, %s", repository.Spec.Repository.SecretName)
return nil, fmt.Errorf("no valid provider credentials found in secret %s. Please provide at least one of the following: <githubAppId, githubAppInstallationId, githubAppPrivateKey>, <githubToken>, <gitlabToken> in the secret referenced in TerraformRepository.spec.repository.secretName", repository.Spec.Repository.SecretName)
}

err = provider.Init()
Expand Down
3 changes: 1 addition & 2 deletions internal/controllers/terraformpullrequest/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ var _ = Describe("TerraformPullRequest controller", func() {
},
},
Spec: configv1alpha1.TerraformPullRequestSpec{
Provider: "gitlab",
Branch: "test",
Branch: "test",
Repository: configv1alpha1.TerraformLayerRepository{
Name: "test-repository",
Namespace: "default",
Expand Down
5 changes: 4 additions & 1 deletion internal/controllers/terraformpullrequest/github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ func (g *Github) IsAPITokenConfigPresent() bool {
}

func (g *Github) Init() error {
apiUrl, subscription, err := inferBaseURL(g.Url)
apiUrl, subscription, err := inferBaseURL(utils.NormalizeUrl(g.Url))
if err != nil {
return err
}
httpClient := &http.Client{}
if g.IsAppConfigPresent() {
appId, err := strconv.ParseInt(g.AppId, 10, 64)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Gitlab struct {
}

func (g *Gitlab) Init() error {
apiUrl, err := inferBaseURL(g.Url)
apiUrl, err := inferBaseURL(utils.NormalizeUrl(g.Url))
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ metadata:
annotations:
webhook.terraform.padok.cloud/branch-commit: 04410b5b7d90b82ad658b86564a9aa4bce411ac9
spec:
provider: mock
branch: feature-branch
id: "42"
base: main
Expand All @@ -26,10 +25,9 @@ metadata:
annotations:
webhook.terraform.padok.cloud/branch-commit: 04410b5b7d90b82ad658b86564a9aa4bce411ac9
spec:
provider: no-exist
branch: feature-branch
id: "42"
base: main
repository:
name: burrito
name: burrito-no-provider
namespace: default
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ metadata:
annotations:
webhook.terraform.padok.cloud/branch-commit: 04410b5b7d90b82ad658b86564a9aa4bce411ac9
spec:
provider: mock
branch: feature-branch
id: "42"
base: main
Expand Down Expand Up @@ -64,7 +63,6 @@ metadata:
annotations:
webhook.terraform.padok.cloud/branch-commit: 04410b5b7d90b82ad658b86564a9aa4bce411ac9
spec:
provider: mock
branch: feature-branch-2
id: "84"
base: main
Expand Down Expand Up @@ -106,7 +104,6 @@ metadata:
annotations:
webhook.terraform.padok.cloud/branch-commit: 04410b5b7d90b82ad658b86564a9aa4bce411ac9
spec:
provider: mock
branch: feature-branch-3
id: "48"
base: main
Expand Down Expand Up @@ -148,7 +145,6 @@ metadata:
annotations:
webhook.terraform.padok.cloud/branch-commit: 04410b5b7d90b82ad658b86564a9aa4bce411ac9
spec:
provider: mock
branch: update-readme
id: "100"
base: main
Expand Down
36 changes: 36 additions & 0 deletions internal/controllers/terraformpullrequest/testdata/repository.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,39 @@ spec:
url: [email protected]:padok-team/burrito-examples.git
terraform:
enabled: true
---
apiVersion: v1
kind: Secret
metadata:
name: burrito-repo
namespace: default
type: Opaque
stringData:
enableMock: "true"
---
apiVersion: config.terraform.padok.cloud/v1alpha1
kind: TerraformRepository
metadata:
labels:
app.kubernetes.io/instance: in-cluster-burrito
name: burrito-no-provider
namespace: default
spec:
overrideRunnerSpec:
imagePullSecrets:
- name: ghcr-creds
repository:
secretName: burrito-repo-no-provider
url: [email protected]:padok-team/burrito-examples.git
terraform:
enabled: true
---
apiVersion: v1
kind: Secret
metadata:
name: burrito-repo-no-provider
namespace: default
type: Opaque
stringData:
username: user
password: password
13 changes: 13 additions & 0 deletions internal/testing/loading.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/gruntwork-io/go-commons/errors"
configv1alpha1 "github.com/padok-team/burrito/api/v1alpha1"
coordination "k8s.io/api/coordination/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -24,6 +25,7 @@ type Resources struct {
Runs []*configv1alpha1.TerraformRun
PullRequests []*configv1alpha1.TerraformPullRequest
Leases []*coordination.Lease
Secrets []*corev1.Secret
}

type GenericResource struct {
Expand Down Expand Up @@ -107,6 +109,13 @@ func LoadResources(client client.Client, path string) {
panic(err)
}
}
for _, r := range resources.Secrets {
deepCopy := r.DeepCopy()
err := client.Create(context.TODO(), deepCopy)
if err != nil {
panic(err)
}
}
}

func parseResources(path string) (*Resources, error) {
Expand Down Expand Up @@ -167,6 +176,10 @@ func parseResources(path string) (*Resources, error) {
pr := &configv1alpha1.TerraformPullRequest{}
err = yaml.Unmarshal([]byte(doc), &pr)
resources.PullRequests = append(resources.PullRequests, pr)
case "Secret":
secret := &corev1.Secret{}
err = yaml.Unmarshal([]byte(doc), &secret)
resources.Secrets = append(resources.Secrets, secret)
default:
continue
}
Expand Down
6 changes: 0 additions & 6 deletions internal/webhook/event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ var PushEventMultiplePathChanges = event.PushEvent{
}

var PullRequestOpenedEventNotAffected = event.PullRequestEvent{
Provider: "github",
URL: "https://github.com/example/repo",
Revision: "feature/branch",
Base: "main",
Expand All @@ -118,7 +117,6 @@ var PullRequestOpenedEventNotAffected = event.PullRequestEvent{
}

var PullRequestClosedEventNotAffected = event.PullRequestEvent{
Provider: "github",
URL: "https://github.com/example/repo",
Revision: "feature/branch",
Base: "main",
Expand All @@ -128,7 +126,6 @@ var PullRequestClosedEventNotAffected = event.PullRequestEvent{
}

var PullRequestOpenedEventSingleAffected = event.PullRequestEvent{
Provider: "github",
URL: "https://github.com/padok-team/burrito-examples",
Revision: "feature/branch",
Base: "main",
Expand All @@ -138,7 +135,6 @@ var PullRequestOpenedEventSingleAffected = event.PullRequestEvent{
}

var PullRequestClosedEventSingleAffected = event.PullRequestEvent{
Provider: "github",
URL: "https://github.com/padok-team/burrito-closed-single-pr",
Revision: "feature/branch",
Base: "main",
Expand All @@ -148,7 +144,6 @@ var PullRequestClosedEventSingleAffected = event.PullRequestEvent{
}

var PullRequestOpenedEventMultipleAffected = event.PullRequestEvent{
Provider: "github",
URL: "https://github.com/example/other-repo",
Revision: "feature/branch",
Base: "main",
Expand All @@ -158,7 +153,6 @@ var PullRequestOpenedEventMultipleAffected = event.PullRequestEvent{
}

var PullRequestClosedEventMultipleAffected = event.PullRequestEvent{
Provider: "github",
URL: "https://github.com/padok-team/burrito-closed-multi-pr",
Revision: "feature/branch",
Base: "main",
Expand Down
1 change: 0 additions & 1 deletion internal/webhook/event/pullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
)

type PullRequestEvent struct {
Provider string
URL string
Revision string
Base string
Expand Down
1 change: 0 additions & 1 deletion internal/webhook/github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ func (g *Github) GetEvent(p interface{}) (event.Event, error) {
return nil, err
}
e = &event.PullRequestEvent{
Provider: "github",
ID: strconv.FormatInt(payload.PullRequest.Number, 10),
URL: utils.NormalizeUrl(payload.Repository.HTMLURL),
Revision: payload.PullRequest.Head.Ref,
Expand Down
34 changes: 14 additions & 20 deletions internal/webhook/github/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,6 @@ import (
"github.com/stretchr/testify/assert"
)

func TestGithub_IsFromProvider(t *testing.T) {
github := github.Github{}

req, err := http.NewRequest("GET", "/", nil)
assert.NoError(t, err)
req.Header.Set("X-GitHub-Event", "test")
assert.True(t, github.IsFromProvider(req))

req, err = http.NewRequest("GET", "/", nil)
assert.NoError(t, err)
req.Header.Set("X-GitLab-Event", "test")
assert.False(t, github.IsFromProvider(req))
}

func TestGithub_GetEvent_PushEvent(t *testing.T) {
payloadFile, err := os.Open("testdata/github-push-main-event.json")
if err != nil {
Expand All @@ -58,7 +44,10 @@ func TestGithub_GetEvent_PushEvent(t *testing.T) {
}

secret := "test-secret"
github := github.Github{}
github := github.Github{
Secret: secret,
}
err = github.Init()
assert.NoError(t, err)

req.Header.Set("X-GitHub-Event", "push")
Expand All @@ -68,8 +57,9 @@ func TestGithub_GetEvent_PushEvent(t *testing.T) {
assert.NoError(t, err)
expectedMac := hex.EncodeToString(mac.Sum(nil))
req.Header.Set("X-Hub-Signature", fmt.Sprintf("sha1=%s", expectedMac))

evt, err := github.GetEvent(req)
parsed, ok := github.ParseFromProvider(req)
assert.True(t, ok)
evt, err := github.GetEvent(parsed)
assert.NoError(t, err)
assert.IsType(t, &event.PushEvent{}, evt)

Expand Down Expand Up @@ -105,7 +95,10 @@ func TestGithub_GetEvent_PullRequestEvent(t *testing.T) {
}

secret := "test-secret"
github := github.Github{}
github := github.Github{
Secret: secret,
}
err = github.Init()
assert.NoError(t, err)

req.Header.Set("X-GitHub-Event", "pull_request")
Expand All @@ -116,13 +109,14 @@ func TestGithub_GetEvent_PullRequestEvent(t *testing.T) {
expectedMac := hex.EncodeToString(mac.Sum(nil))
req.Header.Set("X-Hub-Signature", fmt.Sprintf("sha1=%s", expectedMac))

evt, err := github.GetEvent(req)
parsed, ok := github.ParseFromProvider(req)
assert.True(t, ok)
evt, err := github.GetEvent(parsed)
assert.NoError(t, err)
assert.IsType(t, &event.PullRequestEvent{}, evt)

pullRequestEvt := evt.(*event.PullRequestEvent)
assert.Equal(t, "20", pullRequestEvt.ID)
assert.Equal(t, "github", pullRequestEvt.Provider)
assert.Equal(t, "https://github.com/padok-team/burrito-examples", pullRequestEvt.URL)
assert.Equal(t, "demo", pullRequestEvt.Revision)
assert.Equal(t, "main", pullRequestEvt.Base)
Expand Down
1 change: 0 additions & 1 deletion internal/webhook/gitlab/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ func (g *Gitlab) GetEvent(p interface{}) (event.Event, error) {
case gitlab.MergeRequestEventPayload:
log.Infof("parsing Gitlab merge request event payload")
e = &event.PullRequestEvent{
Provider: "gitlab",
ID: strconv.Itoa(int(payload.ObjectAttributes.IID)),
URL: utils.NormalizeUrl(payload.Project.WebURL),
Revision: payload.ObjectAttributes.SourceBranch,
Expand Down
Loading

0 comments on commit fc2a0cb

Please sign in to comment.