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

WIP: Test suite #358

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
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
76 changes: 73 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ all-docker-darwin: deps-docker test-docker build-docker-darwin

gen: deps
GO111MODULE=on $(GOCMD) generate ./...
deps:
deps:
GO111MODULE=on ${GOCMD} get -v
test: gen
GO111MODULE=on $(GOCMD) fmt ./...
GO111MODULE=on $(GOCMD) vet ./...
GO111MODULE=on $(GOCMD) test -v ./...
GO111MODULE=on $(GOCMD) test -v -run '^Test(A[^P].*|[^A].*)' ./...

clean:
$(GOCMD) clean
Expand All @@ -42,7 +42,7 @@ build-darwin-arm64: test
GO111MODULE=on CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 $(GOCMD) build -ldflags '${LDFLAGS} -X "${PKG}/cmd.lagoonCLIBuildGoVersion=${GO_VER}"' -o builds/lagoon-cli-${VERSION}-darwin-arm64 -v

docs: test
LAGOON_GEN_DOCS=true GO111MODULE=on $(GOCMD) run main.go --docs
LAGOON_GEN_DOCS=true GO111MODULE=on $(GOCMD) run main.go --docs

tidy:
GO111MODULE=on $(GOCMD) mod tidy
Expand Down Expand Up @@ -98,3 +98,73 @@ install-linux:
install-darwin:
cp builds/lagoon-cli-${VERSION}-darwin-amd64 ${ARTIFACT_DESTINATION}/lagoon

release-patch:
$(eval VERSION=$(shell ${PWD}/increment_ver.sh -p $(shell git describe --abbrev=0 --tags)))
git tag $(VERSION)
mkdocs gh-deploy
git push $(GIT_ORIGIN) main --tags

release-minor:
$(eval VERSION=$(shell ${PWD}/increment_ver.sh -m $(shell git describe --abbrev=0 --tags)))
git tag $(VERSION)
mkdocs gh-deploy
git push $(GIT_ORIGIN) main --tags

release-major:
$(eval VERSION=$(shell ${PWD}/increment_ver.sh -M $(shell git describe --abbrev=0 --tags)))
git tag $(VERSION)
mkdocs gh-deploy
git push $(GIT_ORIGIN) main --tags

api-tests: gen
GO111MODULE=on $(GOCMD) fmt ./...
GO111MODULE=on $(GOCMD) vet ./...
GO111MODULE=on $(GOCMD) test -v -run '^(TestAPI)' ./...

# upstream
CI_BUILD_TAG ?= lagoon-cli
CORE_REPO=https://github.com/uselagoon/lagoon.git
CORE_TREEISH=make-export-refactoring

LAGOON_CORE_IMAGE_REPO=testlagoon
LAGOON_CORE_IMAGE_TAG=main

TEMP_CONFIG_FILE := temp_config.yaml

generate-config:
TOKEN=$(TOKEN) \
envsubst < local-dev/config.tpl > $(TEMP_CONFIG_FILE)

clean-config:
@rm -f $(TEMP_CONFIG_FILE)

.PHONY: cli-tests-with-development-api
cli-tests-with-development-api: development-api
TOKEN=$$(docker run -e JWTSECRET=super-secret-string \
-e JWTAUDIENCE=api.dev \
-e JWTUSER=localadmin \
uselagoon/tests \
python3 /ansible/tasks/api/admin_token.py) \
&& $(MAKE) generate-config TOKEN=$$TOKEN \
&& $(MAKE) api-tests \
&& $(MAKE) clean-config \
&& $(MAKE) CI_BUILD_TAG=$(CI_BUILD_TAG) development-api-down

.PHONY: development-api
development-api:
export LAGOON_CORE=$$(mktemp -d ./lagoon-core.XXX) \
&& git clone $(CORE_REPO) "$$LAGOON_CORE" \
&& cd "$$LAGOON_CORE" \
&& git checkout $(CORE_TREEISH) \
&& IMAGE_REPO=$(LAGOON_CORE_IMAGE_REPO) IMAGE_REPO_TAG=$(LAGOON_CORE_IMAGE_TAG) COMPOSE_STACK_NAME=core-$(CI_BUILD_TAG) docker compose -p core-$(CI_BUILD_TAG) pull \
&& IMAGE_REPO=$(LAGOON_CORE_IMAGE_REPO) IMAGE_REPO_TAG=$(LAGOON_CORE_IMAGE_TAG) COMPOSE_STACK_NAME=core-$(CI_BUILD_TAG) $(MAKE) compose-api-logs-development

.PHONY: development-api-down
development-api-down:
cd lagoon-core* && \
docker-compose -p core-$(CI_BUILD_TAG) --compatibility down -v --remove-orphans

.PHONY: down
down:
$(MAKE) development-api-down
docker-compose -p $(CI_BUILD_TAG) --compatibility down -v --remove-orphans
80 changes: 80 additions & 0 deletions cmd/environment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package cmd

import (
"bytes"
"fmt"
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
"testing"

"github.com/spf13/cobra"
)

func TestAPIEnvironmentCommands(t *testing.T) {
tests := []struct {
name string
cmdArgs []string
setupCmd func(*cobra.Command, pflag.FlagSet)
expectOut []string
expectErr bool
expectedErrString string
}{
{
name: "List Backups",
cmdArgs: []string{"list", "backups", "--project=lagoon-demo", "--environment=main"},
setupCmd: func(cmd *cobra.Command, flags pflag.FlagSet) {
cmd.AddCommand(listCmd)
listCmd.AddCommand(listBackupsCmd)
},
expectOut: []string{"e2e1d31b4a7dfc1687f469b6673f6bf2c0aabee0cc6b3f1bdbde710a9bc6280f", "files", "e2e1d31b4a7dfc1687f469b6673f6bf2c0aabee0cc6b3f1bdbde710a9bc6280d", "mariadb"},
expectErr: false,
},
{
name: "Update Environment",
cmdArgs: []string{"update", "environment", "--project=lagoon-demo", "--environment=pr-175", "--auto-idle=0"},
setupCmd: func(cmd *cobra.Command, flags pflag.FlagSet) {
cmd.AddCommand(updateCmd)
updateCmd.AddCommand(updateEnvironmentCmd)
},
expectOut: []string{"success", "pr-175"},
expectErr: false,
},
{
name: "Delete Environment",
cmdArgs: []string{"delete", "environment", "--project=lagoon-demo", "--environment=pr-175", "--force"},
setupCmd: func(cmd *cobra.Command, flags pflag.FlagSet) {
cmd.AddCommand(deleteCmd)
deleteCmd.AddCommand(deleteEnvCmd)
},
expectOut: []string{"success"},
expectErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := rootCmd
tt.cmdArgs = append(tt.cmdArgs, "--output-json", "--config-file=../temp_config.yaml")
cmd.SetArgs(tt.cmdArgs)
flags := pflag.FlagSet{}
tt.setupCmd(cmd, flags)

var out bytes.Buffer
cmd.SetOut(&out)
cmd.SetErr(&out)

err := cmd.Execute()
if err != nil && tt.expectErr {
assert.NotEmpty(t, err)
fmt.Println("err:", err)
return
} else if err != nil {
t.Fatalf("Error executing command: %v", err)
}

for _, eo := range tt.expectOut {
assert.Contains(t, out.String(), eo)
}

})
}
}
111 changes: 111 additions & 0 deletions cmd/project_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package cmd

import (
"bytes"
"fmt"
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
"testing"

"github.com/spf13/cobra"
)

func TestAPIProjectCommands(t *testing.T) {
tests := []struct {
name string
cmdArgs []string
setupCmd func(*cobra.Command, pflag.FlagSet)
expectOut []string
expectErr bool
expectedErrString string
}{
{
name: "Add Project",
cmdArgs: []string{"add", "project", "--project=test-project", "--production-environment=main", "--deploytarget=4", "--git-url=https://github.com/lagoon-examples/drupal10-base"},
setupCmd: func(cmd *cobra.Command, flags pflag.FlagSet) {
cmd.AddCommand(addCmd)
addCmd.AddCommand(addProjectCmd)
},
expectOut: []string{"success", "test-project", "https://github.com/lagoon-examples/drupal10-base"},
expectErr: false,
},
{
name: "Add Project to an Organization",
cmdArgs: []string{"add", "project", "--project=test-organization-project", "--organization-name=lagoon-demo-organization", "--production-environment=main", "--deploytarget=4", "--git-url=https://github.com/lagoon-examples/drupal10-base"},
setupCmd: func(cmd *cobra.Command, flags pflag.FlagSet) {
cmd.AddCommand(addCmd)
addCmd.AddCommand(addProjectCmd)
},
expectOut: []string{"success", "test-organization-project", "https://github.com/lagoon-examples/drupal10-base", "lagoon-demo-organization"},
expectErr: false,
},
{
name: "Update a Project",
cmdArgs: []string{"update", "project", "--project=lagoon-demo", "--auto-idle=0"},
setupCmd: func(cmd *cobra.Command, flags pflag.FlagSet) {
cmd.AddCommand(updateCmd)
updateCmd.AddCommand(updateProjectCmd)
},
expectOut: []string{"success", "lagoon-demo"},
expectErr: false,
},
{
name: "Remove a Project from an Organization",
cmdArgs: []string{"delete", "organization-project", "--project=test-organization-project", "--organization-name=lagoon-demo-organization", "--force"},
setupCmd: func(cmd *cobra.Command, flags pflag.FlagSet) {
cmd.AddCommand(deleteCmd)
deleteCmd.AddCommand(removeProjectFromOrganizationCmd)
},
expectOut: []string{"success", "test-organization-project", "lagoon-demo-organization"},
expectErr: false,
},
{
name: "Delete a Project",
cmdArgs: []string{"delete", "project", "--project=test-project", "--force"},
setupCmd: func(cmd *cobra.Command, flags pflag.FlagSet) {
cmd.AddCommand(deleteCmd)
deleteCmd.AddCommand(deleteProjectCmd)
},
expectOut: []string{"success"},
expectErr: false,
},
{
name: "Delete an Organization Project",
cmdArgs: []string{"delete", "project", "--project=test-organization-project", "--force"},
setupCmd: func(cmd *cobra.Command, flags pflag.FlagSet) {
cmd.AddCommand(deleteCmd)
deleteCmd.AddCommand(deleteProjectCmd)
},
expectOut: []string{"success"},
expectErr: false,
},
// TODO: Add tests for metadata commands
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := rootCmd
tt.cmdArgs = append(tt.cmdArgs, "--output-json", "--config-file=../temp_config.yaml")
cmd.SetArgs(tt.cmdArgs)
flags := pflag.FlagSet{}
tt.setupCmd(cmd, flags)

var out bytes.Buffer
cmd.SetOut(&out)
cmd.SetErr(&out)

err := cmd.Execute()
if err != nil && tt.expectErr {
assert.NotEmpty(t, err)
fmt.Println("err:", err)
return
} else if err != nil {
t.Fatalf("Error executing command: %v", err)
}

for _, eo := range tt.expectOut {
assert.Contains(t, out.String(), eo)
}

})
}
}
1 change: 0 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,6 @@ func versionCheck(lagoon string) error {

func getLagoonConfigFile(configPath *string, configName *string, configExtension *string, createConfig bool, cmd *cobra.Command) error {
// check if we have an envvar or flag to define our confg file
var configFilePath string
configFilePath, err := cmd.Flags().GetString("config-file")
if err != nil {
return fmt.Errorf("error reading flag `config-file`: %v", err)
Expand Down
9 changes: 9 additions & 0 deletions local-dev/config.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
current: test
default: test
lagoons:
test:
graphql: "http://localhost:3000/graphql"
hostname: "localhost"
port: "2020"
token: ${TOKEN}
version: v1.9.0