Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
est-suse committed Oct 31, 2023
2 parents 5ac61ab + 2ff8ae8 commit a5448e5
Show file tree
Hide file tree
Showing 43 changed files with 921 additions and 452 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/run-distros.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name: Run Distros Framework
on:
push:

pull_request:
branches: [ main ]

permissions:
contents: read

Expand Down Expand Up @@ -37,5 +40,5 @@ jobs:
uses: golangci/golangci-lint-action@v3
with:
version: v1.53
args: --timeout 5m --issues-exit-code=0
args: --timeout 10m --issues-exit-code=0

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
*.terraform*
*.tfstate*
workloads/amd64/plan.yaml
/config/product.yaml
/config/.env
/config/*.tfvars
203 changes: 155 additions & 48 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,86 +1,193 @@
include ./config.mk
include ./config/.env

TAG_NAME := $(if $(TAG_NAME),$(TAG_NAME),distros)

TAGNAME ?= default

.PHONY: test-env-up
test-env-up:
docker build . -q -f ./scripts/Dockerfile.build -t acceptance-test-${TAGNAME}
@docker build . -q -f ./scripts/Dockerfile.build -t acceptance-test-${TAG_NAME}

.PHONY: test-run
test-run:
@docker run -d --name acceptance-test-${IMGNAME} -t \
-e AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}" \
-e AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}" \
-v ${ACCESS_KEY_LOCAL}:/go/src/github.com/rancher/distros-test-framework/config/.ssh/aws_key.pem \
acceptance-test-${TAGNAME} sh -c 'cd ./entrypoint; \
if [ -n "${TESTDIR}" ]; then \
if [ "${TESTDIR}" = "upgradecluster" ]; then \
go test -timeout=45m -v ./upgradecluster/... -installVersionOrCommit "${INSTALLTYPE}"; \
elif [ "${TESTDIR}" = "versionbump" ]; then \
go test -timeout=45m -v -tags=versionbump ./versionbump/... -cmd "${CMD}" -expectedValue "${VALUE}" \
-expectedValueUpgrade "${VALUEUPGRADED}" -installVersionOrCommit "${INSTALLTYPE}" -channel "${CHANNEL}" -testCase "${TESTCASE}" \
-deployWorkload "${DEPLOYWORKLOAD}" -workloadName "${WORKLOADNAME}" -description "${DESCRIPTION}"; \
elif [ "${TESTDIR}" = "mixedoscluster" ]; then \
go test -timeout=45m -v -tags=mixedos ./mixedoscluster/...; \
fi; \
elif [ -z "${TESTDIR}" ]; then \
go test -timeout=45m -v ./createcluster/...; \
fi;'

.PHONY: test-logs
@docker run -dt --name acceptance-test-${IMG_NAME} \
-e AWS_ACCESS_KEY_ID=$${AWS_ACCESS_KEY_ID} \
-e AWS_SECRET_ACCESS_KEY=$${AWS_SECRET_ACCESS_KEY} \
--env-file ./config/.env \
-v ${ACCESS_KEY_LOCAL}:/go/src/github.com/rancher/distros-test-framework/config/.ssh/aws_key.pem \
-v ./scripts/test-runner.sh:/go/src/github.com/rancher/distros-test-framework/scripts/test-runner.sh \
acceptance-test-${TAG_NAME} && \
make image-stats IMG_NAME=${IMG_NAME} && \
make test-logs USE=IMG_NAME acceptance-test-${IMG_NAME}


## Use this to run on the same environement + cluster from the previous last container -${TAGNAME} created
test-run-state:
DOCKER_COMMIT=$$? \
CONTAINER_ID=$(shell docker ps -a -q --filter ancestor=acceptance-test-${TAG_NAME} | head -n 1); \
if [ -z "$${CONTAINER_ID}" ]; then \
echo "No matching container found."; \
exit 1; \
else \
docker commit $$CONTAINER_ID teststate:latest; \
if [ $$DOCKER_COMMIT -eq 0 ]; then \
docker run -dt --name acceptance-test-${TEST_STATE} --env-file ./config/.env \
-e AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID} \
-e AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY} \
-v ${ACCESS_KEY_LOCAL}:/go/src/github.com/rancher/distros-test-framework/config/.ssh/aws_key.pem \
-v ./scripts/test-runner.sh:/go/src/github.com/rancher/distros-test-framework/scripts/test-runner.sh \
teststate:latest && \
make test-logs USE=TEST_STATE acceptance-test-${TEST_STATE} \
echo "Docker run exit code: $$?"; \
else \
echo "Failed to commit container"; \
exit 1; \
fi; \
fi

## use this to test a new run on a totally new fresh environment after delete also aws resources
test-complete: test-env-clean test-env-down remove-tf-state test-env-up test-run

test-logs:
@docker logs -f acceptance-test${IMGNAME}
@if [ "${USE}" = "IMG_NAME" ]; then \
docker logs -f acceptance-test-${IMG_NAME}; \
elif [ "${USE}" = "TEST_STATE" ]; then \
docker logs -f acceptance-test-${TEST_STATE}; \
fi;

image-stats:
@./scripts/docker_stats.sh $$IMG_NAME 2>> /tmp/image-${IMG_NAME}_stats_output.log &

.PHONY: test-env-down
test-env-down:
@echo "Removing containers and images"
@docker stop $$(docker ps -a -q --filter="name=acceptance-test*")
@docker rm $$(docker ps -a -q --filter="name=acceptance-test*") ; \
docker rmi --force $$(docker images -q --filter="reference=acceptance-test*")
@echo "Removing containers"
@docker ps -a -q --filter="name=acceptance-test*" | xargs -r docker rm -f 2>/tmp/container_${IMG_NAME}.log || true
@echo "Removing acceptance-test images"
@docker images -q --filter="reference=acceptance-test*" | xargs -r docker rmi -f 2>/tmp/container_${IMG_NAME}.log || true
@echo "Removing dangling images"
@docker images -q -f "dangling=true" | xargs -r docker rmi -f 2>/tmp/container_${IMG_NAME}.log || true
@echo "Removing state images"
@docker images -q --filter="reference=teststate:latest" | xargs -r docker rmi -f 2>/tmp/container_${IMG_NAME}.log || true

.PHONY: test-env-clean
test-env-clean:
@./scripts/delete_resources.sh

.PHONY: test-complete
test-complete: test-env-clean test-env-down remove-tf-state test-env-up test-run

#========================= Run acceptance tests locally =========================#
.PHONY: remove-tf-state
remove-tf-state:
@rm -rf ./modules/${PRODUCT}/.terraform
@rm -rf ./modules/${PRODUCT}/.terraform.lock.hcl ./modules/${PRODUCT}/terraform.tfstate ./modules/${PRODUCT}/terraform.tfstate.backup

@rm -rf ./modules/${ENV_PRODUCT}/.terraform
@rm -rf ./modules/${ENV_PRODUCT}/.terraform.lock.hcl ./modules/${ENV_PRODUCT}/terraform.tfstate ./modules/${ENV_PRODUCT}/terraform.tfstate.backup

#========================= Run acceptance tests locally =========================#

.PHONY: test-create
test-create:
@go test -timeout=45m -v ./entrypoint/createcluster/...
@go test -timeout=45m -v -count=1 ./entrypoint/createcluster/...


.PHONY: test-upgrade-suc
test-upgrade-suc:
@go test -timeout=45m -v -tags=upgradesuc ./entrypoint/upgradecluster/... -upgradeVersion ${UPGRADEVERSION}
@go test -timeout=45m -v -tags=upgradesuc -count=1 ./entrypoint/upgradecluster/... -sucUpgradeVersion ${SUC_UPGRADE_VERSION}


.PHONY: test-upgrade-manual
test-upgrade-manual:
@go test -timeout=45m -v -tags=upgrademanual ./entrypoint/upgradecluster/... -installVersionOrCommit ${INSTALLTYPE}
@go test -timeout=45m -v -tags=upgrademanual -count=1 ./entrypoint/upgradecluster/... -installVersionOrCommit ${INSTALL_VERSION_OR_COMMIT} -channel ${CHANNEL}

.PHONY: test-version-bump
test-version-bump:
-cmd "${CMD}" \
-expectedValue ${VALUE} \
-expectedValueUpgrade ${VALUEUPGRADED} \
-installVersionOrCommit ${INSTALLTYPE} -channel ${CHANNEL} \
-testCase "${TESTCASE}" -deployWorkload ${DEPLOYWORKLOAD} -workloadName ${WORKLOADNAME} -description "${DESCRIPTION}"

.PHONY: test-create-mixedos
test-create-mixedos:
@go test -timeout=45m -v ./entrypoint/mixedoscluster/...
@go test -timeout=45m -v -count=1 ./entrypoint/mixedoscluster/... $(if ${SONOBUOY_VERSION},-sonobuoyVersion ${SONOBUOY_VERSION})

.PHONY: test-create-dualstack
test-create-dualstack:
@go test -timeout=45m -v -count=1 ./entrypoint/dualstack/...

.PHONY: test-version-bump
test-version-bump:
@go test -timeout=45m -v -count=1 ./entrypoint/versionbump/... -tags=versionbump \
-cmd "${CMD}" \
-expectedValue ${EXPECTED_VALUE} \
$(if ${VALUE_UPGRADED},-expectedValueUpgrade ${VALUE_UPGRADED}) \
$(if ${INSTALL_VERSION_OR_COMMIT},-installVersionOrCommit ${INSTALL_VERSION_OR_COMMIT}) \
$(if ${CHANNEL},-channel ${CHANNEL}) \
$(if ${TEST_CASE},-testCase "${TEST_CASE}") \
$(if ${WORKLOAD_NAME},-workloadName ${WORKLOAD_NAME}) \
$(if ${DESCRIPTION},-description "${DESCRIPTION}") \
$(if ${DEPLOY_WORKLOAD},-deployWorkload ${DEPLOY_WORKLOAD}) \


.PHONY: test-etcd-bump
test-etcd-bump:
@go test -timeout=45m -v -count=1 ./entrypoint/versionbump/... -tags=etcd \
-expectedValue ${EXPECTED_VALUE} \
$(if ${VALUE_UPGRADED},-expectedValueUpgrade ${VALUE_UPGRADED}) \
$(if ${INSTALL_VERSION_OR_COMMIT},-installVersionOrCommit ${INSTALL_VERSION_OR_COMMIT}) \
$(if ${CHANNEL},-channel ${CHANNEL}) \
$(if ${TEST_CASE},-testCase "${TEST_CASE}") \
$(if ${WORKLOAD_NAME},-workloadName ${WORKLOAD_NAME}) \
$(if ${DEPLOY_WORKLOAD},-deployWorkload ${DEPLOY_WORKLOAD})


.PHONY: test-runc-bump
test-runc-bump:
@go test -timeout=45m -v -count=1 ./entrypoint/versionbump/... -tags=runc \
-expectedValue ${EXPECTED_VALUE} \
$(if ${VALUE_UPGRADED},-expectedValueUpgrade ${VALUE_UPGRADED}) \
$(if ${INSTALL_VERSION_OR_COMMIT},-installVersionOrCommit ${INSTALL_VERSION_OR_COMMIT}) \
$(if ${CHANNEL},-channel ${CHANNEL}) \
$(if ${TEST_CASE},-testCase "${TEST_CASE}") \
$(if ${WORKLOAD_NAME},-workloadName ${WORKLOAD_NAME}) \
$(if ${DEPLOY_WORKLOAD},-deployWorkload ${DEPLOY_WORKLOAD})


.PHONY: test-cilium-bump
test-cilium-bump:
@go test -timeout=45m -v -count=1 ./entrypoint/versionbump/... -tags=cilium \
-expectedValue ${EXPECTED_VALUE} \
$(if ${VALUE_UPGRADED},-expectedValueUpgrade ${VALUE_UPGRADED}) \
$(if ${INSTALL_VERSION_OR_COMMIT},-installVersionOrCommit ${INSTALL_VERSION_OR_COMMIT}) \
$(if ${CHANNEL},-channel ${CHANNEL}) \
$(if ${TEST_CASE},-testCase "${TEST_CASE}") \
$(if ${WORKLOAD_NAME},-workloadName ${WORKLOAD_NAME}) \
$(if ${DEPLOY_WORKLOAD},-deployWorkload ${DEPLOY_WORKLOAD})


.PHONY: test-canal-bump
test-canal-bump:
@go test -timeout=45m -v -count=1 ./entrypoint/versionbump/... -tags=canal \
-expectedValue ${EXPECTED_VALUE} \
$(if ${VALUE_UPGRADED},-expectedValueUpgrade ${VALUE_UPGRADED}) \
$(if ${INSTALL_VERSION_OR_COMMIT},-installVersionOrCommit ${INSTALL_VERSION_OR_COMMIT}) \
$(if ${CHANNEL},-channel ${CHANNEL}) \
$(if ${TEST_CASE},-testCase "${TEST_CASE}") \
$(if ${WORKLOAD_NAME},-workloadName ${WORKLOAD_NAME}) \
$(if ${DEPLOY_WORKLOAD},-deployWorkload ${DEPLOY_WORKLOAD})


.PHONY: test-coredns-bump
test-coredns-bump:
@go test -timeout=45m -v -count=1 ./entrypoint/versionbump/... -tags=coredns \
-expectedValue ${EXPECTED_VALUE} \
$(if ${VALUE_UPGRADED},-expectedValueUpgrade ${VALUE_UPGRADED}) \
$(if ${INSTALL_VERSION_OR_COMMIT},-installVersionOrCommit ${INSTALL_VERSION_OR_COMMIT}) \
$(if ${CHANNEL},-channel ${CHANNEL}) \
$(if ${TEST_CASE},-testCase "${TEST_CASE}") \
$(if ${WORKLOAD_NAME},-workloadName ${WORKLOAD_NAME}) \
$(if ${DEPLOY_WORKLOAD},-deployWorkload ${DEPLOY_WORKLOAD})


.PHONY: test-cniplugin-bump
test-cniplugin-bump:
@go test -timeout=45m -v -count=1 ./entrypoint/versionbump/... -tags=cniplugin \
-expectedValue ${EXPECTED_VALUE} \
$(if ${VALUE_UPGRADED},-expectedValueUpgrade ${VALUE_UPGRADED}) \
$(if ${INSTALL_VERSION_OR_COMMIT},-installVersionOrCommit ${INSTALL_VERSION_OR_COMMIT}) \
$(if ${CHANNEL},-channel ${CHANNEL}) \
$(if ${TEST_CASE},-testCase "${TEST_CASE}") \
$(if ${WORKLOAD_NAME},-workloadName ${WORKLOAD_NAME}) \
$(if ${DEPLOY_WORKLOAD},-deployWorkload ${DEPLOY_WORKLOAD})

#========================= TestCode Static Quality Check =========================#
.PHONY: vet-lint
vet-lint:
@echo "Running go vet and lint"
@go vet ./${TESTDIR} && golangci-lint run --tests
@go vet ./${TEST_DIR} && golangci-lint run --tests
13 changes: 0 additions & 13 deletions config.mk

This file was deleted.

63 changes: 44 additions & 19 deletions config/reader.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package config

import (
"bufio"
"fmt"
"os"
"strings"
"sync"

"github.com/spf13/viper"
)

var (
Expand All @@ -14,15 +14,14 @@ var (
)

type ProductConfig struct {
TFVars string `mapstructure:"ENV_TFVARS"`
Product string `mapstructure:"ENV_PRODUCT"`
TFVars string
Product string
}

// AddConfigEnv returns a singleton of ProductConfig from yaml config file
func AddConfigEnv(path string) (*ProductConfig, error) {
once.Do(func() {
var err error
product, err = loadConfigEnv(path)
product, err = loadEnv(path)
if err != nil {
return
}
Expand All @@ -31,28 +30,54 @@ func AddConfigEnv(path string) (*ProductConfig, error) {
return product, nil
}

func loadConfigEnv(path string) (config *ProductConfig, err error) {
viper.AddConfigPath(path)
viper.SetConfigName("product")
viper.SetConfigType("yaml")
viper.AutomaticEnv()

err = viper.ReadInConfig()
if err != nil {
func loadEnv(fullPath string) (config *ProductConfig, err error) {
if err = setEnv(fullPath); err != nil {
return nil, err
}

config = &ProductConfig{}
config.TFVars = os.Getenv("ENV_TFVARS")
config.Product = os.Getenv("ENV_PRODUCT")

if config.TFVars == "" || (config.TFVars != "k3s.tfvars" && config.TFVars != "rke2.tfvars") {
fmt.Printf("unknown tfvars: %s\n", config.TFVars)
os.Exit(1)
}
err = viper.Unmarshal(&config)

if config.Product == "" || (config.Product != "k3s" && config.Product != "rke2") {
fmt.Printf("unknown product: %s\n", config.Product)
os.Exit(1)
}

if config.TFVars == "" || (config.TFVars != "k3s.tfvars" && config.TFVars != "rke2.tfvars") {
fmt.Printf("unknown tfvars: %s\n", config.TFVars)
os.Exit(1)
return config, nil
}

func setEnv(fullPath string) error {
file, err := os.Open(fullPath)
if err != nil {
fmt.Println("failed to open file:", err)
return err
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
continue
}

key, value := parts[0], parts[1]
err = os.Setenv(key, value)
if err != nil {
return err
}
}

if err = scanner.Err(); err != nil {
return err
}

return config, err
return nil
}
Loading

0 comments on commit a5448e5

Please sign in to comment.