Skip to content

Commit

Permalink
PoC: Add hosted integration testing
Browse files Browse the repository at this point in the history
  • Loading branch information
sebrandon1 committed Dec 19, 2024
1 parent 75a2d0c commit 57f3250
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 0 deletions.
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
// +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
// +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)
}

0 comments on commit 57f3250

Please sign in to comment.