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

ci: run unit tests #7

Merged
merged 4 commits into from
Jan 19, 2024
Merged
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build/
build/
junit-report.xml
17 changes: 16 additions & 1 deletion .semaphore/semaphore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ agent:
type: e2-standard-2
os_image: ubuntu2004
blocks:
- name: 'Lint'
- name: Tests
dependencies: []
task:
jobs:
Expand All @@ -15,6 +15,14 @@ blocks:
- checkout
- go install github.com/mgechev/revive@latest
- make lint
- name: Unit tests
commands:
- checkout
- make test
epilogue:
always:
commands:
- test-results publish junit-report.xml
- name: "Security checks"
dependencies: []
task:
Expand All @@ -40,3 +48,10 @@ blocks:
always:
commands:
- 'if [ -f results.xml ]; then test-results publish --name="Security checks" results.xml; fi'

after_pipeline:
task:
jobs:
- name: Submit Reports
commands:
- test-results gen-pipeline-report
5 changes: 5 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM golang:1.21

RUN go install gotest.tools/gotestsum@latest

WORKDIR /app
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ check.docker: check.prepare
lint:
revive -formatter friendly -config lint.toml ./...

test:
docker compose run --rm app gotestsum --format short-verbose --junitfile junit-report.xml --packages="./..." -- -p 1

build:
rm -rf build
env GOOS=linux go build -o build/controller main.go
Expand Down
16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: '3.6'
services:
app:
build:
context: .
dockerfile: Dockerfile.dev
tty: true
environment: {}
command: "/build/controller"
container_name: 'agent-k8s-controller'
volumes:
- go-pkg-cache:/go
- .:/app
volumes:
go-pkg-cache:
driver: local
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/renderedtext/agent-k8s-stack
go 1.21

require (
github.com/stretchr/testify v1.8.2
k8s.io/api v0.28.4
k8s.io/apimachinery v0.28.4
k8s.io/client-go v0.28.4
Expand All @@ -12,6 +13,7 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand All @@ -29,6 +31,8 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emicklei/go-restful/v3 v3.9.0 h1:XwGDlfxEnQZzuopoqxwSEllNcCOM9DhhFyhFIIGKwxE=
github.com/emicklei/go-restful/v3 v3.9.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84=
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-openapi/jsonpointer v0.19.6 h1:eCs3fxoIi3Wh6vtgmLTOjdhSpiqphQ+DaPn38N2ZdrE=
Expand Down Expand Up @@ -60,6 +62,8 @@ github.com/onsi/ginkgo/v2 v2.9.4 h1:xR7vG4IXt5RWx6FfIjyAtsoMAtnc3C/rFXBBd2AjZwE=
github.com/onsi/ginkgo/v2 v2.9.4/go.mod h1:gCQYp2Q+kSoIj7ykSVb9nskRSsR6PUj4AiLywzIhbKM=
github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
github.com/onsi/gomega v1.27.6/go.mod h1:PIQNjfQwkP3aQAH7lf7j87O/5FiNr+ZR8+ipb+qQlhg=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
Expand Down
2 changes: 1 addition & 1 deletion pkg/agenttypes/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (r *Registry) OnUpdate(oldObj, newObj interface{}) {

agentType, err := parseAgentType(newSecret)
if err != nil {
klog.Errorf("Error when updating agent type: %v", err)
klog.Errorf("Error when parsing agent type: %v", err)
return
}

Expand Down
161 changes: 161 additions & 0 deletions pkg/agenttypes/registry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package agenttypes

import (
"encoding/base64"
"fmt"
"math/rand"
"testing"

"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Test__Registry(t *testing.T) {
r, err := NewRegistry()
require.NoError(t, err)

t.Run("on add -> no startup parameters", func(t *testing.T) {
agentTypeName := randAgentTypeName()
secretName := randSecretName()
token := randomString(t)
require.Nil(t, r.Get(agentTypeName))

s := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: secretName},
Data: map[string][]byte{
"agentTypeName": []byte(agentTypeName),
"registrationToken": []byte(token),
},
}

r.OnAdd(s, false)
agentType := r.Get(agentTypeName)
require.NotNil(t, agentType)
require.Equal(t, agentTypeName, agentType.AgentTypeName)
require.Equal(t, token, agentType.RegistrationToken)
require.Equal(t, secretName, agentType.SecretName)
require.Empty(t, agentType.AgentStartupParameters)
})

t.Run("on add -> with startup parameters", func(t *testing.T) {
agentTypeName := randAgentTypeName()
secretName := randSecretName()
token := randomString(t)
require.Nil(t, r.Get(agentTypeName))

s := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: secretName},
Data: map[string][]byte{
"agentTypeName": []byte(agentTypeName),
"registrationToken": []byte(token),
"agentStartupParameters": []byte("--kubernetes-pod-spec my-spec"),
},
}

r.OnAdd(s, false)
agentType := r.Get(agentTypeName)
require.NotNil(t, agentType)
require.Equal(t, agentTypeName, agentType.AgentTypeName)
require.Equal(t, token, agentType.RegistrationToken)
require.Equal(t, []string{"--kubernetes-pod-spec", "my-spec"}, agentType.AgentStartupParameters)
})

