-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75a2d0c
commit 57f3250
Showing
4 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |