This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: increase test coverage of go-sdk (#526)
* started refactoring initialization logic a bit Signed-off-by: warber <bernd.warmuth@dynatrace.com> * enhanced tests, fixed handling error when event could not be decoded, enhanced example, and correctly pass logger implementation to cp-connector Signed-off-by: warber <bernd.warmuth@dynatrace.com> * added codecov Signed-off-by: warber <bernd.warmuth@dynatrace.com> * fixed variable name and added additional checks in constructor func test Signed-off-by: warber <bernd.warmuth@dynatrace.com> * cleanup Signed-off-by: warber <bernd.warmuth@dynatrace.com> * fixed tests.yml workflow Signed-off-by: warber <bernd.warmuth@dynatrace.com> * bit of cleanup Signed-off-by: warber <bernd.warmuth@dynatrace.com> * review Signed-off-by: warber <bernd.warmuth@dynatrace.com>
- Loading branch information
Showing
9 changed files
with
293 additions
and
139 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
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,22 @@ | ||
coverage: | ||
status: | ||
project: | ||
default: | ||
# basic | ||
target: auto | ||
threshold: 2% # allow cov to drop by 2% (just in case) | ||
patch: | ||
default: | ||
threshold: 1% # allow patch | ||
|
||
ignore: | ||
- "**/*.yaml" # ignore all yaml files (Kubernetes manifests, etc...) | ||
- "**/*.yml" # same as above | ||
- "**/*.md" # ignore all markdown files, those are not relevant for building/testing | ||
- "**/*.sh" # ignore shell scripts | ||
|
||
comment: | ||
layout: "diff, files, flags" | ||
|
||
github_checks: | ||
annotations: false |
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
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
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 |
---|---|---|
@@ -1,74 +1,71 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
keptnapi "github.com/keptn/go-utils/pkg/api/utils" | ||
"github.com/keptn/go-utils/pkg/sdk/connector/logger" | ||
"github.com/keptn/go-utils/pkg/sdk/internal/config" | ||
"github.com/stretchr/testify/require" | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_createAPI(t *testing.T) { | ||
type fakeHTTPClientFactory struct { | ||
GetFn func() (*http.Client, error) | ||
} | ||
|
||
apiInit := Initializer{ | ||
Internal: func(client *http.Client, apiMappings ...keptnapi.InClusterAPIMappings) (*keptnapi.InternalAPISet, error) { | ||
return &keptnapi.InternalAPISet{}, nil | ||
}, | ||
Remote: func(baseURL string, options ...func(*keptnapi.APISet)) (*keptnapi.APISet, error) { | ||
return &keptnapi.APISet{}, nil | ||
}, | ||
} | ||
func (f *fakeHTTPClientFactory) Get() (*http.Client, error) { | ||
return f.GetFn() | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
env config.EnvConfig | ||
wantInternal bool | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "test no env internal NATS ", | ||
env: config.EnvConfig{}, | ||
wantInternal: true, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "test FAIL for no http address", | ||
env: config.EnvConfig{ | ||
KeptnAPIEndpoint: "ssh://mynotsogoodendpoint", | ||
}, | ||
wantErr: true, | ||
wantInternal: false, | ||
}, | ||
{ | ||
name: "test FAIL for no good address", | ||
env: config.EnvConfig{ | ||
KeptnAPIEndpoint: ":///MALFORMEDendpoint", | ||
}, | ||
wantErr: true, | ||
wantInternal: false, | ||
}, | ||
{ | ||
name: "test PASS for http address", | ||
env: config.EnvConfig{ | ||
KeptnAPIEndpoint: "http://endpoint", | ||
}, | ||
wantErr: false, | ||
wantInternal: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := createAPI(nil, tt.env, apiInit) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("createAPI() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if err == nil && tt.wantInternal && !reflect.DeepEqual(got, &keptnapi.InternalAPISet{}) { | ||
t.Errorf("createAPI() got = %v, wanted internal API", got) | ||
} else if err == nil && !tt.wantInternal && !reflect.DeepEqual(got, &keptnapi.APISet{}) { | ||
t.Errorf("createAPI() got = %v, want remote execution plane", got) | ||
} | ||
func Test_Initialize(t *testing.T) { | ||
t.Run("Remote use case - invalid keptn api endpoint", func(t *testing.T) { | ||
env := config.EnvConfig{KeptnAPIEndpoint: "://mynotsogoodendpoint"} | ||
result, err := Initialize(env, CreateClientGetter(env), logger.NewDefaultLogger()) | ||
require.Error(t, err) | ||
require.Nil(t, result) | ||
}) | ||
t.Run("Remote use case - no http address as keptn api endpoint", func(t *testing.T) { | ||
env := config.EnvConfig{KeptnAPIEndpoint: "ssh://mynotsogoodendpoint"} | ||
result, err := Initialize(env, CreateClientGetter(env), logger.NewDefaultLogger()) | ||
require.Error(t, err) | ||
require.Nil(t, result) | ||
|
||
}) | ||
} | ||
}) | ||
t.Run("Remote use case - remote api set is used", func(t *testing.T) { | ||
env := config.EnvConfig{KeptnAPIEndpoint: "http://endpoint"} | ||
result, err := Initialize(env, CreateClientGetter(env), logger.NewDefaultLogger()) | ||
require.NoError(t, err) | ||
require.NotNil(t, result.ControlPlane) | ||
require.NotNil(t, result.EventSenderCallback) | ||
require.NotNil(t, result.KeptnAPI) | ||
require.IsType(t, &keptnapi.APISet{}, result.KeptnAPI) | ||
require.NotNil(t, result.ResourceHandler) | ||
}) | ||
t.Run("Internal Use case - internal api set is used", func(t *testing.T) { | ||
env := config.EnvConfig{} | ||
result, err := Initialize(env, CreateClientGetter(env), logger.NewDefaultLogger()) | ||
require.NoError(t, err) | ||
require.NotNil(t, result.ControlPlane) | ||
require.NotNil(t, result.EventSenderCallback) | ||
require.NotNil(t, result.KeptnAPI) | ||
require.IsType(t, &keptnapi.InternalAPISet{}, result.KeptnAPI) | ||
require.NotNil(t, result.ResourceHandler) | ||
}) | ||
t.Run("HTTP client creation fails", func(t *testing.T) { | ||
env := config.EnvConfig{KeptnAPIEndpoint: "http://endpoint"} | ||
result, err := Initialize(env, &fakeHTTPClientFactory{GetFn: func() (*http.Client, error) { return nil, fmt.Errorf("err") }}, logger.NewDefaultLogger()) | ||
require.Error(t, err) | ||
require.Nil(t, result) | ||
}) | ||
t.Run("HTTP client creation returns nil client", func(t *testing.T) { | ||
env := config.EnvConfig{KeptnAPIEndpoint: "http://endpoint"} | ||
result, err := Initialize(env, &fakeHTTPClientFactory{GetFn: func() (*http.Client, error) { return nil, nil }}, logger.NewDefaultLogger()) | ||
require.NoError(t, err) | ||
require.NotNil(t, result.ControlPlane) | ||
require.NotNil(t, result.EventSenderCallback) | ||
require.NotNil(t, result.KeptnAPI) | ||
require.IsType(t, &keptnapi.APISet{}, result.KeptnAPI) | ||
require.NotNil(t, result.ResourceHandler) | ||
}) | ||
} |
Oops, something went wrong.