t.Run("on update, same version -> nothing is updated", func(t *testing.T) {
agentTypeName := randAgentTypeName()
secretName := randSecretName()
oldToken := randomString(t)
require.Nil(t, r.Get(agentTypeName))

old := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: secretName, ResourceVersion: "1"},
Data: map[string][]byte{
"agentTypeName": []byte(agentTypeName),
"registrationToken": []byte(oldToken),
},
}

// agent type is added
r.OnAdd(old, false)

// update with same resource version
// does not update the agent type information.
new := old.DeepCopy()
new.Data["registrationToken"] = []byte(randomString(t))
r.OnUpdate(old, new)

agentType := r.Get(agentTypeName)
require.NotNil(t, agentType)
require.Equal(t, oldToken, agentType.RegistrationToken)
})

t.Run("on update -> agent type is updated", func(t *testing.T) {
agentTypeName := randAgentTypeName()
secretName := randSecretName()
oldToken := randomString(t)
require.Nil(t, r.Get(agentTypeName))

old := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: secretName, ResourceVersion: "1"},
Data: map[string][]byte{
"agentTypeName": []byte(agentTypeName),
"registrationToken": []byte(oldToken),
},
}

// agent type is added
r.OnAdd(old, false)

// update with same resource version
// does not update the agent type information.
new := old.DeepCopy()
newToken := randomString(t)
new.Data["registrationToken"] = []byte(newToken)
new.ObjectMeta.ResourceVersion = "2"
r.OnUpdate(old, new)

agentType := r.Get(agentTypeName)
require.NotNil(t, agentType)
require.Equal(t, newToken, agentType.RegistrationToken)
})

t.Run("on delete -> agent type is deleted", func(t *testing.T) {
agentTypeName := randAgentTypeName()
secretName := randSecretName()
token := randomString(t)
require.Nil(t, r.Get(agentTypeName))

s := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: secretName, ResourceVersion: "1"},
Data: map[string][]byte{
"agentTypeName": []byte(agentTypeName),
"registrationToken": []byte(token),
},
}

// agent type is added
r.OnAdd(s, false)
require.NotNil(t, r.Get(agentTypeName))

// agent type is deleted
r.OnDelete(s)
require.Nil(t, r.Get(agentTypeName))
})
}

func randAgentTypeName() string {
return fmt.Sprintf("s1-test-%d", rand.Int())
}

func randSecretName() string {
return fmt.Sprintf("secret-%d", rand.Int())
}

func randomString(t *testing.T) string {
buffer := make([]byte, 15)

// #nosec
_, err := rand.Read(buffer)
require.NoError(t, err)
return base64.URLEncoding.EncodeToString(buffer)
}
5 changes: 5 additions & 0 deletions pkg/controller/job_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ func (s *JobScheduler) OnDelete(obj interface{}) {
Info("Job deleted")
}

func (s *JobScheduler) JobExists(jobID string) bool {
_, ok := s.current[jobID]
return ok
}

func getFailedMessage(job *batchv1.Job) string {
for _, cond := range job.Status.Conditions {
if cond.Type == batchv1.JobFailed {
Expand Down
Loading