Skip to content

Commit

Permalink
Add test cases and fix typos in documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jumboduck committed Nov 20, 2023
1 parent 3650e37 commit f6468dd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
20 changes: 7 additions & 13 deletions cmd/kosli/reportApproval.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
const reportApprovalExample = `
# Report that an artifact with a provided fingerprint (sha256) has been approved for
# deployment to environment <yourEnvironmentName>.
# The approval is for all git commits since last approval to this environment.
# The approval is for all git commits since the last approval to this environment.
kosli report approval \
--api-token yourAPIToken \
--description "An optional description for the approval" \
Expand All @@ -32,7 +32,7 @@ kosli report approval \
--fingerprint yourArtifactFingerprint
# Report that a file type artifact has been approved for deployment to environment <yourEnvironmentName>.
# The approval is for all git commits since last approval to this environment.
# The approval is for all git commits since the last approval to this environment.
kosli report approval FILE.tgz \
--api-token yourAPIToken \
--artifact-type file \
Expand Down Expand Up @@ -120,13 +120,6 @@ func newReportApprovalCmd(out io.Writer) *cobra.Command {
addFingerprintFlags(cmd, o.fingerprintOptions)
addDryRunFlag(cmd)

// err := DeprecateFlags(cmd, map[string]string{
// "oldest-commit": "use --environment and oldest commit will be calculated",
// })
// if err != nil {
// logger.Error("failed to configure deprecated flags: %v", err)
// }

err := RequireFlags(cmd, []string{"flow"})
if err != nil {
logger.Error("failed to configure required flags: %v", err)
Expand Down Expand Up @@ -178,16 +171,15 @@ func (o *reportApprovalOptions) run(args []string, request bool) error {
DryRun: false,
Password: global.ApiToken,
}
// error and not dry run -> print error message and return err
// error and dry run -> set src_commit_list to o.newestCommit do not send oldestCommit
// no error we get back None -> set src_commit_list to o.newestCommit do not send oldestCommit
// no error we get back a git commit -> call o.payloadCommitList()

lastApprovedGitCommitResponse, err := kosliClient.Do(getLastApprovedGitCommitParams)

if err != nil {
if !global.DryRun {
// error and not dry run -> print error message and return err
return err
} else {
// error and dry run -> set src_commit_list to o.newestCommit do not send oldestCommit
o.payload.CommitList = []string{o.newestSrcCommit}
}
} else {
Expand All @@ -199,13 +191,15 @@ func (o *reportApprovalOptions) run(args []string, request bool) error {
}

if responseData["commit_sha"] != nil {
// no error we get back a git commit -> call o.payloadCommitList()
o.oldestSrcCommit = responseData["commit_sha"].(string)
o.payload.OldestCommit = o.oldestSrcCommit
o.payload.CommitList, err = o.payloadCommitList()
if err != nil {
return err
}
} else {
// no error we get back None -> set src_commit_list to o.newestCommit do not send oldestCommit
o.payload.CommitList = []string{o.newestSrcCommit}
}

Expand Down
26 changes: 26 additions & 0 deletions cmd/kosli/reportApproval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type ApprovalReportTestSuite struct {
defaultKosliArguments string
artifactFingerprint string
flowName string
envName string
}

func (suite *ApprovalReportTestSuite) SetupTest() {
Expand All @@ -24,9 +25,11 @@ func (suite *ApprovalReportTestSuite) SetupTest() {
suite.defaultKosliArguments = fmt.Sprintf(" --host %s --org %s --api-token %s", global.Host, global.Org, global.ApiToken)
suite.artifactFingerprint = "847411c6124e719a4e8da2550ac5c116b7ff930493ce8a061486b48db8a5aaa0"
suite.flowName = "approval-test"
suite.envName = "staging"

CreateFlow(suite.flowName, suite.T())
CreateArtifact(suite.flowName, suite.artifactFingerprint, "foobar", suite.T())
CreateEnv(global.Org, suite.envName, "K8S", suite.T())
}

func (suite *ApprovalReportTestSuite) TestApprovalReportCmd() {
Expand All @@ -37,6 +40,29 @@ func (suite *ApprovalReportTestSuite) TestApprovalReportCmd() {
--newest-commit HEAD --oldest-commit HEAD~3` + suite.defaultKosliArguments,
golden: fmt.Sprintf("approval created for artifact: %s\n", suite.artifactFingerprint),
},
{
name: "report approval with an environment name works",
cmd: `report approval --fingerprint ` + suite.artifactFingerprint + ` --flow ` + suite.flowName + ` --repo-root ../..
--newest-commit HEAD --oldest-commit HEAD~3` + ` --environment staging` + suite.defaultKosliArguments,
golden: fmt.Sprintf("approval created for artifact: %s\n", suite.artifactFingerprint),
},
{
name: "report approval with an environment name and no oldest-commit and no newest-commit works",
cmd: `report approval --fingerprint ` + suite.artifactFingerprint + ` --flow ` + suite.flowName + ` --repo-root ../.. ` +
` --environment ` + suite.envName + suite.defaultKosliArguments,
golden: fmt.Sprintf("approval created for artifact: %s\n", suite.artifactFingerprint),
},
{
wantError: true,
name: "report approval with no environment name or oldest commit fails",
cmd: `report approval --fingerprint ` + suite.artifactFingerprint + ` --flow ` + suite.flowName + ` --repo-root ../.. ` +
suite.defaultKosliArguments,
golden: "Error: at least one of --environment, --oldest-commit is required\n",
},
// Here is a case we need to investigate how to test:
// - Create approval with '--newest-commit HEAD~5', '--oldest-commit HEAD~7' and '--environment staging',
// then create approval only with '--environment staging',
// the resulting payload should contain a commit list of 4 commits, and an oldest_commit of HEAD~5
}
runTestCmd(suite.T(), tests)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kosli/requestApproval.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The request should be reviewed in the Kosli UI.
const requestApprovalExample = `
# Request an approval for an artifact with a provided fingerprint (sha256)
# for deployment to environment <yourEnvironmentName>.
# The approval is for all git commits since last approval to this environment.
# The approval is for all git commits since the last approval to this environment.
kosli request approval \
--api-token yourAPIToken \
--description "An optional description for the approval" \
Expand All @@ -26,7 +26,7 @@ kosli request approval \
--fingerprint yourArtifactFingerprint
# Request that a file type artifact needs approval for deployment to environment <yourEnvironmentName>.
# The approval is for all git commits since last approval to this environment.
# The approval is for all git commits since the last approval to this environment.
kosli request approval FILE.tgz \
--api-token yourAPIToken \
--artifact-type file \
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ services:
server-index:
networks: [ cli_net ]
depends_on: [ mongo_rs_initiate, minio ]
image: 772819027869.dkr.ecr.eu-central-1.amazonaws.com/merkely:latest
image: 772819027869.dkr.ecr.eu-central-1.amazonaws.com/merkely:0d3e1c6
command: /app/src/documentdb/wait_till_ready_or_raise.py
container_name: cli_kosli_server-index
read_only: true
Expand All @@ -32,7 +32,7 @@ services:
condition: service_started
server-index:
condition: service_completed_successfully
image: 772819027869.dkr.ecr.eu-central-1.amazonaws.com/merkely:latest
image: 772819027869.dkr.ecr.eu-central-1.amazonaws.com/merkely:0d3e1c6
env_file: [ "./mongo/mongo.env" ]
environment:
KOSLI_HOSTNAME: localhost
Expand Down

0 comments on commit f6468dd

Please sign in to comment.