Skip to content

Commit

Permalink
Add tests for handle_git_sources
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalKalke committed Feb 20, 2025
1 parent 49dbeec commit ae429e6
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 4 deletions.
1 change: 1 addition & 0 deletions components/buildless-serverless/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ require (
github.com/spf13/cobra v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stoewer/go-strcase v1.2.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions components/buildless-serverless/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
serverlessv1alpha2 "github.com/kyma-project/serverless/api/v1alpha2"
"github.com/kyma-project/serverless/internal/config"
"github.com/kyma-project/serverless/internal/controller/git"
"github.com/kyma-project/serverless/internal/controller/resources"
appsv1 "k8s.io/api/apps/v1"
"reflect"
Expand Down Expand Up @@ -42,6 +43,7 @@ type StateMachine struct {
Client client.Client
FunctionConfig config.FunctionConfig
Scheme *apimachineryruntime.Scheme
GitClient git.LastCommitChecker
}

func (m *StateMachine) stateFnName() string {
Expand Down Expand Up @@ -102,6 +104,7 @@ func New(client client.Client, functionConfig config.FunctionConfig, instance *s
FunctionConfig: functionConfig,
Client: client,
Scheme: scheme,
GitClient: git.GoGitCommitChecker{},
}
sm.State.saveStatusSnapshot()
return &sm
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion components/buildless-serverless/internal/controller/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ import (
"github.com/go-git/go-git/v5/storage/memory"
)

func GetLatestCommit(url, reference string) (string, error) {
//go:generate mockery --name=LastCommitChecker --output=automock --outpkg=automock --case=underscore
type LastCommitChecker interface {
GetLatestCommit(url, reference string) (string, error)
}

type GoGitCommitChecker struct {
}

func (g GoGitCommitChecker) GetLatestCommit(url, reference string) (string, error) {

r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"fmt"
serverlessv1alpha2 "github.com/kyma-project/serverless/api/v1alpha2"
"github.com/kyma-project/serverless/internal/controller/fsm"
"github.com/kyma-project/serverless/internal/controller/git"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
)

// dla inlienów state commit pusty, nextState
// latestCommit istnieje i nie ma errora spodziewamy sie commita, nextState, nie zminiły się conditiony
// latestCommit pusty, zmienił się condition, stop
func sFnHandleGitSources(_ context.Context, m *fsm.StateMachine) (fsm.StateFn, *ctrl.Result, error) {

if !m.State.Function.HasGitSources() {
Expand All @@ -18,14 +20,14 @@ func sFnHandleGitSources(_ context.Context, m *fsm.StateMachine) (fsm.StateFn, *

gitRepository := m.State.Function.Spec.Source.GitRepository

latestCommit, err := git.GetLatestCommit(gitRepository.URL, gitRepository.Reference)
latestCommit, err := m.GitClient.GetLatestCommit(gitRepository.URL, gitRepository.Reference)
if err != nil {
m.State.Function.UpdateCondition(
serverlessv1alpha2.ConditionConfigurationReady,
metav1.ConditionFalse,
serverlessv1alpha2.ConditionReasonGitSourceCheckFailed,
fmt.Sprintf("Git repository: %s source check failed: %s", gitRepository.URL, err.Error()))
return stop()
return nil, nil, err
}

m.State.Commit = latestCommit
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package state

import (
"context"
"errors"
serverlessv1alpha2 "github.com/kyma-project/serverless/api/v1alpha2"
"github.com/kyma-project/serverless/internal/controller/fsm"
"github.com/kyma-project/serverless/internal/controller/git/automock"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"
)

// dla inlienów state commit pusty, nextState
// latestCommit istnieje i nie ma errora spodziewamy sie commita, nextState, nie zminiły się conditiony
// latestCommit pusty, zmienił się condition, stop
func Test_sFnHandleGitSources(t *testing.T) {
t.Run("for inline function where the commit should be empty and move to the nextState", func(t *testing.T) {
// Arrange
// machine with our function
m := fsm.StateMachine{
State: fsm.SystemState{
Function: serverlessv1alpha2.Function{
ObjectMeta: metav1.ObjectMeta{
Name: "nice-matsumoto-name",
Namespace: "festive-dewdney-ns"},
Spec: serverlessv1alpha2.FunctionSpec{
Runtime: serverlessv1alpha2.NodeJs22,
Source: serverlessv1alpha2.Source{
Inline: &serverlessv1alpha2.InlineSource{
Source: "xenodochial-napier"}}}}},
Log: zap.NewNop().Sugar(),
}

// Act
next, result, err := sFnHandleGitSources(context.Background(), &m)

// Assert
// we are not expecting error
require.Nil(t, err)
// no result
require.Nil(t, result)
// with expected next state
require.NotNil(t, next)
requireEqualFunc(t, sFnHandleDeployment, next)
// function conditions remain unchanged
require.Empty(t, m.State.Function.Status.Conditions)
// no commit change, it should be changed only for git functions
require.Equal(t, "", m.State.Commit)
})
t.Run("for git function where the commit should not be empty and move to the nextState", func(t *testing.T) {
// Arrange
// machine with our function
gitMock := new(automock.LastCommitChecker)
gitMock.On("GetLatestCommit", mock.Anything, mock.Anything).Return("latest-test-commit", nil)
m := fsm.StateMachine{
State: fsm.SystemState{
Function: serverlessv1alpha2.Function{
ObjectMeta: metav1.ObjectMeta{
Name: "nice-matsumoto-name",
Namespace: "festive-dewdney-ns"},
Spec: serverlessv1alpha2.FunctionSpec{
Runtime: serverlessv1alpha2.NodeJs22,
Source: serverlessv1alpha2.Source{
GitRepository: &serverlessv1alpha2.GitRepositorySource{
URL: "test-url",
Repository: serverlessv1alpha2.Repository{
BaseDir: "main",
Reference: "test-reference",
},
}}}}},
Log: zap.NewNop().Sugar(),
GitClient: gitMock,
}

// Act
next, result, err := sFnHandleGitSources(context.Background(), &m)

// Assert
// we are not expecting error
require.Nil(t, err)
// no result
require.Nil(t, result)
// with expected next state
require.NotNil(t, next)
requireEqualFunc(t, sFnHandleDeployment, next)
// function conditions remain unchanged
require.Empty(t, m.State.Function.Status.Conditions)
// commit change, it should be changed only for git functions
require.Equal(t, "latest-test-commit", m.State.Commit)
})
t.Run("for git function where the commit should be empty and stop with condition", func(t *testing.T) {
// Arrange
// machine with our function
gitMock := new(automock.LastCommitChecker)
gitMock.On("GetLatestCommit", mock.Anything, mock.Anything).Return("", errors.New("test-error"))
m := fsm.StateMachine{
State: fsm.SystemState{
Function: serverlessv1alpha2.Function{
ObjectMeta: metav1.ObjectMeta{
Name: "nice-matsumoto-name",
Namespace: "festive-dewdney-ns"},
Spec: serverlessv1alpha2.FunctionSpec{
Runtime: serverlessv1alpha2.NodeJs22,
Source: serverlessv1alpha2.Source{
GitRepository: &serverlessv1alpha2.GitRepositorySource{
URL: "test-url",
Repository: serverlessv1alpha2.Repository{
BaseDir: "main",
Reference: "test-reference",
},
}}}}},
Log: zap.NewNop().Sugar(),
GitClient: gitMock,
}

// Act
next, result, err := sFnHandleGitSources(context.Background(), &m)

// Assert
// we are expecting error
require.NotNil(t, err)
require.ErrorContains(t, err, "test-error")
// no result (error)
require.Nil(t, result)
// function has proper condition
requireContainsCondition(t, m.State.Function.Status,
serverlessv1alpha2.ConditionConfigurationReady,
metav1.ConditionFalse,
serverlessv1alpha2.ConditionReasonGitSourceCheckFailed,
"Git repository: test-url source check failed: test-error")
// no next state
require.Nil(t, next)
// commit did not change
require.Equal(t, "", m.State.Commit)
})
}

0 comments on commit ae429e6

Please sign in to comment.