Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions .github/workflows/daily-cli-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ jobs:
kosli_querying_api_token: ${{ secrets.KOSLI_API_TOKEN_PROD }}
sonarqube_token: ${{ secrets.KOSLI_SONARQUBE_TOKEN }}

aws-contract-tests:
name: AWS Contract Tests
contract-tests:
name: Contract Tests
runs-on: ubuntu-latest
permissions:
id-token: write
Expand All @@ -81,8 +81,10 @@ jobs:
role-duration-seconds: 2400
role-session-name: ${{ github.event.repository.name }}

- name: Run AWS contract tests
run: make test_smoke_aws
- name: Run contract tests
run: make test_contract
env:
KOSLI_GITHUB_TOKEN: ${{ secrets.KOSLI_GITHUB_TOKEN }}
Comment thread
jumboduck marked this conversation as resolved.

slack-notification-on-failure:
runs-on: ubuntu-24.04
Expand All @@ -93,7 +95,7 @@ jobs:
[
set-trail-name,
test,
aws-contract-tests,
contract-tests,
]
if: ${{ always() && contains(join(needs.*.result, ','), 'failure') && github.ref == 'refs/heads/main' }}
steps:
Expand Down
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ test_smoke_aws: ensure_gotestsum ## Run AWS contract and smoke tests against rea
@echo "Requires AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to be set"
@$(GOTESTSUM) -- -v -p=1 -run "LambdaContract_RealAWS|AWSTestSuite/TestGetLambdaPackageData|AWSTestSuite/TestGetEcsTasksData|AWSTestSuite/TestGetS3Data" ./internal/aws/

test_smoke_github: ensure_gotestsum ## Run GitHub contract tests against real GitHub API (requires KOSLI_GITHUB_TOKEN)
@echo "Running GitHub contract tests against real GitHub API..."
@echo "Requires KOSLI_GITHUB_TOKEN to be set"
@$(GOTESTSUM) -- -v -p=1 -run "GitHubContract_RealGitHub" ./internal/github/

test_contract: test_smoke_aws test_smoke_github ## Run all contract tests against real external services


test_docs: deps vet ensure_network test_setup ## Test docs
./bin/test_docs_cmds.sh docs.kosli.com/content/use_cases/simulating_a_devops_system/_index.md
Expand Down
24 changes: 24 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,30 @@
- [x] Slice 3: Show params in `--show-input` output
- [x] Slice 4: Update help text and examples

## Fakes & contract tests for GitHub API integration

### Slice 1: FakeGitHubClient + contract tests (`internal/github`) ← active

- [x] `TestGitHubContract_Fake`: V2 returns PRs for commit with PRs
- [x] `TestGitHubContract_Fake`: V2 returns empty for commit with no PRs
- [x] `TestGitHubContract_Fake`: V2 returns error when Err is injected
- [x] `TestGitHubContract_Fake`: V1 returns PRs for commit with PRs
- [x] `TestGitHubContract_Fake`: V1 returns empty for commit with no PRs
- [x] `TestGitHubContract_Fake`: V1 returns error when Err is injected
- [x] `TestGitHubContract_RealGitHub`: same contract, env-gated on `KOSLI_GITHUB_TOKEN`

### Slice 2: Thread fake through command layer ← active

- [x] Add `ProviderAndLabel() (string, string)` to `types.PRRetriever` interface
- [x] Implement on `GithubConfig` → `("github", "pull request")`
- [x] Implement on `GitlabConfig` → `("gitlab", "merge request")`
- [x] Implement on `AzureConfig` → `("azure", "pull request")`
- [x] Implement on bitbucket `Config` → `("bitbucket", "pull request")`
- [x] Implement on `FakeGitHubClient` → `("github", "pull request")`
- [x] Replace reflection in `getGitProviderAndLabel` with `retriever.ProviderAndLabel()`
- [x] Inject fake in `assertPRGithub_test.go`
- [x] Inject fake in `attestPRGithub_test.go`

## Fakes & contract tests for cloud provider integrations (#758)

### Lambda (done — this PR)
Expand Down
11 changes: 6 additions & 5 deletions cmd/kosli/assertPRGithub.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import (
"io"

ghUtils "github.com/kosli-dev/cli/internal/github"
"github.com/kosli-dev/cli/internal/types"
"github.com/spf13/cobra"
)

type assertPullRequestGithubOptions struct {
githubConfig *ghUtils.GithubConfig
commit string
retriever types.PRRetriever
commit string
}

const assertPRGithubShortDesc = `Assert a Github pull request for a git commit exists. `

const assertPRGithubLongDesc = assertPRGithubShortDesc + `
The command exits with non-zero exit code
The command exits with non-zero exit code
if no pull requests were found for the commit.`

