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

PoC: Add hosted integration testing #714

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions .github/workflows/integration-testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Integration Testing

on:
workflow_dispatch:
pull_request:
branches:
- main

jobs:
integration:
runs-on: ubuntu-22.04
env:
SHELL: /bin/bash
SKIP_PRELOAD_IMAGES: true
KUBECONFIG: '/home/runner/.kube/config'
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Go 1.23
uses: actions/setup-go@v5
with:
go-version: 1.23.4

- name: Setup cluster
uses: palmsoftware/[email protected]

- name: Run integration tests
run: make integration-test
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ install: deps-update
test:
go test ./...

integration-test:
go test -tags=integration ./...

coverage-html:
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out
Expand Down
155 changes: 155 additions & 0 deletions integration/deployment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
//go:build integration
sebrandon1 marked this conversation as resolved.
Show resolved Hide resolved
// +build integration

package integration

import (
"testing"
"time"

"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/openshift-kni/eco-goinfra/pkg/deployment"
"github.com/openshift-kni/eco-goinfra/pkg/namespace"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
)

const (
namespacePrefix = "ecogoinfra-deployment"
containerImage = "nginx:latest"
timeoutDuration = time.Duration(60) * time.Second
)

func TestDeploymentCreate(t *testing.T) {
t.Parallel()
client := clients.New("")

// Create a random namespace for the test
randomNamespace := generateRandomNamespace(namespacePrefix)

// Create the namespace in the cluster using the namespaces package
_, err := namespace.NewBuilder(client, randomNamespace).Create()
assert.Nil(t, err)

// Defer the deletion of the namespace
defer func() {
// Delete the namespace
err := namespace.NewBuilder(client, randomNamespace).Delete()
assert.Nil(t, err)
}()

var deploymentName = "create-test"

builder := deployment.NewBuilder(client, deploymentName, randomNamespace, map[string]string{
"app": "test",
}, corev1.Container{
Name: "test",
Image: containerImage,
Command: []string{
"sleep",
"3600",
},
})

// Create a deployment in the namespace
_, err = builder.CreateAndWaitUntilReady(timeoutDuration)
assert.Nil(t, err)

// Check if the deployment was created
builder, err = deployment.Pull(client, deploymentName, randomNamespace)
assert.Nil(t, err)
assert.NotNil(t, builder.Object)
}

func TestDeploymentDelete(t *testing.T) {
t.Parallel()
client := clients.New("")

// Create a random namespace for the test
randomNamespace := generateRandomNamespace(namespacePrefix)

// Create the namespace in the cluster using the namespaces package
_, err := namespace.NewBuilder(client, randomNamespace).Create()
assert.Nil(t, err)

// Defer the deletion of the namespace
defer func() {
// Delete the namespace
err := namespace.NewBuilder(client, randomNamespace).Delete()
assert.Nil(t, err)
}()

var deploymentName = "delete-test"

builder := deployment.NewBuilder(client, deploymentName, randomNamespace, map[string]string{
"app": "test",
}, corev1.Container{
Name: "test",
Image: containerImage,
Command: []string{
"sleep",
"3600",
},
})

// Create a deployment in the namespace
_, err = builder.CreateAndWaitUntilReady(timeoutDuration)
assert.Nil(t, err)

// Check if the deployment was created
builder, err = deployment.Pull(client, deploymentName, randomNamespace)
assert.Nil(t, err)
assert.NotNil(t, builder.Object)

// Delete the deployment
err = builder.DeleteAndWait(timeoutDuration)
assert.Nil(t, err)

// Check if the deployment was deleted
builder, err = deployment.Pull(client, deploymentName, randomNamespace)
assert.Equal(t, "deployment object delete-test does not exist in namespace "+randomNamespace, err.Error())
}

func TestDeploymentWithReplicas(t *testing.T) {
t.Parallel()
client := clients.New("")

// Create a random namespace for the test
randomNamespace := generateRandomNamespace(namespacePrefix)

// Create the namespace in the cluster using the namespaces package
_, err := namespace.NewBuilder(client, randomNamespace).Create()
assert.Nil(t, err)

// Defer the deletion of the namespace
defer func() {
// Delete the namespace
err := namespace.NewBuilder(client, randomNamespace).Delete()
assert.Nil(t, err)
}()

var deploymentName = "replicas-test"

builder := deployment.NewBuilder(client, deploymentName, randomNamespace, map[string]string{
"app": "test",
}, corev1.Container{
Name: "test",
Image: containerImage,
Command: []string{
"sleep",
"3600",
},
}).WithReplicas(2)

// Create a deployment in the namespace
_, err = builder.CreateAndWaitUntilReady(timeoutDuration)
assert.Nil(t, err)

// Check if the deployment was created
builder, err = deployment.Pull(client, deploymentName, randomNamespace)
assert.Nil(t, err)
assert.NotNil(t, builder.Object)

// Check if the deployment has the correct number of replicas
assert.Equal(t, int32(2), *builder.Object.Spec.Replicas)
}
30 changes: 30 additions & 0 deletions integration/integration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build integration
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid using random values. I think we can run all test cases in eco-goinfra-intergation-tests namespace.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually like the random namespaces when running in parallel. Keeps everything separated nicely and doesn't allow for any cross contamination (if any, potentially) of any of the tests. We can discuss at next meeting if needed.

// +build integration

package integration

import (
"math/rand"
"time"
)

var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))

func generateRandomString(length int) string {
charset := "abcdefghijklmnopqrstuvwxyz"

//nolint:varnamelen
b := make([]byte, length)

for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}

return string(b)
}

func generateRandomNamespace(prefix string) string {
// Generate a random 12-character string
return prefix + "-" + generateRandomString(12)
}
Loading