const assertPRGithubExample = `
Expand All @@ -38,7 +39,7 @@ func newAssertPullRequestGithubCmd(out io.Writer) *cobra.Command {
Example: assertPRGithubExample,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
o.githubConfig = ghUtils.NewGithubConfig(githubFlagsValues.Token, githubFlagsValues.BaseURL,
o.retriever = ghUtils.NewGithubRetrieverFunc(githubFlagsValues.Token, githubFlagsValues.BaseURL,
githubFlagsValues.Org, githubFlagsValues.Repository)
return o.run(args)
},
Expand All @@ -58,7 +59,7 @@ func newAssertPullRequestGithubCmd(out io.Writer) *cobra.Command {
}

func (o *assertPullRequestGithubOptions) run(args []string) error {
pullRequestsEvidence, err := o.githubConfig.PREvidenceForCommitV2(o.commit)
pullRequestsEvidence, err := o.retriever.PREvidenceForCommitV2(o.commit)
if err != nil {
return err
}
Expand Down
43 changes: 27 additions & 16 deletions cmd/kosli/assertPRGithub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,69 @@ import (
"fmt"
"testing"

"github.com/kosli-dev/cli/internal/testHelpers"
ghUtils "github.com/kosli-dev/cli/internal/github"
"github.com/kosli-dev/cli/internal/types"
"github.com/stretchr/testify/suite"
)

// Define the suite, and absorb the built-in basic suite
// functionality from testify - including a T() method which
// returns the current testing context
type AssertPRGithubCommandTestSuite struct {
suite.Suite
defaultKosliArguments string
commitWithPR string
commitWithNoPR string
}

func (suite *AssertPRGithubCommandTestSuite) SetupTest() {
testHelpers.SkipIfEnvVarUnset(suite.T(), []string{"KOSLI_GITHUB_TOKEN"})
suite.commitWithPR = "480e5a00379a52b8e184d6815080242a878ca295"
suite.commitWithNoPR = "7d1db1c8b7e71ee0ce369f1b722cc8844d3a7af6"

ghUtils.NewGithubRetrieverFunc = func(token, baseURL, org, repository string) types.PRRetriever {
return &ghUtils.FakeGitHubClient{
PRsByCommit: map[string][]*types.PREvidence{
suite.commitWithPR: {{URL: "https://github.com/kosli-dev/cli/pull/1", State: "MERGED"}},
},
}
}

Comment thread
jumboduck marked this conversation as resolved.
global = &GlobalOpts{
ApiToken: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImNkNzg4OTg5In0.e8i_lA_QrEhFncb05Xw6E_tkCHU9QfcY4OLTVUCHffY",
Org: "docs-cmd-test-user",
Host: "http://localhost:8001",
}
suite.defaultKosliArguments = fmt.Sprintf(" --host %s --org %s --api-token %s", global.Host, global.Org, global.ApiToken)
suite.defaultKosliArguments = fmt.Sprintf(" --github-token fake --host %s --org %s --api-token %s", global.Host, global.Org, global.ApiToken)
Comment thread
jumboduck marked this conversation as resolved.
}

func (suite *AssertPRGithubCommandTestSuite) TearDownTest() {
ghUtils.ResetGithubRetrieverFunc()
}

func (suite *AssertPRGithubCommandTestSuite) TestAssertPRGithubCmd() {
tests := []cmdTestCase{
{
name: "assert Github PR evidence passes when commit has a PR in github",
cmd: `assert pullrequest github --github-org kosli-dev --repository cli
--commit ` + testHelpers.GithubCommitWithPR() + suite.defaultKosliArguments,
golden: fmt.Sprintf("found [1] pull request(s) in Github for commit: %s\n", testHelpers.GithubCommitWithPR()),
--commit ` + suite.commitWithPR + suite.defaultKosliArguments,
golden: fmt.Sprintf("found [1] pull request(s) in Github for commit: %s\n", suite.commitWithPR),
},
{
wantError: true,
name: "assert Github PR evidence fails when commit has no PRs in github",
cmd: `assert pullrequest github --github-org kosli-dev --repository cli
--commit 19aab7f063147614451c88969602a10afbabb43d` + suite.defaultKosliArguments,
golden: "Error: assert failed: found no pull request(s) in Github for commit: 19aab7f063147614451c88969602a10afbabb43d\n",
cmd: `assert pullrequest github --github-org kosli-dev --repository cli
--commit ` + suite.commitWithNoPR + suite.defaultKosliArguments,
golden: fmt.Sprintf("Error: assert failed: found no pull request(s) in Github for commit: %s\n", suite.commitWithNoPR),
},
{
wantError: true,
name: "assert Github PR evidence fails when commit does not exist",
cmd: `assert pullrequest github --github-org kosli-dev --repository cli
--commit 19aab7f063147614451c88969602a10afba123ab` + suite.defaultKosliArguments,
golden: "Error: assert failed: found no pull request(s) in Github for commit: 19aab7f063147614451c88969602a10afba123ab\n",
cmd: `assert pullrequest github --github-org kosli-dev --repository cli
--commit 0000000000000000000000000000000000000000` + suite.defaultKosliArguments,
golden: "Error: assert failed: found no pull request(s) in Github for commit: 0000000000000000000000000000000000000000\n",
},
}

runTestCmd(suite.T(), tests)
}

// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestAssertPRGithubCommandTestSuite(t *testing.T) {
suite.Run(t, new(AssertPRGithubCommandTestSuite))
}
2 changes: 1 addition & 1 deletion cmd/kosli/attestPRGithub.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func newAttestGithubPRCmd(out io.Writer) *cobra.Command {
},
RunE: func(cmd *cobra.Command, args []string) error {
o.repoURLExplicit = cmd.Flags().Changed("repo-url")
o.retriever = ghUtils.NewGithubConfig(githubFlagsValues.Token, githubFlagsValues.BaseURL,
o.retriever = ghUtils.NewGithubRetrieverFunc(githubFlagsValues.Token, githubFlagsValues.BaseURL,
githubFlagsValues.Org, o.repoName)
return o.run(args)
},
Expand Down
Loading
Loading